2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // cvar.c -- dynamic variable tracking
24 const char *cvar_dummy_description = "custom cvar";
25 static const char *cvar_null_string = "";
27 cvar_state_t cvars_all;
28 cvar_state_t cvars_null;
35 cvar_t *Cvar_FindVar(cvar_state_t *cvars, const char *var_name, int neededflags)
40 // use hash lookup to minimize search time
41 hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)) % CVAR_HASHSIZE;
42 for (hash = cvars->hashtable[hashindex];hash;hash = hash->next)
43 if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
46 for (int i = 0; i < hash->cvar->aliasindex; i++)
47 if (!strcmp (var_name, hash->cvar->aliases[i]) && (hash->cvar->flags & neededflags))
52 cvar_t *Cvar_FindVarAfter(cvar_state_t *cvars, const char *prev_var_name, int neededflags)
58 var = Cvar_FindVar(cvars, prev_var_name, neededflags);
66 // search for the next cvar matching the needed flags
69 if (var->flags & neededflags)
76 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)
81 // use hash lookup to minimize search time
82 hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)) % CVAR_HASHSIZE;
83 if(parent) *parent = NULL;
84 if(prev_alpha) *prev_alpha = NULL;
85 if(link) *link = &cvars->hashtable[hashindex]->cvar;
86 for (hash = cvars->hashtable[hashindex];hash;hash = hash->next)
88 if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
91 for (int i = 0; i < hash->cvar->aliasindex; i++)
92 if (!strcmp (var_name, hash->cvar->aliases[i]) && (hash->cvar->flags & neededflags))
94 if(parent) *parent = hash->cvar;
98 if(!prev_alpha || hash->cvar == cvars->vars)
101 *prev_alpha = cvars->vars;
102 // if prev_alpha happens to become NULL then there has been some inconsistency elsewhere
103 // already - should I still insert '*prev_alpha &&' in the loop?
104 while((*prev_alpha)->next != hash->cvar)
105 *prev_alpha = (*prev_alpha)->next;
114 float Cvar_VariableValueOr(cvar_state_t *cvars, const char *var_name, float def, int neededflags)
118 var = Cvar_FindVar(cvars, var_name, neededflags);
121 return atof (var->string);
124 float Cvar_VariableValue(cvar_state_t *cvars, const char *var_name, int neededflags)
126 return Cvar_VariableValueOr(cvars, var_name, 0, neededflags);
134 const char *Cvar_VariableStringOr(cvar_state_t *cvars, const char *var_name, const char *def, int neededflags)
138 var = Cvar_FindVar(cvars, var_name, neededflags);
144 const char *Cvar_VariableString(cvar_state_t *cvars, const char *var_name, int neededflags)
146 return Cvar_VariableStringOr(cvars, var_name, cvar_null_string, neededflags);
151 Cvar_VariableDefString
154 const char *Cvar_VariableDefString(cvar_state_t *cvars, const char *var_name, int neededflags)
158 var = Cvar_FindVar(cvars, var_name, neededflags);
160 return cvar_null_string;
161 return var->defstring;
166 Cvar_VariableDescription
169 const char *Cvar_VariableDescription(cvar_state_t *cvars, const char *var_name, int neededflags)
173 var = Cvar_FindVar(cvars, var_name, neededflags);
175 return cvar_null_string;
176 return var->description;
182 Cvar_CompleteVariable
185 const char *Cvar_CompleteVariable(cvar_state_t *cvars, const char *partial, int neededflags)
190 len = strlen(partial);
196 for (cvar=cvars->vars ; cvar ; cvar=cvar->next)
197 if (!strncasecmp (partial,cvar->name, len) && (cvar->flags & neededflags))
205 CVar_CompleteCountPossible
207 New function for tab-completion system
209 Thanks to Fett erich@heintz.com
212 int Cvar_CompleteCountPossible(cvar_state_t *cvars, const char *partial, int neededflags)
219 len = strlen(partial);
224 // Loop through the cvars and count all possible matches
225 for (cvar = cvars->vars; cvar; cvar = cvar->next)
226 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
229 for(int i = 0; i < cvar->aliasindex; i++)
230 if (!strncasecmp(partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
237 CVar_CompleteBuildList
239 New function for tab-completion system
241 Thanks to Fett erich@heintz.com
245 const char **Cvar_CompleteBuildList(cvar_state_t *cvars, const char *partial, int neededflags)
250 size_t sizeofbuf = (Cvar_CompleteCountPossible(cvars, partial, neededflags) + 1) * sizeof(const char *);
253 len = strlen(partial);
254 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof(const char *));
255 // Loop through the alias list and print all matches
256 for (cvar = cvars->vars; cvar; cvar = cvar->next)
257 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
258 buf[bpos++] = cvar->name;
260 for(int i = 0; i < cvar->aliasindex; i++)
261 if (!strncasecmp(partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
262 buf[bpos++] = cvar->aliases[i];
269 void Cvar_PrintHelp(cvar_t *cvar, const char *name, qbool full)
271 // Aliases are purple, cvars are yellow
272 if (strcmp(cvar->name, name))
276 Con_Printf("%s^7 is \"%s\" [\"%s\"]", name, ((cvar->flags & CF_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring);
277 if (strcmp(cvar->name, name))
278 Con_Printf(" (also ^3%s^7)", cvar->name);
280 Con_Printf(" %s", cvar->description);
284 // written by LadyHavoc
285 void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, int neededflags)
288 size_t len = strlen(partial);
289 // Loop through the command list and print all matches
290 for (cvar = cvars->vars; cvar; cvar = cvar->next)
291 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
292 Cvar_PrintHelp(cvar, cvar->name, true);
294 for (int i = 0; i < cvar->aliasindex; i++)
295 if (!strncasecmp (partial, cvar->aliases[i], len) && (cvar->flags & neededflags))
296 Cvar_PrintHelp(cvar, cvar->aliases[i], true);
301 // check if a cvar is held by some progs
302 static qbool Cvar_IsAutoCvar(cvar_t *var)
306 for (i = 0;i < PRVM_PROG_MAX;i++)
308 prog = &prvm_prog_list[i];
309 if (prog->loaded && var->globaldefindex[i] >= 0)
315 // we assume that prog is already set to the target progs
316 static void Cvar_UpdateAutoCvar(cvar_t *var)
323 for (i = 0;i < PRVM_PROG_MAX;i++)
325 prog = &prvm_prog_list[i];
326 if (prog->loaded && var->globaldefindex[i] >= 0)
328 // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs
329 switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL)
332 PRVM_GLOBALFIELDFLOAT(prog->globaldefs[var->globaldefindex[i]].ofs) = var->value;
337 for (j = 0;j < 3;j++)
339 while (*s && ISWHITESPACE(*s))
344 while (!ISWHITESPACE(*s))
349 VectorCopy(v, PRVM_GLOBALFIELDVECTOR(prog->globaldefs[var->globaldefindex[i]].ofs));
352 PRVM_ChangeEngineString(prog, var->globaldefindex_stringno[i], var->string);
353 PRVM_GLOBALFIELDSTRING(prog->globaldefs[var->globaldefindex[i]].ofs) = var->globaldefindex_stringno[i];
360 // called after loading a savegame
361 void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars)
364 for (var = cvars->vars ; var ; var = var->next)
365 Cvar_UpdateAutoCvar(var);
373 extern cvar_t sv_disablenotify;
374 static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
379 changed = strcmp(var->string, value) != 0;
380 // LadyHavoc: don't reallocate when there is no change
384 // LadyHavoc: don't reallocate when the buffer is the same size
385 valuelen = strlen(value);
386 if (!var->string || strlen(var->string) != valuelen)
388 Z_Free ((char *)var->string); // free the old value string
390 var->string = (char *)Z_Malloc (valuelen + 1);
392 memcpy ((char *)var->string, value, valuelen + 1);
393 var->value = atof (var->string);
394 var->integer = (int) var->value;
395 if ((var->flags & CF_NOTIFY) && sv.active && !sv_disablenotify.integer)
396 SV_BroadcastPrintf("\003^3Server cvar \"%s\" changed to \"%s\"\n", var->name, var->string);
398 // TODO: add infostring support to the server?
399 if ((var->flags & CF_SERVERINFO) && changed && sv.active)
401 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
404 MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
405 MSG_WriteString (&sv.reliable_datagram, var->name);
406 MSG_WriteString (&sv.reliable_datagram, var->string);
410 if (var->flags & CF_USERINFO)
411 CL_SetInfo(var->name, var->string, true, false, false, false);
413 Cvar_UpdateAutoCvar(var);
415 // Call the function stored in the cvar for bounds checking, cleanup, etc
420 void Cvar_SetQuick (cvar_t *var, const char *value)
424 Con_Print("Cvar_SetQuick: var == NULL\n");
428 if (developer_extra.integer)
429 Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
431 Cvar_SetQuick_Internal(var, value);
434 void Cvar_Set(cvar_state_t *cvars, const char *var_name, const char *value)
437 var = Cvar_FindVar(cvars, var_name, ~0);
440 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
443 Cvar_SetQuick(var, value);
446 void Cvar_Set_NoCallback(cvar_t *var, const char *value)
448 void (*callback_save)(cvar_t *) = var->callback;
449 var->callback = NULL;
450 Cvar_SetQuick_Internal(var, value);
451 var->callback = callback_save;
459 void Cvar_SetValueQuick(cvar_t *var, float value)
461 char val[MAX_INPUTLINE];
463 if ((float)((int)value) == value)
464 dpsnprintf(val, sizeof(val), "%i", (int)value);
466 dpsnprintf(val, sizeof(val), "%f", value);
467 Cvar_SetQuick(var, val);
470 void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
472 char val[MAX_INPUTLINE];
474 if ((float)((int)value) == value)
475 dpsnprintf(val, sizeof(val), "%i", (int)value);
477 dpsnprintf(val, sizeof(val), "%f", value);
478 Cvar_Set(cvars, var_name, val);
481 void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *))
483 variable->callback = callback;
486 void Cvar_RegisterAlias(cvar_t *variable, const char *alias )
488 cvar_state_t *cvars = &cvars_all;
492 variable->aliases = (char **)Mem_Realloc(zonemempool, variable->aliases, sizeof(char *) * (variable->aliasindex + 1));
494 variable->aliases[variable->aliasindex] = (char *)Z_Malloc(strlen(alias) + 1);
495 memcpy(variable->aliases[variable->aliasindex], alias, strlen(alias) + 1);
496 variable->aliasindex++;
498 // link to head of list in this hash table index
499 hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
500 hashindex = CRC_Block((const unsigned char *)alias, strlen(alias)) % CVAR_HASHSIZE;
501 hash->next = cvars->hashtable[hashindex];
502 cvars->hashtable[hashindex] = hash;
503 hash->cvar = variable;
510 Links a variable to the variable list and hashtable
513 static void Cvar_Link(cvar_t *variable, cvar_state_t *cvars)
515 cvar_t *current, *next;
519 * Link the variable in
520 * alphanumerical order
522 for( current = NULL, next = cvars->vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
525 current->next = variable;
527 cvars->vars = variable;
528 variable->next = next;
530 // link to head of list in this hash table index
531 hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
532 hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
533 hash->next = cvars->hashtable[hashindex];
534 hash->cvar = variable;
535 cvars->hashtable[hashindex] = hash;
540 Cvar_RegisterVariable
542 Adds a freestanding variable to the variable list.
545 void Cvar_RegisterVariable (cvar_t *variable)
547 cvar_state_t *cvars = NULL;
548 cvar_t *current, *cvar;
553 switch (variable->flags & (CF_CLIENT | CF_SERVER))
557 case CF_CLIENT | CF_SERVER:
561 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with no CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
564 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with weird CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
568 if (developer_extra.integer)
569 Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
571 // first check to see if it has already been defined
572 cvar = Cvar_FindVar(cvars, variable->name, ~0);
575 if (cvar->flags & CF_ALLOCATED)
577 if (developer_extra.integer)
578 Con_DPrintf("... replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
579 // fixed variables replace allocated ones
580 // (because the engine directly accesses fixed variables)
581 // NOTE: this isn't actually used currently
582 // (all cvars are registered before config parsing)
583 variable->flags |= (cvar->flags & ~CF_ALLOCATED);
584 // cvar->string is now owned by variable instead
585 variable->string = cvar->string;
586 variable->defstring = cvar->defstring;
587 variable->value = atof (variable->string);
588 variable->integer = (int) variable->value;
589 // Preserve autocvar status.
590 memcpy(variable->globaldefindex, cvar->globaldefindex, sizeof(variable->globaldefindex));
591 memcpy(variable->globaldefindex_stringno, cvar->globaldefindex_stringno, sizeof(variable->globaldefindex_stringno));
592 // replace cvar with this one...
593 variable->next = cvar->next;
594 if (cvars->vars == cvar)
596 // head of the list is easy to change
597 cvars->vars = variable;
601 // otherwise find it somewhere in the list
602 for (current = cvars->vars;current->next != cvar;current = current->next)
604 current->next = variable;
607 // get rid of old allocated cvar
608 // (but not cvar->string and cvar->defstring, because we kept those)
609 Z_Free((char *)cvar->name);
613 Con_DPrintf("Can't register variable %s, already defined\n", variable->name);
617 // check for overlap with a command
618 if (Cmd_Exists(&cmd_client, variable->name) || Cmd_Exists(&cmd_server, variable->name))
620 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
624 // copy the value off, because future sets will Z_Free it
625 oldstr = (char *)variable->string;
626 alloclen = strlen(variable->string) + 1;
627 variable->string = (char *)Z_Malloc (alloclen);
628 memcpy ((char *)variable->string, oldstr, alloclen);
629 variable->defstring = (char *)Z_Malloc (alloclen);
630 memcpy ((char *)variable->defstring, oldstr, alloclen);
631 variable->value = atof (variable->string);
632 variable->integer = (int) variable->value;
633 variable->aliasindex = 0;
635 // Mark it as not an autocvar.
636 for (i = 0;i < PRVM_PROG_MAX;i++)
637 variable->globaldefindex[i] = -1;
639 Cvar_Link(variable, cvars);
646 Adds a newly allocated variable to the variable list or sets its value.
649 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int flags, const char *newdescription)
654 if (developer_extra.integer)
655 Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
657 // first check to see if it has already been defined
658 cvar = Cvar_FindVar(cvars, name, ~0);
661 cvar->flags |= flags;
662 Cvar_SetQuick_Internal (cvar, value);
663 if(newdescription && (cvar->flags & CF_ALLOCATED))
665 if(cvar->description != cvar_dummy_description)
666 Z_Free((char *)cvar->description);
669 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
671 cvar->description = cvar_dummy_description;
676 // check for pure evil
679 Con_Printf("Cvar_Get: invalid variable name\n");
683 // check for overlap with a command
684 if (Cmd_Exists(&cmd_client, name) || Cmd_Exists(&cmd_server, name))
686 Con_Printf("Cvar_Get: %s is a command\n", name);
690 // allocate a new cvar, cvar name, and cvar string
691 // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
692 // FIXME: these never get Z_Free'd
693 cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
694 cvar->flags = flags | CF_ALLOCATED;
695 cvar->name = (char *)Mem_strdup(zonemempool, name);
696 cvar->string = (char *)Mem_strdup(zonemempool, value);
697 cvar->defstring = (char *)Mem_strdup(zonemempool, value);
698 cvar->value = atof (cvar->string);
699 cvar->integer = (int) cvar->value;
700 cvar->aliases = (char **)Z_Malloc(sizeof(char **));
701 memset(cvar->aliases, 0, sizeof(char *));
703 if(newdescription && *newdescription)
704 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
706 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
708 // Mark it as not an autocvar.
709 for (i = 0;i < PRVM_PROG_MAX;i++)
710 cvar->globaldefindex[i] = -1;
712 Cvar_Link(cvar, cvars);
717 qbool Cvar_Readonly (cvar_t *var, const char *cmd_name)
719 if (var->flags & CF_READONLY)
722 Con_Printf("%s: ",cmd_name);
723 Con_Printf("%s", var->name);
724 Con_Printf(" is read-only\n");
734 Handles variable inspection and changing from the console
737 qbool Cvar_Command (cmd_state_t *cmd)
739 cvar_state_t *cvars = cmd->cvars;
743 v = Cvar_FindVar(cvars, Cmd_Argv(cmd, 0), (cmd->cvars_flagsmask));
747 // perform a variable print or set
748 if (Cmd_Argc(cmd) == 1)
750 Cvar_PrintHelp(v, Cmd_Argv(cmd, 0), true);
754 if (developer_extra.integer)
755 Con_DPrint("Cvar_Command: ");
757 if(Cvar_Readonly(v, NULL))
760 Cvar_SetQuick(v, Cmd_Argv(cmd, 1));
761 if (developer_extra.integer)
766 void Cvar_UnlockDefaults(cmd_state_t *cmd)
768 cvar_state_t *cvars = cmd->cvars;
770 // unlock the default values of all cvars
771 for (var = cvars->vars ; var ; var = var->next)
772 var->flags &= ~CF_DEFAULTSET;
775 void Cvar_LockDefaults_f(cmd_state_t *cmd)
777 cvar_state_t *cvars = cmd->cvars;
779 // lock in the default values of all cvars
780 for (var = cvars->vars ; var ; var = var->next)
782 if (!(var->flags & CF_DEFAULTSET))
786 //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
787 var->flags |= CF_DEFAULTSET;
788 Z_Free((char *)var->defstring);
789 alloclen = strlen(var->string) + 1;
790 var->defstring = (char *)Z_Malloc(alloclen);
791 memcpy((char *)var->defstring, var->string, alloclen);
796 void Cvar_SaveInitState(cvar_state_t *cvars)
799 for (c = cvars->vars;c;c = c->next)
802 c->initflags = c->flags;
803 c->initdefstring = Mem_strdup(zonemempool, c->defstring);
804 c->initstring = Mem_strdup(zonemempool, c->string);
805 c->initvalue = c->value;
806 c->initinteger = c->integer;
807 VectorCopy(c->vector, c->initvector);
811 void Cvar_RestoreInitState(cvar_state_t *cvars)
816 for (cp = &cvars->vars;(c = *cp);)
820 // restore this cvar, it existed at init
821 if (((c->flags ^ c->initflags) & CF_MAXFLAGSVAL)
822 || strcmp(c->defstring ? c->defstring : "", c->initdefstring ? c->initdefstring : "")
823 || strcmp(c->string ? c->string : "", c->initstring ? c->initstring : ""))
825 Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
827 Z_Free((char *)c->defstring);
828 c->defstring = Mem_strdup(zonemempool, c->initdefstring);
830 Z_Free((char *)c->string);
831 c->string = Mem_strdup(zonemempool, c->initstring);
833 c->flags = c->initflags;
834 c->value = c->initvalue;
835 c->integer = c->initinteger;
836 VectorCopy(c->initvector, c->vector);
841 if (!(c->flags & CF_ALLOCATED))
843 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it was registered after init!\n", c->name);
844 // In this case, at least reset it to the default.
845 if((c->flags & CF_PERSISTENT) == 0)
846 Cvar_SetQuick(c, c->defstring);
850 if (Cvar_IsAutoCvar(c))
852 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it is an autocvar used by running progs!\n", c->name);
853 // In this case, at least reset it to the default.
854 if((c->flags & CF_PERSISTENT) == 0)
855 Cvar_SetQuick(c, c->defstring);
859 // remove this cvar, it did not exist at init
860 Con_DPrintf("Cvar_RestoreInitState: Destroying cvar \"%s\"\n", c->name);
861 // unlink struct from hash
862 hashindex = CRC_Block((const unsigned char *)c->name, strlen(c->name)) % CVAR_HASHSIZE;
863 for (cp2 = &cvars->hashtable[hashindex]->cvar;(c2 = *cp2);)
867 *cp2 = cvars->hashtable[hashindex]->next->cvar;
871 cp2 = &cvars->hashtable[hashindex]->next->cvar;
873 // unlink struct from main list
877 Z_Free((char *)c->defstring);
879 Z_Free((char *)c->string);
880 if (c->description && c->description != cvar_dummy_description)
881 Z_Free((char *)c->description);
888 void Cvar_ResetToDefaults_All_f(cmd_state_t *cmd)
890 cvar_state_t *cvars = cmd->cvars;
892 // restore the default values of all cvars
893 for (var = cvars->vars ; var ; var = var->next)
895 if((var->flags & CF_PERSISTENT) == 0)
896 Cvar_SetQuick(var, var->defstring);
900 void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd)
902 cvar_state_t *cvars = cmd->cvars;
904 // restore the default values of all cvars
905 for (var = cvars->vars ; var ; var = var->next)
907 if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == 0)
908 Cvar_SetQuick(var, var->defstring);
913 void Cvar_ResetToDefaults_SaveOnly_f(cmd_state_t *cmd)
915 cvar_state_t *cvars = cmd->cvars;
917 // restore the default values of all cvars
918 for (var = cvars->vars ; var ; var = var->next)
920 if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == CF_ARCHIVE)
921 Cvar_SetQuick(var, var->defstring);
929 Writes lines containing "set variable value" for all variables
930 with the archive flag set to true.
933 void Cvar_WriteVariables (cvar_state_t *cvars, qfile_t *f)
936 char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
938 // don't save cvars that match their default value
939 for (var = cvars->vars ; var ; var = var->next) {
940 if ((var->flags & CF_ARCHIVE) && (strcmp(var->string, var->defstring) || ((var->flags & CF_ALLOCATED) && !(var->flags & CF_DEFAULTSET))))
942 Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$", false);
943 Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$", false);
944 FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CF_ALLOCATED ? "seta " : "", buf1, buf2);
949 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
950 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
956 void Cvar_List_f(cmd_state_t *cmd)
958 cvar_state_t *cvars = cmd->cvars;
965 if (Cmd_Argc(cmd) > 1)
967 partial = Cmd_Argv(cmd, 1);
968 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
970 partial = va(vabuf, sizeof(vabuf), "%s*", partial);
974 partial = va(vabuf, sizeof(vabuf), "*");
979 for (cvar = cvars->vars; cvar; cvar = cvar->next)
981 if (matchpattern_with_separator(cvar->name, partial, false, "", false))
983 Cvar_PrintHelp(cvar, cvar->name, true);
986 for (int i = 0; i < cvar->aliasindex; i++)
988 if (matchpattern_with_separator(cvar->aliases[i], partial, false, "", false))
990 Cvar_PrintHelp(cvar, cvar->aliases[i], true);
996 if (Cmd_Argc(cmd) > 1)
999 Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
1001 Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", Cmd_Argv(cmd,1));
1004 Con_Printf("%i cvar(s)\n", count);
1006 // 2000-01-09 CvarList command by Maddes
1008 void Cvar_Set_f(cmd_state_t *cmd)
1010 cvar_state_t *cvars = cmd->cvars;
1013 // make sure it's the right number of parameters
1014 if (Cmd_Argc(cmd) < 3)
1016 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
1020 // check if it's read-only
1021 cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1023 if(Cvar_Readonly(cvar,"Set"))
1026 if (developer_extra.integer)
1027 Con_DPrint("Set: ");
1029 // all looks ok, create/modify the cvar
1030 Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1033 void Cvar_SetA_f(cmd_state_t *cmd)
1035 cvar_state_t *cvars = cmd->cvars;
1038 // make sure it's the right number of parameters
1039 if (Cmd_Argc(cmd) < 3)
1041 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
1045 // check if it's read-only
1046 cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1048 if(Cvar_Readonly(cvar,"SetA"))
1051 if (developer_extra.integer)
1052 Con_DPrint("SetA: ");
1054 // all looks ok, create/modify the cvar
1055 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);
1058 void Cvar_Del_f(cmd_state_t *cmd)
1060 cvar_state_t *cvars = cmd->cvars;
1061 int neededflags = ~0;
1063 cvar_t *parent, **link;
1064 cvar_t *cvar, *prev;
1066 if(Cmd_Argc(cmd) < 2)
1068 Con_Printf("%s: wrong number of parameters, usage: unset <variablename1> [<variablename2> ...]\n", Cmd_Argv(cmd, 0));
1071 for(i = 1; i < Cmd_Argc(cmd); ++i)
1073 cvar = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
1077 Con_Printf("%s: %s is not defined\n", Cmd_Argv(cmd, 0), Cmd_Argv(cmd, i));
1080 if(Cvar_Readonly(cvar, Cmd_Argv(cmd, 0)))
1082 if(!(cvar->flags & CF_ALLOCATED))
1084 Con_Printf("%s: %s is static and cannot be deleted\n", Cmd_Argv(cmd, 0), cvar->name);
1087 if(cvar == cvars->vars)
1089 cvars->vars = cvar->next;
1093 // in this case, prev must be set, otherwise there has been some inconsistensy
1094 // elsewhere already... should I still check for prev != NULL?
1095 prev->next = cvar->next;
1099 parent->next = cvar->next;
1102 if(cvar->description != cvar_dummy_description)
1103 Z_Free((char *)cvar->description);
1105 Z_Free((char *)cvar->name);
1106 Z_Free((char *)cvar->string);
1107 Z_Free((char *)cvar->defstring);
1112 #ifdef FILLALLCVARSWITHRUBBISH
1113 void Cvar_FillAll_f(cmd_state_t *cmd)
1119 if(Cmd_Argc(cmd) != 2)
1121 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(cmd, 0));
1122 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(cmd, 0));
1125 n = atoi(Cmd_Argv(cmd, 1));
1129 buf = Z_Malloc(n + 1);
1131 for(var = cvars->vars; var; var = var->next)
1133 for(i = 0, p = buf, q = var->name; i < n; ++i)
1139 if(verify && strcmp(var->string, buf))
1141 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);
1143 Cvar_SetQuick(var, buf);
1147 #endif /* FILLALLCVARSWITHRUBBISH */