From 37c5dfa49f64fe2adf43ed9b8c73f152d04538fa Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Fri, 18 Jun 2021 00:05:01 +0300 Subject: [PATCH] engine: generalize maps command and map command autocompletion --- engine/common/common.h | 1 + engine/common/con_utils.c | 60 +++++++++++++++++++++++++-------------- engine/server/sv_cmds.c | 11 ++++--- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/engine/common/common.h b/engine/common/common.h index c5f962f9..8dc94eb9 100644 --- a/engine/common/common.h +++ b/engine/common/common.h @@ -494,6 +494,7 @@ int Cmd_AddRefCommand( const char *cmd_name, xcommand_t function, const char *de void Cmd_RemoveCommand( const char *cmd_name ); qboolean Cmd_Exists( const char *cmd_name ); void Cmd_LookupCmds( void *buffer, void *ptr, setpair_t callback ); +int Cmd_ListMaps( search_t *t , char *lastmapname, size_t len ); qboolean Cmd_GetMapList( const char *s, char *completedname, int length ); qboolean Cmd_GetDemoList( const char *s, char *completedname, int length ); qboolean Cmd_GetMovieList( const char *s, char *completedname, int length ); diff --git a/engine/common/con_utils.c b/engine/common/con_utils.c index fbdaf316..55b9676d 100644 --- a/engine/common/con_utils.c +++ b/engine/common/con_utils.c @@ -51,26 +51,16 @@ static con_autocomplete_t con; */ /* ===================================== -Cmd_GetMapList +Cmd_ListMaps -Prints or complete map filename ===================================== */ -qboolean Cmd_GetMapList( const char *s, char *completedname, int length ) +int Cmd_ListMaps( search_t *t, char *lastmapname, size_t len ) { - search_t *t; - file_t *f; - string message, compiler, generator, matchbuf; - byte buf[MAX_SYSPATH]; // 1 kb - int i, nummaps; - - t = FS_Search( va( "maps/%s*.bsp", s ), true, con_gamemaps->value ); - if( !t ) return false; - - COM_FileBase( t->filenames[0], matchbuf ); - if( completedname && length ) - Q_strncpy( completedname, matchbuf, length ); - if( t->numfilenames == 1 ) return true; + byte buf[MAX_SYSPATH]; // 1 kb + file_t *f; + int i, nummaps; + string mapname, message, compiler, generator; for( i = 0, nummaps = 0; i < t->numfilenames; i++ ) { @@ -78,9 +68,7 @@ qboolean Cmd_GetMapList( const char *s, char *completedname, int length ) const char *ext = COM_FileExtension( t->filenames[i] ); int ver = -1, lumpofs = 0, lumplen = 0; char *ents = NULL, *pfile; - qboolean validmap = false; int version = 0; - char *szBuf; string version_description; if( Q_stricmp( ext, "bsp" )) continue; @@ -157,7 +145,7 @@ qboolean Cmd_GetMapList( const char *s, char *completedname, int length ) } if( f ) FS_Close(f); - COM_FileBase( t->filenames[i], matchbuf ); + COM_FileBase( t->filenames[i], mapname ); switch( ver ) { @@ -179,11 +167,41 @@ qboolean Cmd_GetMapList( const char *s, char *completedname, int length ) default: Q_strncpy( version_description, "??", sizeof( version_description )); break; } - Con_Printf( "%16s (%s) ^3%s^7 ^2%s %s^7\n", matchbuf, version_description, message, compiler, generator ); + Con_Printf( "%16s (%s) ^3%s^7 ^2%s %s^7\n", mapname, version_description, message, compiler, generator ); nummaps++; } - Con_Printf( "\n^3 %i maps found.\n", nummaps ); + if( lastmapname && len ) + Q_strncpy( lastmapname, mapname, len ); + + return nummaps; +} + +/* +===================================== +Cmd_GetMapList + +Prints or complete map filename +===================================== +*/ +qboolean Cmd_GetMapList( const char *s, char *completedname, int length ) +{ + search_t *t; + string matchbuf; + int i, nummaps; + + t = FS_Search( va( "maps/%s*.bsp", s ), true, con_gamemaps->value ); + if( !t ) return false; + + COM_FileBase( t->filenames[0], matchbuf ); + if( completedname && length ) + Q_strncpy( completedname, matchbuf, length ); + if( t->numfilenames == 1 ) return true; + + nummaps = Cmd_ListMaps( t, matchbuf, sizeof( matchbuf )); + + Con_Printf( "\n^3 %d maps found.\n", nummaps ); + Mem_Free( t ); // cut shortestMatch to the amount common with s diff --git a/engine/server/sv_cmds.c b/engine/server/sv_cmds.c index 7e86b7ac..7d785ae3 100644 --- a/engine/server/sv_cmds.c +++ b/engine/server/sv_cmds.c @@ -244,7 +244,7 @@ void SV_Maps_f( void ) { const char *separator = "-------------------"; const char *argStr = Cmd_Argv( 1 ); // Substr - int listIndex; + int nummaps; search_t *mapList; if( Cmd_Argc() != 2 ) @@ -261,12 +261,11 @@ void SV_Maps_f( void ) return; } - for( listIndex = 0; listIndex != mapList->numfilenames; ++listIndex ) - { - Msg( "%s\n", mapList->filenames[listIndex] + 5 ); // Do not show "maps/" - } + nummaps = Cmd_ListMaps( mapList, NULL, 0 ); - Msg( "%s\nDirectory: \"%s/maps\" - Maps listed: %d\n", separator, GI->basedir, mapList->numfilenames ); + Mem_Free( mapList ); + + Msg( "%s\nDirectory: \"%s/maps\" - Maps listed: %d\n", separator, GI->basedir, nummaps ); } /*