[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.
This commit is contained in:
Bill Currie 2022-04-23 12:22:45 +09:00
parent 87b3c64801
commit 12c84046f3
240 changed files with 8069 additions and 4224 deletions

View file

@ -84,15 +84,31 @@ static plitem_t *play_list; // string or array of strings
static int play_pos = -1; // position in play_list (0 for string)
// -1 = invalid (both)
static cvar_t *bgmvolume; // volume cvar
static cvar_t *mus_ogglist; // tracklist cvar
static float bgmvolume;
static cvar_t bgmvolume_cvar = {
.name = "bgmvolume",
.description =
"Volume of CD music",
.default_value = "1",
.flags = CVAR_ARCHIVE,
.value = { .type = &cexpr_float, .value = &bgmvolume },
};
static char *mus_ogglist;
static cvar_t mus_ogglist_cvar = {
.name = "mus_ogglist",
.description =
"filename of track to music file map",
.default_value = "tracklist.cfg",
.flags = CVAR_NONE,
.value = { .type = 0, .value = &mus_ogglist },
};
static void
set_volume (void)
{
if (cd_channel && cd_channel->sfx) {
int vol = bgmvolume->value * 255;
int vol = bgmvolume * 255;
cd_channel->master_vol = vol;
cd_channel->leftvol = cd_channel->rightvol = cd_channel->master_vol;
@ -151,14 +167,14 @@ Load_Tracklist (void)
ogglistvalid = false;
mus_enabled = false;
if (!mus_ogglist || strequal (mus_ogglist->string, "none")) {
if (!mus_ogglist || strequal (mus_ogglist, "none")) {
return -1; // bail if we don't have a valid filename
}
oggfile = QFS_FOpenFile (mus_ogglist->string);
oggfile = QFS_FOpenFile (mus_ogglist);
if (!oggfile) {
Sys_Printf ("Mus_OggInit: open of file \"%s\" failed\n",
mus_ogglist->string);
mus_ogglist);
return -1;
}
@ -324,7 +340,7 @@ I_OGGMus_Info (void)
return;
Sys_Printf ("\n" "Tracklist loaded from file:\n%s\n"
"---------------------------\n", mus_ogglist->string);
"---------------------------\n", mus_ogglist);
/* loop, and count up the Highest key number. */
for (iter = 1, count = 0; count < keycount && iter <= 99 ; iter++) {
@ -439,15 +455,14 @@ I_OGGMus_Update (void)
/* called when the mus_ogglist cvar is changed */
static void
Mus_OggChange (cvar_t *ogglist)
Mus_OggChange (void *data, const cvar_t *cvar)
{
mus_ogglist = ogglist;
Load_Tracklist ();
}
/* change volume on sound object */
static void
Mus_VolChange (cvar_t *bgmvolume)
Mus_VolChange (void *data, const cvar_t *bgmvolume)
{
set_volume ();
}
@ -456,18 +471,15 @@ static void
Mus_gamedir (int phase)
{
if (phase)
Mus_OggChange (mus_ogglist);
Load_Tracklist ();
}
static void
I_OGGMus_Init (void)
{
/* check list file cvar, open list file, create map, close file. */
mus_ogglist = Cvar_Get ("mus_ogglist", "tracklist.cfg", CVAR_NONE,
Mus_OggChange,
"filename of track to music file map");
bgmvolume = Cvar_Get ("bgmvolume", "1.0", CVAR_ARCHIVE, Mus_VolChange,
"Volume of CD music");
Cvar_Register (&mus_ogglist_cvar, Mus_OggChange, 0);
Cvar_Register (&bgmvolume_cvar, Mus_VolChange, 0);
QFS_GamedirCallback (Mus_gamedir);
}