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, int 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, int 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)
77 static cvar_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name, cvar_t **parent, cvar_t ***link, cvar_t **prev_alpha, int neededflags)
82 // use hash lookup to minimize search time
83 hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)) % CVAR_HASHSIZE;
84 if(parent) *parent = NULL;
85 if(prev_alpha) *prev_alpha = NULL;
86 if(link) *link = &cvars->hashtable[hashindex]->cvar;
87 for (hash = cvars->hashtable[hashindex];hash;hash = hash->next)
89 if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
92 for (char **alias = hash->cvar->aliases; alias && *alias; alias++)
93 if (!strcmp (var_name, *alias) && (hash->cvar->flags & neededflags))
95 if(parent) *parent = hash->cvar;
99 if(!prev_alpha || hash->cvar == cvars->vars)
102 *prev_alpha = cvars->vars;
103 // if prev_alpha happens to become NULL then there has been some inconsistency elsewhere
104 // already - should I still insert '*prev_alpha &&' in the loop?
105 while((*prev_alpha)->next != hash->cvar)
106 *prev_alpha = (*prev_alpha)->next;
115 float Cvar_VariableValueOr(cvar_state_t *cvars, const char *var_name, float def, int neededflags)
119 var = Cvar_FindVar(cvars, var_name, neededflags);
122 return atof (var->string);
125 float Cvar_VariableValue(cvar_state_t *cvars, const char *var_name, int neededflags)
127 return Cvar_VariableValueOr(cvars, var_name, 0, neededflags);
135 const char *Cvar_VariableStringOr(cvar_state_t *cvars, const char *var_name, const char *def, int neededflags)
139 var = Cvar_FindVar(cvars, var_name, neededflags);
145 const char *Cvar_VariableString(cvar_state_t *cvars, const char *var_name, int neededflags)
147 return Cvar_VariableStringOr(cvars, var_name, cvar_null_string, neededflags);
152 Cvar_VariableDefString
155 const char *Cvar_VariableDefString(cvar_state_t *cvars, const char *var_name, int neededflags)
159 var = Cvar_FindVar(cvars, var_name, neededflags);
161 return cvar_null_string;
162 return var->defstring;
167 Cvar_VariableDescription
170 const char *Cvar_VariableDescription(cvar_state_t *cvars, const char *var_name, int neededflags)
174 var = Cvar_FindVar(cvars, var_name, neededflags);
176 return cvar_null_string;
177 return var->description;
183 Cvar_CompleteVariable
186 const char *Cvar_CompleteVariable(cvar_state_t *cvars, const char *partial, int neededflags)
191 len = strlen(partial);
197 for (cvar=cvars->vars ; cvar ; cvar=cvar->next)
198 if (!strncasecmp (partial,cvar->name, len) && (cvar->flags & neededflags))
206 CVar_CompleteCountPossible
208 New function for tab-completion system
210 Thanks to Fett erich@heintz.com
213 int Cvar_CompleteCountPossible(cvar_state_t *cvars, const char *partial, int neededflags)
220 len = strlen(partial);
225 // Loop through the cvars and count all possible matches
226 for (cvar = cvars->vars; cvar; cvar = cvar->next)
227 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
230 for (char **alias = cvar->aliases; alias && *alias; alias++)
231 if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
238 CVar_CompleteBuildList
240 New function for tab-completion system
242 Thanks to Fett erich@heintz.com
246 const char **Cvar_CompleteBuildList(cvar_state_t *cvars, const char *partial, int neededflags)
251 size_t sizeofbuf = (Cvar_CompleteCountPossible(cvars, partial, neededflags) + 1) * sizeof(const char *);
254 len = strlen(partial);
255 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof(const char *));
256 // Loop through the alias list and print all matches
257 for (cvar = cvars->vars; cvar; cvar = cvar->next)
258 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
259 buf[bpos++] = cvar->name;
261 for (char **alias = cvar->aliases; alias && *alias; alias++)
262 if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
263 buf[bpos++] = *alias;
270 void Cvar_PrintHelp(cvar_t *cvar, const char *name, qbool full)
272 // Aliases are purple, cvars are yellow
273 if (strcmp(cvar->name, name))
277 Con_Printf("%s^7 is \"%s\" [\"%s\"]", name, ((cvar->flags & CF_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring);
278 if (strcmp(cvar->name, name))
279 Con_Printf(" (also ^3%s^7)", cvar->name);
281 Con_Printf(" %s", cvar->description);
285 // written by LadyHavoc
286 void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, int neededflags)
289 size_t len = strlen(partial);
290 // Loop through the command list and print all matches
291 for (cvar = cvars->vars; cvar; cvar = cvar->next)
292 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
293 Cvar_PrintHelp(cvar, cvar->name, true);
295 for (char **alias = cvar->aliases; alias && *alias; alias++)
296 if (!strncasecmp (partial, *alias, len) && (cvar->flags & neededflags))
297 Cvar_PrintHelp(cvar, *alias, true);
302 // check if a cvar is held by some progs
303 static qbool Cvar_IsAutoCvar(cvar_t *var)
307 for (i = 0;i < PRVM_PROG_MAX;i++)
309 prog = &prvm_prog_list[i];
310 if (prog->loaded && var->globaldefindex[i] >= 0)
316 // we assume that prog is already set to the target progs
317 static void Cvar_UpdateAutoCvar(cvar_t *var)
324 for (i = 0;i < PRVM_PROG_MAX;i++)
326 prog = &prvm_prog_list[i];
327 if (prog->loaded && var->globaldefindex[i] >= 0)
329 // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs
330 switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL)
333 PRVM_GLOBALFIELDFLOAT(prog->globaldefs[var->globaldefindex[i]].ofs) = var->value;
338 for (j = 0;j < 3;j++)
340 while (*s && ISWHITESPACE(*s))
345 while (!ISWHITESPACE(*s))
350 VectorCopy(v, PRVM_GLOBALFIELDVECTOR(prog->globaldefs[var->globaldefindex[i]].ofs));
353 PRVM_ChangeEngineString(prog, var->globaldefindex_stringno[i], var->string);
354 PRVM_GLOBALFIELDSTRING(prog->globaldefs[var->globaldefindex[i]].ofs) = var->globaldefindex_stringno[i];
361 // called after loading a savegame
362 void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars)
365 for (var = cvars->vars ; var ; var = var->next)
366 Cvar_UpdateAutoCvar(var);
369 void Cvar_Callback(cvar_t *var)
373 Con_Print("Cvar_Callback: var == NULL\n");
386 extern cvar_t sv_disablenotify;
387 static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
392 changed = strcmp(var->string, value) != 0;
393 // LadyHavoc: don't reallocate when there is no change
397 // LadyHavoc: don't reallocate when the buffer is the same size
398 valuelen = strlen(value);
399 if (!var->string || strlen(var->string) != valuelen)
401 Z_Free ((char *)var->string); // free the old value string
403 var->string = (char *)Z_Malloc (valuelen + 1);
405 memcpy ((char *)var->string, value, valuelen + 1);
406 var->value = atof (var->string);
407 var->integer = (int) var->value;
408 if ((var->flags & CF_NOTIFY) && sv.active && !sv_disablenotify.integer)
409 SV_BroadcastPrintf("\003^3Server cvar \"%s\" changed to \"%s\"\n", var->name, var->string);
411 // TODO: add infostring support to the server?
412 if ((var->flags & CF_SERVERINFO) && changed && sv.active)
414 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
417 MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
418 MSG_WriteString (&sv.reliable_datagram, var->name);
419 MSG_WriteString (&sv.reliable_datagram, var->string);
423 if (var->flags & CF_USERINFO)
424 CL_SetInfo(var->name, var->string, true, false, false, false);
426 Cvar_UpdateAutoCvar(var);
428 // Call the function stored in the cvar for bounds checking, cleanup, etc
432 void Cvar_SetQuick (cvar_t *var, const char *value)
436 Con_Print("Cvar_SetQuick: var == NULL\n");
440 if (developer_extra.integer)
441 Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
443 Cvar_SetQuick_Internal(var, value);
446 void Cvar_Set(cvar_state_t *cvars, const char *var_name, const char *value)
449 var = Cvar_FindVar(cvars, var_name, ~0);
452 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
455 Cvar_SetQuick(var, value);
463 void Cvar_SetValueQuick(cvar_t *var, float value)
465 char val[MAX_INPUTLINE];
467 if ((float)((int)value) == value)
468 dpsnprintf(val, sizeof(val), "%i", (int)value);
470 dpsnprintf(val, sizeof(val), "%f", value);
471 Cvar_SetQuick(var, val);
474 void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
476 char val[MAX_INPUTLINE];
478 if ((float)((int)value) == value)
479 dpsnprintf(val, sizeof(val), "%i", (int)value);
481 dpsnprintf(val, sizeof(val), "%f", value);
482 Cvar_Set(cvars, var_name, val);
485 void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *))
487 if (variable == NULL)
489 Con_Print("Cvar_RegisterCallback: var == NULL\n");
492 variable->callback = callback;
495 void Cvar_RegisterAlias(cvar_t *variable, const char *alias )
497 cvar_state_t *cvars = &cvars_all;
503 Con_Printf("Cvar_RegisterAlias: invalid alias name\n");
507 // check for overlap with a command
508 if (Cmd_Exists(cmd_local, alias))
510 Con_Printf("Cvar_RegisterAlias: %s is a command\n", alias);
514 if(Cvar_FindVar(&cvars_all, alias, 0))
516 Con_Printf("Cvar_RegisterAlias: %s is a cvar\n", alias);
520 if(!variable->aliases)
521 variable->aliases = (char **)Z_Malloc(sizeof(char *) * 2); // For NULL terminator
523 variable->aliases = (char **)Mem_Realloc(zonemempool, variable->aliases, sizeof(char *) * (variable->aliases_size + 1));
525 variable->aliases[variable->aliases_size + 1] = NULL;
528 variable->aliases[variable->aliases_size] = (char *)Z_Malloc(strlen(alias) + 1);
529 memcpy(variable->aliases[variable->aliases_size], alias, strlen(alias) + 1);
530 variable->aliases_size++;
532 // link to head of list in this hash table index
533 hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
534 hashindex = CRC_Block((const unsigned char *)alias, strlen(alias)) % CVAR_HASHSIZE;
535 hash->next = cvars->hashtable[hashindex];
536 cvars->hashtable[hashindex] = hash;
537 hash->cvar = variable;
544 Links a variable to the variable list and hashtable
547 static void Cvar_Link(cvar_t *variable, cvar_state_t *cvars)
549 cvar_t *current, *next;
553 * Link the variable in
554 * alphanumerical order
556 for( current = NULL, next = cvars->vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
559 current->next = variable;
561 cvars->vars = variable;
562 variable->next = next;
564 // link to head of list in this hash table index
565 hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
566 hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
567 hash->next = cvars->hashtable[hashindex];
568 hash->cvar = variable;
569 cvars->hashtable[hashindex] = hash;
574 Cvar_RegisterVariable
576 Adds a freestanding variable to the variable list.
579 void Cvar_RegisterVariable (cvar_t *variable)
581 cvar_state_t *cvars = NULL;
582 cvar_t *current, *cvar;
585 switch (variable->flags & (CF_CLIENT | CF_SERVER))
589 case CF_CLIENT | CF_SERVER:
593 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with no CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
596 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with weird CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
600 if (developer_extra.integer)
601 Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
603 // first check to see if it has already been defined
604 cvar = Cvar_FindVar(cvars, variable->name, ~0);
607 if (cvar->flags & CF_ALLOCATED)
609 if (developer_extra.integer)
610 Con_DPrintf("... replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
611 // fixed variables replace allocated ones
612 // (because the engine directly accesses fixed variables)
613 // NOTE: this isn't actually used currently
614 // (all cvars are registered before config parsing)
615 variable->flags |= (cvar->flags & ~CF_ALLOCATED);
616 // cvar->string is now owned by variable instead
617 variable->string = cvar->string;
618 variable->defstring = cvar->defstring;
619 variable->value = atof (variable->string);
620 variable->integer = (int) variable->value;
621 // Preserve autocvar status.
622 memcpy(variable->globaldefindex, cvar->globaldefindex, sizeof(variable->globaldefindex));
623 memcpy(variable->globaldefindex_stringno, cvar->globaldefindex_stringno, sizeof(variable->globaldefindex_stringno));
624 // replace cvar with this one...
625 variable->next = cvar->next;
626 if (cvars->vars == cvar)
628 // head of the list is easy to change
629 cvars->vars = variable;
633 // otherwise find it somewhere in the list
634 for (current = cvars->vars;current->next != cvar;current = current->next)
636 current->next = variable;
639 // get rid of old allocated cvar
640 // (but not cvar->string and cvar->defstring, because we kept those)
641 Z_Free((char *)cvar->name);
645 Con_DPrintf("Can't register variable %s, already defined\n", variable->name);
649 // check for overlap with a command
650 if (Cmd_Exists(cmd_local, variable->name) || Cmd_Exists(cmd_local, variable->name))
652 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
656 // copy the value off, because future sets will Z_Free it
657 variable->name = (char *)Mem_strdup(zonemempool, variable->name);
658 variable->string = (char *)Mem_strdup(zonemempool, variable->string);
659 variable->defstring = (char *)Mem_strdup(zonemempool, variable->string);
660 variable->value = atof (variable->string);
661 variable->integer = (int) variable->value;
662 variable->aliases = NULL;
663 variable->aliases_size = 0;
664 variable->initstate = NULL;
666 // Mark it as not an autocvar.
667 for (i = 0;i < PRVM_PROG_MAX;i++)
668 variable->globaldefindex[i] = -1;
670 Cvar_Link(variable, cvars);
677 Adds a newly allocated variable to the variable list or sets its value.
680 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int flags, const char *newdescription)
685 if (developer_extra.integer)
686 Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
688 // first check to see if it has already been defined
689 cvar = Cvar_FindVar(cvars, name, ~0);
692 cvar->flags |= flags;
693 Cvar_SetQuick_Internal (cvar, value);
694 if(newdescription && (cvar->flags & CF_ALLOCATED))
696 if(cvar->description != cvar_dummy_description)
697 Z_Free((char *)cvar->description);
700 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
702 cvar->description = cvar_dummy_description;
707 // check for pure evil
710 Con_Printf("Cvar_Get: invalid variable name\n");
714 // check for overlap with a command
715 if (Cmd_Exists(cmd_local, name) || Cmd_Exists(cmd_local, name))
717 Con_Printf("Cvar_Get: %s is a command\n", name);
721 // allocate a new cvar, cvar name, and cvar string
722 // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
723 // FIXME: these never get Z_Free'd
724 cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
725 cvar->flags = flags | CF_ALLOCATED;
726 cvar->name = (char *)Mem_strdup(zonemempool, name);
727 cvar->string = (char *)Mem_strdup(zonemempool, value);
728 cvar->defstring = (char *)Mem_strdup(zonemempool, value);
729 cvar->value = atof (cvar->string);
730 cvar->integer = (int) cvar->value;
731 cvar->aliases = NULL;
732 cvar->aliases_size = 0;
733 cvar->initstate = NULL;
734 memset(cvar->aliases, 0, sizeof(char *));
736 if(newdescription && *newdescription)
737 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
739 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
741 // Mark it as not an autocvar.
742 for (i = 0;i < PRVM_PROG_MAX;i++)
743 cvar->globaldefindex[i] = -1;
745 Cvar_Link(cvar, cvars);
750 qbool Cvar_Readonly (cvar_t *var, const char *cmd_name)
752 if (var->flags & CF_READONLY)
755 Con_Printf("%s: ",cmd_name);
756 Con_Printf("%s", var->name);
757 Con_Printf(" is read-only\n");
767 Handles variable inspection and changing from the console
770 qbool Cvar_Command (cmd_state_t *cmd)
772 cvar_state_t *cvars = cmd->cvars;
776 v = Cvar_FindVar(cvars, Cmd_Argv(cmd, 0), (cmd->cvars_flagsmask));
780 // perform a variable print or set
781 if (Cmd_Argc(cmd) == 1)
783 Cvar_PrintHelp(v, Cmd_Argv(cmd, 0), true);
787 if (developer_extra.integer)
788 Con_DPrint("Cvar_Command: ");
790 if(Cvar_Readonly(v, NULL))
793 Cvar_SetQuick(v, Cmd_Argv(cmd, 1));
794 if (developer_extra.integer)
799 void Cvar_UnlockDefaults(cmd_state_t *cmd)
801 cvar_state_t *cvars = cmd->cvars;
803 // unlock the default values of all cvars
804 for (var = cvars->vars ; var ; var = var->next)
805 var->flags &= ~CF_DEFAULTSET;
808 void Cvar_LockDefaults_f(cmd_state_t *cmd)
810 cvar_state_t *cvars = cmd->cvars;
812 // lock in the default values of all cvars
813 for (var = cvars->vars ; var ; var = var->next)
815 if (!(var->flags & CF_DEFAULTSET))
819 //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
820 var->flags |= CF_DEFAULTSET;
821 Z_Free((char *)var->defstring);
822 alloclen = strlen(var->string) + 1;
823 var->defstring = (char *)Z_Malloc(alloclen);
824 memcpy((char *)var->defstring, var->string, alloclen);
829 void Cvar_SaveInitState(cvar_state_t *cvars)
832 for (c = cvars->vars;c;c = c->next)
834 c->initstate = (cvar_t *)Z_Malloc(sizeof(cvar_t));
835 memcpy(c->initstate, c, sizeof(cvar_t));
839 void Cvar_RestoreInitState(cvar_state_t *cvars)
844 for (cp = &cvars->vars;(c = *cp);)
848 // restore this cvar, it existed at init
849 if (((c->flags ^ c->initstate->flags) & CF_MAXFLAGSVAL)
850 || strcmp(c->defstring ? c->defstring : "", c->initstate->defstring ? c->initstate->defstring : "")
851 || strcmp(c->string ? c->string : "", c->initstate->string ? c->initstate->string : ""))
853 Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
855 Z_Free((char *)c->defstring);
856 c->defstring = Mem_strdup(zonemempool, c->initstate->defstring);
858 Z_Free((char *)c->string);
859 c->string = Mem_strdup(zonemempool, c->initstate->string);
861 c->flags = c->initstate->flags;
862 c->value = c->initstate->value;
863 c->integer = c->initstate->integer;
864 VectorCopy(c->initstate->vector, c->vector);
869 if (!(c->flags & CF_ALLOCATED))
871 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it was registered after init!\n", c->name);
872 // In this case, at least reset it to the default.
873 if((c->flags & CF_PERSISTENT) == 0)
874 Cvar_SetQuick(c, c->defstring);
878 if (Cvar_IsAutoCvar(c))
880 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it is an autocvar used by running progs!\n", c->name);
881 // In this case, at least reset it to the default.
882 if((c->flags & CF_PERSISTENT) == 0)
883 Cvar_SetQuick(c, c->defstring);
887 // remove this cvar, it did not exist at init
888 Con_DPrintf("Cvar_RestoreInitState: Destroying cvar \"%s\"\n", c->name);
889 // unlink struct from hash
890 hashindex = CRC_Block((const unsigned char *)c->name, strlen(c->name)) % CVAR_HASHSIZE;
891 for (cp2 = &cvars->hashtable[hashindex]->cvar;(c2 = *cp2);)
895 *cp2 = cvars->hashtable[hashindex]->next->cvar;
899 cp2 = &cvars->hashtable[hashindex]->next->cvar;
901 // unlink struct from main list
905 Z_Free((char *)c->defstring);
907 Z_Free((char *)c->string);
908 if (c->description && c->description != cvar_dummy_description)
909 Z_Free((char *)c->description);
916 void Cvar_ResetToDefaults_All_f(cmd_state_t *cmd)
918 cvar_state_t *cvars = cmd->cvars;
920 // restore the default values of all cvars
921 for (var = cvars->vars ; var ; var = var->next)
923 if((var->flags & CF_PERSISTENT) == 0)
924 Cvar_SetQuick(var, var->defstring);
928 void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd)
930 cvar_state_t *cvars = cmd->cvars;
932 // restore the default values of all cvars
933 for (var = cvars->vars ; var ; var = var->next)
935 if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == 0)
936 Cvar_SetQuick(var, var->defstring);
941 void Cvar_ResetToDefaults_SaveOnly_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_ARCHIVE)) == CF_ARCHIVE)
949 Cvar_SetQuick(var, var->defstring);
957 Writes lines containing "set variable value" for all variables
958 with the archive flag set to true.
961 void Cvar_WriteVariables (cvar_state_t *cvars, qfile_t *f)
964 char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
966 // don't save cvars that match their default value
967 for (var = cvars->vars ; var ; var = var->next) {
968 if ((var->flags & CF_ARCHIVE) && (strcmp(var->string, var->defstring) || ((var->flags & CF_ALLOCATED) && !(var->flags & CF_DEFAULTSET))))
970 Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$", false);
971 Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$", false);
972 FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CF_ALLOCATED ? "seta " : "", buf1, buf2);
977 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
978 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
984 void Cvar_List_f(cmd_state_t *cmd)
986 cvar_state_t *cvars = cmd->cvars;
993 if (Cmd_Argc(cmd) > 1)
995 partial = Cmd_Argv(cmd, 1);
996 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
998 partial = va(vabuf, sizeof(vabuf), "%s*", partial);
1002 partial = va(vabuf, sizeof(vabuf), "*");
1007 for (cvar = cvars->vars; cvar; cvar = cvar->next)
1009 if (matchpattern_with_separator(cvar->name, partial, false, "", false))
1011 Cvar_PrintHelp(cvar, cvar->name, true);
1014 for (char **alias = cvar->aliases; alias && *alias; alias++)
1016 if (matchpattern_with_separator(*alias, partial, false, "", false))
1018 Cvar_PrintHelp(cvar, *alias, true);
1024 if (Cmd_Argc(cmd) > 1)
1027 Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
1029 Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", Cmd_Argv(cmd,1));
1032 Con_Printf("%i cvar(s)\n", count);
1034 // 2000-01-09 CvarList command by Maddes
1036 void Cvar_Set_f(cmd_state_t *cmd)
1038 cvar_state_t *cvars = cmd->cvars;
1041 // make sure it's the right number of parameters
1042 if (Cmd_Argc(cmd) < 3)
1044 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
1048 // check if it's read-only
1049 cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1051 if(Cvar_Readonly(cvar,"Set"))
1054 if (developer_extra.integer)
1055 Con_DPrint("Set: ");
1057 // all looks ok, create/modify the cvar
1058 Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1061 void Cvar_SetA_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("SetA: wrong number of parameters, usage: seta <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,"SetA"))
1079 if (developer_extra.integer)
1080 Con_DPrint("SetA: ");
1082 // all looks ok, create/modify the cvar
1083 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);
1086 void Cvar_Del_f(cmd_state_t *cmd)
1088 cvar_state_t *cvars = cmd->cvars;
1089 int neededflags = ~0;
1091 cvar_t *parent, **link;
1092 cvar_t *cvar, *prev;
1094 if(Cmd_Argc(cmd) < 2)
1096 Con_Printf("%s: wrong number of parameters, usage: unset <variablename1> [<variablename2> ...]\n", Cmd_Argv(cmd, 0));
1099 for(i = 1; i < Cmd_Argc(cmd); ++i)
1101 cvar = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
1105 Con_Printf("%s: %s is not defined\n", Cmd_Argv(cmd, 0), Cmd_Argv(cmd, i));
1108 if(Cvar_Readonly(cvar, Cmd_Argv(cmd, 0)))
1110 if(!(cvar->flags & CF_ALLOCATED))
1112 Con_Printf("%s: %s is static and cannot be deleted\n", Cmd_Argv(cmd, 0), cvar->name);
1115 if(cvar == cvars->vars)
1117 cvars->vars = cvar->next;
1121 // in this case, prev must be set, otherwise there has been some inconsistensy
1122 // elsewhere already... should I still check for prev != NULL?
1123 prev->next = cvar->next;
1127 parent->next = cvar->next;
1130 if(cvar->description != cvar_dummy_description)
1131 Z_Free((char *)cvar->description);
1133 Z_Free((char *)cvar->name);
1134 Z_Free((char *)cvar->string);
1135 Z_Free((char *)cvar->defstring);
1140 #ifdef FILLALLCVARSWITHRUBBISH
1141 void Cvar_FillAll_f(cmd_state_t *cmd)
1147 if(Cmd_Argc(cmd) != 2)
1149 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(cmd, 0));
1150 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(cmd, 0));
1153 n = atoi(Cmd_Argv(cmd, 1));
1157 buf = Z_Malloc(n + 1);
1159 for(var = cvars->vars; var; var = var->next)
1161 for(i = 0, p = buf, q = var->name; i < n; ++i)
1167 if(verify && strcmp(var->string, buf))
1169 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);
1171 Cvar_SetQuick(var, buf);
1175 #endif /* FILLALLCVARSWITHRUBBISH */