From 93fd580b18b12906f91fb80a0b07427f21c53c0d Mon Sep 17 00:00:00 2001 From: sezero Date: Thu, 29 Dec 2011 12:28:21 +0000 Subject: [PATCH] more cvar cleanups: Added Cvar_LockVar, Cvar_UnlockVar, Cvar_UnlockAll and Cvar_FindVarAfter. Don't expose cvar_vars directly and use Cvar_FindVarAfter instead. several typo fixes, etc. git-svn-id: http://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@570 af15c1b1-3010-417e-b628-4374ebc0bcbd --- Quake/console.c | 3 ++- Quake/cvar.c | 67 +++++++++++++++++++++++++++++++++++++++++++----- Quake/cvar.h | 60 ++++++++++++++++++++++++------------------- Quake/net_dgrm.c | 20 ++------------- 4 files changed, 98 insertions(+), 52 deletions(-) diff --git a/Quake/console.c b/Quake/console.c index bc4c5897..e072e358 100644 --- a/Quake/console.c +++ b/Quake/console.c @@ -887,7 +887,8 @@ void BuildTabList (const char *partial) bash_partial[0] = 0; bash_singlematch = 1; - for (cvar=cvar_vars ; cvar ; cvar=cvar->next) + cvar = Cvar_FindVarAfter ("", CVAR_NONE); + for ( ; cvar ; cvar=cvar->next) if (!Q_strncmp (partial, cvar->name, len)) AddToTabList (cvar->name, "cvar"); diff --git a/Quake/cvar.c b/Quake/cvar.c index 208b89e6..e6213023 100644 --- a/Quake/cvar.c +++ b/Quake/cvar.c @@ -22,7 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.h" -cvar_t *cvar_vars; +static cvar_t *cvar_vars; static char cvar_null_string[] = ""; //============================================================================== @@ -243,6 +243,59 @@ cvar_t *Cvar_FindVar (const char *var_name) return NULL; } +cvar_t *Cvar_FindVarAfter (const char *prev_name, unsigned int with_flags) +{ + cvar_t *var; + + if (*prev_name) + { + var = Cvar_FindVar (prev_name); + if (!var) + return NULL; + var = var->next; + } + else + var = cvar_vars; + + // search for the next cvar matching the needed flags + while (var) + { + if ((var->flags & with_flags) || !with_flags) + break; + var = var->next; + } + return var; +} + +/* +============ +Cvar_LockVar +============ +*/ +void Cvar_LockVar (const char *var_name) +{ + cvar_t *var = Cvar_FindVar (var_name); + if (var) + var->flags |= CVAR_LOCKED; +} + +void Cvar_UnlockVar (const char *var_name) +{ + cvar_t *var = Cvar_FindVar (var_name); + if (var) + var->flags &= ~CVAR_LOCKED; +} + +void Cvar_UnlockAll (void) +{ + cvar_t *var; + + for (var = cvar_vars ; var ; var = var->next) + { + var->flags &= ~CVAR_LOCKED; + } +} + /* ============ Cvar_VariableValue @@ -318,7 +371,7 @@ void Cvar_Reset (const char *name) void Cvar_SetQuick (cvar_t *var, const char *value) { if (var->flags & (CVAR_ROM|CVAR_LOCKED)) - return; // cvar is marked read-only or locked temporarily + return; if (!(var->flags & CVAR_REGISTERED)) return; @@ -356,8 +409,8 @@ void Cvar_SetQuick (cvar_t *var, const char *value) } //johnfitz - if(var->callback) - var->callback(var); + if (var->callback) + var->callback (var); } void Cvar_SetValueQuick (cvar_t *var, const float value) @@ -386,7 +439,7 @@ Cvar_Set */ void Cvar_Set (const char *var_name, const char *value) { - cvar_t *var; + cvar_t *var; var = Cvar_FindVar (var_name); if (!var) @@ -467,10 +520,10 @@ void Cvar_RegisterVariable (cvar_t *variable) qboolean set_rom; cvar_t *cursor,*prev; //johnfitz -- sorted list insert -// first check to see if it has allready been defined +// first check to see if it has already been defined if (Cvar_FindVar (variable->name)) { - Con_Printf ("Can't register variable %s, allready defined\n", variable->name); + Con_Printf ("Can't register variable %s, already defined\n", variable->name); return; } diff --git a/Quake/cvar.h b/Quake/cvar.h index 2507aea7..19717751 100644 --- a/Quake/cvar.h +++ b/Quake/cvar.h @@ -22,21 +22,24 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef __CVAR_H__ #define __CVAR_H__ -// cvar.h - /* -cvar_t variables are used to hold scalar or string variables that can be changed or displayed at the console or prog code as well as accessed directly -in C code. +cvar_t variables are used to hold scalar or string variables that can +be changed or displayed at the console or prog code as well as accessed +directly in C code. -it is sufficient to initialize a cvar_t with just the first two fields, or -you can add a ,true flag for variables that you want saved to the configuration -file when the game is quit: +it is sufficient to initialize a cvar_t with just the first two fields, +or you can add a ,true flag for variables that you want saved to the +configuration file when the game is quit: cvar_t r_draworder = {"r_draworder","1"}; cvar_t scr_screensize = {"screensize","1",true}; -Cvars must be registered before use, or they will have a 0 value instead of the float interpretation of the string. Generally, all cvar_t declarations should be registered in the apropriate init function before any console commands are executed: +Cvars must be registered before use, or they will have a 0 value instead +of the float interpretation of the string. +Generally, all cvar_t declarations should be registered in the apropriate +init function before any console commands are executed: + Cvar_RegisterVariable (&host_framerate); @@ -52,16 +55,18 @@ teamplay = cvar("teamplay"); cvar_set ("registered", "1"); The user can access cvars from the console in two ways: -r_draworder prints the current value +r_draworder prints the current value r_draworder 0 sets the current value to 0 + Cvars are restricted from having the same names as commands to keep this interface from being ambiguous. + */ #define CVAR_NONE 0 #define CVAR_ARCHIVE (1U << 0) // if set, causes it to be saved to config -#define CVAR_NOTIFY (1U << 1) // changes will be broadcasted to all players (H2) -#define CVAR_SERVERINFO (1U << 2) // added to serverinfo will be sent to clients (net_dgrm.c, qwsv) +#define CVAR_NOTIFY (1U << 1) // changes will be broadcasted to all players (q1) +#define CVAR_SERVERINFO (1U << 2) // added to serverinfo will be sent to clients (q1/net_dgrm.c and qwsv) #define CVAR_USERINFO (1U << 3) // added to userinfo, will be sent to server (qwcl) #define CVAR_CHANGED (1U << 4) #define CVAR_ROM (1U << 6) @@ -78,18 +83,18 @@ typedef struct cvar_s const char *string; unsigned int flags; float value; - struct cvar_s *next; const char *default_string; //johnfitz -- remember defaults for reset function - cvarcallback_t callback; //johnfitz + cvarcallback_t callback; + struct cvar_s *next; } cvar_t; +void Cvar_RegisterVariable (cvar_t *variable); +// registers a cvar that already has the name, string, and optionally +// the archive elements set. + void Cvar_SetCallback (cvar_t *var, cvarcallback_t func); // set a callback function to the var -void Cvar_RegisterVariable (cvar_t *variable); -// registers a cvar that allready has the name, string, and optionally the -// archive elements set. - void Cvar_Set (const char *var_name, const char *value); // equivelant to " " typed at the console @@ -109,13 +114,9 @@ void Cvar_SetValueQuick (cvar_t *var, const float value); float Cvar_VariableValue (const char *var_name); // returns 0 if not defined or non numeric -const char *Cvar_VariableString (const char *var_name); +const char *Cvar_VariableString (const char *var_name); // returns an empty string if not defined -const char *Cvar_CompleteVariable (const char *partial); -// attempts to match a partial variable name for command line completion -// returns NULL if nothing fits - qboolean Cvar_Command (void); // called by Cmd_ExecuteString when Cmd_Argv(0) doesn't match a known // command. Returns true if the command was a variable reference that @@ -123,13 +124,20 @@ qboolean Cvar_Command (void); void Cvar_WriteVariables (FILE *f); // Writes lines containing "set variable value" for all variables -// with the archive flag set to true. +// with the CVAR_ARCHIVE flag set -cvar_t *Cvar_FindVar (const char *var_name); +cvar_t *Cvar_FindVar (const char *var_name); +cvar_t *Cvar_FindVarAfter (const char *prev_name, unsigned int with_flags); -void Cvar_Init (void); +void Cvar_LockVar (const char *var_name); +void Cvar_UnlockVar (const char *var_name); +void Cvar_UnlockAll (void); -extern cvar_t *cvar_vars; +void Cvar_Init (void); + +const char *Cvar_CompleteVariable (const char *partial); +// attempts to match a partial variable name for command line completion +// returns NULL if nothing fits #endif /* __CVAR_H__ */ diff --git a/Quake/net_dgrm.c b/Quake/net_dgrm.c index 4bcd0f60..a2f8eea7 100644 --- a/Quake/net_dgrm.c +++ b/Quake/net_dgrm.c @@ -956,27 +956,11 @@ static qsocket_t *_Datagram_CheckNewConnections (void) if (command == CCREQ_RULE_INFO) { const char *prevCvarName; - cvar_t *var; + cvar_t *var; // find the search start location prevCvarName = MSG_ReadString(); - if (*prevCvarName) - { - var = Cvar_FindVar (prevCvarName); - if (!var) - return NULL; - var = var->next; - } - else - var = cvar_vars; - - // search for the next server cvar - while (var) - { - if (var->flags & CVAR_SERVERINFO) - break; - var = var->next; - } + var = Cvar_FindVarAfter (prevCvarName, CVAR_SERVERINFO); // send the response SZ_Clear(&net_message);