2 Copyright (C) 1996-1997 Id Software, Inc.
3 Copyright (C) 2000-2021 DarkPlaces contributors
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 // cvar.c -- dynamic variable tracking
25 const char *cvar_dummy_description = "custom cvar";
26 static const char *cvar_null_string = "";
28 cvar_state_t cvars_all;
29 cvar_state_t cvars_null;
36 cvar_t *Cvar_FindVar(cvar_state_t *cvars, const char *var_name, unsigned neededflags)
41 // use hash lookup to minimize search time
42 hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)) % CVAR_HASHSIZE;
43 for (hash = cvars->hashtable[hashindex];hash;hash = hash->next)
44 if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
47 for (char **alias = hash->cvar->aliases; alias && *alias; alias++)
48 if (!strcmp (var_name, *alias) && (hash->cvar->flags & neededflags))
53 cvar_t *Cvar_FindVarAfter(cvar_state_t *cvars, const char *prev_var_name, unsigned neededflags)
59 var = Cvar_FindVar(cvars, prev_var_name, neededflags);
67 // search for the next cvar matching the needed flags
70 if (var->flags & neededflags)
78 * Returns a pointer to the pointer stored in hashtable[] (or the one it links to)
79 * because we'll need to update that when deleting a cvar as other cvar(s) may share its hashindex.
81 static cvar_hash_t **Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name, cvar_t **prev_alpha, unsigned neededflags)
85 cvar_hash_t **hashlinkptr;
87 // use hash lookup to minimize search time
88 hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)) % CVAR_HASHSIZE;
89 if(prev_alpha) *prev_alpha = NULL;
90 hashlinkptr = &cvars->hashtable[hashindex];
91 for (hashlinkptr = &cvars->hashtable[hashindex]; *hashlinkptr; hashlinkptr = &(*hashlinkptr)->next)
93 cvar = (*hashlinkptr)->cvar;
94 if (!strcmp (var_name, cvar->name) && (cvar->flags & neededflags))
97 for (char **alias = cvar->aliases; alias && *alias; alias++)
98 if (!strcmp (var_name, *alias) && (cvar->flags & neededflags))
104 if(!prev_alpha || cvar == cvars->vars)
106 *prev_alpha = cvars->vars;
107 // if prev_alpha happens to become NULL then there has been some inconsistency elsewhere
108 // already - should I still insert '*prev_alpha &&' in the loop?
109 while((*prev_alpha)->next != cvar)
110 *prev_alpha = (*prev_alpha)->next;
119 float Cvar_VariableValueOr(cvar_state_t *cvars, const char *var_name, float def, unsigned neededflags)
123 var = Cvar_FindVar(cvars, var_name, neededflags);
126 return atof (var->string);
129 float Cvar_VariableValue(cvar_state_t *cvars, const char *var_name, unsigned neededflags)
131 return Cvar_VariableValueOr(cvars, var_name, 0, neededflags);
139 const char *Cvar_VariableStringOr(cvar_state_t *cvars, const char *var_name, const char *def, unsigned neededflags)
143 var = Cvar_FindVar(cvars, var_name, neededflags);
149 const char *Cvar_VariableString(cvar_state_t *cvars, const char *var_name, unsigned neededflags)
151 return Cvar_VariableStringOr(cvars, var_name, cvar_null_string, neededflags);
156 Cvar_VariableDefString
159 const char *Cvar_VariableDefString(cvar_state_t *cvars, const char *var_name, unsigned neededflags)
163 var = Cvar_FindVar(cvars, var_name, neededflags);
165 return cvar_null_string;
166 return var->defstring;
171 Cvar_VariableDescription
174 const char *Cvar_VariableDescription(cvar_state_t *cvars, const char *var_name, unsigned neededflags)
178 var = Cvar_FindVar(cvars, var_name, neededflags);
180 return cvar_null_string;
181 return var->description;
187 Cvar_CompleteVariable
190 const char *Cvar_CompleteVariable(cvar_state_t *cvars, const char *partial, unsigned neededflags)
195 len = strlen(partial);
201 for (cvar=cvars->vars ; cvar ; cvar=cvar->next)
202 if (!strncasecmp (partial,cvar->name, len) && (cvar->flags & neededflags))
210 CVar_CompleteCountPossible
212 New function for tab-completion system
214 Thanks to Fett erich@heintz.com
217 int Cvar_CompleteCountPossible(cvar_state_t *cvars, const char *partial, unsigned neededflags)
224 len = strlen(partial);
229 // Loop through the cvars and count all possible matches
230 for (cvar = cvars->vars; cvar; cvar = cvar->next)
231 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
234 for (char **alias = cvar->aliases; alias && *alias; alias++)
235 if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
242 CVar_CompleteBuildList
244 New function for tab-completion system
246 Thanks to Fett erich@heintz.com
250 const char **Cvar_CompleteBuildList(cvar_state_t *cvars, const char *partial, unsigned neededflags)
255 size_t sizeofbuf = (Cvar_CompleteCountPossible(cvars, partial, neededflags) + 1) * sizeof(const char *);
258 len = strlen(partial);
259 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof(const char *));
260 // Loop through the alias list and print all matches
261 for (cvar = cvars->vars; cvar; cvar = cvar->next)
262 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
263 buf[bpos++] = cvar->name;
265 for (char **alias = cvar->aliases; alias && *alias; alias++)
266 if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
267 buf[bpos++] = *alias;
274 void Cvar_PrintHelp(cvar_t *cvar, const char *name, qbool full)
276 // Aliases are purple, cvars are yellow
277 if (strcmp(cvar->name, name))
278 Con_Printf("^6%s^7 (alias of ^3%s^7)", name, cvar->name);
280 Con_Printf("^3%s^7", name);
281 Con_Printf(" is \"%s^7\" [\"%s^7\"]", ((cvar->flags & CF_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring);
283 Con_Printf(" %s", cvar->description);
287 // written by LadyHavoc
288 void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, unsigned neededflags)
291 size_t len = strlen(partial);
292 // Loop through the command list and print all matches
293 for (cvar = cvars->vars; cvar; cvar = cvar->next)
294 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
295 Cvar_PrintHelp(cvar, cvar->name, true);
297 for (char **alias = cvar->aliases; alias && *alias; alias++)
298 if (!strncasecmp (partial, *alias, len) && (cvar->flags & neededflags))
299 Cvar_PrintHelp(cvar, *alias, true);
304 /// Check if a cvar is held by some progs
305 /// in which case its name is returned, otherwise NULL.
306 static const char *Cvar_IsAutoCvar(cvar_t *var)
310 for (i = 0;i < PRVM_PROG_MAX;i++)
312 prog = &prvm_prog_list[i];
313 if (prog->loaded && var->globaldefindex[i] >= 0)
319 /// we assume that prog is already set to the target progs
320 static void Cvar_UpdateAutoCvar(cvar_t *var)
327 for (i = 0;i < PRVM_PROG_MAX;i++)
329 prog = &prvm_prog_list[i];
330 if (prog->loaded && var->globaldefindex[i] >= 0)
332 // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs
333 switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL)
336 PRVM_GLOBALFIELDFLOAT(prog->globaldefs[var->globaldefindex[i]].ofs) = var->value;
341 for (j = 0;j < 3;j++)
343 while (*s && ISWHITESPACE(*s))
348 while (!ISWHITESPACE(*s))
353 VectorCopy(v, PRVM_GLOBALFIELDVECTOR(prog->globaldefs[var->globaldefindex[i]].ofs));
356 PRVM_ChangeEngineString(prog, var->globaldefindex_stringno[i], var->string);
357 PRVM_GLOBALFIELDSTRING(prog->globaldefs[var->globaldefindex[i]].ofs) = var->globaldefindex_stringno[i];
364 /// called after loading a savegame
365 void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars)
368 for (var = cvars->vars ; var ; var = var->next)
369 Cvar_UpdateAutoCvar(var);
372 void Cvar_Callback(cvar_t *var)
376 Con_Print(CON_WARN "Cvar_Callback: var == NULL\n");
389 extern cvar_t sv_disablenotify;
390 static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
395 changed = strcmp(var->string, value) != 0;
396 // LadyHavoc: don't reallocate when there is no change
400 // LadyHavoc: don't reallocate when the buffer is the same size
401 valuelen = strlen(value);
402 if (!var->string || strlen(var->string) != valuelen)
404 Z_Free ((char *)var->string); // free the old value string
406 var->string = (char *)Z_Malloc (valuelen + 1);
408 memcpy ((char *)var->string, value, valuelen + 1);
409 var->value = atof (var->string);
410 var->integer = (int) var->value;
411 if ((var->flags & CF_NOTIFY) && sv.active && !sv_disablenotify.integer)
412 SV_BroadcastPrintf("\003^3Server cvar \"%s\" changed to \"%s\"\n", var->name, var->string);
414 // TODO: add infostring support to the server?
415 if ((var->flags & CF_SERVERINFO) && changed && sv.active)
417 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
420 MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
421 MSG_WriteString (&sv.reliable_datagram, var->name);
422 MSG_WriteString (&sv.reliable_datagram, var->string);
426 if (var->flags & CF_USERINFO)
427 CL_SetInfo(var->name, var->string, true, false, false, false);
429 Cvar_UpdateAutoCvar(var);
432 // Call the function stored in the cvar for bounds checking, cleanup, etc
436 void Cvar_SetQuick (cvar_t *var, const char *value)
440 Con_Print(CON_WARN "Cvar_SetQuick: var == NULL\n");
444 if (!(var->flags & CF_REGISTERED) && !(var->flags & CF_ALLOCATED))
446 Con_Printf(CON_WARN "Warning: Cvar_SetQuick() cannot set unregistered cvar \"%s\"\n", var->name);
447 return; // setting an unregistered engine cvar crashes
450 if (developer_extra.integer)
451 Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
453 Cvar_SetQuick_Internal(var, value);
456 void Cvar_Set(cvar_state_t *cvars, const char *var_name, const char *value)
459 var = Cvar_FindVar(cvars, var_name, ~0);
462 Con_Printf(CON_WARN "Cvar_Set: variable %s not found\n", var_name);
465 Cvar_SetQuick(var, value);
473 void Cvar_SetValueQuick(cvar_t *var, float value)
475 char val[MAX_INPUTLINE];
477 if ((float)((int)value) == value)
478 dpsnprintf(val, sizeof(val), "%i", (int)value);
480 dpsnprintf(val, sizeof(val), "%f", value);
481 Cvar_SetQuick(var, val);
484 void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
486 char val[MAX_INPUTLINE];
488 if ((float)((int)value) == value)
489 dpsnprintf(val, sizeof(val), "%i", (int)value);
491 dpsnprintf(val, sizeof(val), "%f", value);
492 Cvar_Set(cvars, var_name, val);
495 void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *))
497 if (variable == NULL)
499 Con_Print(CON_WARN "Cvar_RegisterCallback: var == NULL\n");
503 if (!(variable->flags & cmd_local->cvars_flagsmask))
505 if (developer_extra.integer)
506 Con_DPrintf("^6Cvar_RegisterCallback: rejecting cvar \"%s\"\n", variable->name);
510 variable->callback = callback;
513 void Cvar_RegisterVirtual(cvar_t *variable, const char *name )
515 cvar_state_t *cvars = &cvars_all;
519 if (!(variable->flags & cmd_local->cvars_flagsmask))
521 if (developer_extra.integer)
522 Con_DPrintf("^6Cvar_RegisterVirtual: rejecting cvar \"%s\" alias \"%s\"\n", variable->name, name);
528 Con_Printf(CON_WARN "Cvar_RegisterVirtual: invalid virtual cvar name\n");
532 // check for overlap with a command
533 if (Cmd_Exists(cmd_local, name))
535 Con_Printf(CON_WARN "Cvar_RegisterVirtual: %s is a command\n", name);
539 if(Cvar_FindVar(&cvars_all, name, 0))
541 Con_Printf(CON_WARN "Cvar_RegisterVirtual: %s is a cvar\n", name);
545 // Resize the variable->aliases list to have room for another entry and a null terminator.
546 // This zero-pads when resizing, so we don't need to write the NULL terminator manually here.
547 // Also if aliases is NULL this allocates fresh for the correct size, so it's fine to just do this.
548 variable->aliases = (char **)Z_Realloc(variable->aliases, sizeof(char *) * (variable->aliases_size + 2));
549 // Add the new alias, and increment the number of aliases in the list
550 variable->aliases[variable->aliases_size++] = Z_strdup(name);
552 // link to head of list in this hash table index
553 hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
554 hashindex = CRC_Block((const unsigned char *)name, strlen(name)) % CVAR_HASHSIZE;
555 hash->next = cvars->hashtable[hashindex];
556 cvars->hashtable[hashindex] = hash;
557 hash->cvar = variable;
564 Links a variable to the variable list and hashtable
567 static void Cvar_Link(cvar_t *variable, cvar_state_t *cvars)
569 cvar_t *current, *next;
573 * Link the variable in
574 * alphanumerical order
576 for( current = NULL, next = cvars->vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
579 current->next = variable;
581 cvars->vars = variable;
582 variable->next = next;
584 // link to head of list in this hash table index
585 hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
586 hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
587 hash->next = cvars->hashtable[hashindex];
588 hash->cvar = variable;
589 cvars->hashtable[hashindex] = hash;
594 Cvar_RegisterVariable
596 Adds a freestanding variable to the variable list.
599 void Cvar_RegisterVariable (cvar_t *variable)
601 cvar_state_t *cvars = &cvars_all;
602 cvar_t *current, *cvar;
605 if (!(variable->flags & cmd_local->cvars_flagsmask))
607 if (developer_extra.integer)
608 Con_DPrintf("^2Cvar_RegisterVariable: rejecting cvar \"%s\"\n", variable->name);
612 if (developer_extra.integer)
613 Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
615 // first check to see if it has already been defined
616 cvar = Cvar_FindVar(cvars, variable->name, ~0);
619 if (cvar->flags & CF_ALLOCATED)
621 if (developer_extra.integer)
622 Con_DPrintf("... replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
623 // fixed variables replace allocated ones
624 // (because the engine directly accesses fixed variables)
625 // NOTE: this isn't actually used currently
626 // (all cvars are registered before config parsing)
627 variable->flags &= ~CF_ALLOCATED;
628 // cvar->string is now owned by variable instead
629 variable->string = cvar->string;
630 variable->defstring = cvar->defstring;
631 variable->value = atof (variable->string);
632 variable->integer = (int) variable->value;
633 // Preserve autocvar status.
634 memcpy(variable->globaldefindex, cvar->globaldefindex, sizeof(variable->globaldefindex));
635 memcpy(variable->globaldefindex_stringno, cvar->globaldefindex_stringno, sizeof(variable->globaldefindex_stringno));
636 // replace cvar with this one...
637 variable->next = cvar->next;
638 if (cvars->vars == cvar)
640 // head of the list is easy to change
641 cvars->vars = variable;
645 // otherwise find it somewhere in the list
646 for (current = cvars->vars;current->next != cvar;current = current->next)
648 current->next = variable;
651 // get rid of old allocated cvar
652 // (but not cvar->string and cvar->defstring, because we kept those)
653 Z_Free((char *)cvar->name);
657 Con_DPrintf("Can't register variable %s, already defined\n", variable->name);
661 // check for overlap with a command
662 if (Cmd_Exists(cmd_local, variable->name))
664 Con_Printf(CON_WARN "Cvar_RegisterVariable: %s is a command\n", variable->name);
668 // copy the value off, because future sets will Z_Free it
669 variable->name = Z_strdup(variable->name);
670 variable->string = Z_strdup(variable->string);
671 variable->defstring = Z_strdup(variable->string);
672 variable->value = atof (variable->string);
673 variable->integer = (int) variable->value;
674 variable->aliases = NULL;
675 variable->aliases_size = 0;
676 variable->initstring = NULL;
678 // Mark it as not an autocvar.
679 for (i = 0;i < PRVM_PROG_MAX;i++)
680 variable->globaldefindex[i] = -1;
682 // Safe for Cvar_SetQuick()
683 variable->flags |= CF_REGISTERED;
685 Cvar_Link(variable, cvars);
692 Adds a newly allocated variable to the variable list or sets its value.
695 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, unsigned flags, const char *newdescription)
700 if (developer_extra.integer)
701 Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
703 // first check to see if it has already been defined
704 cvar = Cvar_FindVar(cvars, name, ~0);
707 cvar->flags |= flags;
708 Cvar_SetQuick_Internal (cvar, value);
709 if(newdescription && (cvar->flags & CF_ALLOCATED))
711 if(cvar->description != cvar_dummy_description)
712 Z_Free((char *)cvar->description);
715 cvar->description = Z_strdup(newdescription);
717 cvar->description = cvar_dummy_description;
722 // check for pure evil
725 Con_Printf(CON_WARN "Cvar_Get: invalid variable name\n");
729 // check for overlap with a command
730 if (Cmd_Exists(cmd_local, name))
732 Con_Printf(CON_WARN "Cvar_Get: %s is a command\n", name);
736 // allocate a new cvar, cvar name, and cvar string
737 // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
738 // FIXME: these never get Z_Free'd
739 cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
740 cvar->flags = flags | CF_ALLOCATED;
741 cvar->name = Z_strdup(name);
742 cvar->string = Z_strdup(value);
743 cvar->defstring = Z_strdup(value);
744 cvar->value = atof (cvar->string);
745 cvar->integer = (int) cvar->value;
746 cvar->aliases = NULL;
747 cvar->aliases_size = 0;
748 cvar->initstring = NULL;
750 if(newdescription && *newdescription)
751 cvar->description = Z_strdup(newdescription);
753 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
755 // Mark it as not an autocvar.
756 for (i = 0;i < PRVM_PROG_MAX;i++)
757 cvar->globaldefindex[i] = -1;
759 Cvar_Link(cvar, cvars);
764 /// For "quiet" mode pass NULL as the callername.
765 /// Returns true if the cvar was deleted.
766 static qbool Cvar_Delete(cvar_state_t *cvars, const char *name, const char *callername)
769 cvar_hash_t **hashlinkptr, *oldhashlink;
770 const char *progname;
772 hashlinkptr = Cvar_FindVarLink(cvars, name, &prev, ~0);
776 Con_Printf("%s: cvar \"%s\" is not defined.\n", callername, name);
779 cvar = (*hashlinkptr)->cvar;
781 if(!(cvar->flags & CF_ALLOCATED))
784 Con_Printf(CON_WARN "%s: engine cvar \"%s\" cannot be deleted!\n", callername, cvar->name);
787 if ((progname = Cvar_IsAutoCvar(cvar)))
790 Con_Printf(CON_WARN "%s: unable to delete cvar \"%s\", it is an autocvar used by running %s progs!\n", callername, cvar->name, progname);
794 if(cvar == cvars->vars)
795 cvars->vars = cvar->next;
797 prev->next = cvar->next;
799 if(cvar->description != cvar_dummy_description)
800 Z_Free((char *)cvar->description);
801 Z_Free((char *)cvar->name);
802 Z_Free((char *)cvar->string);
803 Z_Free((char *)cvar->defstring);
806 oldhashlink = *hashlinkptr;
807 *hashlinkptr = (*hashlinkptr)->next;
813 qbool Cvar_Readonly (cvar_t *var, const char *cmd_name)
815 if (var->flags & CF_READONLY)
819 Con_Printf("%s: ",cmd_name);
820 Con_Printf("%s is read-only\n", var->name);
830 Handles variable inspection and changing from the console
833 qbool Cvar_Command (cmd_state_t *cmd)
835 cvar_state_t *cvars = cmd->cvars;
839 v = Cvar_FindVar(cvars, Cmd_Argv(cmd, 0), (cmd->cvars_flagsmask));
843 // perform a variable print or set
844 if (Cmd_Argc(cmd) == 1)
846 Cvar_PrintHelp(v, Cmd_Argv(cmd, 0), true);
850 if (developer_extra.integer)
851 Con_DPrint("Cvar_Command: ");
853 if(Cvar_Readonly(v, NULL))
856 Cvar_SetQuick(v, Cmd_Argv(cmd, 1));
857 if (developer_extra.integer)
862 void Cvar_UnlockDefaults(cmd_state_t *cmd)
864 cvar_state_t *cvars = cmd->cvars;
866 // unlock the default values of all cvars
867 for (var = cvars->vars ; var ; var = var->next)
868 var->flags &= ~CF_DEFAULTSET;
871 void Cvar_LockDefaults_f(cmd_state_t *cmd)
873 cvar_state_t *cvars = cmd->cvars;
875 // lock in the default values of all cvars
876 for (var = cvars->vars ; var ; var = var->next)
878 if (!(var->flags & CF_DEFAULTSET))
882 //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
883 var->flags |= CF_DEFAULTSET;
884 Z_Free((char *)var->defstring);
885 alloclen = strlen(var->string) + 1;
886 var->defstring = (char *)Z_Malloc(alloclen);
887 memcpy((char *)var->defstring, var->string, alloclen);
892 void Cvar_SaveInitState(cvar_state_t *cvars)
896 for (c = cvars->vars;c;c = c->next)
897 if ((c->flags & (CF_PERSISTENT | CF_READONLY)) == 0)
898 c->initstring = Z_strdup(c->string);
901 void Cvar_RestoreInitState(cvar_state_t *cvars)
906 for (cp = cvars->vars; (c = cp);++i)
908 cp = c->next; // get next cvar now in case we delete this cvar
912 // restore this cvar, it existed at init
913 Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
915 /* bones_was_here: intentionally NOT restoring defstring in this function.
916 * The only callsite, Host_LoadConfig_f, will re-exec quake.rc, default.cfg, etc.
917 * Defaults are unlocked here, so Cvar_LockDefaults_f will reset the defstring.
918 * This is more correct than restoring the defstring here
919 * because a gamedir change could load configs that should change defaults.
922 Cvar_SetQuick(c, c->initstring);
923 c->flags &= ~CF_DEFAULTSET;
925 else if (!(c->flags & (CF_PERSISTENT | CF_READONLY))) // cvars with those flags have no initstring AND may not be deleted or reset
927 // remove this cvar, it did not exist at init
928 Con_DPrintf("Cvar_RestoreInitState: cvar \"%s\"", c->name);
929 if (Cvar_Delete(cvars, c->name, NULL))
930 Con_DPrint("deleted.\n");
933 // In this case, at least reset it to the default.
934 Con_DPrint("reset.\n");
935 Cvar_SetQuick(c, c->defstring);
941 void Cvar_ResetToDefaults_All_f(cmd_state_t *cmd)
943 cvar_state_t *cvars = cmd->cvars;
945 // restore the default values of all cvars
946 for (var = cvars->vars ; var ; var = var->next)
948 if((var->flags & (CF_PERSISTENT | CF_READONLY)) == 0)
949 Cvar_SetQuick(var, var->defstring);
953 void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd)
955 cvar_state_t *cvars = cmd->cvars;
957 // restore the default values of all cvars
958 for (var = cvars->vars ; var ; var = var->next)
960 if ((var->flags & (CF_PERSISTENT | CF_READONLY | CF_ARCHIVE)) == 0)
961 Cvar_SetQuick(var, var->defstring);
966 void Cvar_ResetToDefaults_SaveOnly_f(cmd_state_t *cmd)
968 cvar_state_t *cvars = cmd->cvars;
970 // restore the default values of all cvars
971 for (var = cvars->vars ; var ; var = var->next)
973 if ((var->flags & (CF_PERSISTENT | CF_READONLY | CF_ARCHIVE)) == CF_ARCHIVE)
974 Cvar_SetQuick(var, var->defstring);
982 Writes lines containing "set variable value" for all variables
983 with the archive flag set to true.
986 void Cvar_WriteVariables (cvar_state_t *cvars, qfile_t *f)
989 char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
991 // don't save cvars that match their default value
992 for (var = cvars->vars ; var ; var = var->next) {
993 if ((var->flags & CF_ARCHIVE) && (strcmp(var->string, var->defstring) || ((var->flags & CF_ALLOCATED) && !(var->flags & CF_DEFAULTSET))))
995 Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$", false);
996 Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$", false);
997 FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CF_ALLOCATED ? "seta " : "", buf1, buf2);
1002 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
1003 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
1009 void Cvar_List_f(cmd_state_t *cmd)
1011 cvar_state_t *cvars = cmd->cvars;
1013 const char *partial;
1018 if (Cmd_Argc(cmd) > 1)
1020 partial = Cmd_Argv(cmd, 1);
1021 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1023 partial = va(vabuf, sizeof(vabuf), "%s*", partial);
1027 partial = va(vabuf, sizeof(vabuf), "*");
1032 for (cvar = cvars->vars; cvar; cvar = cvar->next)
1034 if (matchpattern_with_separator(cvar->name, partial, false, "", false))
1036 Cvar_PrintHelp(cvar, cvar->name, true);
1039 for (char **alias = cvar->aliases; alias && *alias; alias++)
1041 if (matchpattern_with_separator(*alias, partial, false, "", false))
1043 Cvar_PrintHelp(cvar, *alias, true);
1049 if (Cmd_Argc(cmd) > 1)
1052 Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
1054 Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", Cmd_Argv(cmd,1));
1057 Con_Printf("%i cvar(s)\n", count);
1059 // 2000-01-09 CvarList command by Maddes
1061 void Cvar_Set_f(cmd_state_t *cmd)
1063 cvar_state_t *cvars = cmd->cvars;
1066 // make sure it's the right number of parameters
1067 if (Cmd_Argc(cmd) < 3)
1069 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
1073 // check if it's read-only
1074 cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1076 if(Cvar_Readonly(cvar,"Set"))
1079 if (developer_extra.integer)
1080 Con_DPrint("Set: ");
1082 // all looks ok, create/modify the cvar
1083 Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1086 void Cvar_SetA_f(cmd_state_t *cmd)
1088 cvar_state_t *cvars = cmd->cvars;
1091 // make sure it's the right number of parameters
1092 if (Cmd_Argc(cmd) < 3)
1094 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
1098 // check if it's read-only
1099 cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1101 if(Cvar_Readonly(cvar,"SetA"))
1104 if (developer_extra.integer)
1105 Con_DPrint("SetA: ");
1107 // all looks ok, create/modify the cvar
1108 Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask | CF_ARCHIVE, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1111 void Cvar_Del_f(cmd_state_t *cmd)
1113 cvar_state_t *cvars = cmd->cvars;
1116 if(Cmd_Argc(cmd) < 2)
1118 Con_Printf("%s: wrong number of parameters, usage: unset <variablename1> [<variablename2> ...]\n", Cmd_Argv(cmd, 0));
1121 for(i = 1; i < Cmd_Argc(cmd); ++i)
1122 Cvar_Delete(cvars, Cmd_Argv(cmd, i), Cmd_Argv(cmd, 0));
1125 #ifdef FILLALLCVARSWITHRUBBISH
1126 void Cvar_FillAll_f(cmd_state_t *cmd)
1132 if(Cmd_Argc(cmd) != 2)
1134 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(cmd, 0));
1135 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(cmd, 0));
1138 n = atoi(Cmd_Argv(cmd, 1));
1142 buf = Z_Malloc(n + 1);
1144 for(var = cvars->vars; var; var = var->next)
1146 for(i = 0, p = buf, q = var->name; i < n; ++i)
1152 if(verify && strcmp(var->string, buf))
1154 Con_Printf("\n%s does not contain the right rubbish, either this is the first run or a possible overrun was detected, or something changed it intentionally; it DOES contain: %s\n", var->name, var->string);
1156 Cvar_SetQuick(var, buf);
1160 #endif /* FILLALLCVARSWITHRUBBISH */