From b80c19bd77810177675724f721457b860c74a63b Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 15 Nov 2021 22:04:29 +0900 Subject: [PATCH] [util] Save and load cvars to/from plist configs Other than some backwards compatibility, this completes the configuration updates for now. --- include/QF/cvar.h | 4 ++++ libs/util/cvar.c | 33 +++++++++++++++++++++++++++++++++ qw/source/cl_main.c | 3 ++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/include/QF/cvar.h b/include/QF/cvar.h index 946a20f13..461ff4bf6 100644 --- a/include/QF/cvar.h +++ b/include/QF/cvar.h @@ -120,6 +120,10 @@ qboolean Cvar_Command (void); // with the archive flag set to true. void Cvar_WriteVariables (QFile *f); +struct plitem_s; +void Cvar_SaveConfig (struct plitem_s *config); +void Cvar_LoadConfig (struct plitem_s *config); + // attempts to match a partial variable name for command line completion // returns NULL if nothing fits const char *Cvar_CompleteVariable (const char *partial) __attribute__((pure)); diff --git a/libs/util/cvar.c b/libs/util/cvar.c index 10915818d..333e1ca9b 100644 --- a/libs/util/cvar.c +++ b/libs/util/cvar.c @@ -45,6 +45,7 @@ #include "QF/cvar.h" #include "QF/hash.h" #include "QF/mathlib.h" +#include "QF/plist.h" #include "QF/qargs.h" #include "QF/quakefs.h" #include "QF/sys.h" @@ -330,6 +331,38 @@ Cvar_WriteVariables (QFile *f) Qprintf (f, "seta %s \"%s\"\n", var->name, var->string); } +VISIBLE void +Cvar_SaveConfig (plitem_t *config) +{ + plitem_t *cvars = PL_NewDictionary (0); //FIXME hashlinks + PL_D_AddObject (config, "cvars", cvars); + for (cvar_t *var = cvar_vars; var; var = var->next) { + if (var->flags & CVAR_ARCHIVE) { + PL_D_AddObject (cvars, var->name, PL_NewString (var->string)); + } + } +} + +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_Set (var, value); + } + } + } +} + #define SYS_DEVELOPER(developer) #developer, static const char *developer_flags[] = { "dev", diff --git a/qw/source/cl_main.c b/qw/source/cl_main.c index 4f9b6068f..540fcedec 100644 --- a/qw/source/cl_main.c +++ b/qw/source/cl_main.c @@ -1508,7 +1508,7 @@ Host_WriteConfiguration (void) if (host_initialized && cl_writecfg->int_val) { plitem_t *config = PL_NewDictionary (0); //FIXME hashlinks IN_SaveConfig (config); - //Cvar_WriteVariables (f); + Cvar_SaveConfig (config); const char *path = va (0, "%s/quakeforge.cfg", qfs_gamedir->dir.def); QFile *f = QFS_WOpen (path, 0); @@ -1544,6 +1544,7 @@ Host_ReadConfiguration (const char *cfg_name) } IN_LoadConfig (config); + Cvar_LoadConfig (config); PL_Free (config); return 1;