From 66ca44e0403fc27219516062cc19238dc2ac64b5 Mon Sep 17 00:00:00 2001 From: Yamagi Burmeister Date: Mon, 2 Sep 2019 17:15:51 +0200 Subject: [PATCH] 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. --- doc/04_cvarlist.md | 10 ++++++++ src/common/cvar.c | 52 +++++++++++++++++++++++++++++++++++++- src/common/header/shared.h | 3 +++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/doc/04_cvarlist.md b/doc/04_cvarlist.md index c2f01221..16141adc 100644 --- a/doc/04_cvarlist.md +++ b/doc/04_cvarlist.md @@ -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 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 **: Reset the cvar to it's default value. + +* **resetall**: Reset all known cvar to their default values. diff --git a/src/common/cvar.c b/src/common/cvar.c index 3237d091..bfbb5d68 100644 --- a/src/common/cvar.c +++ b/src/common/cvar.c @@ -189,6 +189,7 @@ Cvar_Get(char *var_name, char *var_value, int flags) if (var) { var->flags |= flags; + var->default_string = CopyString(var_value); return var; } @@ -216,6 +217,7 @@ Cvar_Get(char *var_name, char *var_value, int flags) var = Z_Malloc(sizeof(*var)); var->name = CopyString(var_name); var->string = CopyString(var_value); + var->default_string = CopyString(var_value); var->modified = true; var->value = strtod(var->string, (char **)NULL); @@ -648,14 +650,60 @@ Cvar_Serverinfo(void) 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 \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 */ void Cvar_Init(void) { - Cmd_AddCommand("set", Cvar_Set_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("reset"); + Cmd_RemoveCommand("resetall"); Cmd_RemoveCommand("set"); } diff --git a/src/common/header/shared.h b/src/common/header/shared.h index ae762532..8fca721e 100644 --- a/src/common/header/shared.h +++ b/src/common/header/shared.h @@ -354,6 +354,9 @@ typedef struct cvar_s qboolean modified; /* set each time the cvar is changed */ float value; struct cvar_s *next; + + /* Added by YQ2. Must be at the end to preserve ABI. */ + char *default_string; } cvar_t; #endif /* CVAR */