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, qboolean alias)
40 // use hash lookup to minimize search time
41 hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)) % CVAR_HASHSIZE;
42 for (var = cvars->hashtable[hashindex];var;var = var->nextonhashchain)
43 if (!strcmp (var_name, var->name))
45 if(!alias && var->alias)
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, false);
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));
83 if(parent) *parent = NULL;
84 if(prev_alpha) *prev_alpha = NULL;
85 if(link) *link = &cvars->hashtable[hashindex];
86 for (var = cvars->hashtable[hashindex];var;var = var->nextonhashchain)
88 if (!strcmp (var_name, var->name) && (var->flags & neededflags))
90 if(!prev_alpha || var == cvars->vars)
93 *prev_alpha = cvars->vars;
94 // if prev_alpha happens to become NULL then there has been some inconsistency elsewhere
95 // already - should I still insert '*prev_alpha &&' in the loop?
96 while((*prev_alpha)->next != var)
97 *prev_alpha = (*prev_alpha)->next;
100 if(parent) *parent = var;
111 float Cvar_VariableValueOr(cvar_state_t *cvars, const char *var_name, float def, int neededflags)
115 var = Cvar_FindVar(cvars, var_name, neededflags, false);
118 return atof (var->string);
121 float Cvar_VariableValue(cvar_state_t *cvars, const char *var_name, int neededflags)
123 return Cvar_VariableValueOr(cvars, var_name, 0, neededflags);
131 const char *Cvar_VariableStringOr(cvar_state_t *cvars, const char *var_name, const char *def, int neededflags)
135 var = Cvar_FindVar(cvars, var_name, neededflags, false);
141 const char *Cvar_VariableString(cvar_state_t *cvars, const char *var_name, int neededflags)
143 return Cvar_VariableStringOr(cvars, var_name, cvar_null_string, neededflags);
148 Cvar_VariableDefString
151 const char *Cvar_VariableDefString(cvar_state_t *cvars, const char *var_name, int neededflags)
155 var = Cvar_FindVar(cvars, var_name, neededflags, false);
157 return cvar_null_string;
158 return var->defstring;
163 Cvar_VariableDescription
166 const char *Cvar_VariableDescription(cvar_state_t *cvars, const char *var_name, int neededflags)
170 var = Cvar_FindVar(cvars, var_name, neededflags, false);
172 return cvar_null_string;
173 return var->description;
179 Cvar_CompleteVariable
182 const char *Cvar_CompleteVariable(cvar_state_t *cvars, const char *partial, int neededflags)
187 len = strlen(partial);
193 for (cvar=cvars->vars ; cvar ; cvar=cvar->next)
194 if (!strncasecmp (partial,cvar->name, len) && (cvar->flags & neededflags))
202 CVar_CompleteCountPossible
204 New function for tab-completion system
206 Thanks to Fett erich@heintz.com
209 int Cvar_CompleteCountPossible(cvar_state_t *cvars, const char *partial, int neededflags)
216 len = strlen(partial);
221 // Loop through the cvars and count all possible matches
222 for (cvar = cvars->vars; cvar; cvar = cvar->next)
223 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
230 CVar_CompleteBuildList
232 New function for tab-completion system
234 Thanks to Fett erich@heintz.com
238 const char **Cvar_CompleteBuildList(cvar_state_t *cvars, const char *partial, int neededflags)
243 size_t sizeofbuf = (Cvar_CompleteCountPossible(cvars, partial, neededflags) + 1) * sizeof(const char *);
246 len = strlen(partial);
247 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof(const char *));
248 // Loop through the alias list and print all matches
249 for (cvar = cvars->vars; cvar; cvar = cvar->next)
250 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
251 buf[bpos++] = cvar->name;
257 void Cvar_PrintHelp(cvar_t *cvar, qboolean full)
261 Con_Printf("^3%s^7", cvar->name);
263 cvar_actual = cvar->alias;
264 Con_Printf(" (now ^3%s^7)", cvar_actual->name);
269 Con_Printf(" is \"%s\" [\"%s\"] ", ((cvar_actual->flags & CVAR_PRIVATE) ? "********"/*hunter2*/ : cvar_actual->string), cvar_actual->defstring);
272 Con_Printf("%s", cvar_actual->description);
276 // written by LadyHavoc
277 void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, int neededflags)
280 size_t len = strlen(partial);
281 // Loop through the command list and print all matches
282 for (cvar = cvars->vars; cvar; cvar = cvar->next)
283 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
284 Cvar_PrintHelp(cvar, true);
287 // check if a cvar is held by some progs
288 static qboolean Cvar_IsAutoCvar(cvar_t *var)
292 for (i = 0;i < PRVM_PROG_MAX;i++)
294 prog = &prvm_prog_list[i];
295 if (prog->loaded && var->globaldefindex[i] >= 0)
301 // we assume that prog is already set to the target progs
302 static void Cvar_UpdateAutoCvar(cvar_t *var)
309 for (i = 0;i < PRVM_PROG_MAX;i++)
311 prog = &prvm_prog_list[i];
312 if (prog->loaded && var->globaldefindex[i] >= 0)
314 // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs
315 switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL)
318 PRVM_GLOBALFIELDFLOAT(prog->globaldefs[var->globaldefindex[i]].ofs) = var->value;
323 for (j = 0;j < 3;j++)
325 while (*s && ISWHITESPACE(*s))
330 while (!ISWHITESPACE(*s))
335 VectorCopy(v, PRVM_GLOBALFIELDVECTOR(prog->globaldefs[var->globaldefindex[i]].ofs));
338 PRVM_ChangeEngineString(prog, var->globaldefindex_stringno[i], var->string);
339 PRVM_GLOBALFIELDSTRING(prog->globaldefs[var->globaldefindex[i]].ofs) = var->globaldefindex_stringno[i];
346 // called after loading a savegame
347 void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars)
350 for (var = cvars->vars ; var ; var = var->next)
351 Cvar_UpdateAutoCvar(var);
359 extern cvar_t sv_disablenotify;
360 static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
362 cvar_state_t *cvars = &cvars_all;
366 char new_value[MAX_INPUTLINE];
371 changed = strcmp(var->string, value) != 0;
372 // LadyHavoc: don't reallocate when there is no change
376 memcpy(new_value,value,MAX_INPUTLINE);
378 // Call the function stored in the cvar for bounds checking, cleanup, etc
380 var->callback(new_value);
382 // LadyHavoc: don't reallocate when the buffer is the same size
383 valuelen = strlen(new_value);
384 if (!var->string || strlen(var->string) != valuelen)
386 Z_Free ((char *)var->string); // free the old value string
388 var->string = (char *)Z_Malloc (valuelen + 1);
390 memcpy ((char *)var->string, new_value, valuelen + 1);
391 var->value = atof (var->string);
392 var->integer = (int) var->value;
393 if ((var->flags & CVAR_NOTIFY) && changed && sv.active && !sv_disablenotify.integer)
394 SV_BroadcastPrintf("\001^3Server cvar \"%s\" changed to \"%s\"\n", var->name, var->string);
396 // TODO: add infostring support to the server?
397 if ((var->flags & CVAR_SERVERINFO) && changed && sv.active)
399 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
402 MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
403 MSG_WriteString (&sv.reliable_datagram, var->name);
404 MSG_WriteString (&sv.reliable_datagram, var->string);
408 if ((var->flags & CVAR_USERINFO) && cls.state != ca_dedicated)
409 CL_SetInfo(var->name, var->string, true, false, false, false);
410 else if ((var->flags & CVAR_NQUSERINFOHACK) && cls.state != ca_dedicated)
412 // update the cls.userinfo to have proper values for the
413 // silly nq config variables.
415 // this is done when these variables are changed rather than at
416 // connect time because if the user or code checks the userinfo and it
417 // holds weird values it may cause confusion...
418 if (!strcmp(var->name, "_cl_color"))
420 int top = (var->integer >> 4) & 15, bottom = var->integer & 15;
421 CL_SetInfo("topcolor", va(vabuf, sizeof(vabuf), "%i", top), true, false, false, false);
422 CL_SetInfo("bottomcolor", va(vabuf, sizeof(vabuf), "%i", bottom), true, false, false, false);
423 if (cls.protocol != PROTOCOL_QUAKEWORLD && cls.netcon)
425 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
426 MSG_WriteString(&cls.netcon->message, va(vabuf, sizeof(vabuf), "color %i %i", top, bottom));
429 else if (!strcmp(var->name, "_cl_rate"))
430 CL_SetInfo("rate", va(vabuf, sizeof(vabuf), "%i", var->integer), true, false, false, false);
431 else if (!strcmp(var->name, "_cl_rate_burstsize"))
432 CL_SetInfo("rate_burstsize", va(vabuf, sizeof(vabuf), "%i", var->integer), true, false, false, false);
433 else if (!strcmp(var->name, "_cl_playerskin"))
434 CL_SetInfo("playerskin", var->string, true, false, false, false);
435 else if (!strcmp(var->name, "_cl_playermodel"))
436 CL_SetInfo("playermodel", var->string, true, false, false, false);
437 else if (!strcmp(var->name, "_cl_name"))
438 CL_SetInfo("name", var->string, true, false, false, false);
439 else if (!strcmp(var->name, "rcon_secure"))
441 // whenever rcon_secure is changed to 0, clear rcon_password for
442 // security reasons (prevents a send-rcon-password-as-plaintext
443 // attack based on NQ protocol session takeover and svc_stufftext)
444 if(var->integer <= 0)
445 Cvar_Set(cvars, "rcon_password", "");
448 else if (!strcmp(var->name, "net_slist_favorites"))
449 NetConn_UpdateFavorites();
453 Cvar_UpdateAutoCvar(var);
456 void Cvar_SetQuick (cvar_t *var, const char *value)
460 Con_Print("Cvar_SetQuick: var == NULL\n");
464 if (developer_extra.integer)
465 Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
467 Cvar_SetQuick_Internal(var, value);
470 void Cvar_Set(cvar_state_t *cvars, const char *var_name, const char *value)
473 var = Cvar_FindVar(cvars, var_name, ~0, false);
476 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
479 Cvar_SetQuick(var, value);
487 void Cvar_SetValueQuick(cvar_t *var, float value)
489 char val[MAX_INPUTLINE];
491 if ((float)((int)value) == value)
492 dpsnprintf(val, sizeof(val), "%i", (int)value);
494 dpsnprintf(val, sizeof(val), "%f", value);
495 Cvar_SetQuick(var, val);
498 void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
500 char val[MAX_INPUTLINE];
502 if ((float)((int)value) == value)
503 dpsnprintf(val, sizeof(val), "%i", (int)value);
505 dpsnprintf(val, sizeof(val), "%f", value);
506 Cvar_Set(cvars, var_name, val);
509 void Cvar_RegisterCallback(cvar_t *var, void (*callback)(char *))
511 var->callback = callback;
514 void Cvar_RegisterAlias(cvar_t *alias, cvar_t *target)
516 if(!(alias->flags & CVAR_ALIAS)) {
517 Con_Warnf("Cvar_RegisterAlias: \"%s\" is not declared as an alias\n", alias->name);
520 // We'll want to register it first
521 Cvar_RegisterVariable(alias);
523 alias->alias = target;
528 Cvar_RegisterVariable
530 Adds a freestanding variable to the variable list.
533 void Cvar_RegisterVariable (cvar_t *variable)
535 cvar_state_t *cvars = NULL;
537 cvar_t *current, *next, *cvar;
542 if(!(variable->flags & CVAR_ALIAS))
544 switch (variable->flags & (CVAR_CLIENT | CVAR_SERVER))
548 case CVAR_CLIENT | CVAR_SERVER:
552 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with no CVAR_CLIENT | CVAR_SERVER flags\n", variable->name, variable->string, variable->flags);
555 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with weird CVAR_CLIENT | CVAR_SERVER flags\n", variable->name, variable->string, variable->flags);
564 if (developer_extra.integer)
565 Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
567 // first check to see if it has already been defined
568 cvar = Cvar_FindVar(cvars, variable->name, ~0, true);
571 if (cvar->flags & CVAR_ALLOCATED)
573 if (developer_extra.integer)
574 Con_DPrintf("... replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
575 // fixed variables replace allocated ones
576 // (because the engine directly accesses fixed variables)
577 // NOTE: this isn't actually used currently
578 // (all cvars are registered before config parsing)
579 variable->flags |= (cvar->flags & ~CVAR_ALLOCATED);
580 // cvar->string is now owned by variable instead
581 variable->string = cvar->string;
582 variable->defstring = cvar->defstring;
583 variable->value = atof (variable->string);
584 variable->integer = (int) variable->value;
585 // Preserve autocvar status.
586 memcpy(variable->globaldefindex, cvar->globaldefindex, sizeof(variable->globaldefindex));
587 memcpy(variable->globaldefindex_stringno, cvar->globaldefindex_stringno, sizeof(variable->globaldefindex_stringno));
588 // replace cvar with this one...
589 variable->next = cvar->next;
590 if (cvars->vars == cvar)
592 // head of the list is easy to change
593 cvars->vars = variable;
597 // otherwise find it somewhere in the list
598 for (current = cvars->vars;current->next != cvar;current = current->next)
600 current->next = variable;
603 // get rid of old allocated cvar
604 // (but not cvar->string and cvar->defstring, because we kept those)
605 Z_Free((char *)cvar->name);
609 Con_DPrintf("Can't register variable %s, already defined\n", variable->name);
613 // check for overlap with a command
614 if (Cmd_Exists(&cmd_client, variable->name) || Cmd_Exists(&cmd_server, variable->name))
616 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
619 if(!(variable->flags & CVAR_ALIAS))
621 // copy the value off, because future sets will Z_Free it
622 oldstr = (char *)variable->string;
623 alloclen = strlen(variable->string) + 1;
624 variable->string = (char *)Z_Malloc (alloclen);
625 memcpy ((char *)variable->string, oldstr, alloclen);
626 variable->defstring = (char *)Z_Malloc (alloclen);
627 memcpy ((char *)variable->defstring, oldstr, alloclen);
628 variable->value = atof (variable->string);
629 variable->integer = (int) variable->value;
632 // Mark it as not an autocvar.
633 for (i = 0;i < PRVM_PROG_MAX;i++)
634 variable->globaldefindex[i] = -1;
636 // link the variable in
637 // alphanumerical order
638 for( current = NULL, next = cvars->vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
641 current->next = variable;
643 cvars->vars = variable;
645 variable->next = next;
647 // link to head of list in this hash table index
648 hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
649 variable->nextonhashchain = cvars->hashtable[hashindex];
650 cvars->hashtable[hashindex] = variable;
657 Adds a newly allocated variable to the variable list or sets its value.
660 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int flags, const char *newdescription)
663 cvar_t *current, *next, *cvar;
666 if (developer_extra.integer)
667 Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
669 // first check to see if it has already been defined
670 cvar = Cvar_FindVar(cvars, name, ~0, true);
673 cvar->flags |= flags;
674 Cvar_SetQuick_Internal (cvar, value);
675 if(newdescription && (cvar->flags & CVAR_ALLOCATED))
677 if(cvar->description != cvar_dummy_description)
678 Z_Free((char *)cvar->description);
681 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
683 cvar->description = cvar_dummy_description;
688 // check for pure evil
691 Con_Printf("Cvar_Get: invalid variable name\n");
695 // check for overlap with a command
696 if (Cmd_Exists(&cmd_client, name) || Cmd_Exists(&cmd_server, name))
698 Con_Printf("Cvar_Get: %s is a command\n", name);
702 // allocate a new cvar, cvar name, and cvar string
703 // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
704 // FIXME: these never get Z_Free'd
705 cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
706 cvar->flags = flags | CVAR_ALLOCATED;
707 cvar->name = (char *)Mem_strdup(zonemempool, name);
708 cvar->string = (char *)Mem_strdup(zonemempool, value);
709 cvar->defstring = (char *)Mem_strdup(zonemempool, value);
710 cvar->value = atof (cvar->string);
711 cvar->integer = (int) cvar->value;
713 if(newdescription && *newdescription)
714 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
716 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
718 // Mark it as not an autocvar.
719 for (i = 0;i < PRVM_PROG_MAX;i++)
720 cvar->globaldefindex[i] = -1;
722 // link the variable in
723 // alphanumerical order
724 for( current = NULL, next = cvars->vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next )
727 current->next = cvar;
732 // link to head of list in this hash table index
733 hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name)) % CVAR_HASHSIZE;
734 cvar->nextonhashchain = cvars->hashtable[hashindex];
735 cvars->hashtable[hashindex] = cvar;
740 qboolean Cvar_Readonly (cvar_t *var, const char *cmd_name)
742 if (var->flags & CVAR_READONLY || (var->alias && (var->alias->flags & CVAR_READONLY)))
745 Con_Printf("%s: ",cmd_name);
746 Con_Printf("%s", var->name);
747 if (var->alias && var->alias->name)
748 Con_Printf(" (from %s)",var->alias->name);
749 Con_Printf(" is read-only\n");
759 Handles variable inspection and changing from the console
762 qboolean Cvar_Command (cmd_state_t *cmd)
764 cvar_state_t *cvars = cmd->cvars;
768 v = Cvar_FindVar(cvars, Cmd_Argv(cmd, 0), (cmd->cvars_flagsmask), true);
772 // perform a variable print or set
773 if (Cmd_Argc(cmd) == 1)
775 Cvar_PrintHelp(v, true);
779 if (developer_extra.integer)
780 Con_DPrint("Cvar_Command: ");
782 if(Cvar_Readonly(v, NULL))
785 Cvar_SetQuick(v, Cmd_Argv(cmd, 1));
786 if (developer_extra.integer)
792 void Cvar_UnlockDefaults(cmd_state_t *cmd)
794 cvar_state_t *cvars = cmd->cvars;
796 // unlock the default values of all cvars
797 for (var = cvars->vars ; var ; var = var->next)
799 if(var->flags & CVAR_ALIAS)
801 var->flags &= ~CVAR_DEFAULTSET;
806 void Cvar_LockDefaults_f(cmd_state_t *cmd)
808 cvar_state_t *cvars = cmd->cvars;
810 // lock in the default values of all cvars
811 for (var = cvars->vars ; var ; var = var->next)
813 if(var->flags & CVAR_ALIAS)
815 if (!(var->flags & CVAR_DEFAULTSET))
819 //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
820 var->flags |= CVAR_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 if(c->flags & CVAR_ALIAS)
837 c->initflags = c->flags;
838 c->initdefstring = Mem_strdup(zonemempool, c->defstring);
839 c->initstring = Mem_strdup(zonemempool, c->string);
840 c->initvalue = c->value;
841 c->initinteger = c->integer;
842 VectorCopy(c->vector, c->initvector);
846 void Cvar_RestoreInitState(cvar_state_t *cvars)
851 for (cp = &cvars->vars;(c = *cp);)
853 if(c->flags & CVAR_ALIAS)
857 // restore this cvar, it existed at init
858 if (((c->flags ^ c->initflags) & CVAR_MAXFLAGSVAL)
859 || strcmp(c->defstring ? c->defstring : "", c->initdefstring ? c->initdefstring : "")
860 || strcmp(c->string ? c->string : "", c->initstring ? c->initstring : ""))
862 Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
864 Z_Free((char *)c->defstring);
865 c->defstring = Mem_strdup(zonemempool, c->initdefstring);
867 Z_Free((char *)c->string);
868 c->string = Mem_strdup(zonemempool, c->initstring);
870 c->flags = c->initflags;
871 c->value = c->initvalue;
872 c->integer = c->initinteger;
873 VectorCopy(c->initvector, c->vector);
878 if (!(c->flags & CVAR_ALLOCATED))
880 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it was registered after init!\n", c->name);
881 // In this case, at least reset it to the default.
882 if((c->flags & CVAR_NORESETTODEFAULTS) == 0)
883 Cvar_SetQuick(c, c->defstring);
887 if (Cvar_IsAutoCvar(c))
889 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it is an autocvar used by running progs!\n", c->name);
890 // In this case, at least reset it to the default.
891 if((c->flags & CVAR_NORESETTODEFAULTS) == 0)
892 Cvar_SetQuick(c, c->defstring);
896 // remove this cvar, it did not exist at init
897 Con_DPrintf("Cvar_RestoreInitState: Destroying cvar \"%s\"\n", c->name);
898 // unlink struct from hash
899 hashindex = CRC_Block((const unsigned char *)c->name, strlen(c->name)) % CVAR_HASHSIZE;
900 for (cp2 = &cvars->hashtable[hashindex];(c2 = *cp2);)
904 *cp2 = c2->nextonhashchain;
908 cp2 = &c2->nextonhashchain;
910 // unlink struct from main list
914 Z_Free((char *)c->defstring);
916 Z_Free((char *)c->string);
917 if (c->description && c->description != cvar_dummy_description)
918 Z_Free((char *)c->description);
925 void Cvar_ResetToDefaults_All_f(cmd_state_t *cmd)
927 cvar_state_t *cvars = cmd->cvars;
929 // restore the default values of all cvars
930 for (var = cvars->vars ; var ; var = var->next)
932 if(var->flags & CVAR_ALIAS)
934 if((var->flags & CVAR_NORESETTODEFAULTS) == 0)
935 Cvar_SetQuick(var, var->defstring);
940 void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd)
942 cvar_state_t *cvars = cmd->cvars;
944 // restore the default values of all cvars
945 for (var = cvars->vars ; var ; var = var->next)
947 if(var->flags & CVAR_ALIAS)
949 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == 0)
950 Cvar_SetQuick(var, var->defstring);
955 void Cvar_ResetToDefaults_SaveOnly_f(cmd_state_t *cmd)
957 cvar_state_t *cvars = cmd->cvars;
959 // restore the default values of all cvars
960 for (var = cvars->vars ; var ; var = var->next)
962 if(var->flags & CVAR_ALIAS)
964 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == CVAR_SAVE)
965 Cvar_SetQuick(var, var->defstring);
974 Writes lines containing "set variable value" for all variables
975 with the archive flag set to true.
978 void Cvar_WriteVariables (cvar_state_t *cvars, qfile_t *f)
981 char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
983 // don't save cvars that match their default value
984 for (var = cvars->vars ; var ; var = var->next) {
985 if(var->flags & CVAR_ALIAS)
987 if ((var->flags & CVAR_SAVE) && (strcmp(var->string, var->defstring) || ((var->flags & CVAR_ALLOCATED) && !(var->flags & CVAR_DEFAULTSET))))
989 Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$", false);
990 Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$", false);
991 FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", buf1, buf2);
997 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
998 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
1004 void Cvar_List_f(cmd_state_t *cmd)
1006 cvar_state_t *cvars = cmd->cvars;
1008 const char *partial;
1013 if (Cmd_Argc(cmd) > 1)
1015 partial = Cmd_Argv(cmd, 1);
1016 len = strlen(partial);
1017 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1027 for (cvar = cvars->vars; cvar; cvar = cvar->next)
1029 if (len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp (partial,cvar->name,len)))
1032 Cvar_PrintHelp(cvar, true);
1039 Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
1041 Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", partial);
1044 Con_Printf("%i cvar(s)\n", count);
1046 // 2000-01-09 CvarList command by Maddes
1048 void Cvar_Set_f(cmd_state_t *cmd)
1050 cvar_state_t *cvars = cmd->cvars;
1053 // make sure it's the right number of parameters
1054 if (Cmd_Argc(cmd) < 3)
1056 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
1060 // check if it's read-only
1061 cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0, true);
1063 if(Cvar_Readonly(cvar,"Set"))
1066 if (developer_extra.integer)
1067 Con_DPrint("Set: ");
1069 // all looks ok, create/modify the cvar
1070 Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1073 void Cvar_SetA_f(cmd_state_t *cmd)
1075 cvar_state_t *cvars = cmd->cvars;
1078 // make sure it's the right number of parameters
1079 if (Cmd_Argc(cmd) < 3)
1081 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
1085 // check if it's read-only
1086 cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0, true);
1088 if(Cvar_Readonly(cvar,"SetA"))
1091 if (developer_extra.integer)
1092 Con_DPrint("SetA: ");
1094 // all looks ok, create/modify the cvar
1095 Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask | CVAR_SAVE, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1098 void Cvar_Del_f(cmd_state_t *cmd)
1100 cvar_state_t *cvars = cmd->cvars;
1101 int neededflags = ~0;
1103 cvar_t *cvar, *parent, **link, *prev;
1105 if(Cmd_Argc(cmd) < 2)
1107 Con_Printf("%s: wrong number of parameters, usage: unset <variablename1> [<variablename2> ...]\n", Cmd_Argv(cmd, 0));
1110 for(i = 1; i < Cmd_Argc(cmd); ++i)
1112 cvar = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
1115 Con_Printf("%s: %s is not defined\n", Cmd_Argv(cmd, 0), Cmd_Argv(cmd, i));
1118 if(Cvar_Readonly(cvar, Cmd_Argv(cmd, 0)))
1120 if(!(cvar->flags & CVAR_ALLOCATED))
1122 Con_Printf("%s: %s is static and cannot be deleted\n", Cmd_Argv(cmd, 0), cvar->name);
1125 if(cvar == cvars->vars)
1127 cvars->vars = cvar->next;
1131 // in this case, prev must be set, otherwise there has been some inconsistensy
1132 // elsewhere already... should I still check for prev != NULL?
1133 prev->next = cvar->next;
1137 parent->nextonhashchain = cvar->nextonhashchain;
1139 *link = cvar->nextonhashchain;
1140 if(!(cvar->flags & CVAR_ALIAS))
1142 if(cvar->description != cvar_dummy_description)
1143 Z_Free((char *)cvar->description);
1145 Z_Free((char *)cvar->name);
1146 Z_Free((char *)cvar->string);
1147 Z_Free((char *)cvar->defstring);
1153 #ifdef FILLALLCVARSWITHRUBBISH
1154 void Cvar_FillAll_f(cmd_state_t *cmd)
1160 if(Cmd_Argc(cmd) != 2)
1162 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(cmd, 0));
1163 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(cmd, 0));
1166 n = atoi(Cmd_Argv(cmd, 1));
1170 buf = Z_Malloc(n + 1);
1172 for(var = cvars->vars; var; var = var->next)
1174 if(var->flags & CVAR_ALIAS)
1176 for(i = 0, p = buf, q = var->name; i < n; ++i)
1182 if(verify && strcmp(var->string, buf))
1184 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);
1186 Cvar_SetQuick(var, buf);
1190 #endif /* FILLALLCVARSWITHRUBBISH */