mirror of
https://git.code.sf.net/p/quake/quakeforge-old
synced 2024-11-26 05:40:49 +00:00
332 lines
6.5 KiB
C
332 lines
6.5 KiB
C
/*
|
|
cvar.c - dynamic variable tracking
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
Portions Copyright (C) 1999,2000 Nelson Rush.
|
|
Copyright (C) 1999,2000 contributors of the QuakeForge project
|
|
Please see the file "AUTHORS" for a list of contributors
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU General Public License
|
|
as published by the Free Software Foundation; either version 2
|
|
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.
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
#include <zone.h>
|
|
#include <qtypes.h>
|
|
#include <qstructs.h>
|
|
#include <lib_replace.h>
|
|
#include <console.h>
|
|
#include <cmd.h>
|
|
#include <client.h>
|
|
#include <stdlib.h>
|
|
#ifdef UQUAKE
|
|
#include <server.h>
|
|
#endif
|
|
#if defined(QUAKEWORLD) && defined(SERVERONLY)
|
|
#include <server.h>
|
|
#endif
|
|
#include <string.h>
|
|
|
|
#if !defined(HAVE_SNPRINTF) && defined(HAVE__SNPRINTF)
|
|
# define snprintf _snprintf
|
|
#endif
|
|
|
|
cvar_t *cvar_vars;
|
|
char *cvar_null_string = "";
|
|
|
|
/*
|
|
============
|
|
Cvar_FindVar
|
|
============
|
|
*/
|
|
cvar_t *Cvar_FindVar (char *var_name)
|
|
{
|
|
cvar_t *var;
|
|
|
|
for (var=cvar_vars ; var ; var=var->next)
|
|
if (!Q_strcmp (var_name, var->name))
|
|
return var;
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
============
|
|
Cvar_VariableValue
|
|
============
|
|
*/
|
|
float Cvar_VariableValue (char *var_name)
|
|
{
|
|
cvar_t *var;
|
|
|
|
var = Cvar_FindVar (var_name);
|
|
if (!var)
|
|
return 0;
|
|
return Q_atof (var->string);
|
|
}
|
|
|
|
|
|
/*
|
|
============
|
|
Cvar_VariableString
|
|
============
|
|
*/
|
|
char *Cvar_VariableString (char *var_name)
|
|
{
|
|
cvar_t *var;
|
|
|
|
var = Cvar_FindVar (var_name);
|
|
if (!var)
|
|
return cvar_null_string;
|
|
return var->string;
|
|
}
|
|
|
|
|
|
/*
|
|
============
|
|
Cvar_CompleteVariable
|
|
============
|
|
*/
|
|
char *Cvar_CompleteVariable (char *partial)
|
|
{
|
|
cvar_t *cvar;
|
|
int len;
|
|
|
|
len = Q_strlen(partial);
|
|
|
|
if (!len)
|
|
return NULL;
|
|
|
|
// check exact match
|
|
for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
|
|
if (!strcmp (partial,cvar->name))
|
|
return cvar->name;
|
|
|
|
// check partial match
|
|
for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
|
|
if (!Q_strncmp (partial,cvar->name, len))
|
|
return cvar->name;
|
|
|
|
return NULL;
|
|
}
|
|
|
|
|
|
#ifdef SERVERONLY
|
|
void SV_SendServerInfoChange(char *key, char *value);
|
|
#endif
|
|
|
|
/*
|
|
============
|
|
Cvar_Set
|
|
============
|
|
*/
|
|
#if defined(QUAKEWORLD)
|
|
void Cvar_Set (char *var_name, char *value)
|
|
{
|
|
cvar_t *var;
|
|
|
|
var = Cvar_FindVar (var_name);
|
|
if (!var)
|
|
{ // there is an error in C code if this happens
|
|
Con_Printf ("Cvar_Set: variable %s not found\n", var_name);
|
|
return;
|
|
}
|
|
if(var->type&CVAR_ROM && !var->first) return;
|
|
#ifdef SERVERONLY
|
|
if (var->type&CVAR_SERVERINFO)
|
|
{
|
|
Info_SetValueForKey (svs.info, var_name, value, MAX_SERVERINFO_STRING);
|
|
SV_SendServerInfoChange(var_name, value);
|
|
// SV_BroadcastCommand ("fullserverinfo \"%s\"\n", svs.info);
|
|
}
|
|
#else
|
|
if (var->type&CVAR_USERINFO)
|
|
{
|
|
Info_SetValueForKey (cls.userinfo, var_name, value, MAX_INFO_STRING);
|
|
if (cls.state >= ca_connected)
|
|
{
|
|
MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
|
|
SZ_Print (&cls.netchan.message, va("setinfo \"%s\" \"%s\"\n", var_name, value));
|
|
}
|
|
}
|
|
#endif
|
|
|
|
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);
|
|
var->first = false;
|
|
}
|
|
#elif defined(UQUAKE)
|
|
void Cvar_Set (char *var_name, char *value)
|
|
{
|
|
cvar_t *var;
|
|
qboolean changed;
|
|
|
|
var = Cvar_FindVar (var_name);
|
|
if (!var)
|
|
{ // there is an error in C code if this happens
|
|
Con_Printf ("Cvar_Set: variable %s not found\n", var_name);
|
|
return;
|
|
}
|
|
|
|
// Don't change if this is a CVAR_ROM
|
|
if(var->type&CVAR_ROM && !var->first) return;
|
|
|
|
changed = Q_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->type&CVAR_NOTIFY && changed)
|
|
{
|
|
if (sv.active)
|
|
SV_BroadcastPrintf ("\"%s\" changed to \"%s\"\n", var->name, var->string);
|
|
}
|
|
var->first = false;
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
============
|
|
Cvar_SetValue
|
|
============
|
|
*/
|
|
void Cvar_SetValue (char *var_name, float value)
|
|
{
|
|
char val[32];
|
|
|
|
snprintf(val, sizeof(val), "%f",value);
|
|
Cvar_Set (var_name, val);
|
|
}
|
|
|
|
|
|
/*
|
|
============
|
|
Cvar_RegisterVariable
|
|
|
|
Adds a freestanding variable to the variable list.
|
|
============
|
|
*/
|
|
void Cvar_RegisterVariable (cvar_t *variable)
|
|
{
|
|
char value[512];
|
|
|
|
// first check to see if it has allready been defined
|
|
if (Cvar_FindVar (variable->name))
|
|
{
|
|
Con_Printf ("Can't register variable %s, allready 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;
|
|
}
|
|
|
|
// link the variable in
|
|
variable->next = cvar_vars;
|
|
cvar_vars = variable;
|
|
|
|
// copy the value off, because future sets will Z_Free it
|
|
strcpy (value, variable->string);
|
|
variable->string = Z_Malloc (1);
|
|
|
|
// set it through the function to be consistant
|
|
variable->first = true;
|
|
Cvar_Set (variable->name, value);
|
|
}
|
|
|
|
/*
|
|
============
|
|
Cvar_Command
|
|
|
|
Handles variable inspection and changing from the console
|
|
============
|
|
*/
|
|
qboolean Cvar_Command (void)
|
|
{
|
|
cvar_t *v;
|
|
|
|
// check variables
|
|
v = Cvar_FindVar (Cmd_Argv(0));
|
|
if (!v)
|
|
return false;
|
|
|
|
// perform a variable print or set
|
|
if (Cmd_Argc() == 1)
|
|
{
|
|
Con_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
|
|
return true;
|
|
}
|
|
|
|
Cvar_Set (v->name, Cmd_Argv(1));
|
|
return true;
|
|
}
|
|
|
|
|
|
/*
|
|
============
|
|
Cvar_WriteVariables
|
|
|
|
Writes lines containing "set variable value" for all variables
|
|
with the archive flag set to true.
|
|
============
|
|
*/
|
|
void Cvar_WriteVariables (QFile *f)
|
|
{
|
|
cvar_t *var;
|
|
|
|
for (var = cvar_vars ; var ; var = var->next)
|
|
if (var->type&CVAR_ARCHIVE)
|
|
Qprintf (f, "%s \"%s\"\n", var->name, var->string);
|
|
}
|
|
|
|
void Cvar_Set_f(void)
|
|
{
|
|
cvar_t *var;
|
|
char *value;
|
|
char *var_name;
|
|
|
|
if (Cmd_Argc() != 3)
|
|
{
|
|
Con_Printf ("usage: set <cvar> <value>\n");
|
|
return;
|
|
}
|
|
var_name = Cmd_Argv (1);
|
|
value = Cmd_Argv (2);
|
|
var = Cvar_FindVar (var_name);
|
|
if (var)
|
|
{
|
|
Cvar_Set (var->name, value);
|
|
}
|
|
else
|
|
{
|
|
var = (cvar_t*)calloc(1,sizeof(cvar_t));
|
|
var->type=CVAR_USER_CREATED|CVAR_HEAP;
|
|
var->name = strdup (var_name);
|
|
var->string = value;
|
|
Cvar_RegisterVariable (var);
|
|
}
|
|
}
|
|
|
|
void Cvar_Init()
|
|
{
|
|
Cmd_AddCommand ("set", Cvar_Set_f);
|
|
}
|