Add new cvar operation 'reset' and 'resetall'.

cvar operations are special commands that allow the programmatic
manipulation of cvar values. 'reset' resets a given cvar to it's
default value, e.g. `reset r_mode' would reset `r_mode` to `4`.
'resetall' resets all known cvar with the exception of `game`.

The code is based upon q2pro.

This is part of issue #414.
This commit is contained in:
Yamagi Burmeister 2019-09-02 17:15:51 +02:00
parent 5136ceb123
commit 66ca44e040
3 changed files with 64 additions and 1 deletions

View file

@ -253,3 +253,13 @@ it's `+set busywait 0` (setting the `busywait` cvar) and `-portable`
* **gl3_particle_square**: If set to `1`, particles are rendered as * **gl3_particle_square**: If set to `1`, particles are rendered as
squares, like in the old software renderer or Quake 1. Default is `0`. squares, like in the old software renderer or Quake 1. Default is `0`.
## cvar operations
cvar operations are special commands that allow the manipulation of cvar
values. They can be used for simple scripts and the like.
* **reset <cvar>**: Reset the cvar to it's default value.
* **resetall**: Reset all known cvar to their default values.

View file

@ -189,6 +189,7 @@ Cvar_Get(char *var_name, char *var_value, int flags)
if (var) if (var)
{ {
var->flags |= flags; var->flags |= flags;
var->default_string = CopyString(var_value);
return var; return var;
} }
@ -216,6 +217,7 @@ Cvar_Get(char *var_name, char *var_value, int flags)
var = Z_Malloc(sizeof(*var)); var = Z_Malloc(sizeof(*var));
var->name = CopyString(var_name); var->name = CopyString(var_name);
var->string = CopyString(var_value); var->string = CopyString(var_value);
var->default_string = CopyString(var_value);
var->modified = true; var->modified = true;
var->value = strtod(var->string, (char **)NULL); var->value = strtod(var->string, (char **)NULL);
@ -648,14 +650,60 @@ Cvar_Serverinfo(void)
return Cvar_BitInfo(CVAR_SERVERINFO); return Cvar_BitInfo(CVAR_SERVERINFO);
} }
/*
* Resets a cvar to its default value.
*/
void Cvar_Reset_f(void)
{
cvar_t *var;
if (Cmd_Argc() < 2)
{
Com_Printf("Usage: %s <cvar>\n", Cmd_Argv(0));
return;
}
var = Cvar_FindVar(Cmd_Argv(1));
if (!var)
{
Com_Printf("%s is not a cvar\n", Cmd_Argv(1));
return;
}
Com_Printf("%s: %s\n", var->name, var->default_string);
Cvar_Set(var->name, var->default_string);
}
void Cvar_ResetAll_f(void)
{
cvar_t *var;
for (var = cvar_vars; var; var = var->next)
{
if ((var->flags & CVAR_NOSET))
{
continue;
}
else if (strcmp(var->name, "game") == 0)
{
continue;
}
Cvar_Set(var->name, var->default_string);
}
}
/* /*
* Reads in all archived cvars * Reads in all archived cvars
*/ */
void void
Cvar_Init(void) Cvar_Init(void)
{ {
Cmd_AddCommand("set", Cvar_Set_f);
Cmd_AddCommand("cvarlist", Cvar_List_f); Cmd_AddCommand("cvarlist", Cvar_List_f);
Cmd_AddCommand("reset", Cvar_Reset_f);
Cmd_AddCommand("resetall", Cvar_ResetAll_f);
Cmd_AddCommand("set", Cvar_Set_f);
} }
/* /*
@ -676,6 +724,8 @@ Cvar_Fini(void)
} }
Cmd_RemoveCommand("cvarlist"); Cmd_RemoveCommand("cvarlist");
Cmd_RemoveCommand("reset");
Cmd_RemoveCommand("resetall");
Cmd_RemoveCommand("set"); Cmd_RemoveCommand("set");
} }

View file

@ -354,6 +354,9 @@ typedef struct cvar_s
qboolean modified; /* set each time the cvar is changed */ qboolean modified; /* set each time the cvar is changed */
float value; float value;
struct cvar_s *next; struct cvar_s *next;
/* Added by YQ2. Must be at the end to preserve ABI. */
char *default_string;
} cvar_t; } cvar_t;
#endif /* CVAR */ #endif /* CVAR */