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
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2019-07-09 11:14:57 +00:00
|
|
|
#include <ctype.h>
|
2001-02-21 19:35:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/cvar.h"
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
#include "QF/cmem.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/hash.h"
|
2022-04-24 15:23:28 +00:00
|
|
|
#include "QF/heapsort.h"
|
2002-04-25 20:08:18 +00:00
|
|
|
#include "QF/mathlib.h"
|
2021-11-15 13:04:29 +00:00
|
|
|
#include "QF/plist.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"
|
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
static exprenum_t developer_enum;
|
|
|
|
static exprtype_t developer_type = {
|
|
|
|
.name = "developer",
|
|
|
|
.size = sizeof (int),
|
|
|
|
.binops = cexpr_flag_binops,
|
|
|
|
.unops = cexpr_flag_unops,
|
|
|
|
.data = &developer_enum,
|
|
|
|
.get_string = cexpr_enum_get_string,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SYS_DEVELOPER(dev) (SYS_##dev & ~SYS_dev),
|
|
|
|
static int developer_values[] = {
|
|
|
|
SYS_dev,
|
|
|
|
#include "QF/sys_developer.h"
|
|
|
|
};
|
|
|
|
#undef SYS_DEVELOPER
|
2022-09-24 09:26:25 +00:00
|
|
|
#define SYS_DEVELOPER(dev) {#dev, &developer_type, developer_values + SYS_DeveloperID_##dev + 1},
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
static exprsym_t developer_symbols[] = {
|
|
|
|
{"dev", &developer_type, developer_values + 0},
|
|
|
|
#include "QF/sys_developer.h"
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
#undef SYS_DEVELOPER
|
|
|
|
static exprtab_t developer_symtab = {
|
|
|
|
developer_symbols,
|
|
|
|
};
|
|
|
|
static exprenum_t developer_enum = {
|
|
|
|
&developer_type,
|
|
|
|
&developer_symtab,
|
|
|
|
};
|
|
|
|
|
|
|
|
VISIBLE int developer;
|
|
|
|
static cvar_t developer_cvar = {
|
|
|
|
.name = "developer",
|
|
|
|
.description =
|
|
|
|
"set to enable extra debugging information",
|
|
|
|
.default_value = "0",
|
|
|
|
.flags = CVAR_NONE,
|
|
|
|
.value = { .type = &developer_type, .value = &developer },
|
|
|
|
};
|
2007-03-10 12:00:59 +00:00
|
|
|
static const char *cvar_null_string = "";
|
|
|
|
static hashtab_t *cvar_hash;
|
2022-04-24 15:23:28 +00:00
|
|
|
static hashtab_t *user_cvar_hash;
|
2007-03-10 12:00:59 +00:00
|
|
|
static hashtab_t *calias_hash;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
static cvar_t *
|
|
|
|
cvar_create (const char *name, const char *value)
|
|
|
|
{
|
|
|
|
cvar_t *var = calloc (1, sizeof (cvar_t) + sizeof (char *));
|
|
|
|
var->name = strdup (name);
|
|
|
|
var->description = cvar_null_string;
|
|
|
|
var->default_value = cvar_null_string;
|
|
|
|
var->flags = CVAR_USER_CREATED;
|
|
|
|
var->value.value = var + 1;
|
|
|
|
*(char **)var->value.value = strdup (value);
|
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
Hash_Add (user_cvar_hash, var);
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cvar_destroy (cvar_t *var)
|
|
|
|
{
|
|
|
|
if (!(var->flags & CVAR_USER_CREATED)) {
|
|
|
|
Sys_Error ("Attempt to destroy non-user cvar");
|
|
|
|
}
|
2022-05-12 10:08:40 +00:00
|
|
|
Hash_Free (user_cvar_hash, Hash_Del (user_cvar_hash, var->name));
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +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
|
|
|
{
|
2022-05-12 10:08:40 +00:00
|
|
|
cvar_t *var = Hash_Find (cvar_hash, var_name);
|
|
|
|
if (!var) {
|
|
|
|
var = Hash_Find (user_cvar_hash, var_name);
|
|
|
|
}
|
|
|
|
return var;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2011-07-05 12:59:47 +00:00
|
|
|
alias = (cvar_alias_t *) Hash_Find (calias_hash, alias_name);
|
2001-02-21 19:35:06 +00:00
|
|
|
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
|
|
|
{
|
2011-07-05 12:59:47 +00:00
|
|
|
cvar_alias_t *alias;
|
2001-02-21 19:35:06 +00:00
|
|
|
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->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
|
|
|
}
|
|
|
|
|
2011-07-05 12:59:47 +00:00
|
|
|
cvar_t *
|
|
|
|
Cvar_RemoveAlias (const char *name)
|
|
|
|
{
|
|
|
|
cvar_alias_t *alias;
|
|
|
|
cvar_t *var;
|
|
|
|
|
|
|
|
alias = (cvar_alias_t *) Hash_Find (calias_hash, name);
|
|
|
|
if (!alias) {
|
|
|
|
Sys_Printf ("Cvar_RemoveAlias: %s is not an alias\n", name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
var = alias->cvar;
|
|
|
|
Hash_Free (calias_hash, Hash_Del (calias_hash, name));
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
static float
|
|
|
|
cvar_value (cvar_t *var)
|
|
|
|
{
|
|
|
|
if (!var->value.type) {
|
|
|
|
return atof (*(char **)var->value.value);
|
|
|
|
} else if (var->value.type == &cexpr_int) {
|
|
|
|
return *(int *)var->value.value;
|
|
|
|
} else if (var->value.type == &cexpr_float) {
|
|
|
|
return *(float *)var->value.value;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE float
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_Value (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;
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
return cvar_value (var);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
cvar_string (const cvar_t *var)
|
|
|
|
{
|
|
|
|
if (!var->value.type) {
|
|
|
|
return *(char **)var->value.value;
|
|
|
|
} else if (var->value.type->get_string) {
|
|
|
|
return var->value.type->get_string (&var->value, 0);
|
|
|
|
}
|
|
|
|
return cvar_null_string;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char *
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_String (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;
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
return cvar_string (var);
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE const char *
|
|
|
|
Cvar_VarString (const cvar_t *var)
|
|
|
|
{
|
|
|
|
return cvar_string (var);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
typedef struct {
|
|
|
|
const char *match;
|
|
|
|
size_t match_len;
|
|
|
|
int num_matches;
|
|
|
|
} cvar_count_ctx_t;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
static void
|
|
|
|
cvar_match_count (void *ele, void *data)
|
|
|
|
{
|
|
|
|
cvar_count_ctx_t *ctx = data;
|
|
|
|
const cvar_t *cvar = ele;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
if (strncmp (cvar->name, ctx->match, ctx->match_len) == 0) {
|
|
|
|
ctx->num_matches++;
|
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-04-24 15:23:28 +00:00
|
|
|
cvar_count_ctx_t ctx = {
|
|
|
|
.match = partial,
|
|
|
|
.match_len = strlen (partial),
|
|
|
|
.num_matches = 0,
|
|
|
|
};
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
Hash_ForEach (cvar_hash, cvar_match_count, &ctx);
|
|
|
|
Hash_ForEach (user_cvar_hash, cvar_match_count, &ctx);
|
|
|
|
// this is a bit of a hack, but both cvar_alias_t and cvar_t have
|
|
|
|
// name in the first file, so it will work out as that's the only
|
|
|
|
// criteron for a match
|
|
|
|
Hash_ForEach (calias_hash, cvar_match_count, &ctx);
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
return ctx.num_matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char *match;
|
|
|
|
size_t match_len;
|
|
|
|
const char **list;
|
|
|
|
int index;
|
|
|
|
} cvar_copy_ctx_t;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
static void
|
|
|
|
cvar_match_copy (void *ele, void *data)
|
|
|
|
{
|
|
|
|
cvar_copy_ctx_t *ctx = data;
|
|
|
|
const cvar_t *cvar = ele;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
if (strncmp (cvar->name, ctx->match, ctx->match_len) == 0) {
|
|
|
|
ctx->list[ctx->index++] = cvar->name;
|
|
|
|
}
|
2001-06-28 04:05:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
static int
|
|
|
|
cvar_cmp_name (const void *_a, const void *_b)
|
|
|
|
{
|
|
|
|
const char * const *a = _a;
|
|
|
|
const char * const *b = _b;
|
|
|
|
return strcmp (*a, *b);
|
|
|
|
}
|
2001-06-28 04:05:14 +00:00
|
|
|
|
2022-04-24 15:23:28 +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
|
|
|
{
|
2022-04-24 15:23:28 +00:00
|
|
|
int num_matches = Cvar_CompleteCountPossible (partial);
|
2001-06-28 04:05:14 +00:00
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
cvar_copy_ctx_t ctx = {
|
|
|
|
.match = partial,
|
|
|
|
.match_len = strlen (partial),
|
|
|
|
.list = malloc((num_matches + 1) * sizeof (char *)),
|
|
|
|
.index = 0,
|
|
|
|
};
|
2001-06-28 04:05:14 +00:00
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
Hash_ForEach (cvar_hash, cvar_match_copy, &ctx);
|
|
|
|
Hash_ForEach (user_cvar_hash, cvar_match_copy, &ctx);
|
|
|
|
// this is a bit of a hack, but both cvar_alias_t and cvar_t have
|
|
|
|
// name in the first file, so it will work out as that's the only
|
|
|
|
// criteron for a match
|
|
|
|
Hash_ForEach (calias_hash, cvar_match_copy, &ctx);
|
|
|
|
ctx.list[ctx.index] = 0;
|
|
|
|
heapsort (ctx.list, ctx.index, sizeof (char *), cvar_cmp_name);
|
|
|
|
return ctx.list;
|
2012-05-21 23:23:22 +00:00
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-11-25 08:46:16 +00:00
|
|
|
VISIBLE void
|
|
|
|
Cvar_AddListener (cvar_t *cvar, cvar_listener_t listener, void *data)
|
|
|
|
{
|
|
|
|
if (!cvar->listeners) {
|
|
|
|
cvar->listeners = malloc (sizeof (*cvar->listeners));
|
|
|
|
LISTENER_SET_INIT (cvar->listeners, 8);
|
|
|
|
}
|
|
|
|
LISTENER_ADD (cvar->listeners, listener, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
Cvar_RemoveListener (cvar_t *cvar, cvar_listener_t listener, void *data)
|
|
|
|
{
|
|
|
|
if (cvar->listeners) {
|
|
|
|
LISTENER_REMOVE (cvar->listeners, listener, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
static int
|
|
|
|
cvar_setvar (cvar_t *var, const char *value)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
int changed = 0;
|
2001-02-22 08:12:13 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (!var->value.type) {
|
|
|
|
char **str_value = var->value.value;
|
|
|
|
changed = !*str_value || !strequal (*str_value, value);
|
|
|
|
if (var->validator) {
|
|
|
|
changed = changed && var->validator (var);
|
|
|
|
}
|
|
|
|
if (changed) {
|
|
|
|
free (*str_value);
|
|
|
|
*str_value = strdup (value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exprenum_t *enm = var->value.type->data;
|
|
|
|
exprctx_t context = {
|
|
|
|
.memsuper = new_memsuper (),
|
|
|
|
.symtab = enm ? enm->symtab : 0,
|
|
|
|
.msg_prefix = var->name,
|
|
|
|
};
|
|
|
|
if (context.symtab && !context.symtab->tab) {
|
|
|
|
cexpr_init_symtab (context.symtab, &context);
|
|
|
|
}
|
|
|
|
context.result = cexpr_value (var->value.type, &context);
|
|
|
|
if (!cexpr_eval_string (value, &context)) {
|
|
|
|
changed = memcmp (context.result->value, var->value.value,
|
|
|
|
var->value.type->size) != 0;
|
|
|
|
if (var->validator) {
|
|
|
|
changed = changed && var->validator (var);
|
|
|
|
}
|
|
|
|
if (changed) {
|
|
|
|
memcpy (var->value.value, context.result->value,
|
|
|
|
var->value.type->size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete_memsuper (context.memsuper);
|
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (changed && var->listeners) {
|
|
|
|
LISTENER_INVOKE (var->listeners, var);
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
Cvar_SetVar (cvar_t *var, const char *value)
|
|
|
|
{
|
2001-02-21 19:35:06 +00:00
|
|
|
if (var->flags & CVAR_ROM) {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_dev, "Cvar \"%s\" is read-only, cannot modify\n",
|
2010-11-23 05:09:30 +00:00
|
|
|
var->name);
|
2001-02-21 19:35:06 +00:00
|
|
|
return;
|
|
|
|
}
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
cvar_setvar (var, value);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_Set (const char *var_name, const char *value)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
cvar_t *var;
|
|
|
|
|
|
|
|
var = Cvar_FindVar (var_name);
|
|
|
|
|
|
|
|
if (!var)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Cvar_SetVar (var, 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) {
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Sys_Printf ("\"%s\" is \"%s\"\n", v->name, cvar_string (v));
|
2001-02-21 19:35:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_SetVar (v, Cmd_Argv (1));
|
2001-02-21 19:35:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
static void
|
|
|
|
cvar_write_variable (void *ele, void *data)
|
|
|
|
{
|
|
|
|
cvar_t *cvar = ele;
|
|
|
|
QFile *f = data;
|
|
|
|
|
|
|
|
if (cvar->flags & CVAR_ARCHIVE) {
|
|
|
|
Qprintf (f, "seta %s \"%s\"\n", cvar->name, cvar_string (cvar));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
/*
|
|
|
|
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
|
|
|
{
|
2022-04-24 15:23:28 +00:00
|
|
|
Hash_ForEach (cvar_hash, cvar_write_variable, f);
|
|
|
|
Hash_ForEach (user_cvar_hash, cvar_write_variable, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cvar_write_config (void *ele, void *data)
|
|
|
|
{
|
|
|
|
cvar_t *cvar = ele;
|
|
|
|
plitem_t *cfg = data;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
if (cvar->flags & CVAR_ARCHIVE) {
|
|
|
|
PL_D_AddObject (cfg, cvar->name, PL_NewString (cvar_string (cvar)));
|
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 13:04:29 +00:00
|
|
|
VISIBLE void
|
|
|
|
Cvar_SaveConfig (plitem_t *config)
|
|
|
|
{
|
2022-05-12 08:54:23 +00:00
|
|
|
plitem_t *cvars = PL_NewDictionary (0);
|
2021-11-15 13:04:29 +00:00
|
|
|
PL_D_AddObject (config, "cvars", cvars);
|
2022-04-24 15:23:28 +00:00
|
|
|
Hash_ForEach (cvar_hash, cvar_write_config, cvars);
|
|
|
|
Hash_ForEach (user_cvar_hash, cvar_write_config, cvars);
|
2021-11-15 13:04:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
Cvar_LoadConfig (plitem_t *config)
|
|
|
|
{
|
|
|
|
plitem_t *cvars = PL_ObjectForKey (config, "cvars");
|
|
|
|
|
|
|
|
if (!cvars) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0, count = PL_D_NumKeys (cvars); i < count; i++) {
|
|
|
|
const char *cvar_name = PL_KeyAtIndex (cvars, i);
|
|
|
|
const char *value = PL_String (PL_ObjectForKey (cvars, cvar_name));
|
|
|
|
if (value) {
|
|
|
|
cvar_t *var = Cvar_FindVar (cvar_name);
|
|
|
|
if (var) {
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_SetVar (var, value);
|
|
|
|
var->flags |= CVAR_ARCHIVE;
|
2021-12-14 22:25:03 +00:00
|
|
|
} else {
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
var = cvar_create (cvar_name, value);
|
|
|
|
var->flags |= CVAR_ARCHIVE;
|
2021-11-15 13:04:29 +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) {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_dev,
|
2010-11-23 05:09:30 +00:00
|
|
|
"Cvar \"%s\" is read-only, cannot modify\n",
|
|
|
|
var_name);
|
2001-02-21 19:35:06 +00:00
|
|
|
} else {
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_SetVar (var, value);
|
|
|
|
var->flags |= orflags;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
var = cvar_create (var_name, value);
|
|
|
|
var->flags |= orflags;
|
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);
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (!var) {
|
2010-11-23 01:40:54 +00:00
|
|
|
Sys_Printf ("Unknown variable \"%s\"\n", name);
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (var->flags & CVAR_ROM) {
|
|
|
|
Sys_Printf ("Variable \"%s\" is read-only\n", name);
|
|
|
|
return;
|
|
|
|
}
|
2010-11-23 01:40:54 +00:00
|
|
|
break;
|
|
|
|
}
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (var->value.type == &cexpr_float) {
|
|
|
|
*(float *) var->value.value += inc;
|
|
|
|
} else if (var->value.type == &cexpr_int) {
|
|
|
|
*(int *) var->value.value += inc;
|
|
|
|
} else {
|
|
|
|
Sys_Printf ("Variable \"%s\" cannot be incremented\n", name);
|
|
|
|
}
|
2010-11-23 01:40:54 +00:00
|
|
|
}
|
|
|
|
|
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] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if ((var->flags & CVAR_ROM) || var->value.type != &cexpr_int) {
|
|
|
|
Sys_Printf ("Variable \"%s\" cannot be toggled\n", Cmd_Argv (1));
|
|
|
|
return;
|
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
*(int *) var->value.value = !*(int *) var->value.value;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
var = cvar_create (name, Cmd_Argv (Cmd_Argc () - 1));
|
2010-11-23 01:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (!strcmp (Cmd_Argv (i), cvar_string (var)))
|
|
|
|
break;
|
2010-11-23 01:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i == Cmd_Argc ())
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_SetVar (var, Cmd_Argv (2)); // no match
|
2010-11-23 01:40:54 +00:00
|
|
|
else if (i + 1 == Cmd_Argc ())
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_SetVar (var, Cmd_Argv (2)); // matched last value in list
|
2010-11-23 01:40:54 +00:00
|
|
|
else
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_SetVar (var, Cmd_Argv (i + 1)); // matched earlier in list
|
2010-11-23 01:40:54 +00:00
|
|
|
}
|
|
|
|
|
2012-08-13 06:03:40 +00:00
|
|
|
static void
|
|
|
|
Cvar_Reset (cvar_t *var)
|
|
|
|
{
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_SetVar (var, var->default_value);
|
2012-08-13 06:03:40 +00:00
|
|
|
}
|
|
|
|
|
2010-11-23 01:40:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
static void
|
|
|
|
cvar_reset_var (void *ele, void *data)
|
|
|
|
{
|
|
|
|
cvar_t *var = ele;
|
|
|
|
if (!(var->flags & CVAR_ROM))
|
|
|
|
Cvar_Reset (var);
|
|
|
|
}
|
|
|
|
|
2012-08-13 06:03:40 +00:00
|
|
|
static void
|
|
|
|
Cvar_ResetAll_f (void)
|
2010-11-23 01:40:54 +00:00
|
|
|
{
|
2022-04-24 15:23:28 +00:00
|
|
|
Hash_ForEach (cvar_hash, cvar_reset_var, 0);
|
|
|
|
}
|
2010-11-23 01:40:54 +00:00
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
static int
|
|
|
|
cvar_cmp (const void *_a, const void *_b)
|
|
|
|
{
|
|
|
|
const cvar_t * const *a = _a;
|
|
|
|
const cvar_t * const *b = _b;
|
|
|
|
return strcmp ((*a)->name, (*b)->name);
|
2010-11-23 01:40:54 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-02-21 19:35:06 +00:00
|
|
|
Cvar_CvarList_f (void)
|
|
|
|
{
|
|
|
|
int showhelp = 0;
|
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++;
|
|
|
|
}
|
2022-04-24 15:23:28 +00:00
|
|
|
|
|
|
|
void **cvar_list = Hash_GetList (cvar_hash);
|
|
|
|
int num_vars = Hash_NumElements (cvar_hash);
|
|
|
|
heapsort (cvar_list, num_vars, sizeof (void *), cvar_cmp);
|
|
|
|
|
|
|
|
for (cvar_t **cvar = (cvar_t **) cvar_list; *cvar; cvar++) {
|
|
|
|
cvar_t *var = *cvar;
|
|
|
|
const char *flags = va (0, "%c%c%c%c",
|
|
|
|
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,
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
var->name, cvar_string (var));
|
2001-10-30 23:33:47 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
Sys_Printf ("------------\n%d variables\n", num_vars);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 10:08:40 +00:00
|
|
|
static void
|
|
|
|
cvar_free_memory (void *ele, void *data)
|
|
|
|
{
|
|
|
|
const cvar_t *cvar = ele;
|
|
|
|
if (!cvar->value.type) {
|
|
|
|
char **str_value = cvar->value.value;
|
|
|
|
free (*str_value);
|
|
|
|
*str_value = 0;
|
2023-03-05 08:07:56 +00:00
|
|
|
} else if (cvar->value.type->data) {
|
|
|
|
exprenum_t *enm = cvar->value.type->data;
|
|
|
|
if (enm->symtab && enm->symtab->tab) {
|
|
|
|
Hash_DelTable (enm->symtab->tab);
|
2023-03-05 09:12:03 +00:00
|
|
|
enm->symtab->tab = 0;
|
2023-03-05 08:07:56 +00:00
|
|
|
}
|
2022-05-12 10:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cvar->listeners) {
|
|
|
|
DARRAY_CLEAR (cvar->listeners);
|
|
|
|
free (cvar->listeners);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cvar_shutdown (void *data)
|
|
|
|
{
|
|
|
|
Hash_ForEach (cvar_hash, cvar_free_memory, 0);
|
|
|
|
Hash_DelTable (cvar_hash);
|
|
|
|
Hash_DelTable (user_cvar_hash);
|
|
|
|
Hash_DelTable (calias_hash);
|
|
|
|
}
|
|
|
|
|
2001-06-04 04:52:14 +00:00
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
cvar_get_key (const void *c, void *unused)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cvar_t *cvar = (cvar_t*)c;
|
|
|
|
return cvar->name;
|
|
|
|
}
|
|
|
|
|
2022-05-12 10:08:40 +00:00
|
|
|
static void
|
|
|
|
cvar_free (void *c, void *unused)
|
|
|
|
{
|
|
|
|
cvar_t *cvar = (cvar_t*)c;
|
|
|
|
|
|
|
|
free (*(char **) cvar->value.value);
|
|
|
|
free ((char *) cvar->name);
|
|
|
|
free (cvar);
|
|
|
|
}
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
static void
|
2001-03-05 05:11:26 +00:00
|
|
|
calias_free (void *c, void *unused)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2011-07-05 12:59:47 +00:00
|
|
|
cvar_alias_t *calias = (cvar_alias_t *) c;
|
2001-02-21 19:35:06 +00:00
|
|
|
free (calias->name);
|
|
|
|
free (calias);
|
|
|
|
}
|
|
|
|
|
2001-06-04 04:52:14 +00:00
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
calias_get_key (const void *c, void *unused)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2011-07-05 12:59:47 +00:00
|
|
|
cvar_alias_t *calias = (cvar_alias_t *) c;
|
2001-02-21 19:35:06 +00:00
|
|
|
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)
|
|
|
|
{
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
cvar_hash = Hash_NewTable (1021, cvar_get_key, 0, 0, 0);
|
2022-05-12 10:08:40 +00:00
|
|
|
user_cvar_hash = Hash_NewTable (1021, cvar_get_key, cvar_free, 0, 0);
|
2020-03-25 06:43:16 +00:00
|
|
|
calias_hash = Hash_NewTable (1021, calias_get_key, calias_free, 0, 0);
|
2022-05-12 10:08:40 +00:00
|
|
|
|
|
|
|
Sys_RegisterShutdown (cvar_shutdown, 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)
|
|
|
|
{
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_Register (&developer_cvar, 0, 0);
|
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
|
|
|
}
|
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
VISIBLE void
|
|
|
|
Cvar_Register (cvar_t *var, cvar_listener_t listener, void *data)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
cvar_t *user_var;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (Cmd_Exists (var->name)) {
|
|
|
|
Sys_Printf ("Cvar_Get: %s is a command\n", var->name);
|
|
|
|
return;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (var->flags & CVAR_REGISTERED) {
|
|
|
|
Sys_Error ("Cvar %s already registered", var->name);
|
|
|
|
}
|
|
|
|
|
2022-04-24 15:23:28 +00:00
|
|
|
if ((user_var = Hash_Find (user_cvar_hash, var->name))) {
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
cvar_setvar (var, cvar_string (user_var));
|
|
|
|
cvar_destroy (user_var);
|
2001-02-21 19:35:06 +00:00
|
|
|
} else {
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
cvar_setvar (var, var->default_value);
|
2021-11-25 08:46:16 +00:00
|
|
|
}
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
var->flags |= CVAR_REGISTERED;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (listener) {
|
|
|
|
Cvar_AddListener (var, listener, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Hash_Add (cvar_hash, var);
|
|
|
|
|
|
|
|
if (var->listeners) {
|
|
|
|
LISTENER_INVOKE (var->listeners, var);
|
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
}
|
2022-04-24 15:23:28 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
cvar_select_t select;
|
|
|
|
void *data;
|
|
|
|
const cvar_t **list;
|
|
|
|
int index;
|
|
|
|
} cvar_select_ctx_t;
|
|
|
|
|
|
|
|
static void
|
|
|
|
cvar_select (void *ele, void *data)
|
|
|
|
{
|
|
|
|
const cvar_t *cvar = ele;
|
|
|
|
cvar_select_ctx_t *ctx = data;
|
|
|
|
|
|
|
|
if (ctx->select (cvar, ctx->data)) {
|
|
|
|
ctx->list[ctx->index++] = cvar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE const cvar_t **
|
|
|
|
Cvar_Select (cvar_select_t select, void *data)
|
|
|
|
{
|
|
|
|
int num_cvars = Hash_NumElements (cvar_hash)
|
|
|
|
+ Hash_NumElements (user_cvar_hash);
|
|
|
|
cvar_select_ctx_t ctx = {
|
|
|
|
.select = select,
|
|
|
|
.data = data,
|
|
|
|
.list = malloc ((num_cvars + 1) * sizeof (cvar_t *)),
|
|
|
|
.index = 0,
|
|
|
|
};
|
|
|
|
Hash_ForEach (cvar_hash, cvar_select, &ctx);
|
|
|
|
Hash_ForEach (user_cvar_hash, cvar_select, &ctx);
|
|
|
|
ctx.list[num_cvars] = 0;
|
|
|
|
return ctx.list;
|
|
|
|
}
|