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)
78 float Cvar_VariableValue (const char *var_name)
82 var = Cvar_FindVar (var_name);
85 return atof (var->string);
94 const char *Cvar_VariableString (const char *var_name)
98 var = Cvar_FindVar (var_name);
100 return cvar_null_string;
106 Cvar_VariableDefString
109 const char *Cvar_VariableDefString (const char *var_name)
113 var = Cvar_FindVar (var_name);
115 return cvar_null_string;
116 return var->defstring;
121 Cvar_VariableDescription
124 const char *Cvar_VariableDescription (const char *var_name)
128 var = Cvar_FindVar (var_name);
130 return cvar_null_string;
131 return var->description;
137 Cvar_CompleteVariable
140 const char *Cvar_CompleteVariable (const char *partial)
145 len = strlen(partial);
151 for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
152 if (!strncasecmp (partial,cvar->name, len))
160 CVar_CompleteCountPossible
162 New function for tab-completion system
164 Thanks to Fett erich@heintz.com
167 int Cvar_CompleteCountPossible (const char *partial)
174 len = strlen(partial);
179 // Loop through the cvars and count all possible matches
180 for (cvar = cvar_vars; cvar; cvar = cvar->next)
181 if (!strncasecmp(partial, cvar->name, len))
188 CVar_CompleteBuildList
190 New function for tab-completion system
192 Thanks to Fett erich@heintz.com
196 const char **Cvar_CompleteBuildList (const char *partial)
201 size_t sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) * sizeof (const char *);
204 len = strlen(partial);
205 buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
206 // Loop through the alias list and print all matches
207 for (cvar = cvar_vars; cvar; cvar = cvar->next)
208 if (!strncasecmp(partial, cvar->name, len))
209 buf[bpos++] = cvar->name;
215 // written by LordHavoc
216 void Cvar_CompleteCvarPrint (const char *partial)
219 size_t len = strlen(partial);
220 // Loop through the command list and print all matches
221 for (cvar = cvar_vars; cvar; cvar = cvar->next)
222 if (!strncasecmp(partial, cvar->name, len))
223 Con_Printf ("^3%s^7 is \"%s\" [\"%s\"] %s\n", cvar->name, cvar->string, cvar->defstring, cvar->description);
232 void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
236 prvm_prog_t *tmpprog;
239 changed = strcmp(var->string, value) != 0;
240 // LordHavoc: don't reallocate when there is no change
244 // LordHavoc: don't reallocate when the buffer is the same size
245 valuelen = strlen(value);
246 if (!var->string || strlen(var->string) != valuelen)
248 Z_Free (var->string); // free the old value string
250 var->string = (char *)Z_Malloc (valuelen + 1);
252 memcpy (var->string, value, valuelen + 1);
253 var->value = atof (var->string);
254 var->integer = (int) var->value;
255 if ((var->flags & CVAR_NOTIFY) && changed && sv.active)
256 SV_BroadcastPrintf("\"%s\" changed to \"%s\"\n", var->name, var->string);
258 // TODO: add infostring support to the server?
259 if ((var->flags & CVAR_SERVERINFO) && changed && sv.active)
261 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
264 MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
265 MSG_WriteString (&sv.reliable_datagram, var->name);
266 MSG_WriteString (&sv.reliable_datagram, var->string);
270 if ((var->flags & CVAR_USERINFO) && cls.state != ca_dedicated)
271 CL_SetInfo(var->name, var->string, true, false, false, false);
272 else if ((var->flags & CVAR_NQUSERINFOHACK) && cls.state != ca_dedicated)
274 // update the cls.userinfo to have proper values for the
275 // silly nq config variables.
277 // this is done when these variables are changed rather than at
278 // connect time because if the user or code checks the userinfo and it
279 // holds weird values it may cause confusion...
280 if (!strcmp(var->name, "_cl_color"))
282 int top = (var->integer >> 4) & 15, bottom = var->integer & 15;
283 CL_SetInfo("topcolor", va("%i", top), true, false, false, false);
284 CL_SetInfo("bottomcolor", va("%i", bottom), true, false, false, false);
285 if (cls.protocol != PROTOCOL_QUAKEWORLD && cls.netcon)
287 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
288 MSG_WriteString(&cls.netcon->message, va("color %i %i", top, bottom));
291 else if (!strcmp(var->name, "_cl_rate"))
292 CL_SetInfo("rate", va("%i", var->integer), true, false, false, false);
293 else if (!strcmp(var->name, "_cl_playerskin"))
294 CL_SetInfo("playerskin", var->string, true, false, false, false);
295 else if (!strcmp(var->name, "_cl_playermodel"))
296 CL_SetInfo("playermodel", var->string, true, false, false, false);
297 else if (!strcmp(var->name, "_cl_name"))
298 CL_SetInfo("name", var->string, true, false, false, false);
299 else if (!strcmp(var->name, "rcon_secure"))
301 // whenever rcon_secure is changed to 0, clear rcon_password for
302 // security reasons (prevents a send-rcon-password-as-plaintext
303 // attack based on NQ protocol session takeover and svc_stufftext)
304 if(var->integer <= 0)
305 Cvar_Set("rcon_password", "");
307 else if (!strcmp(var->name, "net_slist_favorites"))
308 NetConn_UpdateFavorites();
312 for(i = 0; i < PRVM_MAXPROGS; ++i)
314 if(PRVM_ProgLoaded(i))
317 if(var->globaldefindex_progid[i] == prog->id)
319 // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs
322 prvm_eval_t *val = (prvm_eval_t *)(prog->globals.generic + prog->globaldefs[var->globaldefindex[i]].ofs);
323 switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL)
326 val->_float = var->value;
330 VectorClear(val->vector);
331 for (j = 0;j < 3;j++)
333 while (*s && ISWHITESPACE(*s))
337 val->vector[j] = atof(s);
338 while (!ISWHITESPACE(*s))
345 PRVM_ChangeEngineString(var->globaldefindex_stringno[i], var->string);
346 val->string = var->globaldefindex_stringno[i];
355 void Cvar_SetQuick (cvar_t *var, const char *value)
359 Con_Print("Cvar_SetQuick: var == NULL\n");
363 if (developer.integer >= 100)
364 Con_Printf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
366 Cvar_SetQuick_Internal(var, value);
369 void Cvar_Set (const char *var_name, const char *value)
372 var = Cvar_FindVar (var_name);
375 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
378 Cvar_SetQuick(var, value);
386 void Cvar_SetValueQuick(cvar_t *var, float value)
388 char val[MAX_INPUTLINE];
390 if ((float)((int)value) == value)
391 dpsnprintf(val, sizeof(val), "%i", (int)value);
393 dpsnprintf(val, sizeof(val), "%f", value);
394 Cvar_SetQuick(var, val);
397 void Cvar_SetValue(const char *var_name, float value)
399 char val[MAX_INPUTLINE];
401 if ((float)((int)value) == value)
402 dpsnprintf(val, sizeof(val), "%i", (int)value);
404 dpsnprintf(val, sizeof(val), "%f", value);
405 Cvar_Set(var_name, val);
410 Cvar_RegisterVariable
412 Adds a freestanding variable to the variable list.
415 void Cvar_RegisterVariable (cvar_t *variable)
418 cvar_t *current, *next, *cvar;
422 if (developer.integer >= 100)
423 Con_Printf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
425 // first check to see if it has already been defined
426 cvar = Cvar_FindVar (variable->name);
429 if (cvar->flags & CVAR_ALLOCATED)
431 if (developer.integer >= 100)
432 Con_Printf("... replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
433 // fixed variables replace allocated ones
434 // (because the engine directly accesses fixed variables)
435 // NOTE: this isn't actually used currently
436 // (all cvars are registered before config parsing)
437 variable->flags |= (cvar->flags & ~CVAR_ALLOCATED);
438 // cvar->string is now owned by variable instead
439 variable->string = cvar->string;
440 variable->defstring = cvar->defstring;
441 variable->value = atof (variable->string);
442 variable->integer = (int) variable->value;
443 // replace cvar with this one...
444 variable->next = cvar->next;
445 if (cvar_vars == cvar)
447 // head of the list is easy to change
448 cvar_vars = variable;
452 // otherwise find it somewhere in the list
453 for (current = cvar_vars;current->next != cvar;current = current->next)
455 current->next = variable;
458 // get rid of old allocated cvar
459 // (but not cvar->string and cvar->defstring, because we kept those)
464 Con_Printf("Can't register variable %s, already defined\n", variable->name);
468 // check for overlap with a command
469 if (Cmd_Exists (variable->name))
471 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
475 // copy the value off, because future sets will Z_Free it
476 oldstr = variable->string;
477 alloclen = strlen(variable->string) + 1;
478 variable->string = (char *)Z_Malloc (alloclen);
479 memcpy (variable->string, oldstr, alloclen);
480 variable->defstring = (char *)Z_Malloc (alloclen);
481 memcpy (variable->defstring, oldstr, alloclen);
482 variable->value = atof (variable->string);
483 variable->integer = (int) variable->value;
485 // link the variable in
486 // alphanumerical order
487 for( current = NULL, next = cvar_vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
490 current->next = variable;
492 cvar_vars = variable;
494 variable->next = next;
496 // link to head of list in this hash table index
497 hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
498 variable->nextonhashchain = cvar_hashtable[hashindex];
499 cvar_hashtable[hashindex] = variable;
506 Adds a newly allocated variable to the variable list or sets its value.
509 cvar_t *Cvar_Get (const char *name, const char *value, int flags, const char *newdescription)
512 cvar_t *current, *next, *cvar;
515 if (developer.integer >= 100)
516 Con_Printf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
518 // first check to see if it has already been defined
519 cvar = Cvar_FindVar (name);
522 cvar->flags |= flags;
523 Cvar_SetQuick_Internal (cvar, value);
524 if(newdescription && (cvar->flags & CVAR_ALLOCATED))
526 if(cvar->description != cvar_dummy_description)
527 Z_Free(cvar->description);
531 alloclen = strlen(newdescription) + 1;
532 cvar->description = (char *)Z_Malloc(alloclen);
533 memcpy(cvar->description, newdescription, alloclen);
536 cvar->description = cvar_dummy_description;
541 // check for pure evil
544 Con_Printf("Cvar_Get: invalid variable name\n");
548 // check for overlap with a command
549 if (Cmd_Exists (name))
551 Con_Printf("Cvar_Get: %s is a command\n", name);
555 // allocate a new cvar, cvar name, and cvar string
556 // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
557 // FIXME: these never get Z_Free'd
558 cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
559 cvar->flags = flags | CVAR_ALLOCATED;
560 alloclen = strlen(name) + 1;
561 cvar->name = (char *)Z_Malloc(alloclen);
562 memcpy(cvar->name, name, alloclen);
563 alloclen = strlen(value) + 1;
564 cvar->string = (char *)Z_Malloc(alloclen);
565 memcpy(cvar->string, value, alloclen);
566 cvar->defstring = (char *)Z_Malloc(alloclen);
567 memcpy(cvar->defstring, value, alloclen);
568 cvar->value = atof (cvar->string);
569 cvar->integer = (int) cvar->value;
571 if(newdescription && *newdescription)
573 alloclen = strlen(newdescription) + 1;
574 cvar->description = (char *)Z_Malloc(alloclen);
575 memcpy(cvar->description, newdescription, alloclen);
578 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
580 // link the variable in
581 // alphanumerical order
582 for( current = NULL, next = cvar_vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next )
585 current->next = cvar;
590 // link to head of list in this hash table index
591 hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name)) % CVAR_HASHSIZE;
592 cvar->nextonhashchain = cvar_hashtable[hashindex];
593 cvar_hashtable[hashindex] = cvar;
603 Handles variable inspection and changing from the console
606 qboolean Cvar_Command (void)
611 v = Cvar_FindVar (Cmd_Argv(0));
615 // perform a variable print or set
618 Con_Printf("\"%s\" is \"%s\" [\"%s\"]\n", v->name, ((v->flags & CVAR_PRIVATE) ? "********"/*hunter2*/ : v->string), v->defstring);
622 if (developer.integer >= 100)
623 Con_DPrint("Cvar_Command: ");
625 if (v->flags & CVAR_READONLY)
627 Con_Printf("%s is read-only\n", v->name);
630 Cvar_Set (v->name, Cmd_Argv(1));
631 if (developer.integer >= 100)
637 void Cvar_UnlockDefaults (void)
640 // unlock the default values of all cvars
641 for (var = cvar_vars ; var ; var = var->next)
642 var->flags &= ~CVAR_DEFAULTSET;
646 void Cvar_LockDefaults_f (void)
649 // lock in the default values of all cvars
650 for (var = cvar_vars ; var ; var = var->next)
652 if (!(var->flags & CVAR_DEFAULTSET))
656 //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
657 var->flags |= CVAR_DEFAULTSET;
658 Z_Free(var->defstring);
659 alloclen = strlen(var->string) + 1;
660 var->defstring = (char *)Z_Malloc(alloclen);
661 memcpy(var->defstring, var->string, alloclen);
667 void Cvar_ResetToDefaults_All_f (void)
670 // restore the default values of all cvars
671 for (var = cvar_vars ; var ; var = var->next)
672 if((var->flags & CVAR_NORESETTODEFAULTS) == 0)
673 Cvar_SetQuick(var, var->defstring);
677 void Cvar_ResetToDefaults_NoSaveOnly_f (void)
680 // restore the default values of all cvars
681 for (var = cvar_vars ; var ; var = var->next)
682 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == 0)
683 Cvar_SetQuick(var, var->defstring);
687 void Cvar_ResetToDefaults_SaveOnly_f (void)
690 // restore the default values of all cvars
691 for (var = cvar_vars ; var ; var = var->next)
692 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == CVAR_SAVE)
693 Cvar_SetQuick(var, var->defstring);
701 Writes lines containing "set variable value" for all variables
702 with the archive flag set to true.
705 void Cvar_WriteVariables (qfile_t *f)
708 char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
710 // don't save cvars that match their default value
711 for (var = cvar_vars ; var ; var = var->next)
712 if ((var->flags & CVAR_SAVE) && (strcmp(var->string, var->defstring) || (var->flags & CVAR_ALLOCATED)))
714 Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$");
715 Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$");
716 FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", buf1, buf2);
721 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
722 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
728 void Cvar_List_f (void)
738 partial = Cmd_Argv (1);
739 len = strlen(partial);
747 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
750 for (cvar = cvar_vars; cvar; cvar = cvar->next)
752 if (len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp (partial,cvar->name,len)))
755 Con_Printf("%s is \"%s\" [\"%s\"] %s\n", cvar->name, ((cvar->flags & CVAR_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring, cvar->description);
762 Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
764 Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", partial);
767 Con_Printf("%i cvar(s)\n", count);
769 // 2000-01-09 CvarList command by Maddes
771 void Cvar_Set_f (void)
775 // make sure it's the right number of parameters
778 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
782 // check if it's read-only
783 cvar = Cvar_FindVar(Cmd_Argv(1));
784 if (cvar && cvar->flags & CVAR_READONLY)
786 Con_Printf("Set: %s is read-only\n", cvar->name);
790 if (developer.integer >= 100)
793 // all looks ok, create/modify the cvar
794 Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), 0, Cmd_Argc() > 3 ? Cmd_Argv(3) : NULL);
797 void Cvar_SetA_f (void)
801 // make sure it's the right number of parameters
804 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
808 // check if it's read-only
809 cvar = Cvar_FindVar(Cmd_Argv(1));
810 if (cvar && cvar->flags & CVAR_READONLY)
812 Con_Printf("SetA: %s is read-only\n", cvar->name);
816 if (developer.integer >= 100)
817 Con_DPrint("SetA: ");
819 // all looks ok, create/modify the cvar
820 Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), CVAR_SAVE, Cmd_Argc() > 3 ? Cmd_Argv(3) : NULL);
823 #ifdef FILLALLCVARSWITHRUBBISH
824 void Cvar_FillAll_f()
832 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(0));
833 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(0));
836 n = atoi(Cmd_Argv(1));
840 buf = Z_Malloc(n + 1);
842 for(var = cvar_vars; var; var = var->next)
844 for(i = 0, p = buf, q = var->name; i < n; ++i)
850 if(verify && strcmp(var->string, buf))
852 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);
854 Cvar_SetQuick(var, buf);
858 #endif /* FILLALLCVARSWITHRUBBISH */