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 char *cvar_dummy_description = "custom cvar";
26 cvar_t *cvar_vars = NULL;
27 cvar_t *cvar_hashtable[CVAR_HASHSIZE];
28 char *cvar_null_string = "";
35 cvar_t *Cvar_FindVar (const char *var_name)
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 = cvar_hashtable[hashindex];var;var = var->nextonhashchain)
43 if (!strcmp (var_name, var->name))
49 cvar_t *Cvar_FindVarAfter (const char *prev_var_name, int neededflags)
55 var = Cvar_FindVar (prev_var_name);
63 // search for the next cvar matching the needed flags
66 if ((var->flags & neededflags) || !neededflags)
73 cvar_t *Cvar_FindVarLink (const char *var_name, cvar_t **parent, cvar_t ***link, cvar_t **prev_alpha)
78 // use hash lookup to minimize search time
79 hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name));
80 if(parent) *parent = NULL;
81 if(prev_alpha) *prev_alpha = NULL;
82 if(link) *link = &cvar_hashtable[hashindex];
83 for (var = cvar_hashtable[hashindex];var;var = var->nextonhashchain)
85 if (!strcmp (var_name, var->name))
87 if(!prev_alpha || var == cvar_vars)
90 *prev_alpha = cvar_vars;
91 // if prev_alpha happens to become NULL then there has been some inconsistency elsewhere
92 // already - should I still insert '*prev_alpha &&' in the loop?
93 while((*prev_alpha)->next != var)
94 *prev_alpha = (*prev_alpha)->next;
97 if(parent) *parent = var;
108 float Cvar_VariableValue (const char *var_name)
112 var = Cvar_FindVar (var_name);
115 return atof (var->string);
124 const char *Cvar_VariableString (const char *var_name)
128 var = Cvar_FindVar (var_name);
130 return cvar_null_string;
136 Cvar_VariableDefString
139 const char *Cvar_VariableDefString (const char *var_name)
143 var = Cvar_FindVar (var_name);
145 return cvar_null_string;
146 return var->defstring;
151 Cvar_VariableDescription
154 const char *Cvar_VariableDescription (const char *var_name)
158 var = Cvar_FindVar (var_name);
160 return cvar_null_string;
161 return var->description;
167 Cvar_CompleteVariable
170 const char *Cvar_CompleteVariable (const char *partial)
175 len = strlen(partial);
181 for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
182 if (!strncasecmp (partial,cvar->name, len))
190 CVar_CompleteCountPossible
192 New function for tab-completion system
194 Thanks to Fett erich@heintz.com
197 int Cvar_CompleteCountPossible (const char *partial)
204 len = strlen(partial);
209 // Loop through the cvars and count all possible matches
210 for (cvar = cvar_vars; cvar; cvar = cvar->next)
211 if (!strncasecmp(partial, cvar->name, len))
218 CVar_CompleteBuildList
220 New function for tab-completion system
222 Thanks to Fett erich@heintz.com
226 const char **Cvar_CompleteBuildList (const char *partial)
231 size_t sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) * sizeof (const char *);
234 len = strlen(partial);
235 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
236 // Loop through the alias list and print all matches
237 for (cvar = cvar_vars; cvar; cvar = cvar->next)
238 if (!strncasecmp(partial, cvar->name, len))
239 buf[bpos++] = cvar->name;
245 // written by LordHavoc
246 void Cvar_CompleteCvarPrint (const char *partial)
249 size_t len = strlen(partial);
250 // Loop through the command list and print all matches
251 for (cvar = cvar_vars; cvar; cvar = cvar->next)
252 if (!strncasecmp(partial, cvar->name, len))
253 Con_Printf ("^3%s^7 is \"%s\" [\"%s\"] %s\n", cvar->name, cvar->string, cvar->defstring, cvar->description);
262 void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
266 prvm_prog_t *tmpprog;
269 changed = strcmp(var->string, value) != 0;
270 // LordHavoc: don't reallocate when there is no change
274 // LordHavoc: don't reallocate when the buffer is the same size
275 valuelen = strlen(value);
276 if (!var->string || strlen(var->string) != valuelen)
278 Z_Free (var->string); // free the old value string
280 var->string = (char *)Z_Malloc (valuelen + 1);
282 memcpy (var->string, value, valuelen + 1);
283 var->value = atof (var->string);
284 var->integer = (int) var->value;
285 if ((var->flags & CVAR_NOTIFY) && changed && sv.active)
286 SV_BroadcastPrintf("\"%s\" changed to \"%s\"\n", var->name, var->string);
288 // TODO: add infostring support to the server?
289 if ((var->flags & CVAR_SERVERINFO) && changed && sv.active)
291 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
294 MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
295 MSG_WriteString (&sv.reliable_datagram, var->name);
296 MSG_WriteString (&sv.reliable_datagram, var->string);
300 if ((var->flags & CVAR_USERINFO) && cls.state != ca_dedicated)
301 CL_SetInfo(var->name, var->string, true, false, false, false);
302 else if ((var->flags & CVAR_NQUSERINFOHACK) && cls.state != ca_dedicated)
304 // update the cls.userinfo to have proper values for the
305 // silly nq config variables.
307 // this is done when these variables are changed rather than at
308 // connect time because if the user or code checks the userinfo and it
309 // holds weird values it may cause confusion...
310 if (!strcmp(var->name, "_cl_color"))
312 int top = (var->integer >> 4) & 15, bottom = var->integer & 15;
313 CL_SetInfo("topcolor", va("%i", top), true, false, false, false);
314 CL_SetInfo("bottomcolor", va("%i", bottom), true, false, false, false);
315 if (cls.protocol != PROTOCOL_QUAKEWORLD && cls.netcon)
317 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
318 MSG_WriteString(&cls.netcon->message, va("color %i %i", top, bottom));
321 else if (!strcmp(var->name, "_cl_rate"))
322 CL_SetInfo("rate", va("%i", var->integer), true, false, false, false);
323 else if (!strcmp(var->name, "_cl_playerskin"))
324 CL_SetInfo("playerskin", var->string, true, false, false, false);
325 else if (!strcmp(var->name, "_cl_playermodel"))
326 CL_SetInfo("playermodel", var->string, true, false, false, false);
327 else if (!strcmp(var->name, "_cl_name"))
328 CL_SetInfo("name", var->string, true, false, false, false);
329 else if (!strcmp(var->name, "rcon_secure"))
331 // whenever rcon_secure is changed to 0, clear rcon_password for
332 // security reasons (prevents a send-rcon-password-as-plaintext
333 // attack based on NQ protocol session takeover and svc_stufftext)
334 if(var->integer <= 0)
335 Cvar_Set("rcon_password", "");
337 else if (!strcmp(var->name, "net_slist_favorites"))
338 NetConn_UpdateFavorites();
342 for(i = 0; i < PRVM_MAXPROGS; ++i)
344 if(PRVM_ProgLoaded(i))
347 if(var->globaldefindex_progid[i] == prog->id)
349 // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs
352 prvm_eval_t *val = (prvm_eval_t *)(prog->globals.generic + prog->globaldefs[var->globaldefindex[i]].ofs);
353 switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL)
356 val->_float = var->value;
360 VectorClear(val->vector);
361 for (j = 0;j < 3;j++)
363 while (*s && ISWHITESPACE(*s))
367 val->vector[j] = atof(s);
368 while (!ISWHITESPACE(*s))
375 PRVM_ChangeEngineString(var->globaldefindex_stringno[i], var->string);
376 val->string = var->globaldefindex_stringno[i];
385 void Cvar_SetQuick (cvar_t *var, const char *value)
389 Con_Print("Cvar_SetQuick: var == NULL\n");
393 if (developer_extra.integer)
394 Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
396 Cvar_SetQuick_Internal(var, value);
399 void Cvar_Set (const char *var_name, const char *value)
402 var = Cvar_FindVar (var_name);
405 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
408 Cvar_SetQuick(var, value);
416 void Cvar_SetValueQuick(cvar_t *var, float value)
418 char val[MAX_INPUTLINE];
420 if ((float)((int)value) == value)
421 dpsnprintf(val, sizeof(val), "%i", (int)value);
423 dpsnprintf(val, sizeof(val), "%f", value);
424 Cvar_SetQuick(var, val);
427 void Cvar_SetValue(const char *var_name, float value)
429 char val[MAX_INPUTLINE];
431 if ((float)((int)value) == value)
432 dpsnprintf(val, sizeof(val), "%i", (int)value);
434 dpsnprintf(val, sizeof(val), "%f", value);
435 Cvar_Set(var_name, val);
440 Cvar_RegisterVariable
442 Adds a freestanding variable to the variable list.
445 void Cvar_RegisterVariable (cvar_t *variable)
448 cvar_t *current, *next, *cvar;
452 if (developer_extra.integer)
453 Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
455 // first check to see if it has already been defined
456 cvar = Cvar_FindVar (variable->name);
459 if (cvar->flags & CVAR_ALLOCATED)
461 if (developer_extra.integer)
462 Con_DPrintf("... replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
463 // fixed variables replace allocated ones
464 // (because the engine directly accesses fixed variables)
465 // NOTE: this isn't actually used currently
466 // (all cvars are registered before config parsing)
467 variable->flags |= (cvar->flags & ~CVAR_ALLOCATED);
468 // cvar->string is now owned by variable instead
469 variable->string = cvar->string;
470 variable->defstring = cvar->defstring;
471 variable->value = atof (variable->string);
472 variable->integer = (int) variable->value;
473 // replace cvar with this one...
474 variable->next = cvar->next;
475 if (cvar_vars == cvar)
477 // head of the list is easy to change
478 cvar_vars = variable;
482 // otherwise find it somewhere in the list
483 for (current = cvar_vars;current->next != cvar;current = current->next)
485 current->next = variable;
488 // get rid of old allocated cvar
489 // (but not cvar->string and cvar->defstring, because we kept those)
494 Con_DPrintf("Can't register variable %s, already defined\n", variable->name);
498 // check for overlap with a command
499 if (Cmd_Exists (variable->name))
501 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
505 // copy the value off, because future sets will Z_Free it
506 oldstr = variable->string;
507 alloclen = strlen(variable->string) + 1;
508 variable->string = (char *)Z_Malloc (alloclen);
509 memcpy (variable->string, oldstr, alloclen);
510 variable->defstring = (char *)Z_Malloc (alloclen);
511 memcpy (variable->defstring, oldstr, alloclen);
512 variable->value = atof (variable->string);
513 variable->integer = (int) variable->value;
515 // link the variable in
516 // alphanumerical order
517 for( current = NULL, next = cvar_vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
520 current->next = variable;
522 cvar_vars = variable;
524 variable->next = next;
526 // link to head of list in this hash table index
527 hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
528 variable->nextonhashchain = cvar_hashtable[hashindex];
529 cvar_hashtable[hashindex] = variable;
536 Adds a newly allocated variable to the variable list or sets its value.
539 cvar_t *Cvar_Get (const char *name, const char *value, int flags, const char *newdescription)
542 cvar_t *current, *next, *cvar;
545 if (developer_extra.integer)
546 Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
548 // first check to see if it has already been defined
549 cvar = Cvar_FindVar (name);
552 cvar->flags |= flags;
553 Cvar_SetQuick_Internal (cvar, value);
554 if(newdescription && (cvar->flags & CVAR_ALLOCATED))
556 if(cvar->description != cvar_dummy_description)
557 Z_Free(cvar->description);
561 alloclen = strlen(newdescription) + 1;
562 cvar->description = (char *)Z_Malloc(alloclen);
563 memcpy(cvar->description, newdescription, alloclen);
566 cvar->description = cvar_dummy_description;
571 // check for pure evil
574 Con_Printf("Cvar_Get: invalid variable name\n");
578 // check for overlap with a command
579 if (Cmd_Exists (name))
581 Con_Printf("Cvar_Get: %s is a command\n", name);
585 // allocate a new cvar, cvar name, and cvar string
586 // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
587 // FIXME: these never get Z_Free'd
588 cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
589 cvar->flags = flags | CVAR_ALLOCATED;
590 alloclen = strlen(name) + 1;
591 cvar->name = (char *)Z_Malloc(alloclen);
592 memcpy(cvar->name, name, alloclen);
593 alloclen = strlen(value) + 1;
594 cvar->string = (char *)Z_Malloc(alloclen);
595 memcpy(cvar->string, value, alloclen);
596 cvar->defstring = (char *)Z_Malloc(alloclen);
597 memcpy(cvar->defstring, value, alloclen);
598 cvar->value = atof (cvar->string);
599 cvar->integer = (int) cvar->value;
601 if(newdescription && *newdescription)
603 alloclen = strlen(newdescription) + 1;
604 cvar->description = (char *)Z_Malloc(alloclen);
605 memcpy(cvar->description, newdescription, alloclen);
608 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
610 // link the variable in
611 // alphanumerical order
612 for( current = NULL, next = cvar_vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next )
615 current->next = cvar;
620 // link to head of list in this hash table index
621 hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name)) % CVAR_HASHSIZE;
622 cvar->nextonhashchain = cvar_hashtable[hashindex];
623 cvar_hashtable[hashindex] = cvar;
633 Handles variable inspection and changing from the console
636 qboolean Cvar_Command (void)
641 v = Cvar_FindVar (Cmd_Argv(0));
645 // perform a variable print or set
648 Con_Printf("\"%s\" is \"%s\" [\"%s\"]\n", v->name, ((v->flags & CVAR_PRIVATE) ? "********"/*hunter2*/ : v->string), v->defstring);
652 if (developer_extra.integer)
653 Con_DPrint("Cvar_Command: ");
655 if (v->flags & CVAR_READONLY)
657 Con_Printf("%s is read-only\n", v->name);
660 Cvar_Set (v->name, Cmd_Argv(1));
661 if (developer_extra.integer)
667 void Cvar_UnlockDefaults (void)
670 // unlock the default values of all cvars
671 for (var = cvar_vars ; var ; var = var->next)
672 var->flags &= ~CVAR_DEFAULTSET;
676 void Cvar_LockDefaults_f (void)
679 // lock in the default values of all cvars
680 for (var = cvar_vars ; var ; var = var->next)
682 if (!(var->flags & CVAR_DEFAULTSET))
686 //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
687 var->flags |= CVAR_DEFAULTSET;
688 Z_Free(var->defstring);
689 alloclen = strlen(var->string) + 1;
690 var->defstring = (char *)Z_Malloc(alloclen);
691 memcpy(var->defstring, var->string, alloclen);
697 void Cvar_ResetToDefaults_All_f (void)
700 // restore the default values of all cvars
701 for (var = cvar_vars ; var ; var = var->next)
702 if((var->flags & CVAR_NORESETTODEFAULTS) == 0)
703 Cvar_SetQuick(var, var->defstring);
707 void Cvar_ResetToDefaults_NoSaveOnly_f (void)
710 // restore the default values of all cvars
711 for (var = cvar_vars ; var ; var = var->next)
712 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == 0)
713 Cvar_SetQuick(var, var->defstring);
717 void Cvar_ResetToDefaults_SaveOnly_f (void)
720 // restore the default values of all cvars
721 for (var = cvar_vars ; var ; var = var->next)
722 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == CVAR_SAVE)
723 Cvar_SetQuick(var, var->defstring);
731 Writes lines containing "set variable value" for all variables
732 with the archive flag set to true.
735 void Cvar_WriteVariables (qfile_t *f)
738 char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
740 // don't save cvars that match their default value
741 for (var = cvar_vars ; var ; var = var->next)
742 if ((var->flags & CVAR_SAVE) && (strcmp(var->string, var->defstring) || (var->flags & CVAR_ALLOCATED)))
744 Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$");
745 Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$");
746 FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", buf1, buf2);
751 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
752 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
758 void Cvar_List_f (void)
768 partial = Cmd_Argv (1);
769 len = strlen(partial);
777 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
780 for (cvar = cvar_vars; cvar; cvar = cvar->next)
782 if (len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp (partial,cvar->name,len)))
785 Con_Printf("%s is \"%s\" [\"%s\"] %s\n", cvar->name, ((cvar->flags & CVAR_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring, cvar->description);
792 Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
794 Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", partial);
797 Con_Printf("%i cvar(s)\n", count);
799 // 2000-01-09 CvarList command by Maddes
801 void Cvar_Set_f (void)
805 // make sure it's the right number of parameters
808 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
812 // check if it's read-only
813 cvar = Cvar_FindVar(Cmd_Argv(1));
814 if (cvar && cvar->flags & CVAR_READONLY)
816 Con_Printf("Set: %s is read-only\n", cvar->name);
820 if (developer_extra.integer)
823 // all looks ok, create/modify the cvar
824 Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), 0, Cmd_Argc() > 3 ? Cmd_Argv(3) : NULL);
827 void Cvar_SetA_f (void)
831 // make sure it's the right number of parameters
834 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
838 // check if it's read-only
839 cvar = Cvar_FindVar(Cmd_Argv(1));
840 if (cvar && cvar->flags & CVAR_READONLY)
842 Con_Printf("SetA: %s is read-only\n", cvar->name);
846 if (developer_extra.integer)
847 Con_DPrint("SetA: ");
849 // all looks ok, create/modify the cvar
850 Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), CVAR_SAVE, Cmd_Argc() > 3 ? Cmd_Argv(3) : NULL);
853 void Cvar_Del_f (void)
856 cvar_t *cvar, *parent, **link, *prev;
860 Con_Printf("Del: wrong number of parameters, useage: unset <variablename1> [<variablename2> ...]\n");
863 for(i = 1; i < Cmd_Argc(); ++i)
865 cvar = Cvar_FindVarLink(Cmd_Argv(i), &parent, &link, &prev);
868 Con_Printf("Del: %s is not defined\n", Cmd_Argv(i));
871 if(cvar->flags & CVAR_READONLY)
873 Con_Printf("Del: %s is read-only\n", cvar->name);
876 if(!(cvar->flags & CVAR_ALLOCATED))
878 Con_Printf("Del: %s is static and cannot be deleted\n", cvar->name);
881 if(cvar == cvar_vars)
883 cvar_vars = cvar->next;
887 // in this case, prev must be set, otherwise there has been some inconsistensy
888 // elsewhere already... should I still check for prev != NULL?
889 prev->next = cvar->next;
893 parent->nextonhashchain = cvar->nextonhashchain;
895 *link = cvar->nextonhashchain;
897 if(cvar->description != cvar_dummy_description)
898 Z_Free(cvar->description);
901 Z_Free(cvar->string);
902 Z_Free(cvar->defstring);
907 #ifdef FILLALLCVARSWITHRUBBISH
908 void Cvar_FillAll_f()
916 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(0));
917 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(0));
920 n = atoi(Cmd_Argv(1));
924 buf = Z_Malloc(n + 1);
926 for(var = cvar_vars; var; var = var->next)
928 for(i = 0, p = buf, q = var->name; i < n; ++i)
934 if(verify && strcmp(var->string, buf))
936 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);
938 Cvar_SetQuick(var, buf);
942 #endif /* FILLALLCVARSWITHRUBBISH */