From 9b776ab3434809bc1d07367679253c46117852c9 Mon Sep 17 00:00:00 2001 From: myT Date: Tue, 4 Jul 2017 23:09:41 +0200 Subject: [PATCH] keep cvars and commands alphabetically sorted --- changelog.txt | 2 ++ code/qcommon/cmd.cpp | 33 +++++++++++++++++++++++++-------- code/qcommon/cvar.cpp | 27 ++++++++++++++++++++++++--- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/changelog.txt b/changelog.txt index 651a057..649d04f 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,8 @@ DD Mmm 17 - 1.49 +chg: cvars and commands are alphabetically sorted + fix: commands registered by cgame no longer auto-complete after the cgame qvm has shut down chg: removed r_stencilbits, r_depthbits, r_colorbits, r_texturebits and r_ext_compressed_textures diff --git a/code/qcommon/cmd.cpp b/code/qcommon/cmd.cpp index 99994f5..a0ceea1 100644 --- a/code/qcommon/cmd.cpp +++ b/code/qcommon/cmd.cpp @@ -479,8 +479,31 @@ void Cmd_AddCommandEx( const char* cmd_name, xcommand_t function, qbool cgame ) cmd->function = function; cmd->completion = NULL; cmd->cgame = cgame; - cmd->next = cmd_functions; - cmd_functions = cmd; + + // add the command + if ( cmd_functions == NULL || Q_stricmp(cmd_functions->name, cmd_name) > 0 ) { + // insert as the first command + cmd_function_t* const next = cmd_functions; + cmd_functions = cmd; + cmd->next = next; + } else { + // insert after some other command + cmd_function_t* curr = cmd_functions; + cmd_function_t* prev = cmd_functions; + for (;;) { + if ( Q_stricmp(curr->name, cmd_name) > 0 ) + break; + + prev = curr; + if ( curr->next == NULL ) + break; + + curr = curr->next; + } + cmd_function_t* const next = prev->next; + prev->next = cmd; + cmd->next = next; + } } @@ -577,12 +600,6 @@ void Cmd_ExecuteString( const char* text ) for ( prev = &cmd_functions ; *prev ; prev = &cmd->next ) { cmd = *prev; if ( !Q_stricmp( cmd_argv[0],cmd->name ) ) { - // rearrange the links so that the command will be - // near the head of the list next time it is used - *prev = cmd->next; - cmd->next = cmd_functions; - cmd_functions = cmd; - // perform the action if ( !cmd->function ) { // let the cgame or game handle it diff --git a/code/qcommon/cvar.cpp b/code/qcommon/cvar.cpp index f39d6fd..8063fe2 100644 --- a/code/qcommon/cvar.cpp +++ b/code/qcommon/cvar.cpp @@ -322,13 +322,34 @@ breaks every single mod except CPMA otherwise, but it IS wrong, and critically s var->resetString = CopyString( var_value ); // link the variable in - var->next = cvar_vars; - cvar_vars = var; + if ( cvar_vars == NULL || Q_stricmp(cvar_vars->name, var_name) > 0 ) { + // insert as the first cvar + cvar_t* const next = cvar_vars; + cvar_vars = var; + var->next = next; + } else { + // insert after some other cvar + cvar_t* curr = cvar_vars; + cvar_t* prev = cvar_vars; + for (;;) { + if ( Q_stricmp(curr->name, var_name) > 0 ) + break; + + prev = curr; + if ( curr->next == NULL ) + break; + + curr = curr->next; + } + cvar_t* const next = prev->next; + prev->next = var; + var->next = next; + } var->flags = flags; cvar_modifiedFlags |= flags; // needed so USERINFO cvars created by cgame actually get sent - long hash = Cvar_Hash(var_name); + const long hash = Cvar_Hash( var_name ); var->hashNext = hashTable[hash]; hashTable[hash] = var;