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_hash_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name, cvar_hash_t **parent, cvar_hash_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];
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;
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, qboolean 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 & CVAR_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 qboolean 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)
376 cvar_state_t *cvars = &cvars_all;
380 char new_value[MAX_INPUTLINE];
382 changed = strcmp(var->string, value) != 0;
383 // LadyHavoc: don't reallocate when there is no change
387 memcpy(new_value,value,MAX_INPUTLINE);
389 // Call the function stored in the cvar for bounds checking, cleanup, etc
391 var->callback(new_value);
393 // LadyHavoc: don't reallocate when the buffer is the same size
394 valuelen = strlen(new_value);
395 if (!var->string || strlen(var->string) != valuelen)
397 Z_Free ((char *)var->string); // free the old value string
399 var->string = (char *)Z_Malloc (valuelen + 1);
401 memcpy ((char *)var->string, new_value, valuelen + 1);
402 var->value = atof (var->string);
403 var->integer = (int) var->value;
404 if ((var->flags & CVAR_NOTIFY) && changed && sv.active && !sv_disablenotify.integer)
405 SV_BroadcastPrintf("\003^3Server cvar \"%s\" changed to \"%s\"\n", var->name, var->string);
407 // TODO: add infostring support to the server?
408 if ((var->flags & CVAR_SERVERINFO) && changed && sv.active)
410 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
413 MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
414 MSG_WriteString (&sv.reliable_datagram, var->name);
415 MSG_WriteString (&sv.reliable_datagram, var->string);
419 if ((var->flags & CVAR_USERINFO) && cls.state != ca_dedicated)
420 CL_SetInfo(var->name, var->string, true, false, false, false);
421 else if ((var->flags & CVAR_NQUSERINFOHACK) && cls.state != ca_dedicated)
423 // update the cls.userinfo to have proper values for the
424 // silly nq config variables.
426 // this is done when these variables are changed rather than at
427 // connect time because if the user or code checks the userinfo and it
428 // holds weird values it may cause confusion...
429 if (!strcmp(var->name, "_cl_color"))
431 int top = (var->integer >> 4) & 15, bottom = var->integer & 15;
432 CL_SetInfo("topcolor", va(vabuf, sizeof(vabuf), "%i", top), true, false, false, false);
433 CL_SetInfo("bottomcolor", va(vabuf, sizeof(vabuf), "%i", bottom), true, false, false, false);
434 if (cls.protocol != PROTOCOL_QUAKEWORLD && cls.netcon)
436 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
437 MSG_WriteString(&cls.netcon->message, va(vabuf, sizeof(vabuf), "color %i %i", top, bottom));
440 else if (!strcmp(var->name, "_cl_rate"))
441 CL_SetInfo("rate", va(vabuf, sizeof(vabuf), "%i", var->integer), true, false, false, false);
442 else if (!strcmp(var->name, "_cl_rate_burstsize"))
443 CL_SetInfo("rate_burstsize", va(vabuf, sizeof(vabuf), "%i", var->integer), true, false, false, false);
444 else if (!strcmp(var->name, "_cl_playerskin"))
445 CL_SetInfo("playerskin", var->string, true, false, false, false);
446 else if (!strcmp(var->name, "_cl_playermodel"))
447 CL_SetInfo("playermodel", var->string, true, false, false, false);
448 else if (!strcmp(var->name, "_cl_name"))
449 CL_SetInfo("name", var->string, true, false, false, false);
450 else if (!strcmp(var->name, "rcon_secure"))
452 // whenever rcon_secure is changed to 0, clear rcon_password for
453 // security reasons (prevents a send-rcon-password-as-plaintext
454 // attack based on NQ protocol session takeover and svc_stufftext)
455 if(var->integer <= 0)
456 Cvar_Set(cvars, "rcon_password", "");
459 else if (!strcmp(var->name, "net_slist_favorites"))
460 NetConn_UpdateFavorites();
464 Cvar_UpdateAutoCvar(var);
467 void Cvar_SetQuick (cvar_t *var, const char *value)
471 Con_Print("Cvar_SetQuick: var == NULL\n");
475 if (developer_extra.integer)
476 Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
478 Cvar_SetQuick_Internal(var, value);
481 void Cvar_Set(cvar_state_t *cvars, const char *var_name, const char *value)
484 var = Cvar_FindVar(cvars, var_name, ~0);
487 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
490 Cvar_SetQuick(var, value);
498 void Cvar_SetValueQuick(cvar_t *var, 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_SetQuick(var, val);
509 void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
511 char val[MAX_INPUTLINE];
513 if ((float)((int)value) == value)
514 dpsnprintf(val, sizeof(val), "%i", (int)value);
516 dpsnprintf(val, sizeof(val), "%f", value);
517 Cvar_Set(cvars, var_name, val);
520 void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(char *))
522 variable->callback = callback;
525 void Cvar_RegisterAlias(cvar_t *variable, const char *alias )
527 cvar_state_t *cvars = &cvars_all;
531 variable->aliases = (char **)Mem_Realloc(zonemempool, variable->aliases, sizeof(char *) * (variable->aliasindex + 1));
533 variable->aliases[variable->aliasindex] = (char *)Z_Malloc(strlen(alias) + 1);
534 memcpy(variable->aliases[variable->aliasindex], alias, strlen(alias) + 1);
535 variable->aliasindex++;
537 // link to head of list in this hash table index
538 hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
539 hashindex = CRC_Block((const unsigned char *)alias, strlen(alias)) % CVAR_HASHSIZE;
540 hash->next = cvars->hashtable[hashindex];
541 cvars->hashtable[hashindex] = hash;
542 hash->cvar = variable;
547 Cvar_RegisterVariable
549 Adds a freestanding variable to the variable list.
552 void Cvar_RegisterVariable (cvar_t *variable)
554 cvar_state_t *cvars = NULL;
557 cvar_t *current, *next, *cvar;
562 switch (variable->flags & (CVAR_CLIENT | CVAR_SERVER))
566 case CVAR_CLIENT | CVAR_SERVER:
570 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with no CVAR_CLIENT | CVAR_SERVER flags\n", variable->name, variable->string, variable->flags);
573 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with weird CVAR_CLIENT | CVAR_SERVER flags\n", variable->name, variable->string, variable->flags);
577 if (developer_extra.integer)
578 Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
580 // first check to see if it has already been defined
581 cvar = Cvar_FindVar(cvars, variable->name, ~0);
584 if (cvar->flags & CVAR_ALLOCATED)
586 if (developer_extra.integer)
587 Con_DPrintf("... replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
588 // fixed variables replace allocated ones
589 // (because the engine directly accesses fixed variables)
590 // NOTE: this isn't actually used currently
591 // (all cvars are registered before config parsing)
592 variable->flags |= (cvar->flags & ~CVAR_ALLOCATED);
593 // cvar->string is now owned by variable instead
594 variable->string = cvar->string;
595 variable->defstring = cvar->defstring;
596 variable->value = atof (variable->string);
597 variable->integer = (int) variable->value;
598 // Preserve autocvar status.
599 memcpy(variable->globaldefindex, cvar->globaldefindex, sizeof(variable->globaldefindex));
600 memcpy(variable->globaldefindex_stringno, cvar->globaldefindex_stringno, sizeof(variable->globaldefindex_stringno));
601 // replace cvar with this one...
602 variable->next = cvar->next;
603 if (cvars->vars == cvar)
605 // head of the list is easy to change
606 cvars->vars = variable;
610 // otherwise find it somewhere in the list
611 for (current = cvars->vars;current->next != cvar;current = current->next)
613 current->next = variable;
616 // get rid of old allocated cvar
617 // (but not cvar->string and cvar->defstring, because we kept those)
618 Z_Free((char *)cvar->name);
622 Con_DPrintf("Can't register variable %s, already defined\n", variable->name);
626 // check for overlap with a command
627 if (Cmd_Exists(&cmd_client, variable->name) || Cmd_Exists(&cmd_server, variable->name))
629 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
633 // copy the value off, because future sets will Z_Free it
634 oldstr = (char *)variable->string;
635 alloclen = strlen(variable->string) + 1;
636 variable->string = (char *)Z_Malloc (alloclen);
637 memcpy ((char *)variable->string, oldstr, alloclen);
638 variable->defstring = (char *)Z_Malloc (alloclen);
639 memcpy ((char *)variable->defstring, oldstr, alloclen);
640 variable->value = atof (variable->string);
641 variable->integer = (int) variable->value;
642 variable->aliasindex = 0;
644 // Mark it as not an autocvar.
645 for (i = 0;i < PRVM_PROG_MAX;i++)
646 variable->globaldefindex[i] = -1;
648 // link the variable in
649 // alphanumerical order
650 for( current = NULL, next = cvars->vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
653 current->next = variable;
655 cvars->vars = variable;
657 variable->next = next;
659 // link to head of list in this hash table index
660 hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
661 hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
662 hash->next = cvars->hashtable[hashindex];
663 hash->cvar = variable;
664 cvars->hashtable[hashindex] = hash;
671 Adds a newly allocated variable to the variable list or sets its value.
674 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int flags, const char *newdescription)
678 cvar_t *current, *next, *cvar;
681 if (developer_extra.integer)
682 Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
684 // first check to see if it has already been defined
685 cvar = Cvar_FindVar(cvars, name, ~0);
688 cvar->flags |= flags;
689 Cvar_SetQuick_Internal (cvar, value);
690 if(newdescription && (cvar->flags & CVAR_ALLOCATED))
692 if(cvar->description != cvar_dummy_description)
693 Z_Free((char *)cvar->description);
696 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
698 cvar->description = cvar_dummy_description;
703 // check for pure evil
706 Con_Printf("Cvar_Get: invalid variable name\n");
710 // check for overlap with a command
711 if (Cmd_Exists(&cmd_client, name) || Cmd_Exists(&cmd_server, name))
713 Con_Printf("Cvar_Get: %s is a command\n", name);
717 // allocate a new cvar, cvar name, and cvar string
718 // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
719 // FIXME: these never get Z_Free'd
720 cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
721 cvar->flags = flags | CVAR_ALLOCATED;
722 cvar->name = (char *)Mem_strdup(zonemempool, name);
723 cvar->string = (char *)Mem_strdup(zonemempool, value);
724 cvar->defstring = (char *)Mem_strdup(zonemempool, value);
725 cvar->value = atof (cvar->string);
726 cvar->integer = (int) cvar->value;
727 cvar->aliases = (char **)Z_Malloc(sizeof(char **));
728 memset(cvar->aliases, 0, sizeof(char *));
730 if(newdescription && *newdescription)
731 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
733 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
735 // Mark it as not an autocvar.
736 for (i = 0;i < PRVM_PROG_MAX;i++)
737 cvar->globaldefindex[i] = -1;
739 // link the variable in
740 // alphanumerical order
741 for( current = NULL, next = cvars->vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next )
744 current->next = cvar;
749 // link to head of list in this hash table index
750 hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
751 hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name)) % CVAR_HASHSIZE;
752 hash->next = cvars->hashtable[hashindex];
753 cvars->hashtable[hashindex] = hash;
759 qboolean Cvar_Readonly (cvar_t *var, const char *cmd_name)
761 if (var->flags & CVAR_READONLY)
764 Con_Printf("%s: ",cmd_name);
765 Con_Printf("%s", var->name);
766 Con_Printf(" is read-only\n");
776 Handles variable inspection and changing from the console
779 qboolean Cvar_Command (cmd_state_t *cmd)
781 cvar_state_t *cvars = cmd->cvars;
785 v = Cvar_FindVar(cvars, Cmd_Argv(cmd, 0), (cmd->cvars_flagsmask));
789 // perform a variable print or set
790 if (Cmd_Argc(cmd) == 1)
792 Cvar_PrintHelp(v, Cmd_Argv(cmd, 0), true);
796 if (developer_extra.integer)
797 Con_DPrint("Cvar_Command: ");
799 if(Cvar_Readonly(v, NULL))
802 Cvar_SetQuick(v, Cmd_Argv(cmd, 1));
803 if (developer_extra.integer)
809 void Cvar_UnlockDefaults(cmd_state_t *cmd)
811 cvar_state_t *cvars = cmd->cvars;
813 // unlock the default values of all cvars
814 for (var = cvars->vars ; var ; var = var->next)
815 var->flags &= ~CVAR_DEFAULTSET;
819 void Cvar_LockDefaults_f(cmd_state_t *cmd)
821 cvar_state_t *cvars = cmd->cvars;
823 // lock in the default values of all cvars
824 for (var = cvars->vars ; var ; var = var->next)
826 if (!(var->flags & CVAR_DEFAULTSET))
830 //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
831 var->flags |= CVAR_DEFAULTSET;
832 Z_Free((char *)var->defstring);
833 alloclen = strlen(var->string) + 1;
834 var->defstring = (char *)Z_Malloc(alloclen);
835 memcpy((char *)var->defstring, var->string, alloclen);
840 void Cvar_SaveInitState(cvar_state_t *cvars)
843 for (c = cvars->vars;c;c = c->next)
846 c->initflags = c->flags;
847 c->initdefstring = Mem_strdup(zonemempool, c->defstring);
848 c->initstring = Mem_strdup(zonemempool, c->string);
849 c->initvalue = c->value;
850 c->initinteger = c->integer;
851 VectorCopy(c->vector, c->initvector);
855 void Cvar_RestoreInitState(cvar_state_t *cvars)
860 for (cp = &cvars->vars;(c = *cp);)
864 // restore this cvar, it existed at init
865 if (((c->flags ^ c->initflags) & CVAR_MAXFLAGSVAL)
866 || strcmp(c->defstring ? c->defstring : "", c->initdefstring ? c->initdefstring : "")
867 || strcmp(c->string ? c->string : "", c->initstring ? c->initstring : ""))
869 Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
871 Z_Free((char *)c->defstring);
872 c->defstring = Mem_strdup(zonemempool, c->initdefstring);
874 Z_Free((char *)c->string);
875 c->string = Mem_strdup(zonemempool, c->initstring);
877 c->flags = c->initflags;
878 c->value = c->initvalue;
879 c->integer = c->initinteger;
880 VectorCopy(c->initvector, c->vector);
885 if (!(c->flags & CVAR_ALLOCATED))
887 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it was registered after init!\n", c->name);
888 // In this case, at least reset it to the default.
889 if((c->flags & CVAR_NORESETTODEFAULTS) == 0)
890 Cvar_SetQuick(c, c->defstring);
894 if (Cvar_IsAutoCvar(c))
896 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it is an autocvar used by running progs!\n", c->name);
897 // In this case, at least reset it to the default.
898 if((c->flags & CVAR_NORESETTODEFAULTS) == 0)
899 Cvar_SetQuick(c, c->defstring);
903 // remove this cvar, it did not exist at init
904 Con_DPrintf("Cvar_RestoreInitState: Destroying cvar \"%s\"\n", c->name);
905 // unlink struct from hash
906 hashindex = CRC_Block((const unsigned char *)c->name, strlen(c->name)) % CVAR_HASHSIZE;
907 for (cp2 = &cvars->hashtable[hashindex]->cvar;(c2 = *cp2);)
911 *cp2 = cvars->hashtable[hashindex]->next->cvar;
915 cp2 = &cvars->hashtable[hashindex]->next->cvar;
917 // unlink struct from main list
921 Z_Free((char *)c->defstring);
923 Z_Free((char *)c->string);
924 if (c->description && c->description != cvar_dummy_description)
925 Z_Free((char *)c->description);
932 void Cvar_ResetToDefaults_All_f(cmd_state_t *cmd)
934 cvar_state_t *cvars = cmd->cvars;
936 // restore the default values of all cvars
937 for (var = cvars->vars ; var ; var = var->next)
939 if((var->flags & CVAR_NORESETTODEFAULTS) == 0)
940 Cvar_SetQuick(var, var->defstring);
945 void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd)
947 cvar_state_t *cvars = cmd->cvars;
949 // restore the default values of all cvars
950 for (var = cvars->vars ; var ; var = var->next)
952 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == 0)
953 Cvar_SetQuick(var, var->defstring);
958 void Cvar_ResetToDefaults_SaveOnly_f(cmd_state_t *cmd)
960 cvar_state_t *cvars = cmd->cvars;
962 // restore the default values of all cvars
963 for (var = cvars->vars ; var ; var = var->next)
965 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == CVAR_SAVE)
966 Cvar_SetQuick(var, var->defstring);
975 Writes lines containing "set variable value" for all variables
976 with the archive flag set to true.
979 void Cvar_WriteVariables (cvar_state_t *cvars, qfile_t *f)
982 char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
984 // don't save cvars that match their default value
985 for (var = cvars->vars ; var ; var = var->next) {
986 if ((var->flags & CVAR_SAVE) && (strcmp(var->string, var->defstring) || ((var->flags & CVAR_ALLOCATED) && !(var->flags & CVAR_DEFAULTSET))))
988 Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$", false);
989 Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$", false);
990 FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", buf1, buf2);
996 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
997 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
1003 void Cvar_List_f(cmd_state_t *cmd)
1005 cvar_state_t *cvars = cmd->cvars;
1007 const char *partial;
1012 if (Cmd_Argc(cmd) > 1)
1014 partial = Cmd_Argv(cmd, 1);
1015 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1017 partial = va(vabuf, sizeof(vabuf), "%s*", partial);
1021 partial = va(vabuf, sizeof(vabuf), "*");
1026 for (cvar = cvars->vars; cvar; cvar = cvar->next)
1028 if (matchpattern_with_separator(cvar->name, partial, false, "", false))
1030 Cvar_PrintHelp(cvar, cvar->name, true);
1033 for (int i = 0; i < cvar->aliasindex; i++)
1035 if (matchpattern_with_separator(cvar->aliases[i], partial, false, "", false))
1037 Cvar_PrintHelp(cvar, cvar->aliases[i], true);
1043 if (Cmd_Argc(cmd) > 1)
1046 Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
1048 Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", Cmd_Argv(cmd,1));
1051 Con_Printf("%i cvar(s)\n", count);
1053 // 2000-01-09 CvarList command by Maddes
1055 void Cvar_Set_f(cmd_state_t *cmd)
1057 cvar_state_t *cvars = cmd->cvars;
1060 // make sure it's the right number of parameters
1061 if (Cmd_Argc(cmd) < 3)
1063 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
1067 // check if it's read-only
1068 cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1070 if(Cvar_Readonly(cvar,"Set"))
1073 if (developer_extra.integer)
1074 Con_DPrint("Set: ");
1076 // all looks ok, create/modify the cvar
1077 Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1080 void Cvar_SetA_f(cmd_state_t *cmd)
1082 cvar_state_t *cvars = cmd->cvars;
1085 // make sure it's the right number of parameters
1086 if (Cmd_Argc(cmd) < 3)
1088 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
1092 // check if it's read-only
1093 cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1095 if(Cvar_Readonly(cvar,"SetA"))
1098 if (developer_extra.integer)
1099 Con_DPrint("SetA: ");
1101 // all looks ok, create/modify the cvar
1102 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);
1105 void Cvar_Del_f(cmd_state_t *cmd)
1107 cvar_state_t *cvars = cmd->cvars;
1108 int neededflags = ~0;
1110 cvar_hash_t *hash, *parent, **link;
1111 cvar_t *cvar, *prev;
1113 if(Cmd_Argc(cmd) < 2)
1115 Con_Printf("%s: wrong number of parameters, usage: unset <variablename1> [<variablename2> ...]\n", Cmd_Argv(cmd, 0));
1118 for(i = 1; i < Cmd_Argc(cmd); ++i)
1120 hash = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
1125 Con_Printf("%s: %s is not defined\n", Cmd_Argv(cmd, 0), Cmd_Argv(cmd, i));
1128 if(Cvar_Readonly(cvar, Cmd_Argv(cmd, 0)))
1130 if(!(cvar->flags & CVAR_ALLOCATED))
1132 Con_Printf("%s: %s is static and cannot be deleted\n", Cmd_Argv(cmd, 0), cvar->name);
1135 if(cvar == cvars->vars)
1137 cvars->vars = cvar->next;
1141 // in this case, prev must be set, otherwise there has been some inconsistensy
1142 // elsewhere already... should I still check for prev != NULL?
1143 prev->next = cvar->next;
1147 parent->next = hash->next;
1150 if(cvar->description != cvar_dummy_description)
1151 Z_Free((char *)cvar->description);
1153 Z_Free((char *)cvar->name);
1154 Z_Free((char *)cvar->string);
1155 Z_Free((char *)cvar->defstring);
1160 #ifdef FILLALLCVARSWITHRUBBISH
1161 void Cvar_FillAll_f(cmd_state_t *cmd)
1167 if(Cmd_Argc(cmd) != 2)
1169 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(cmd, 0));
1170 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(cmd, 0));
1173 n = atoi(Cmd_Argv(cmd, 1));
1177 buf = Z_Malloc(n + 1);
1179 for(var = cvars->vars; var; var = var->next)
1181 for(i = 0, p = buf, q = var->name; i < n; ++i)
1187 if(verify && strcmp(var->string, buf))
1189 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);
1191 Cvar_SetQuick(var, buf);
1195 #endif /* FILLALLCVARSWITHRUBBISH */