From 3f4d89c11594f247887a62a78877a36f85fdd2ee Mon Sep 17 00:00:00 2001 From: divverent Date: Tue, 18 May 2010 18:46:29 +0000 Subject: [PATCH] autocvars: update to current value after loading a savegame, to always fulfill the invariant "autocvar = cvar value" git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@10200 d7cf8633-e32d-0410-b094-e92efae38249 --- cvar.c | 83 ++++++++++++++++++++++++++++++++---------------------- cvar.h | 2 ++ host_cmd.c | 3 ++ 3 files changed, 55 insertions(+), 33 deletions(-) diff --git a/cvar.c b/cvar.c index bf6df5ad..c6f69360 100644 --- a/cvar.c +++ b/cvar.c @@ -253,6 +253,55 @@ void Cvar_CompleteCvarPrint (const char *partial) Con_Printf ("^3%s^7 is \"%s\" [\"%s\"] %s\n", cvar->name, cvar->string, cvar->defstring, cvar->description); } +// we assume that prog is already set to the target progs +static void Cvar_UpdateAutoCvar(cvar_t *var) +{ + int i; + if(!prog) + Host_Error("Cvar_UpdateAutoCvar: no prog set"); + i = PRVM_GetProgNr(); + if(var->globaldefindex_progid[i] == prog->id) + { + // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs + int j; + const char *s; + prvm_eval_t *val = (prvm_eval_t *)(prog->globals.generic + prog->globaldefs[var->globaldefindex[i]].ofs); + switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL) + { + case ev_float: + val->_float = var->value; + break; + case ev_vector: + s = var->string; + VectorClear(val->vector); + for (j = 0;j < 3;j++) + { + while (*s && ISWHITESPACE(*s)) + s++; + if (!*s) + break; + val->vector[j] = atof(s); + while (!ISWHITESPACE(*s)) + s++; + if (!*s) + break; + } + break; + case ev_string: + PRVM_ChangeEngineString(var->globaldefindex_stringno[i], var->string); + val->string = var->globaldefindex_stringno[i]; + break; + } + } +} + +// called after loading a savegame +void Cvar_UpdateAllAutoCvars(void) +{ + cvar_t *var; + for (var = cvar_vars ; var ; var = var->next) + Cvar_UpdateAutoCvar(var); +} /* ============ @@ -344,39 +393,7 @@ void Cvar_SetQuick_Internal (cvar_t *var, const char *value) if(PRVM_ProgLoaded(i)) { PRVM_SetProg(i); - if(var->globaldefindex_progid[i] == prog->id) - { - // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs - int j; - const char *s; - prvm_eval_t *val = (prvm_eval_t *)(prog->globals.generic + prog->globaldefs[var->globaldefindex[i]].ofs); - switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL) - { - case ev_float: - val->_float = var->value; - break; - case ev_vector: - s = var->string; - VectorClear(val->vector); - for (j = 0;j < 3;j++) - { - while (*s && ISWHITESPACE(*s)) - s++; - if (!*s) - break; - val->vector[j] = atof(s); - while (!ISWHITESPACE(*s)) - s++; - if (!*s) - break; - } - break; - case ev_string: - PRVM_ChangeEngineString(var->globaldefindex_stringno[i], var->string); - val->string = var->globaldefindex_stringno[i]; - break; - } - } + Cvar_UpdateAutoCvar(var); } } prog = tmpprog; diff --git a/cvar.h b/cvar.h index 1c04d86e..3c94aae2 100644 --- a/cvar.h +++ b/cvar.h @@ -216,6 +216,8 @@ cvar_t *Cvar_Get (const char *name, const char *value, int flags, const char *ne extern char *cvar_dummy_description; // ALWAYS the same pointer extern cvar_t *cvar_vars; // used to list all cvars +void Cvar_UpdateAllAutoCvars(void); // updates ALL autocvars of the active prog to the cvar values (savegame loading) + #ifdef FILLALLCVARSWITHRUBBISH void Cvar_FillAll_f(); #endif /* FILLALLCVARSWITHRUBBISH */ diff --git a/host_cmd.c b/host_cmd.c index f341d7d6..202e4d98 100644 --- a/host_cmd.c +++ b/host_cmd.c @@ -924,6 +924,9 @@ void Host_Loadgame_f (void) // parse the global vars PRVM_ED_ParseGlobals (start); + + // restore the autocvar globals + Cvar_UpdateAllAutoCvars(); } else { -- 2.39.2