Quakespasm's enhanced cvar system

This commit is contained in:
cypress 2024-09-04 17:35:51 -07:00
parent 5b122e8073
commit b76723405a
5 changed files with 574 additions and 109 deletions

View file

@ -1,5 +1,7 @@
/*
Copyright (C) 1996-1997 Id Software, Inc.
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2002-2009 John Fitzgibbons and others
Copyright (C) 2010-2014 QuakeSpasm developers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
@ -8,7 +10,7 @@ of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
@ -21,34 +23,303 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "quakedef.h"
cvar_t *cvar_vars;
char *cvar_null_string = "";
static cvar_t *cvar_vars;
static char cvar_null_string[] = "";
//==============================================================================
//
// USER COMMANDS
//
//==============================================================================
void Cvar_Reset (const char *name); //johnfitz
/*
============
Cvar_List_f -- johnfitz
============
*/
void Cvar_List_f (void)
{
cvar_t *cvar;
const char *partial;
int len, count;
if (Cmd_Argc() > 1)
{
partial = Cmd_Argv (1);
len = Q_strlen(partial);
}
else
{
partial = NULL;
len = 0;
}
count = 0;
for (cvar = cvar_vars ; cvar ; cvar = cvar->next)
{
if (partial && strncmp(partial, cvar->name, len))
{
continue;
}
Con_SafePrintf ("%s%s %s \"%s\"\n",
(cvar->flags & CVAR_ARCHIVE) ? "*" : " ",
(cvar->flags & CVAR_NOTIFY) ? "s" : " ",
cvar->name,
cvar->string);
count++;
}
Con_SafePrintf ("%i cvars", count);
if (partial)
{
Con_SafePrintf (" beginning with \"%s\"", partial);
}
Con_SafePrintf ("\n");
}
/*
============
Cvar_Inc_f -- johnfitz
============
*/
void Cvar_Inc_f (void)
{
switch (Cmd_Argc())
{
default:
case 1:
Con_Printf("inc <cvar> [amount] : increment cvar\n");
break;
case 2:
Cvar_SetValue (Cmd_Argv(1), Cvar_VariableValue(Cmd_Argv(1)) + 1);
break;
case 3:
Cvar_SetValue (Cmd_Argv(1), Cvar_VariableValue(Cmd_Argv(1)) + Q_atof(Cmd_Argv(2)));
break;
}
}
/*
============
Cvar_Toggle_f -- johnfitz
============
*/
void Cvar_Toggle_f (void)
{
switch (Cmd_Argc())
{
default:
case 1:
Con_Printf("toggle <cvar> : toggle cvar\n");
break;
case 2:
if (Cvar_VariableValue(Cmd_Argv(1)))
Cvar_Set (Cmd_Argv(1), "0");
else
Cvar_Set (Cmd_Argv(1), "1");
break;
}
}
/*
============
Cvar_Cycle_f -- johnfitz
============
*/
void Cvar_Cycle_f (void)
{
int i;
if (Cmd_Argc() < 3)
{
Con_Printf("cycle <cvar> <value list>: cycle cvar through a list of values\n");
return;
}
//loop through the args until you find one that matches the current cvar value.
//yes, this will get stuck on a list that contains the same value twice.
//it's not worth dealing with, and i'm not even sure it can be dealt with.
for (i = 2; i < Cmd_Argc(); i++)
{
//zero is assumed to be a string, even though it could actually be zero. The worst case
//is that the first time you call this command, it won't match on zero when it should, but after that,
//it will be comparing strings that all had the same source (the user) so it will work.
if (Q_atof(Cmd_Argv(i)) == 0)
{
if (!strcmp(Cmd_Argv(i), Cvar_VariableString(Cmd_Argv(1))))
break;
}
else
{
if (Q_atof(Cmd_Argv(i)) == Cvar_VariableValue(Cmd_Argv(1)))
break;
}
}
if (i == Cmd_Argc())
Cvar_Set (Cmd_Argv(1), Cmd_Argv(2)); // no match
else if (i + 1 == Cmd_Argc())
Cvar_Set (Cmd_Argv(1), Cmd_Argv(2)); // matched last value in list
else
Cvar_Set (Cmd_Argv(1), Cmd_Argv(i+1)); // matched earlier in list
}
/*
============
Cvar_Reset_f -- johnfitz
============
*/
void Cvar_Reset_f (void)
{
switch (Cmd_Argc())
{
default:
case 1:
Con_Printf ("reset <cvar> : reset cvar to default\n");
break;
case 2:
Cvar_Reset (Cmd_Argv(1));
break;
}
}
/*
============
Cvar_ResetAll_f -- johnfitz
============
*/
void Cvar_ResetAll_f (void)
{
cvar_t *var;
for (var = cvar_vars ; var ; var = var->next)
Cvar_Reset (var->name);
}
/*
============
Cvar_ResetCfg_f -- QuakeSpasm
============
*/
void Cvar_ResetCfg_f (void)
{
cvar_t *var;
for (var = cvar_vars ; var ; var = var->next)
if (var->flags & CVAR_ARCHIVE) Cvar_Reset (var->name);
}
//==============================================================================
//
// INIT
//
//==============================================================================
/*
============
Cvar_Init -- johnfitz
============
*/
void Cvar_Init (void)
{
Cmd_AddCommand ("cvarlist", Cvar_List_f);
Cmd_AddCommand ("toggle", Cvar_Toggle_f);
Cmd_AddCommand ("cycle", Cvar_Cycle_f);
Cmd_AddCommand ("inc", Cvar_Inc_f);
Cmd_AddCommand ("reset", Cvar_Reset_f);
Cmd_AddCommand ("resetall", Cvar_ResetAll_f);
Cmd_AddCommand ("resetcfg", Cvar_ResetCfg_f);
}
//==============================================================================
//
// CVAR FUNCTIONS
//
//==============================================================================
/*
============
Cvar_FindVar
============
*/
cvar_t *Cvar_FindVar (char *var_name)
cvar_t *Cvar_FindVar (const char *var_name)
{
cvar_t *var;
for (var=cvar_vars ; var ; var=var->next)
if (!strcmp (var_name, var->name))
for (var = cvar_vars ; var ; var = var->next)
{
if (!strcmp(var_name, var->name))
return var;
}
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
============
*/
float Cvar_VariableValue (char *var_name)
float Cvar_VariableValue (const char *var_name)
{
cvar_t *var;
var = Cvar_FindVar (var_name);
if (!var)
return 0;
@ -61,10 +332,10 @@ float Cvar_VariableValue (char *var_name)
Cvar_VariableString
============
*/
char *Cvar_VariableString (char *var_name)
const char *Cvar_VariableString (const char *var_name)
{
cvar_t *var;
var = Cvar_FindVar (var_name);
if (!var)
return cvar_null_string;
@ -77,35 +348,114 @@ char *Cvar_VariableString (char *var_name)
Cvar_CompleteVariable
============
*/
char *Cvar_CompleteVariable (char *partial)
const char *Cvar_CompleteVariable (const char *partial)
{
cvar_t *cvar;
int len;
cvar_t *cvar;
int len;
len = Q_strlen(partial);
if (!len)
return NULL;
// check functions
for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
if (!strncmp (partial,cvar->name, len))
for (cvar = cvar_vars ; cvar ; cvar = cvar->next)
{
if (!strncmp(partial, cvar->name, len))
return cvar->name;
}
return NULL;
}
/*
============
Cvar_Reset -- johnfitz
============
*/
void Cvar_Reset (const char *name)
{
cvar_t *var;
var = Cvar_FindVar (name);
if (!var)
Con_Printf ("variable \"%s\" not found\n", name);
else
Cvar_SetQuick (var, var->default_string);
}
void Cvar_SetQuick (cvar_t *var, const char *value)
{
if (var->flags & (CVAR_ROM|CVAR_LOCKED))
return;
if (!(var->flags & CVAR_REGISTERED))
return;
if (!var->string)
var->string = Z_Strdup (value);
else
{
int len;
if (!strcmp(var->string, value))
return; // no change
var->flags |= CVAR_CHANGED;
len = Q_strlen (value);
if (len != Q_strlen(var->string))
{
Z_Free ((void *)var->string);
var->string = (char *) Z_Malloc (len + 1);
}
memcpy ((char *)var->string, value, len + 1);
}
var->value = Q_atof (var->string);
//johnfitz -- save initial value for "reset" command
if (!var->default_string)
var->default_string = Z_Strdup (var->string);
//johnfitz -- during initialization, update default too
else if (!host_initialized)
{
// Sys_Printf("changing default of %s: %s -> %s\n",
// var->name, var->default_string, var->string);
Z_Free ((void *)var->default_string);
var->default_string = Z_Strdup (var->string);
}
//johnfitz
if (var->callback)
var->callback (var);
}
void Cvar_SetValueQuick (cvar_t *var, const float value)
{
char val[32], *ptr = val;
if (value == (float)((int)value))
q_snprintf (val, sizeof(val), "%i", (int)value);
else
{
q_snprintf (val, sizeof(val), "%f", value);
// kill trailing zeroes
while (*ptr)
ptr++;
while (--ptr > val && *ptr == '0' && ptr[-1] != '.')
*ptr = '\0';
}
Cvar_SetQuick (var, val);
}
/*
============
Cvar_Set
============
*/
void Cvar_Set (char *var_name, char *value)
void Cvar_Set (const char *var_name, const char *value)
{
cvar_t *var;
qboolean changed;
cvar_t *var;
var = Cvar_FindVar (var_name);
if (!var)
{ // there is an error in C code if this happens
@ -113,18 +463,7 @@ void Cvar_Set (char *var_name, char *value)
return;
}
changed = strcmp(var->string, value);
Z_Free (var->string); // free the old value string
var->string = Z_Malloc (Q_strlen(value)+1);
Q_strcpy (var->string, value);
var->value = Q_atof (var->string);
if (var->server && changed)
{
if (sv.active)
SV_BroadcastPrintf ("\"%s\" changed to \"%s\"\n", var->name, var->string);
}
Cvar_SetQuick (var, value);
}
/*
@ -132,14 +471,56 @@ void Cvar_Set (char *var_name, char *value)
Cvar_SetValue
============
*/
void Cvar_SetValue (char *var_name, float value)
void Cvar_SetValue (const char *var_name, const float value)
{
char val[32];
sprintf (val, "%f",value);
char val[32], *ptr = val;
if (value == (float)((int)value))
q_snprintf (val, sizeof(val), "%i", (int)value);
else
{
q_snprintf (val, sizeof(val), "%f", value);
// kill trailing zeroes
while (*ptr)
ptr++;
while (--ptr > val && *ptr == '0' && ptr[-1] != '.')
*ptr = '\0';
}
Cvar_Set (var_name, val);
}
/*
============
Cvar_SetROM
============
*/
void Cvar_SetROM (const char *var_name, const char *value)
{
cvar_t *var = Cvar_FindVar (var_name);
if (var)
{
var->flags &= ~CVAR_ROM;
Cvar_SetQuick (var, value);
var->flags |= CVAR_ROM;
}
}
/*
============
Cvar_SetValueROM
============
*/
void Cvar_SetValueROM (const char *var_name, const float value)
{
cvar_t *var = Cvar_FindVar (var_name);
if (var)
{
var->flags &= ~CVAR_ROM;
Cvar_SetValueQuick (var, value);
var->flags |= CVAR_ROM;
}
}
/*
============
@ -150,31 +531,76 @@ Adds a freestanding variable to the variable list.
*/
void Cvar_RegisterVariable (cvar_t *variable)
{
char *oldstr;
// first check to see if it has allready been defined
char value[512];
qboolean set_rom;
cvar_t *cursor,*prev; //johnfitz -- sorted list insert
// 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;
}
// check for overlap with a command
if (Cmd_Exists (variable->name))
{
Con_Printf ("Cvar_RegisterVariable: %s is a command\n", variable->name);
return;
}
// copy the value off, because future sets will Z_Free it
oldstr = variable->string;
variable->string = Z_Malloc (Q_strlen(variable->string)+1);
Q_strcpy (variable->string, oldstr);
variable->value = Q_atof (variable->string);
// link the variable in
variable->next = cvar_vars;
cvar_vars = variable;
//johnfitz -- insert each entry in alphabetical order
if (cvar_vars == NULL ||
strcmp(variable->name, cvar_vars->name) < 0) // insert at front
{
variable->next = cvar_vars;
cvar_vars = variable;
}
else //insert later
{
prev = cvar_vars;
cursor = cvar_vars->next;
while (cursor && (strcmp(variable->name, cursor->name) > 0))
{
prev = cursor;
cursor = cursor->next;
}
variable->next = prev->next;
prev->next = variable;
}
//johnfitz
variable->flags |= CVAR_REGISTERED;
// copy the value off, because future sets will Z_Free it
q_strlcpy (value, variable->string, sizeof(value));
variable->string = NULL;
variable->default_string = NULL;
if (!(variable->flags & CVAR_CALLBACK))
variable->callback = NULL;
// set it through the function to be consistent
set_rom = (variable->flags & CVAR_ROM);
variable->flags &= ~CVAR_ROM;
Cvar_SetQuick (variable, value);
if (set_rom)
variable->flags |= CVAR_ROM;
}
/*
============
Cvar_SetCallback
Set a callback function to the var
============
*/
void Cvar_SetCallback (cvar_t *var, cvarcallback_t func)
{
var->callback = func;
if (func)
var->flags |= CVAR_CALLBACK;
else var->flags &= ~CVAR_CALLBACK;
}
/*
@ -192,7 +618,7 @@ qboolean Cvar_Command (void)
v = Cvar_FindVar (Cmd_Argv(0));
if (!v)
return false;
// perform a variable print or set
if (Cmd_Argc() == 1)
{
@ -216,9 +642,11 @@ with the archive flag set to true.
void Cvar_WriteVariables (FILE *f)
{
cvar_t *var;
for (var = cvar_vars ; var ; var = var->next)
if (var->archive)
{
if (var->flags & CVAR_ARCHIVE)
fprintf (f, "%s \"%s\"\n", var->name, var->string);
}
}

View file

@ -1,5 +1,7 @@
/*
Copyright (C) 1996-1997 Id Software, Inc.
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2002-2009 John Fitzgibbons and others
Copyright (C) 2010-2014 QuakeSpasm developers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
@ -8,7 +10,7 @@ of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
@ -17,21 +19,27 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// cvar.h
#ifndef __CVAR_H__
#define __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);
@ -47,51 +55,89 @@ 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 (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)
#define CVAR_LOCKED (1U << 8) // locked temporarily
#define CVAR_REGISTERED (1U << 10) // the var is added to the list of variables
#define CVAR_CALLBACK (1U << 16) // var has a callback
typedef void (*cvarcallback_t) (struct cvar_s *);
typedef struct cvar_s
{
char *name;
char *string;
qboolean archive; // set to true to cause it to be saved to vars.rc
qboolean server; // notifies players when changed
float value;
struct cvar_s *next;
const char *name;
const char *string;
unsigned int flags;
float value;
const char *default_string; //johnfitz -- remember defaults for reset function
cvarcallback_t callback;
struct cvar_s *next;
} cvar_t;
void Cvar_RegisterVariable (cvar_t *variable);
// registers a cvar that allready has the name, string, and optionally the
// archive elements set.
void Cvar_RegisterVariable (cvar_t *variable);
// registers a cvar that already has the name, string, and optionally
// the archive elements set.
void Cvar_Set (char *var_name, char *value);
void Cvar_SetCallback (cvar_t *var, cvarcallback_t func);
// set a callback function to the var
void Cvar_Set (const char *var_name, const char *value);
// equivelant to "<name> <variable>" typed at the console
void Cvar_SetValue (char *var_name, float value);
void Cvar_SetValue (const char *var_name, const float value);
// expands value to a string and calls Cvar_Set
float Cvar_VariableValue (char *var_name);
void Cvar_SetROM (const char *var_name, const char *value);
void Cvar_SetValueROM (const char *var_name, const float value);
// sets a CVAR_ROM variable from within the engine
void Cvar_SetQuick (cvar_t *var, const char *value);
void Cvar_SetValueQuick (cvar_t *var, const float value);
// these two accept a cvar pointer instead of a var name,
// but are otherwise identical to the "non-Quick" versions.
// the cvar MUST be registered.
float Cvar_VariableValue (const char *var_name);
// returns 0 if not defined or non numeric
char *Cvar_VariableString (char *var_name);
const char *Cvar_VariableString (const char *var_name);
// returns an empty string if not defined
char *Cvar_CompleteVariable (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
// was handled. (print or change)
void Cvar_WriteVariables (FILE *f);
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 (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);
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__ */
extern cvar_t *cvar_vars;

View file

@ -924,31 +924,14 @@ static qsocket_t *_Datagram_CheckNewConnections (void)
if (command == CCREQ_RULE_INFO)
{
char *prevCvarName;
cvar_t *var;
const char *prevCvarName;
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->server)
break;
var = var->next;
}
var = Cvar_FindVarAfter (prevCvarName, CVAR_SERVERINFO);
// send the response
SZ_Clear(&net_message);
// save space for the header, filled in later
MSG_WriteLong(&net_message, 0);

View file

@ -210,6 +210,13 @@ void *Z_TagMalloc (int size, int tag)
return (void *) ((byte *)base + sizeof(memblock_t));
}
char *Z_Strdup (const char *s)
{
size_t sz = strlen(s) + 1;
char *ptr = (char *) Z_Malloc (sz);
memcpy (ptr, s, sz);
return ptr;
}
/*
========================

View file

@ -92,6 +92,7 @@ void *Z_TagMalloc (int size, int tag);
void Z_DumpHeap (void);
void Z_CheckHeap (void);
int Z_FreeMemory (void);
char *Z_Strdup (const char *s);
void *Hunk_Alloc (int size); // returns 0 filled memory
void *Hunk_AllocName (int size, char *name);