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
This commit is contained in:
sezero 2011-12-29 12:28:21 +00:00
parent 62cd7643a3
commit 93fd580b18
4 changed files with 98 additions and 52 deletions

View file

@ -887,7 +887,8 @@ void BuildTabList (const char *partial)
bash_partial[0] = 0; bash_partial[0] = 0;
bash_singlematch = 1; 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)) if (!Q_strncmp (partial, cvar->name, len))
AddToTabList (cvar->name, "cvar"); AddToTabList (cvar->name, "cvar");

View file

@ -22,7 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "quakedef.h" #include "quakedef.h"
cvar_t *cvar_vars; static cvar_t *cvar_vars;
static char cvar_null_string[] = ""; static char cvar_null_string[] = "";
//============================================================================== //==============================================================================
@ -243,6 +243,59 @@ cvar_t *Cvar_FindVar (const char *var_name)
return NULL; 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 Cvar_VariableValue
@ -318,7 +371,7 @@ void Cvar_Reset (const char *name)
void Cvar_SetQuick (cvar_t *var, const char *value) void Cvar_SetQuick (cvar_t *var, const char *value)
{ {
if (var->flags & (CVAR_ROM|CVAR_LOCKED)) if (var->flags & (CVAR_ROM|CVAR_LOCKED))
return; // cvar is marked read-only or locked temporarily return;
if (!(var->flags & CVAR_REGISTERED)) if (!(var->flags & CVAR_REGISTERED))
return; return;
@ -467,10 +520,10 @@ void Cvar_RegisterVariable (cvar_t *variable)
qboolean set_rom; qboolean set_rom;
cvar_t *cursor,*prev; //johnfitz -- sorted list insert 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)) 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; return;
} }

View file

@ -22,21 +22,24 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef __CVAR_H__ #ifndef __CVAR_H__
#define __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 cvar_t variables are used to hold scalar or string variables that can
in C code. 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 it is sufficient to initialize a cvar_t with just the first two fields,
you can add a ,true flag for variables that you want saved to the configuration or you can add a ,true flag for variables that you want saved to the
file when the game is quit: configuration file when the game is quit:
cvar_t r_draworder = {"r_draworder","1"}; cvar_t r_draworder = {"r_draworder","1"};
cvar_t scr_screensize = {"screensize","1",true}; 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); Cvar_RegisterVariable (&host_framerate);
@ -54,14 +57,16 @@ cvar_set ("registered", "1");
The user can access cvars from the console in two ways: 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 r_draworder 0 sets the current value to 0
Cvars are restricted from having the same names as commands to keep this Cvars are restricted from having the same names as commands to keep this
interface from being ambiguous. interface from being ambiguous.
*/ */
#define CVAR_NONE 0 #define CVAR_NONE 0
#define CVAR_ARCHIVE (1U << 0) // if set, causes it to be saved to config #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_NOTIFY (1U << 1) // changes will be broadcasted to all players (q1)
#define CVAR_SERVERINFO (1U << 2) // added to serverinfo will be sent to clients (net_dgrm.c, qwsv) #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_USERINFO (1U << 3) // added to userinfo, will be sent to server (qwcl)
#define CVAR_CHANGED (1U << 4) #define CVAR_CHANGED (1U << 4)
#define CVAR_ROM (1U << 6) #define CVAR_ROM (1U << 6)
@ -78,18 +83,18 @@ typedef struct cvar_s
const char *string; const char *string;
unsigned int flags; unsigned int flags;
float value; float value;
struct cvar_s *next;
const char *default_string; //johnfitz -- remember defaults for reset function const char *default_string; //johnfitz -- remember defaults for reset function
cvarcallback_t callback; //johnfitz cvarcallback_t callback;
struct cvar_s *next;
} cvar_t; } 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); void Cvar_SetCallback (cvar_t *var, cvarcallback_t func);
// set a callback function to the var // 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); void Cvar_Set (const char *var_name, const char *value);
// equivelant to "<name> <variable>" typed at the console // equivelant to "<name> <variable>" typed at the console
@ -112,10 +117,6 @@ float Cvar_VariableValue (const char *var_name);
const char *Cvar_VariableString (const char *var_name); const char *Cvar_VariableString (const char *var_name);
// returns an empty string if not defined // 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); qboolean Cvar_Command (void);
// called by Cmd_ExecuteString when Cmd_Argv(0) doesn't match a known // called by Cmd_ExecuteString when Cmd_Argv(0) doesn't match a known
// command. Returns true if the command was a variable reference that // command. Returns true if the command was a variable reference that
@ -123,13 +124,20 @@ qboolean Cvar_Command (void);
void Cvar_WriteVariables (FILE *f); void Cvar_WriteVariables (FILE *f);
// Writes lines containing "set variable value" for all variables // 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_LockVar (const char *var_name);
void Cvar_UnlockVar (const char *var_name);
void Cvar_UnlockAll (void);
void Cvar_Init (void); void Cvar_Init (void);
extern cvar_t *cvar_vars; 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__ */ #endif /* __CVAR_H__ */

View file

@ -960,23 +960,7 @@ static qsocket_t *_Datagram_CheckNewConnections (void)
// find the search start location // find the search start location
prevCvarName = MSG_ReadString(); prevCvarName = MSG_ReadString();
if (*prevCvarName) var = Cvar_FindVarAfter (prevCvarName, CVAR_SERVERINFO);
{
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;
}
// send the response // send the response
SZ_Clear(&net_message); SZ_Clear(&net_message);