From 7bfc5d9487c7207a9b23d377fd4d8ccb4e44e389 Mon Sep 17 00:00:00 2001 From: myT Date: Wed, 7 Feb 2018 06:51:37 +0100 Subject: [PATCH] using the registered cvar/cmd names in the help panel instead of argument 0 cleaned up cmd.cpp with Cmd_FindCommand --- code/qcommon/cmd.cpp | 93 +++++++++++++++++++++-------------------- code/qcommon/common.cpp | 6 +++ code/qcommon/cvar.cpp | 10 +++++ code/qcommon/qcommon.h | 4 ++ 4 files changed, 68 insertions(+), 45 deletions(-) diff --git a/code/qcommon/cmd.cpp b/code/qcommon/cmd.cpp index 1e69990..92c3b92 100644 --- a/code/qcommon/cmd.cpp +++ b/code/qcommon/cmd.cpp @@ -589,57 +589,55 @@ void Cmd_RemoveCommand( const char* cmd_name ) } -void Cmd_SetHelp( const char* cmd_name, const char* cmd_help ) +static cmd_function_t* Cmd_FindCommand( const char* cmd_name ) { cmd_function_t* cmd; for ( cmd = cmd_functions; cmd; cmd = cmd->next ) { if ( !Q_stricmp( cmd_name, cmd->name ) ) { - Help_AllocSplitText( &cmd->desc, &cmd->help, cmd_help ); - return; + return cmd; } } + + return NULL; +} + + +void Cmd_SetHelp( const char* cmd_name, const char* cmd_help ) +{ + cmd_function_t* cmd = Cmd_FindCommand( cmd_name ); + if ( cmd ) + Help_AllocSplitText( &cmd->desc, &cmd->help, cmd_help ); } qbool Cmd_GetHelp( const char** desc, const char** help, const char* cmd_name ) { - cmd_function_t* cmd; - for ( cmd = cmd_functions; cmd; cmd = cmd->next ) { - if ( !Q_stricmp( cmd_name, cmd->name ) ) { - *desc = cmd->desc; - *help = cmd->help; - return qtrue; - } + cmd_function_t* cmd = Cmd_FindCommand( cmd_name ); + if ( !cmd ) { + *desc = NULL; + *help = NULL; + return qfalse; } - *desc = NULL; - *help = NULL; - return qfalse; + *desc = cmd->desc; + *help = cmd->help; + return qtrue; } void Cmd_SetAutoCompletion( const char* cmd_name, xcommandCompletion_t completion ) { - cmd_function_t* cmd; - for ( cmd = cmd_functions; cmd; cmd = cmd->next ) { - if ( !Q_stricmp( cmd_name, cmd->name ) ) { - cmd->completion = completion; - return; - } - } + cmd_function_t* cmd = Cmd_FindCommand( cmd_name ); + if ( cmd ) + cmd->completion = completion; } void Cmd_AutoCompleteArgument( const char* cmd_name, int startArg, int compArg ) { - const cmd_function_t* cmd; - for ( cmd = cmd_functions; cmd; cmd = cmd->next ) { - if ( !Q_stricmp( cmd_name, cmd->name ) ) { - if ( cmd->completion ) - cmd->completion( startArg, compArg ); - return; - } - } + cmd_function_t* cmd = Cmd_FindCommand( cmd_name ); + if ( cmd && cmd->completion ) + cmd->completion( startArg, compArg ); } @@ -783,15 +781,13 @@ void Cmd_UnregisterTable( const cmdTableItem_t* cmds, int count ) void Cmd_SetModule( const char* cmd_name, module_t module ) { - cmd_function_t* cmd; - for ( cmd = cmd_functions; cmd; cmd = cmd->next ) { - if ( !Q_stricmp( cmd_name, cmd->name ) ) { - cmd->moduleMask |= 1 << (int)module; - if ( cmd->firstModule == MODULE_NONE ) - cmd->firstModule = module; - return; - } - } + cmd_function_t* cmd = Cmd_FindCommand( cmd_name ); + if ( !cmd ) + return; + + cmd->moduleMask |= 1 << (int)module; + if ( cmd->firstModule == MODULE_NONE ) + cmd->firstModule = module; } @@ -816,13 +812,20 @@ void Cmd_UnregisterModule( module_t module ) void Cmd_GetModuleInfo( module_t* firstModule, int* moduleMask, const char* cmd_name ) { - cmd_function_t* cmd; - for ( cmd = cmd_functions; cmd; cmd = cmd->next ) { - if ( !Q_stricmp( cmd_name, cmd->name ) ) { - *firstModule = cmd->firstModule; - *moduleMask = cmd->moduleMask; - return; - } - } + cmd_function_t* cmd = Cmd_FindCommand( cmd_name ); + if ( !cmd ) + return; + + *firstModule = cmd->firstModule; + *moduleMask = cmd->moduleMask; } + +const char* Cmd_GetRegisteredName( const char* cmd_name ) +{ + cmd_function_t* cmd = Cmd_FindCommand( cmd_name ); + if ( !cmd ) + return NULL; + + return cmd->name; +} diff --git a/code/qcommon/common.cpp b/code/qcommon/common.cpp index 92f072b..208ce49 100644 --- a/code/qcommon/common.cpp +++ b/code/qcommon/common.cpp @@ -3423,19 +3423,25 @@ printHelpResult_t Com_PrintHelp( const char* name, printf_t print, qbool printNo qbool isCvar = qfalse; const char *desc; const char *help; + const char* registeredName; module_t firstModule; int moduleMask; if ( Cvar_GetHelp( &desc, &help, name ) ) { isCvar = qtrue; Cvar_GetModuleInfo( &firstModule, &moduleMask, name ); + registeredName = Cvar_GetRegisteredName( name ); } else if ( Cmd_GetHelp( &desc, &help, name ) ) { Cmd_GetModuleInfo( &firstModule, &moduleMask, name ); + registeredName = Cmd_GetRegisteredName( name ); } else { if ( printNotFound ) print( "found no cvar/command with the name '%s'\n", name ); return PHR_NOTFOUND; } + if ( registeredName ) + name = registeredName; + if ( isCvar ) Cvar_PrintFirstHelpLine( name, print ); else diff --git a/code/qcommon/cvar.cpp b/code/qcommon/cvar.cpp index 64f1f17..e71a339 100644 --- a/code/qcommon/cvar.cpp +++ b/code/qcommon/cvar.cpp @@ -633,6 +633,16 @@ void Cvar_GetModuleInfo( module_t *firstModule, int *moduleMask, const char *var } +const char* Cvar_GetRegisteredName( const char *var_name ) +{ + cvar_t* var = Cvar_FindVar( var_name ); + if ( !var ) + return NULL; + + return var->name; +} + + static const char* Cvar_FormatRangeFloat( float vf ) { const int vi = (int)vf; diff --git a/code/qcommon/qcommon.h b/code/qcommon/qcommon.h index 3f9db08..38d5b16 100644 --- a/code/qcommon/qcommon.h +++ b/code/qcommon/qcommon.h @@ -400,6 +400,8 @@ void Cmd_UnregisterModule( module_t module ); void Cmd_GetModuleInfo( module_t* firstModule, int* moduleMask, const char* cmd_name ); +const char* Cmd_GetRegisteredName( const char* cmd_name ); + // auto-completion of command arguments void Cmd_SetAutoCompletion( const char* cmd_name, xcommandCompletion_t complete ); void Cmd_AutoCompleteArgument( const char* cmd_name, int startArg, int compArg ); @@ -526,6 +528,8 @@ void Cvar_RegisterTable( const cvarTableItem_t* cvars, int count, module_t modul void Cvar_SetModule( const char *var_name, module_t module ); void Cvar_GetModuleInfo( module_t *firstModule, int *moduleMask, const char *var_name ); + +const char* Cvar_GetRegisteredName( const char *var_name ); void Cvar_PrintTypeAndRange( const char *var_name, printf_t print ); void Cvar_PrintFirstHelpLine( const char *var_name, printf_t print );