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))
274 Con_Printf("^6%s^7 (alias of ^3%s^7)", name, cvar->name);
276 Con_Printf("^3%s^7", name);
277 Con_Printf(" is \"%s^7\" [\"%s^7\"]", ((cvar->flags & CF_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring);
279 Con_Printf(" %s", cvar->description);
283 // written by LadyHavoc
284 void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, int neededflags)
287 size_t len = strlen(partial);
288 // Loop through the command list and print all matches
289 for (cvar = cvars->vars; cvar; cvar = cvar->next)
290 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
291 Cvar_PrintHelp(cvar, cvar->name, true);
293 for (char **alias = cvar->aliases; alias && *alias; alias++)
294 if (!strncasecmp (partial, *alias, len) && (cvar->flags & neededflags))
295 Cvar_PrintHelp(cvar, *alias, true);
300 // check if a cvar is held by some progs
301 static qbool Cvar_IsAutoCvar(cvar_t *var)
305 for (i = 0;i < PRVM_PROG_MAX;i++)
307 prog = &prvm_prog_list[i];
308 if (prog->loaded && var->globaldefindex[i] >= 0)
314 // we assume that prog is already set to the target progs
315 static void Cvar_UpdateAutoCvar(cvar_t *var)
322 for (i = 0;i < PRVM_PROG_MAX;i++)
324 prog = &prvm_prog_list[i];
325 if (prog->loaded && var->globaldefindex[i] >= 0)
327 // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs
328 switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL)
331 PRVM_GLOBALFIELDFLOAT(prog->globaldefs[var->globaldefindex[i]].ofs) = var->value;
336 for (j = 0;j < 3;j++)
338 while (*s && ISWHITESPACE(*s))
343 while (!ISWHITESPACE(*s))
348 VectorCopy(v, PRVM_GLOBALFIELDVECTOR(prog->globaldefs[var->globaldefindex[i]].ofs));
351 PRVM_ChangeEngineString(prog, var->globaldefindex_stringno[i], var->string);
352 PRVM_GLOBALFIELDSTRING(prog->globaldefs[var->globaldefindex[i]].ofs) = var->globaldefindex_stringno[i];
359 // called after loading a savegame
360 void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars)
363 for (var = cvars->vars ; var ; var = var->next)
364 Cvar_UpdateAutoCvar(var);
367 void Cvar_Callback(cvar_t *var)
371 Con_Print("Cvar_Callback: var == NULL\n");
384 extern cvar_t sv_disablenotify;
385 static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
390 changed = strcmp(var->string, value) != 0;
391 // LadyHavoc: don't reallocate when there is no change
395 // LadyHavoc: don't reallocate when the buffer is the same size
396 valuelen = strlen(value);
397 if (!var->string || strlen(var->string) != valuelen)
399 Z_Free ((char *)var->string); // free the old value string
401 var->string = (char *)Z_Malloc (valuelen + 1);
403 memcpy ((char *)var->string, value, valuelen + 1);
404 var->value = atof (var->string);
405 var->integer = (int) var->value;
406 if ((var->flags & CF_NOTIFY) && sv.active && !sv_disablenotify.integer)
407 SV_BroadcastPrintf("\003^3Server cvar \"%s\" changed to \"%s\"\n", var->name, var->string);
409 // TODO: add infostring support to the server?
410 if ((var->flags & CF_SERVERINFO) && changed && sv.active)
412 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
415 MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
416 MSG_WriteString (&sv.reliable_datagram, var->name);
417 MSG_WriteString (&sv.reliable_datagram, var->string);
421 if (var->flags & CF_USERINFO)
422 CL_SetInfo(var->name, var->string, true, false, false, false);
424 Cvar_UpdateAutoCvar(var);
426 // Call the function stored in the cvar for bounds checking, cleanup, etc
430 void Cvar_SetQuick (cvar_t *var, const char *value)
434 Con_Print("Cvar_SetQuick: var == NULL\n");
438 if (!(var->flags & CF_REGISTERED) && !(var->flags & CF_ALLOCATED))
440 Con_Printf(CON_WARN "Warning: Cvar_SetQuick() cannot set unregistered cvar \"%s\"\n", var->name);
441 return; // setting an unregistered engine cvar crashes
444 if (developer_extra.integer)
445 Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
447 Cvar_SetQuick_Internal(var, value);
450 void Cvar_Set(cvar_state_t *cvars, const char *var_name, const char *value)
453 var = Cvar_FindVar(cvars, var_name, ~0);
456 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
459 Cvar_SetQuick(var, value);
467 void Cvar_SetValueQuick(cvar_t *var, float value)
469 char val[MAX_INPUTLINE];
471 if ((float)((int)value) == value)
472 dpsnprintf(val, sizeof(val), "%i", (int)value);
474 dpsnprintf(val, sizeof(val), "%f", value);
475 Cvar_SetQuick(var, val);
478 void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
480 char val[MAX_INPUTLINE];
482 if ((float)((int)value) == value)
483 dpsnprintf(val, sizeof(val), "%i", (int)value);
485 dpsnprintf(val, sizeof(val), "%f", value);
486 Cvar_Set(cvars, var_name, val);
489 void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *))
491 if (variable == NULL)
493 Con_Print("Cvar_RegisterCallback: var == NULL\n");
496 variable->callback = callback;
499 void Cvar_RegisterVirtual(cvar_t *variable, const char *name )
501 cvar_state_t *cvars = &cvars_all;
505 if (cls.state == ca_dedicated && !(variable->flags & CF_SERVER))
510 Con_Printf(CON_WARN "Cvar_RegisterVirtual: invalid virtual cvar name\n");
514 // check for overlap with a command
515 if (Cmd_Exists(cmd_local, name))
517 Con_Printf(CON_WARN "Cvar_RegisterVirtual: %s is a command\n", name);
521 if(Cvar_FindVar(&cvars_all, name, 0))
523 Con_Printf(CON_WARN "Cvar_RegisterVirtual: %s is a cvar\n", name);
527 // Resize the variable->aliases list to have room for another entry and a null terminator.
528 // This zero-pads when resizing, so we don't need to write the NULL terminator manually here.
529 // Also if aliases is NULL this allocates fresh for the correct size, so it's fine to just do this.
530 variable->aliases = (char **)Z_Realloc(variable->aliases, sizeof(char *) * (variable->aliases_size + 2));
531 // Add the new alias, and increment the number of aliases in the list
532 variable->aliases[variable->aliases_size++] = (char *)Z_strdup(name);
534 // link to head of list in this hash table index
535 hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
536 hashindex = CRC_Block((const unsigned char *)name, strlen(name)) % CVAR_HASHSIZE;
537 hash->next = cvars->hashtable[hashindex];
538 cvars->hashtable[hashindex] = hash;
539 hash->cvar = variable;
546 Links a variable to the variable list and hashtable
549 static void Cvar_Link(cvar_t *variable, cvar_state_t *cvars)
551 cvar_t *current, *next;
555 * Link the variable in
556 * alphanumerical order
558 for( current = NULL, next = cvars->vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
561 current->next = variable;
563 cvars->vars = variable;
564 variable->next = next;
566 // link to head of list in this hash table index
567 hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
568 hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
569 hash->next = cvars->hashtable[hashindex];
570 hash->cvar = variable;
571 cvars->hashtable[hashindex] = hash;
576 Cvar_RegisterVariable
578 Adds a freestanding variable to the variable list.
581 void Cvar_RegisterVariable (cvar_t *variable)
583 cvar_state_t *cvars = NULL;
584 cvar_t *current, *cvar;
587 switch (variable->flags & (CF_CLIENT | CF_SERVER))
589 case CF_CLIENT: // client-only cvar
590 if (cls.state == ca_dedicated)
593 case CF_CLIENT | CF_SERVER:
597 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with no CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
600 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with weird CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
604 if (developer_extra.integer)
605 Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
607 // first check to see if it has already been defined
608 cvar = Cvar_FindVar(cvars, variable->name, ~0);
611 if (cvar->flags & CF_ALLOCATED)
613 if (developer_extra.integer)
614 Con_DPrintf("... replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
615 // fixed variables replace allocated ones
616 // (because the engine directly accesses fixed variables)
617 // NOTE: this isn't actually used currently
618 // (all cvars are registered before config parsing)
619 variable->flags &= ~CF_ALLOCATED;
620 // cvar->string is now owned by variable instead
621 variable->string = cvar->string;
622 variable->defstring = cvar->defstring;
623 variable->value = atof (variable->string);
624 variable->integer = (int) variable->value;
625 // Preserve autocvar status.
626 memcpy(variable->globaldefindex, cvar->globaldefindex, sizeof(variable->globaldefindex));
627 memcpy(variable->globaldefindex_stringno, cvar->globaldefindex_stringno, sizeof(variable->globaldefindex_stringno));
628 // replace cvar with this one...
629 variable->next = cvar->next;
630 if (cvars->vars == cvar)
632 // head of the list is easy to change
633 cvars->vars = variable;
637 // otherwise find it somewhere in the list
638 for (current = cvars->vars;current->next != cvar;current = current->next)
640 current->next = variable;
643 // get rid of old allocated cvar
644 // (but not cvar->string and cvar->defstring, because we kept those)
645 Z_Free((char *)cvar->name);
649 Con_DPrintf("Can't register variable %s, already defined\n", variable->name);
653 // check for overlap with a command
654 if (Cmd_Exists(cmd_local, variable->name))
656 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
660 // copy the value off, because future sets will Z_Free it
661 variable->name = (char *)Mem_strdup(zonemempool, variable->name);
662 variable->string = (char *)Mem_strdup(zonemempool, variable->string);
663 variable->defstring = (char *)Mem_strdup(zonemempool, variable->string);
664 variable->value = atof (variable->string);
665 variable->integer = (int) variable->value;
666 variable->aliases = NULL;
667 variable->aliases_size = 0;
668 variable->initstate = NULL;
670 // Mark it as not an autocvar.
671 for (i = 0;i < PRVM_PROG_MAX;i++)
672 variable->globaldefindex[i] = -1;
674 // Safe for Cvar_SetQuick()
675 variable->flags |= CF_REGISTERED;
677 Cvar_Link(variable, cvars);
684 Adds a newly allocated variable to the variable list or sets its value.
687 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int flags, const char *newdescription)
692 if (developer_extra.integer)
693 Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
695 // first check to see if it has already been defined
696 cvar = Cvar_FindVar(cvars, name, ~0);
699 cvar->flags |= flags;
700 Cvar_SetQuick_Internal (cvar, value);
701 if(newdescription && (cvar->flags & CF_ALLOCATED))
703 if(cvar->description != cvar_dummy_description)
704 Z_Free((char *)cvar->description);
707 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
709 cvar->description = cvar_dummy_description;
714 // check for pure evil
717 Con_Printf("Cvar_Get: invalid variable name\n");
721 // check for overlap with a command
722 if (Cmd_Exists(cmd_local, name))
724 Con_Printf("Cvar_Get: %s is a command\n", name);
728 // allocate a new cvar, cvar name, and cvar string
729 // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
730 // FIXME: these never get Z_Free'd
731 cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
732 cvar->flags = flags | CF_ALLOCATED;
733 cvar->name = (char *)Mem_strdup(zonemempool, name);
734 cvar->string = (char *)Mem_strdup(zonemempool, value);
735 cvar->defstring = (char *)Mem_strdup(zonemempool, value);
736 cvar->value = atof (cvar->string);
737 cvar->integer = (int) cvar->value;
738 cvar->aliases = NULL;
739 cvar->aliases_size = 0;
740 cvar->initstate = NULL;
742 if(newdescription && *newdescription)
743 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
745 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
747 // Mark it as not an autocvar.
748 for (i = 0;i < PRVM_PROG_MAX;i++)
749 cvar->globaldefindex[i] = -1;
751 Cvar_Link(cvar, cvars);
756 qbool Cvar_Readonly (cvar_t *var, const char *cmd_name)
758 if (var->flags & CF_READONLY)
761 Con_Printf("%s: ",cmd_name);
762 Con_Printf("%s", var->name);
763 Con_Printf(" is read-only\n");
773 Handles variable inspection and changing from the console
776 qbool Cvar_Command (cmd_state_t *cmd)
778 cvar_state_t *cvars = cmd->cvars;
782 v = Cvar_FindVar(cvars, Cmd_Argv(cmd, 0), (cmd->cvars_flagsmask));
786 // perform a variable print or set
787 if (Cmd_Argc(cmd) == 1)
789 Cvar_PrintHelp(v, Cmd_Argv(cmd, 0), true);
793 if (developer_extra.integer)
794 Con_DPrint("Cvar_Command: ");
796 if(Cvar_Readonly(v, NULL))
799 Cvar_SetQuick(v, Cmd_Argv(cmd, 1));
800 if (developer_extra.integer)
805 void Cvar_UnlockDefaults(cmd_state_t *cmd)
807 cvar_state_t *cvars = cmd->cvars;
809 // unlock the default values of all cvars
810 for (var = cvars->vars ; var ; var = var->next)
811 var->flags &= ~CF_DEFAULTSET;
814 void Cvar_LockDefaults_f(cmd_state_t *cmd)
816 cvar_state_t *cvars = cmd->cvars;
818 // lock in the default values of all cvars
819 for (var = cvars->vars ; var ; var = var->next)
821 if (!(var->flags & CF_DEFAULTSET))
825 //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
826 var->flags |= CF_DEFAULTSET;
827 Z_Free((char *)var->defstring);
828 alloclen = strlen(var->string) + 1;
829 var->defstring = (char *)Z_Malloc(alloclen);
830 memcpy((char *)var->defstring, var->string, alloclen);
835 void Cvar_SaveInitState(cvar_state_t *cvars)
838 for (c = cvars->vars;c;c = c->next)
840 c->initstate = (cvar_t *)Z_Malloc(sizeof(cvar_t));
841 memcpy(c->initstate, c, sizeof(cvar_t));
845 void Cvar_RestoreInitState(cvar_state_t *cvars)
850 for (cp = &cvars->vars;(c = *cp);)
854 // restore this cvar, it existed at init
855 if (((c->flags ^ c->initstate->flags) & CF_MAXFLAGSVAL)
856 || strcmp(c->defstring ? c->defstring : "", c->initstate->defstring ? c->initstate->defstring : "")
857 || strcmp(c->string ? c->string : "", c->initstate->string ? c->initstate->string : ""))
859 Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
861 Z_Free((char *)c->defstring);
862 c->defstring = Mem_strdup(zonemempool, c->initstate->defstring);
864 Z_Free((char *)c->string);
865 c->string = Mem_strdup(zonemempool, c->initstate->string);
867 c->flags = c->initstate->flags;
868 c->value = c->initstate->value;
869 c->integer = c->initstate->integer;
870 VectorCopy(c->initstate->vector, c->vector);
875 if (!(c->flags & CF_ALLOCATED))
877 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it was registered after init!\n", c->name);
878 // In this case, at least reset it to the default.
879 if((c->flags & CF_PERSISTENT) == 0)
880 Cvar_SetQuick(c, c->defstring);
884 if (Cvar_IsAutoCvar(c))
886 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it is an autocvar used by running progs!\n", c->name);
887 // In this case, at least reset it to the default.
888 if((c->flags & CF_PERSISTENT) == 0)
889 Cvar_SetQuick(c, c->defstring);
893 // remove this cvar, it did not exist at init
894 Con_DPrintf("Cvar_RestoreInitState: Destroying cvar \"%s\"\n", c->name);
895 // unlink struct from hash
896 hashindex = CRC_Block((const unsigned char *)c->name, strlen(c->name)) % CVAR_HASHSIZE;
897 for (cp2 = &cvars->hashtable[hashindex]->cvar;(c2 = *cp2);)
901 *cp2 = cvars->hashtable[hashindex]->next->cvar;
905 cp2 = &cvars->hashtable[hashindex]->next->cvar;
907 // unlink struct from main list
911 Z_Free((char *)c->defstring);
913 Z_Free((char *)c->string);
914 if (c->description && c->description != cvar_dummy_description)
915 Z_Free((char *)c->description);
922 void Cvar_ResetToDefaults_All_f(cmd_state_t *cmd)
924 cvar_state_t *cvars = cmd->cvars;
926 // restore the default values of all cvars
927 for (var = cvars->vars ; var ; var = var->next)
929 if((var->flags & CF_PERSISTENT) == 0)
930 Cvar_SetQuick(var, var->defstring);
934 void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd)
936 cvar_state_t *cvars = cmd->cvars;
938 // restore the default values of all cvars
939 for (var = cvars->vars ; var ; var = var->next)
941 if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == 0)
942 Cvar_SetQuick(var, var->defstring);
947 void Cvar_ResetToDefaults_SaveOnly_f(cmd_state_t *cmd)
949 cvar_state_t *cvars = cmd->cvars;
951 // restore the default values of all cvars
952 for (var = cvars->vars ; var ; var = var->next)
954 if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == CF_ARCHIVE)
955 Cvar_SetQuick(var, var->defstring);
963 Writes lines containing "set variable value" for all variables
964 with the archive flag set to true.
967 void Cvar_WriteVariables (cvar_state_t *cvars, qfile_t *f)
970 char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
972 // don't save cvars that match their default value
973 for (var = cvars->vars ; var ; var = var->next) {
974 if ((var->flags & CF_ARCHIVE) && (strcmp(var->string, var->defstring) || ((var->flags & CF_ALLOCATED) && !(var->flags & CF_DEFAULTSET))))
976 Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$", false);
977 Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$", false);
978 FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CF_ALLOCATED ? "seta " : "", buf1, buf2);
983 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
984 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
990 void Cvar_List_f(cmd_state_t *cmd)
992 cvar_state_t *cvars = cmd->cvars;
999 if (Cmd_Argc(cmd) > 1)
1001 partial = Cmd_Argv(cmd, 1);
1002 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1004 partial = va(vabuf, sizeof(vabuf), "%s*", partial);
1008 partial = va(vabuf, sizeof(vabuf), "*");
1013 for (cvar = cvars->vars; cvar; cvar = cvar->next)
1015 if (matchpattern_with_separator(cvar->name, partial, false, "", false))
1017 Cvar_PrintHelp(cvar, cvar->name, true);
1020 for (char **alias = cvar->aliases; alias && *alias; alias++)
1022 if (matchpattern_with_separator(*alias, partial, false, "", false))
1024 Cvar_PrintHelp(cvar, *alias, true);
1030 if (Cmd_Argc(cmd) > 1)
1033 Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
1035 Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", Cmd_Argv(cmd,1));
1038 Con_Printf("%i cvar(s)\n", count);
1040 // 2000-01-09 CvarList command by Maddes
1042 void Cvar_Set_f(cmd_state_t *cmd)
1044 cvar_state_t *cvars = cmd->cvars;
1047 // make sure it's the right number of parameters
1048 if (Cmd_Argc(cmd) < 3)
1050 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
1054 // check if it's read-only
1055 cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1057 if(Cvar_Readonly(cvar,"Set"))
1060 if (developer_extra.integer)
1061 Con_DPrint("Set: ");
1063 // all looks ok, create/modify the cvar
1064 Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1067 void Cvar_SetA_f(cmd_state_t *cmd)
1069 cvar_state_t *cvars = cmd->cvars;
1072 // make sure it's the right number of parameters
1073 if (Cmd_Argc(cmd) < 3)
1075 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
1079 // check if it's read-only
1080 cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1082 if(Cvar_Readonly(cvar,"SetA"))
1085 if (developer_extra.integer)
1086 Con_DPrint("SetA: ");
1088 // all looks ok, create/modify the cvar
1089 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);
1092 void Cvar_Del_f(cmd_state_t *cmd)
1094 cvar_state_t *cvars = cmd->cvars;
1095 int neededflags = ~0;
1097 cvar_t *parent, **link;
1098 cvar_t *cvar, *prev;
1100 if(Cmd_Argc(cmd) < 2)
1102 Con_Printf("%s: wrong number of parameters, usage: unset <variablename1> [<variablename2> ...]\n", Cmd_Argv(cmd, 0));
1105 for(i = 1; i < Cmd_Argc(cmd); ++i)
1107 cvar = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
1111 Con_Printf("%s: %s is not defined\n", Cmd_Argv(cmd, 0), Cmd_Argv(cmd, i));
1114 if(Cvar_Readonly(cvar, Cmd_Argv(cmd, 0)))
1116 if(!(cvar->flags & CF_ALLOCATED))
1118 Con_Printf("%s: %s is static and cannot be deleted\n", Cmd_Argv(cmd, 0), cvar->name);
1121 if(cvar == cvars->vars)
1123 cvars->vars = cvar->next;
1127 // in this case, prev must be set, otherwise there has been some inconsistensy
1128 // elsewhere already... should I still check for prev != NULL?
1129 prev->next = cvar->next;
1133 parent->next = cvar->next;
1136 if(cvar->description != cvar_dummy_description)
1137 Z_Free((char *)cvar->description);
1139 Z_Free((char *)cvar->name);
1140 Z_Free((char *)cvar->string);
1141 Z_Free((char *)cvar->defstring);
1146 #ifdef FILLALLCVARSWITHRUBBISH
1147 void Cvar_FillAll_f(cmd_state_t *cmd)
1153 if(Cmd_Argc(cmd) != 2)
1155 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(cmd, 0));
1156 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(cmd, 0));
1159 n = atoi(Cmd_Argv(cmd, 1));
1163 buf = Z_Malloc(n + 1);
1165 for(var = cvars->vars; var; var = var->next)
1167 for(i = 0, p = buf, q = var->name; i < n; ++i)
1173 if(verify && strcmp(var->string, buf))
1175 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);
1177 Cvar_SetQuick(var, buf);
1181 #endif /* FILLALLCVARSWITHRUBBISH */