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[65536];
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));
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)
267 changed = strcmp(var->string, value) != 0;
268 // LordHavoc: don't reallocate when there is no change
272 // LordHavoc: don't reallocate when the buffer is the same size
273 valuelen = strlen(value);
274 if (!var->string || strlen(var->string) != valuelen)
276 Z_Free (var->string); // free the old value string
278 var->string = (char *)Z_Malloc (valuelen + 1);
280 memcpy (var->string, value, valuelen + 1);
281 var->value = atof (var->string);
282 var->integer = (int) var->value;
283 if ((var->flags & CVAR_NOTIFY) && changed && sv.active)
284 SV_BroadcastPrintf("\"%s\" changed to \"%s\"\n", var->name, var->string);
286 // TODO: add infostring support to the server?
287 if ((var->flags & CVAR_SERVERINFO) && changed && sv.active)
289 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
292 MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
293 MSG_WriteString (&sv.reliable_datagram, var->name);
294 MSG_WriteString (&sv.reliable_datagram, var->string);
298 if ((var->flags & CVAR_USERINFO) && cls.state != ca_dedicated)
299 CL_SetInfo(var->name, var->string, true, false, false, false);
300 else if ((var->flags & CVAR_NQUSERINFOHACK) && cls.state != ca_dedicated)
302 // update the cls.userinfo to have proper values for the
303 // silly nq config variables.
305 // this is done when these variables are changed rather than at
306 // connect time because if the user or code checks the userinfo and it
307 // holds weird values it may cause confusion...
308 if (!strcmp(var->name, "_cl_color"))
310 int top = (var->integer >> 4) & 15, bottom = var->integer & 15;
311 CL_SetInfo("topcolor", va("%i", top), true, false, false, false);
312 CL_SetInfo("bottomcolor", va("%i", bottom), true, false, false, false);
313 if (cls.protocol != PROTOCOL_QUAKEWORLD && cls.netcon)
315 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
316 MSG_WriteString(&cls.netcon->message, va("color %i %i", top, bottom));
319 else if (!strcmp(var->name, "_cl_rate"))
320 CL_SetInfo("rate", va("%i", var->integer), true, false, false, false);
321 else if (!strcmp(var->name, "_cl_playerskin"))
322 CL_SetInfo("playerskin", var->string, true, false, false, false);
323 else if (!strcmp(var->name, "_cl_playermodel"))
324 CL_SetInfo("playermodel", var->string, true, false, false, false);
325 else if (!strcmp(var->name, "_cl_name"))
326 CL_SetInfo("name", var->string, true, false, false, false);
327 else if (!strcmp(var->name, "rcon_secure"))
329 // whenever rcon_secure is changed to 0, clear rcon_password for
330 // security reasons (prevents a send-rcon-password-as-plaintext
331 // attack based on NQ protocol session takeover and svc_stufftext)
333 Cvar_Set("rcon_password", "");
335 else if (!strcmp(var->name, "net_slist_favorites"))
336 NetConn_UpdateFavorites();
340 void Cvar_SetQuick (cvar_t *var, const char *value)
344 Con_Print("Cvar_SetQuick: var == NULL\n");
348 if (developer.integer >= 100)
349 Con_Printf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
351 Cvar_SetQuick_Internal(var, value);
354 void Cvar_Set (const char *var_name, const char *value)
357 var = Cvar_FindVar (var_name);
360 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
363 Cvar_SetQuick(var, value);
371 void Cvar_SetValueQuick(cvar_t *var, float value)
373 char val[MAX_INPUTLINE];
375 if ((float)((int)value) == value)
376 dpsnprintf(val, sizeof(val), "%i", (int)value);
378 dpsnprintf(val, sizeof(val), "%f", value);
379 Cvar_SetQuick(var, val);
382 void Cvar_SetValue(const char *var_name, float value)
384 char val[MAX_INPUTLINE];
386 if ((float)((int)value) == value)
387 dpsnprintf(val, sizeof(val), "%i", (int)value);
389 dpsnprintf(val, sizeof(val), "%f", value);
390 Cvar_Set(var_name, val);
395 Cvar_RegisterVariable
397 Adds a freestanding variable to the variable list.
400 void Cvar_RegisterVariable (cvar_t *variable)
403 cvar_t *current, *next, *cvar;
407 if (developer.integer >= 100)
408 Con_Printf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
410 // first check to see if it has already been defined
411 cvar = Cvar_FindVar (variable->name);
414 if (cvar->flags & CVAR_ALLOCATED)
416 if (developer.integer >= 100)
417 Con_Printf("... replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
418 // fixed variables replace allocated ones
419 // (because the engine directly accesses fixed variables)
420 // NOTE: this isn't actually used currently
421 // (all cvars are registered before config parsing)
422 variable->flags |= (cvar->flags & ~CVAR_ALLOCATED);
423 // cvar->string is now owned by variable instead
424 variable->string = cvar->string;
425 variable->defstring = cvar->defstring;
426 variable->value = atof (variable->string);
427 variable->integer = (int) variable->value;
428 // replace cvar with this one...
429 variable->next = cvar->next;
430 if (cvar_vars == cvar)
432 // head of the list is easy to change
433 cvar_vars = variable;
437 // otherwise find it somewhere in the list
438 for (current = cvar_vars;current->next != cvar;current = current->next)
440 current->next = variable;
443 // get rid of old allocated cvar
444 // (but not cvar->string and cvar->defstring, because we kept those)
449 Con_Printf("Can't register variable %s, already defined\n", variable->name);
453 // check for overlap with a command
454 if (Cmd_Exists (variable->name))
456 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
460 // copy the value off, because future sets will Z_Free it
461 oldstr = variable->string;
462 alloclen = strlen(variable->string) + 1;
463 variable->string = (char *)Z_Malloc (alloclen);
464 memcpy (variable->string, oldstr, alloclen);
465 variable->defstring = (char *)Z_Malloc (alloclen);
466 memcpy (variable->defstring, oldstr, alloclen);
467 variable->value = atof (variable->string);
468 variable->integer = (int) variable->value;
470 // link the variable in
471 // alphanumerical order
472 for( current = NULL, next = cvar_vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
475 current->next = variable;
477 cvar_vars = variable;
479 variable->next = next;
481 // link to head of list in this hash table index
482 hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name));
483 variable->nextonhashchain = cvar_hashtable[hashindex];
484 cvar_hashtable[hashindex] = variable;
491 Adds a newly allocated variable to the variable list or sets its value.
494 cvar_t *Cvar_Get (const char *name, const char *value, int flags, const char *newdescription)
497 cvar_t *current, *next, *cvar;
500 if (developer.integer >= 100)
501 Con_Printf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
503 // first check to see if it has already been defined
504 cvar = Cvar_FindVar (name);
507 cvar->flags |= flags;
508 Cvar_SetQuick_Internal (cvar, value);
509 if(newdescription && (cvar->flags & CVAR_ALLOCATED))
511 if(cvar->description != cvar_dummy_description)
512 Z_Free(cvar->description);
516 alloclen = strlen(newdescription) + 1;
517 cvar->description = (char *)Z_Malloc(alloclen);
518 memcpy(cvar->description, newdescription, alloclen);
521 cvar->description = cvar_dummy_description;
526 // check for overlap with a command
527 if (Cmd_Exists (name))
529 Con_Printf("Cvar_Get: %s is a command\n", name);
533 // allocate a new cvar, cvar name, and cvar string
534 // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
535 // FIXME: these never get Z_Free'd
536 cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
537 cvar->flags = flags | CVAR_ALLOCATED;
538 alloclen = strlen(name) + 1;
539 cvar->name = (char *)Z_Malloc(alloclen);
540 memcpy(cvar->name, name, alloclen);
541 alloclen = strlen(value) + 1;
542 cvar->string = (char *)Z_Malloc(alloclen);
543 memcpy(cvar->string, value, alloclen);
544 cvar->defstring = (char *)Z_Malloc(alloclen);
545 memcpy(cvar->defstring, value, alloclen);
546 cvar->value = atof (cvar->string);
547 cvar->integer = (int) cvar->value;
549 if(newdescription && *newdescription)
551 alloclen = strlen(newdescription) + 1;
552 cvar->description = (char *)Z_Malloc(alloclen);
553 memcpy(cvar->description, newdescription, alloclen);
556 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
558 // link the variable in
559 // alphanumerical order
560 for( current = NULL, next = cvar_vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next )
563 current->next = cvar;
568 // link to head of list in this hash table index
569 hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name));
570 cvar->nextonhashchain = cvar_hashtable[hashindex];
571 cvar_hashtable[hashindex] = cvar;
581 Handles variable inspection and changing from the console
584 qboolean Cvar_Command (void)
589 v = Cvar_FindVar (Cmd_Argv(0));
593 // perform a variable print or set
596 Con_Printf("\"%s\" is \"%s\" [\"%s\"]\n", v->name, v->string, v->defstring);
600 if (developer.integer >= 100)
601 Con_DPrint("Cvar_Command: ");
603 if (v->flags & CVAR_READONLY)
605 Con_Printf("%s is read-only\n", v->name);
608 Cvar_Set (v->name, Cmd_Argv(1));
609 if (developer.integer >= 100)
615 void Cvar_UnlockDefaults (void)
618 // unlock the default values of all cvars
619 for (var = cvar_vars ; var ; var = var->next)
620 var->flags &= ~CVAR_DEFAULTSET;
624 void Cvar_LockDefaults_f (void)
627 // lock in the default values of all cvars
628 for (var = cvar_vars ; var ; var = var->next)
630 if (!(var->flags & CVAR_DEFAULTSET))
634 //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
635 var->flags |= CVAR_DEFAULTSET;
636 Z_Free(var->defstring);
637 alloclen = strlen(var->string) + 1;
638 var->defstring = (char *)Z_Malloc(alloclen);
639 memcpy(var->defstring, var->string, alloclen);
645 void Cvar_ResetToDefaults_All_f (void)
648 // restore the default values of all cvars
649 for (var = cvar_vars ; var ; var = var->next)
650 if((var->flags & CVAR_NORESETTODEFAULTS) == 0)
651 Cvar_SetQuick(var, var->defstring);
655 void Cvar_ResetToDefaults_NoSaveOnly_f (void)
658 // restore the default values of all cvars
659 for (var = cvar_vars ; var ; var = var->next)
660 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == 0)
661 Cvar_SetQuick(var, var->defstring);
665 void Cvar_ResetToDefaults_SaveOnly_f (void)
668 // restore the default values of all cvars
669 for (var = cvar_vars ; var ; var = var->next)
670 if ((var->flags & (CVAR_NORESETTODEFAULTS | CVAR_SAVE)) == CVAR_SAVE)
671 Cvar_SetQuick(var, var->defstring);
679 Writes lines containing "set variable value" for all variables
680 with the archive flag set to true.
683 void Cvar_WriteVariables (qfile_t *f)
687 // don't save cvars that match their default value
688 for (var = cvar_vars ; var ; var = var->next)
689 if ((var->flags & CVAR_SAVE) && (strcmp(var->string, var->defstring) || (var->flags & CVAR_ALLOCATED)))
690 FS_Printf(f, "%s%s \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", var->name, var->string);
694 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
695 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
701 void Cvar_List_f (void)
711 partial = Cmd_Argv (1);
712 len = strlen(partial);
720 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
723 for (cvar = cvar_vars; cvar; cvar = cvar->next)
725 if (len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp (partial,cvar->name,len)))
728 Con_Printf("%s is \"%s\" [\"%s\"] %s\n", cvar->name, cvar->string, cvar->defstring, cvar->description);
735 Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
737 Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", partial);
740 Con_Printf("%i cvar(s)\n", count);
742 // 2000-01-09 CvarList command by Maddes
744 void Cvar_Set_f (void)
748 // make sure it's the right number of parameters
751 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
755 // check if it's read-only
756 cvar = Cvar_FindVar(Cmd_Argv(1));
757 if (cvar && cvar->flags & CVAR_READONLY)
759 Con_Printf("Set: %s is read-only\n", cvar->name);
763 if (developer.integer >= 100)
766 // all looks ok, create/modify the cvar
767 Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), 0, Cmd_Argc() > 3 ? Cmd_Argv(3) : NULL);
770 void Cvar_SetA_f (void)
774 // make sure it's the right number of parameters
777 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
781 // check if it's read-only
782 cvar = Cvar_FindVar(Cmd_Argv(1));
783 if (cvar && cvar->flags & CVAR_READONLY)
785 Con_Printf("SetA: %s is read-only\n", cvar->name);
789 if (developer.integer >= 100)
790 Con_DPrint("SetA: ");
792 // all looks ok, create/modify the cvar
793 Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), CVAR_SAVE, Cmd_Argc() > 3 ? Cmd_Argv(3) : NULL);
796 void Cvar_Del_f (void)
799 cvar_t *cvar, *parent, **link, *prev;
803 Con_Printf("Del: wrong number of parameters, useage: unset <variablename1> [<variablename2> ...]\n");
806 for(i = 1; i < Cmd_Argc(); ++i)
808 cvar = Cvar_FindVarLink(Cmd_Argv(i), &parent, &link, &prev);
811 Con_Printf("Del: %s is not defined\n", Cmd_Argv(i));
814 if(cvar->flags & CVAR_READONLY)
816 Con_Printf("Del: %s is read-only\n", cvar->name);
819 if(!(cvar->flags & CVAR_ALLOCATED))
821 Con_Printf("Del: %s is static and cannot be deleted\n", cvar->name);
824 if(cvar == cvar_vars)
826 cvar_vars = cvar->next;
830 // in this case, prev must be set, otherwise there has been some inconsistensy
831 // elsewhere already... should I still check for prev != NULL?
832 prev->next = cvar->next;
836 parent->nextonhashchain = cvar->nextonhashchain;
838 *link = cvar->nextonhashchain;
840 if(cvar->description != cvar_dummy_description)
841 Z_Free(cvar->description);
844 Z_Free(cvar->string);
845 Z_Free(cvar->defstring);
850 #ifdef FILLALLCVARSWITHRUBBISH
851 void Cvar_FillAll_f()
859 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(0));
860 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(0));
863 n = atoi(Cmd_Argv(1));
867 buf = Z_Malloc(n + 1);
869 for(var = cvar_vars; var; var = var->next)
871 for(i = 0, p = buf, q = var->name; i < n; ++i)
877 if(verify && strcmp(var->string, buf))
879 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);
881 Cvar_SetQuick(var, buf);
885 #endif /* FILLALLCVARSWITHRUBBISH */