2001-02-21 19:35:06 +00:00
|
|
|
/*
|
|
|
|
cvar.c
|
|
|
|
|
|
|
|
dynamic variable tracking
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
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:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/hash.h"
|
2002-04-25 20:08:18 +00:00
|
|
|
#include "QF/mathlib.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/qargs.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.h"
|
2001-09-21 04:22:46 +00:00
|
|
|
#include "QF/sys.h"
|
2001-10-30 23:33:47 +00:00
|
|
|
#include "QF/va.h"
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2001-05-30 02:41:30 +00:00
|
|
|
#define USER_RO_CVAR "User-created READ-ONLY Cvar"
|
|
|
|
#define USER_CVAR "User-created cvar"
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE cvar_t *developer;
|
|
|
|
VISIBLE cvar_t *cvar_vars;
|
|
|
|
static const char *cvar_null_string = "";
|
|
|
|
static cvar_alias_t *calias_vars;
|
|
|
|
static hashtab_t *cvar_hash;
|
|
|
|
static hashtab_t *calias_hash;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE cvar_t *
|
2001-07-15 07:04:17 +00:00
|
|
|
Cvar_FindVar (const char *var_name)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
return (cvar_t*) Hash_Find (cvar_hash, var_name);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE cvar_t *
|
2001-07-15 07:04:17 +00:00
|
|
|
Cvar_FindAlias (const char *alias_name)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cvar_alias_t *alias;
|
|
|
|
|
|
|
|
alias = (cvar_alias_t*) Hash_Find (calias_hash, alias_name);
|
|
|
|
if (alias)
|
|
|
|
return alias->cvar;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-05 11:50:02 +00:00
|
|
|
cvar_t *
|
2011-07-05 11:28:57 +00:00
|
|
|
Cvar_MakeAlias (const char *name, cvar_t *cvar)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cvar_alias_t *alias;
|
|
|
|
cvar_t *var;
|
|
|
|
|
|
|
|
if (Cmd_Exists (name)) {
|
2011-07-05 11:50:02 +00:00
|
|
|
Sys_Printf ("Cvar_MakeAlias: %s is a command\n", name);
|
|
|
|
return 0;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
if (Cvar_FindVar (name)) {
|
2011-07-05 11:50:02 +00:00
|
|
|
Sys_Printf ("Cvar_MakeAlias: %s is a cvar\n",
|
|
|
|
name);
|
|
|
|
return 0;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
var = Cvar_FindAlias (name);
|
2011-07-05 11:50:02 +00:00
|
|
|
if (var && var != cvar) {
|
|
|
|
Sys_Printf ("Cvar_MakeAlias: %s is an alias to %s\n",
|
|
|
|
name, var->name);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
if (!var) {
|
|
|
|
alias = (cvar_alias_t *) calloc (1, sizeof (cvar_alias_t));
|
|
|
|
|
|
|
|
alias->next = calias_vars;
|
|
|
|
calias_vars = alias;
|
|
|
|
alias->name = strdup (name);
|
|
|
|
alias->cvar = cvar;
|
|
|
|
Hash_Add (calias_hash, alias);
|
|
|
|
}
|
2011-07-05 11:50:02 +00:00
|
|
|
return cvar;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE float
|
2001-07-15 07:04:17 +00:00
|
|
|
Cvar_VariableValue (const char *var_name)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cvar_t *var;
|
|
|
|
|
|
|
|
var = Cvar_FindVar (var_name);
|
|
|
|
if (!var)
|
|
|
|
var = Cvar_FindAlias (var_name);
|
|
|
|
if (!var)
|
|
|
|
return 0;
|
|
|
|
return atof (var->string);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char *
|
2001-07-15 07:04:17 +00:00
|
|
|
Cvar_VariableString (const char *var_name)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cvar_t *var;
|
|
|
|
|
|
|
|
var = Cvar_FindVar (var_name);
|
|
|
|
if (!var)
|
|
|
|
var = Cvar_FindAlias (var_name);
|
|
|
|
if (!var)
|
|
|
|
return cvar_null_string;
|
|
|
|
return var->string;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char *
|
2001-07-15 07:04:17 +00:00
|
|
|
Cvar_CompleteVariable (const char *partial)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cvar_t *cvar;
|
|
|
|
cvar_alias_t *alias;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = strlen (partial);
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// check exact match
|
|
|
|
for (cvar = cvar_vars; cvar; cvar = cvar->next)
|
2007-06-09 06:55:15 +00:00
|
|
|
if (!strcmp (partial, cvar->name))
|
2001-02-21 19:35:06 +00:00
|
|
|
return cvar->name;
|
|
|
|
|
|
|
|
// check aliases too :)
|
|
|
|
for (alias = calias_vars; alias; alias = alias->next)
|
2007-06-09 06:55:15 +00:00
|
|
|
if (!strcmp (partial, alias->name))
|
2001-02-21 19:35:06 +00:00
|
|
|
return alias->name;
|
|
|
|
|
|
|
|
// check partial match
|
|
|
|
for (cvar = cvar_vars; cvar; cvar = cvar->next)
|
2007-06-09 06:55:15 +00:00
|
|
|
if (!strncmp (partial, cvar->name, len))
|
2001-02-21 19:35:06 +00:00
|
|
|
return cvar->name;
|
|
|
|
|
|
|
|
// check aliases too :)
|
|
|
|
for (alias = calias_vars; alias; alias = alias->next)
|
2007-06-09 06:55:15 +00:00
|
|
|
if (!strncmp (partial, alias->name, len))
|
2001-02-21 19:35:06 +00:00
|
|
|
return alias->name;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-06-28 04:05:14 +00:00
|
|
|
/*
|
|
|
|
CVar_CompleteCountPossible
|
|
|
|
|
|
|
|
New function for tab-completion system
|
|
|
|
Added by EvilTypeGuy
|
|
|
|
Thanks to Fett erich@heintz.com
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2001-07-15 07:04:17 +00:00
|
|
|
Cvar_CompleteCountPossible (const char *partial)
|
2001-06-28 04:05:14 +00:00
|
|
|
{
|
|
|
|
cvar_t *cvar;
|
|
|
|
int len;
|
|
|
|
int h;
|
|
|
|
|
|
|
|
h = 0;
|
|
|
|
len = strlen(partial);
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Loop through the cvars and count all possible matches
|
|
|
|
for (cvar = cvar_vars; cvar; cvar = cvar->next)
|
2007-06-09 06:55:15 +00:00
|
|
|
if (!strncmp(partial, cvar->name, len))
|
2001-06-28 04:05:14 +00:00
|
|
|
h++;
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
CVar_CompleteBuildList
|
|
|
|
|
|
|
|
New function for tab-completion system
|
|
|
|
Added by EvilTypeGuy
|
|
|
|
Thanks to Fett erich@heintz.com
|
|
|
|
Thanks to taniwha
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char **
|
2001-07-15 07:04:17 +00:00
|
|
|
Cvar_CompleteBuildList (const char *partial)
|
2001-06-28 04:05:14 +00:00
|
|
|
{
|
|
|
|
cvar_t *cvar;
|
|
|
|
int len = 0;
|
|
|
|
int bpos = 0;
|
2001-08-22 22:03:16 +00:00
|
|
|
int sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) *
|
|
|
|
sizeof (char *);
|
2001-07-15 07:04:17 +00:00
|
|
|
const char **buf;
|
2001-06-28 04:05:14 +00:00
|
|
|
|
|
|
|
len = strlen(partial);
|
|
|
|
buf = malloc(sizeofbuf + sizeof (char *));
|
2002-05-14 06:37:28 +00:00
|
|
|
SYS_CHECKMEM (buf);
|
2001-06-28 04:05:14 +00:00
|
|
|
// Loop through the alias list and print all matches
|
|
|
|
for (cvar = cvar_vars; cvar; cvar = cvar->next)
|
2007-06-09 06:55:15 +00:00
|
|
|
if (!strncmp(partial, cvar->name, len))
|
2001-06-28 04:05:14 +00:00
|
|
|
buf[bpos++] = cvar->name;
|
|
|
|
|
|
|
|
buf[bpos] = NULL;
|
|
|
|
return buf;
|
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-07-15 07:04:17 +00:00
|
|
|
Cvar_Set (cvar_t *var, const char *value)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2001-02-22 08:12:13 +00:00
|
|
|
int changed;
|
2002-04-25 20:08:18 +00:00
|
|
|
int vals;
|
2001-02-22 08:12:13 +00:00
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
if (!var)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (var->flags & CVAR_ROM) {
|
2010-11-23 05:09:30 +00:00
|
|
|
Sys_MaskPrintf (SYS_DEV, "Cvar \"%s\" is read-only, cannot modify\n",
|
|
|
|
var->name);
|
2001-02-21 19:35:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-02-24 04:25:27 +00:00
|
|
|
changed = !strequal (var->string, value);
|
2001-07-15 07:04:17 +00:00
|
|
|
free ((char*)var->string); // free the old value string
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
var->string = strdup (value);
|
|
|
|
var->value = atof (var->string);
|
|
|
|
var->int_val = atoi (var->string);
|
2002-04-25 20:08:18 +00:00
|
|
|
VectorZero (var->vec);
|
|
|
|
vals = sscanf (var->string, "%f %f %f",
|
|
|
|
&var->vec[0], &var->vec[1], &var->vec[2]);
|
|
|
|
if (vals == 1)
|
|
|
|
var->vec[2] = var->vec[1] = var->vec[0];
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2001-03-31 01:02:52 +00:00
|
|
|
if (changed && var->callback)
|
|
|
|
var->callback (var);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Cvar_SetROM
|
|
|
|
|
|
|
|
doesn't check for CVAR_ROM flag
|
|
|
|
*/
|
|
|
|
void
|
2001-07-15 07:04:17 +00:00
|
|
|
Cvar_SetROM (cvar_t *var, const char *value)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2001-02-22 08:12:13 +00:00
|
|
|
int changed;
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
if (!var)
|
|
|
|
return;
|
|
|
|
|
2001-02-24 04:25:27 +00:00
|
|
|
changed = !strequal (var->string, value);
|
2001-07-15 07:04:17 +00:00
|
|
|
free ((char*)var->string); // free the old value string
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
var->string = strdup (value);
|
|
|
|
var->value = atof (var->string);
|
|
|
|
var->int_val = atoi (var->string);
|
|
|
|
sscanf (var->string, "%f %f %f", &var->vec[0], &var->vec[1], &var->vec[2]);
|
|
|
|
|
2001-03-31 01:02:52 +00:00
|
|
|
if (changed && var->callback)
|
|
|
|
var->callback (var);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-02-21 19:35:06 +00:00
|
|
|
Cvar_SetValue (cvar_t *var, float value)
|
|
|
|
{
|
2003-05-08 23:24:02 +00:00
|
|
|
Cvar_Set (var, va ("%g", value));
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Cvar_Command
|
|
|
|
|
|
|
|
Handles variable inspection and changing from the console
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE qboolean
|
2001-02-21 19:35:06 +00:00
|
|
|
Cvar_Command (void)
|
|
|
|
{
|
|
|
|
cvar_t *v;
|
|
|
|
|
|
|
|
// check variables
|
|
|
|
v = Cvar_FindVar (Cmd_Argv (0));
|
|
|
|
if (!v)
|
|
|
|
v = Cvar_FindAlias (Cmd_Argv (0));
|
|
|
|
if (!v)
|
|
|
|
return false;
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
// perform a variable print or set
|
2001-02-21 19:35:06 +00:00
|
|
|
if (Cmd_Argc () == 1) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
|
2001-02-21 19:35:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cvar_Set (v, Cmd_Argv (1));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Cvar_WriteVariables
|
|
|
|
|
2004-03-18 05:58:06 +00:00
|
|
|
Writes lines containing "seta variable value" for all variables
|
2001-02-21 19:35:06 +00:00
|
|
|
with the archive flag set to true.
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-08-27 07:16:28 +00:00
|
|
|
Cvar_WriteVariables (QFile *f)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cvar_t *var;
|
|
|
|
|
|
|
|
for (var = cvar_vars; var; var = var->next)
|
|
|
|
if (var->flags & CVAR_ARCHIVE)
|
2004-03-18 05:58:06 +00:00
|
|
|
Qprintf (f, "seta %s \"%s\"\n", var->name, var->string);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2004-03-18 05:58:06 +00:00
|
|
|
set_cvar (const char *cmd, int orflags)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cvar_t *var;
|
2001-07-15 07:04:17 +00:00
|
|
|
const char *value;
|
|
|
|
const char *var_name;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
if (Cmd_Argc () != 3) {
|
2004-03-18 05:58:06 +00:00
|
|
|
Sys_Printf ("usage: %s <cvar> <value>\n", cmd);
|
2001-02-21 19:35:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-03-18 05:58:06 +00:00
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
var_name = Cmd_Argv (1);
|
|
|
|
value = Cmd_Argv (2);
|
|
|
|
var = Cvar_FindVar (var_name);
|
|
|
|
|
|
|
|
if (!var)
|
|
|
|
var = Cvar_FindAlias (var_name);
|
|
|
|
|
|
|
|
if (var) {
|
|
|
|
if (var->flags & CVAR_ROM) {
|
2010-11-23 05:09:30 +00:00
|
|
|
Sys_MaskPrintf (SYS_DEV,
|
|
|
|
"Cvar \"%s\" is read-only, cannot modify\n",
|
|
|
|
var_name);
|
2001-02-21 19:35:06 +00:00
|
|
|
} else {
|
|
|
|
Cvar_Set (var, value);
|
2004-03-18 05:58:06 +00:00
|
|
|
Cvar_SetFlags (var, var->flags | orflags);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
2004-03-18 05:58:06 +00:00
|
|
|
var = Cvar_Get (var_name, value, CVAR_USER_CREATED | orflags, NULL,
|
2001-05-30 02:41:30 +00:00
|
|
|
USER_CVAR);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2004-03-18 05:58:06 +00:00
|
|
|
Cvar_Set_f (void)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2004-03-18 05:58:06 +00:00
|
|
|
set_cvar ("set", CVAR_NONE);
|
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2004-03-18 05:58:06 +00:00
|
|
|
static void
|
|
|
|
Cvar_Setrom_f (void)
|
|
|
|
{
|
|
|
|
set_cvar ("setrom", CVAR_ROM);
|
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2004-03-18 05:58:06 +00:00
|
|
|
static void
|
|
|
|
Cvar_Seta_f (void)
|
|
|
|
{
|
|
|
|
set_cvar ("seta", CVAR_ARCHIVE);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2010-11-23 01:40:54 +00:00
|
|
|
static void
|
|
|
|
Cvar_Inc_f (void)
|
|
|
|
{
|
|
|
|
cvar_t *var;
|
|
|
|
float inc = 1;
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
switch (Cmd_Argc ()) {
|
|
|
|
default:
|
|
|
|
case 1:
|
|
|
|
Sys_Printf ("inc <cvar> [amount] : increment cvar\n");
|
|
|
|
return;
|
|
|
|
case 3:
|
|
|
|
inc = atof (Cmd_Argv (2));
|
|
|
|
case 2:
|
|
|
|
name = Cmd_Argv (1);
|
|
|
|
var = Cvar_FindVar (name);
|
2011-07-04 12:05:42 +00:00
|
|
|
if (!var)
|
|
|
|
var = Cvar_FindAlias (name);
|
2010-11-23 01:40:54 +00:00
|
|
|
if (!var)
|
|
|
|
Sys_Printf ("Unknown variable \"%s\"\n", name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Cvar_SetValue (var, var->value + inc);
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-02-21 19:35:06 +00:00
|
|
|
Cvar_Toggle_f (void)
|
|
|
|
{
|
|
|
|
cvar_t *var;
|
|
|
|
|
|
|
|
if (Cmd_Argc () != 2) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("toggle <cvar> : toggle a cvar on/off\n");
|
2001-02-21 19:35:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var = Cvar_FindVar (Cmd_Argv (1));
|
|
|
|
if (!var)
|
|
|
|
var = Cvar_FindAlias (Cmd_Argv (1));
|
|
|
|
if (!var) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Unknown variable \"%s\"\n", Cmd_Argv (1));
|
2001-02-21 19:35:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cvar_Set (var, var->int_val ? "0" : "1");
|
|
|
|
}
|
|
|
|
|
2010-11-23 01:40:54 +00:00
|
|
|
static void
|
|
|
|
Cvar_Cycle_f (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const char *name;
|
|
|
|
cvar_t *var;
|
|
|
|
|
|
|
|
if (Cmd_Argc () < 3) {
|
|
|
|
Sys_Printf ("cycle <cvar> <value list>: cycle cvar through a list of "
|
|
|
|
"values\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = Cmd_Argv (1);
|
|
|
|
var = Cvar_FindVar (name);
|
2011-07-04 12:05:42 +00:00
|
|
|
if (!var)
|
|
|
|
var = Cvar_FindAlias (name);
|
2010-11-23 01:40:54 +00:00
|
|
|
if (!var) {
|
|
|
|
var = Cvar_Get (name, Cmd_Argv (Cmd_Argc () - 1), CVAR_USER_CREATED,
|
|
|
|
0, USER_CVAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 -- johnfitz
|
|
|
|
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 string that all had the same source (the user) so it will
|
|
|
|
// work.
|
|
|
|
if (atof (Cmd_Argv (i)) == 0) {
|
|
|
|
if (!strcmp (Cmd_Argv (i), var->string))
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (atof (Cmd_Argv (i)) == var->value)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == Cmd_Argc ())
|
|
|
|
Cvar_Set (var, Cmd_Argv (2)); // no match
|
|
|
|
else if (i + 1 == Cmd_Argc ())
|
|
|
|
Cvar_Set (var, Cmd_Argv (2)); // matched last value in list
|
|
|
|
else
|
|
|
|
Cvar_Set (var, Cmd_Argv (i + 1)); // matched earlier in list
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Cvar_Reset_f (void)
|
|
|
|
{
|
|
|
|
cvar_t *var;
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
switch (Cmd_Argc ()) {
|
|
|
|
default:
|
|
|
|
case 1:
|
|
|
|
Sys_Printf ("reset <cvar> : reset cvar to default\n");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
name = Cmd_Argv (1);
|
|
|
|
var = Cvar_FindVar (name);
|
2011-07-04 12:05:42 +00:00
|
|
|
if (!var)
|
|
|
|
var = Cvar_FindAlias (name);
|
2010-11-23 01:40:54 +00:00
|
|
|
if (!var)
|
|
|
|
Sys_Printf ("Unknown variable \"%s\"\n", name);
|
|
|
|
else
|
|
|
|
Cvar_Reset (var);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Cvar_ResetAll_f (void)
|
|
|
|
{
|
|
|
|
cvar_t *var;
|
|
|
|
|
|
|
|
for (var = cvar_vars; var; var = var->next)
|
|
|
|
if (!(var->flags & CVAR_ROM))
|
|
|
|
Cvar_Reset (var);
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-02-21 19:35:06 +00:00
|
|
|
Cvar_CvarList_f (void)
|
|
|
|
{
|
|
|
|
cvar_t *var;
|
|
|
|
int i;
|
|
|
|
int showhelp = 0;
|
2001-10-30 23:33:47 +00:00
|
|
|
const char *flags;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2001-10-30 23:33:47 +00:00
|
|
|
if (Cmd_Argc () > 1) {
|
2001-02-21 19:35:06 +00:00
|
|
|
showhelp = 1;
|
2001-10-30 23:33:47 +00:00
|
|
|
if (strequal (Cmd_Argv (1), "cfg"))
|
|
|
|
showhelp++;
|
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
for (var = cvar_vars, i = 0; var; var = var->next, i++) {
|
2001-10-30 23:33:47 +00:00
|
|
|
flags = va ("%c%c%c%c",
|
2001-02-21 19:35:06 +00:00
|
|
|
var->flags & CVAR_ROM ? 'r' : ' ',
|
|
|
|
var->flags & CVAR_ARCHIVE ? '*' : ' ',
|
|
|
|
var->flags & CVAR_USERINFO ? 'u' : ' ',
|
|
|
|
var->flags & CVAR_SERVERINFO ? 's' : ' ');
|
2001-10-30 23:33:47 +00:00
|
|
|
if (showhelp == 2)
|
|
|
|
Sys_Printf ("//%s %s\n%s \"%s\"\n\n", flags, var->description,
|
|
|
|
var->name, var->string);
|
|
|
|
else if (showhelp)
|
|
|
|
Sys_Printf ("%s %-20s : %s\n", flags, var->name, var->description);
|
2001-02-21 19:35:06 +00:00
|
|
|
else
|
2001-10-30 23:33:47 +00:00
|
|
|
Sys_Printf ("%s %s\n", flags, var->name);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("------------\n%d variables\n", i);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-03-05 05:11:26 +00:00
|
|
|
cvar_free (void *c, void *unused)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cvar_t *cvar = (cvar_t*)c;
|
2001-07-15 07:04:17 +00:00
|
|
|
free ((char*)cvar->name);
|
|
|
|
free ((char*)cvar->string);
|
2001-02-21 19:35:06 +00:00
|
|
|
free (cvar);
|
|
|
|
}
|
|
|
|
|
2001-06-04 04:52:14 +00:00
|
|
|
static const char *
|
2001-03-05 05:11:26 +00:00
|
|
|
cvar_get_key (void *c, void *unused)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cvar_t *cvar = (cvar_t*)c;
|
|
|
|
return cvar->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-03-05 05:11:26 +00:00
|
|
|
calias_free (void *c, void *unused)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cvar_alias_t *calias = (cvar_alias_t*)c;
|
|
|
|
free (calias->name);
|
|
|
|
free (calias);
|
|
|
|
}
|
|
|
|
|
2001-06-04 04:52:14 +00:00
|
|
|
static const char *
|
2001-03-05 05:11:26 +00:00
|
|
|
calias_get_key (void *c, void *unused)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cvar_alias_t *calias = (cvar_alias_t*)c;
|
|
|
|
return calias->name;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-02-21 19:35:06 +00:00
|
|
|
Cvar_Init_Hash (void)
|
|
|
|
{
|
2001-03-05 05:11:26 +00:00
|
|
|
cvar_hash = Hash_NewTable (1021, cvar_get_key, cvar_free, 0);
|
|
|
|
calias_hash = Hash_NewTable (1021, calias_get_key, calias_free, 0);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-02-21 19:35:06 +00:00
|
|
|
Cvar_Init (void)
|
|
|
|
{
|
2001-04-10 23:39:30 +00:00
|
|
|
developer = Cvar_Get ("developer", "0", CVAR_NONE, NULL,
|
|
|
|
"set to enable extra debugging information");
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
Cmd_AddCommand ("set", Cvar_Set_f, "Set the selected variable, useful on "
|
|
|
|
"the command line (+set variablename setting)");
|
|
|
|
Cmd_AddCommand ("setrom", Cvar_Setrom_f, "Set the selected variable and "
|
2010-01-13 06:42:26 +00:00
|
|
|
"make it read-only, useful on the command line. "
|
2001-08-22 22:03:16 +00:00
|
|
|
"(+setrom variablename setting)");
|
2004-03-18 05:58:06 +00:00
|
|
|
Cmd_AddCommand ("seta", Cvar_Seta_f, "Set the selected variable, and make "
|
|
|
|
"it archived, useful on the command line (+seta "
|
|
|
|
"variablename setting)");
|
2001-02-21 19:35:06 +00:00
|
|
|
Cmd_AddCommand ("toggle", Cvar_Toggle_f, "Toggle a cvar on or off");
|
|
|
|
Cmd_AddCommand ("cvarlist", Cvar_CvarList_f, "List all cvars");
|
2010-11-23 01:40:54 +00:00
|
|
|
Cmd_AddCommand ("cycle", Cvar_Cycle_f,
|
|
|
|
"Cycle a cvar through a list of values");
|
|
|
|
Cmd_AddCommand ("inc", Cvar_Inc_f, "Increment a cvar");
|
|
|
|
Cmd_AddCommand ("reset", Cvar_Reset_f, "Reset a cvar");
|
|
|
|
Cmd_AddCommand ("resetall", Cvar_ResetAll_f, "Reset all cvars");
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cvar_Shutdown (void)
|
|
|
|
{
|
|
|
|
cvar_t *var, *next;
|
|
|
|
cvar_alias_t *alias, *nextalias;
|
|
|
|
|
|
|
|
// Free cvars
|
|
|
|
var = cvar_vars;
|
|
|
|
while (var) {
|
|
|
|
next = var->next;
|
2001-07-15 07:04:17 +00:00
|
|
|
free ((char*)var->string);
|
|
|
|
free ((char*)var->name);
|
2001-02-21 19:35:06 +00:00
|
|
|
free (var);
|
|
|
|
var = next;
|
|
|
|
}
|
|
|
|
// Free aliases
|
|
|
|
alias = calias_vars;
|
|
|
|
while (alias) {
|
|
|
|
nextalias = alias->next;
|
2001-07-15 07:04:17 +00:00
|
|
|
free ((char*)alias->name);
|
2001-02-21 19:35:06 +00:00
|
|
|
free (alias);
|
|
|
|
alias = nextalias;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE cvar_t *
|
2001-07-15 07:04:17 +00:00
|
|
|
Cvar_Get (const char *name, const char *string, int cvarflags,
|
|
|
|
void (*callback)(cvar_t*), const char *description)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
cvar_t *var;
|
|
|
|
|
|
|
|
if (Cmd_Exists (name)) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("Cvar_Get: %s is a command\n", name);
|
2001-02-21 19:35:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
var = Cvar_FindVar (name);
|
|
|
|
if (!var) {
|
|
|
|
cvar_t **v;
|
|
|
|
var = (cvar_t *) calloc (1, sizeof (cvar_t));
|
|
|
|
|
|
|
|
// Cvar doesn't exist, so we create it
|
|
|
|
var->name = strdup (name);
|
|
|
|
var->string = strdup (string);
|
2010-11-23 01:40:54 +00:00
|
|
|
var->default_string = strdup (string);
|
2001-02-21 19:35:06 +00:00
|
|
|
var->flags = cvarflags;
|
2001-03-31 01:02:52 +00:00
|
|
|
var->callback = callback;
|
2001-02-21 19:35:06 +00:00
|
|
|
var->description = description;
|
|
|
|
var->value = atof (var->string);
|
|
|
|
var->int_val = atoi (var->string);
|
|
|
|
sscanf (var->string, "%f %f %f",
|
|
|
|
&var->vec[0], &var->vec[1], &var->vec[2]);
|
|
|
|
Hash_Add (cvar_hash, var);
|
|
|
|
|
|
|
|
for (v = &cvar_vars; *v; v = &(*v)->next)
|
|
|
|
if (strcmp ((*v)->name, var->name) >= 0)
|
|
|
|
break;
|
|
|
|
var->next = *v;
|
|
|
|
*v = var;
|
|
|
|
} else {
|
|
|
|
// Cvar does exist, so we update the flags and return.
|
|
|
|
var->flags &= ~CVAR_USER_CREATED;
|
|
|
|
var->flags |= cvarflags;
|
2001-05-30 02:41:30 +00:00
|
|
|
if (!var->callback)
|
|
|
|
var->callback = callback;
|
|
|
|
if (!var->description
|
|
|
|
|| strequal (var->description, USER_RO_CVAR)
|
|
|
|
|| strequal (var->description, USER_CVAR))
|
|
|
|
var->description = description;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
2001-03-31 01:02:52 +00:00
|
|
|
if (var->callback)
|
|
|
|
var->callback (var);
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Cvar_SetFlags
|
|
|
|
|
|
|
|
sets a Cvar's flags simply and easily
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-02-21 19:35:06 +00:00
|
|
|
Cvar_SetFlags (cvar_t *var, int cvarflags)
|
|
|
|
{
|
|
|
|
if (var == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var->flags = cvarflags;
|
|
|
|
}
|
2010-11-23 01:40:54 +00:00
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
Cvar_Reset (cvar_t *var)
|
|
|
|
{
|
|
|
|
Cvar_Set (var, var->default_string);
|
|
|
|
}
|