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)
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 ("%c3%s%s : \"%s\" (\"%s\") : %s\n", STRING_COLOR_TAG, cvar->name, STRING_COLOR_DEFAULT_STR, cvar->string, cvar->defstring, cvar->description);
232 void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
237 changed = strcmp(var->string, value);
238 // LordHavoc: don't reallocate when there is no change
242 // LordHavoc: don't reallocate when the buffer is the same size
243 valuelen = strlen(value);
244 if (!var->string || strlen(var->string) != valuelen)
246 Z_Free (var->string); // free the old value string
248 var->string = (char *)Z_Malloc (valuelen + 1);
250 memcpy (var->string, value, valuelen + 1);
251 var->value = atof (var->string);
252 var->integer = (int) var->value;
253 if ((var->flags & CVAR_NOTIFY) && changed && sv.active)
254 SV_BroadcastPrintf("\"%s\" changed to \"%s\"\n", var->name, var->string);
256 // TODO: add infostring support to the server?
257 if ((var->flags & CVAR_SERVERINFO) && changed && sv.active)
259 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
262 MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
263 MSG_WriteString (&sv.reliable_datagram, var->name);
264 MSG_WriteString (&sv.reliable_datagram, var->string);
268 if ((var->flags & CVAR_USERINFO) && cls.state != ca_dedicated)
269 CL_SetInfo(var->name, var->string, true, false, false, false);
270 else if ((var->flags & CVAR_NQUSERINFOHACK) && cls.state != ca_dedicated)
272 // update the cls.userinfo to have proper values for the
273 // silly nq config variables.
275 // this is done when these variables are changed rather than at
276 // connect time because if the user or code checks the userinfo and it
277 // holds weird values it may cause confusion...
278 if (!strcmp(var->name, "_cl_color"))
280 int top = (var->integer >> 4) & 15, bottom = var->integer & 15;
281 CL_SetInfo("topcolor", va("%i", top), true, false, false, false);
282 CL_SetInfo("bottomcolor", va("%i", bottom), true, false, false, false);
283 if (cls.protocol != PROTOCOL_QUAKEWORLD && cls.netcon)
285 MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
286 MSG_WriteString(&cls.netcon->message, va("color %i %i", top, bottom));
289 else if (!strcmp(var->name, "_cl_rate"))
290 CL_SetInfo("rate", va("%i", var->integer), true, false, false, false);
291 else if (!strcmp(var->name, "_cl_playerskin"))
292 CL_SetInfo("playerskin", var->string, true, false, false, false);
293 else if (!strcmp(var->name, "_cl_playermodel"))
294 CL_SetInfo("playermodel", var->string, true, false, false, false);
295 else if (!strcmp(var->name, "_cl_name"))
296 CL_SetInfo("name", var->string, true, false, false, false);
300 void Cvar_SetQuick (cvar_t *var, const char *value)
304 Con_Print("Cvar_SetQuick: var == NULL\n");
308 if (developer.integer >= 100)
309 Con_Printf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
311 Cvar_SetQuick_Internal(var, value);
314 void Cvar_Set (const char *var_name, const char *value)
317 var = Cvar_FindVar (var_name);
320 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
323 Cvar_SetQuick(var, value);
331 void Cvar_SetValueQuick(cvar_t *var, float value)
333 char val[MAX_INPUTLINE];
335 if ((float)((int)value) == value)
336 dpsnprintf(val, sizeof(val), "%i", (int)value);
338 dpsnprintf(val, sizeof(val), "%f", value);
339 Cvar_SetQuick(var, val);
342 void Cvar_SetValue(const char *var_name, float value)
344 char val[MAX_INPUTLINE];
346 if ((float)((int)value) == value)
347 dpsnprintf(val, sizeof(val), "%i", (int)value);
349 dpsnprintf(val, sizeof(val), "%f", value);
350 Cvar_Set(var_name, val);
355 Cvar_RegisterVariable
357 Adds a freestanding variable to the variable list.
360 void Cvar_RegisterVariable (cvar_t *variable)
363 cvar_t *current, *next, *cvar;
367 if (developer.integer >= 100)
368 Con_Printf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
370 // first check to see if it has already been defined
371 cvar = Cvar_FindVar (variable->name);
374 if (cvar->flags & CVAR_ALLOCATED)
376 if (developer.integer >= 100)
377 Con_Printf("... replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
378 // fixed variables replace allocated ones
379 // (because the engine directly accesses fixed variables)
380 // NOTE: this isn't actually used currently
381 // (all cvars are registered before config parsing)
382 variable->flags |= (cvar->flags & ~CVAR_ALLOCATED);
383 // cvar->string is now owned by variable instead
384 variable->string = cvar->string;
385 variable->defstring = cvar->defstring;
386 variable->value = atof (variable->string);
387 variable->integer = (int) variable->value;
388 // replace cvar with this one...
389 variable->next = cvar->next;
390 if (cvar_vars == cvar)
392 // head of the list is easy to change
393 cvar_vars = variable;
397 // otherwise find it somewhere in the list
398 for (current = cvar_vars;current->next != cvar;current = current->next)
400 current->next = variable;
403 // get rid of old allocated cvar
404 // (but not cvar->string and cvar->defstring, because we kept those)
409 Con_Printf("Can't register variable %s, already defined\n", variable->name);
413 // check for overlap with a command
414 if (Cmd_Exists (variable->name))
416 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
420 // copy the value off, because future sets will Z_Free it
421 oldstr = variable->string;
422 alloclen = strlen(variable->string) + 1;
423 variable->string = (char *)Z_Malloc (alloclen);
424 memcpy (variable->string, oldstr, alloclen);
425 variable->defstring = (char *)Z_Malloc (alloclen);
426 memcpy (variable->defstring, oldstr, alloclen);
427 variable->value = atof (variable->string);
428 variable->integer = (int) variable->value;
430 // link the variable in
431 // alphanumerical order
432 for( current = NULL, next = cvar_vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
435 current->next = variable;
437 cvar_vars = variable;
439 variable->next = next;
441 // link to head of list in this hash table index
442 hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name));
443 variable->nextonhashchain = cvar_hashtable[hashindex];
444 cvar_hashtable[hashindex] = variable;
451 Adds a newly allocated variable to the variable list or sets its value.
454 cvar_t *Cvar_Get (const char *name, const char *value, int flags, const char *newdescription)
457 cvar_t *current, *next, *cvar;
460 if (developer.integer >= 100)
461 Con_Printf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
463 // first check to see if it has already been defined
464 cvar = Cvar_FindVar (name);
467 cvar->flags |= flags;
468 Cvar_SetQuick_Internal (cvar, value);
469 if(newdescription && (cvar->flags & CVAR_ALLOCATED))
471 if(cvar->description != cvar_dummy_description)
472 Z_Free(cvar->description);
476 alloclen = strlen(newdescription) + 1;
477 cvar->description = (char *)Z_Malloc(alloclen);
478 memcpy(cvar->description, newdescription, alloclen);
481 cvar->description = cvar_dummy_description;
486 // check for overlap with a command
487 if (Cmd_Exists (name))
489 Con_Printf("Cvar_Get: %s is a command\n", name);
493 // allocate a new cvar, cvar name, and cvar string
494 // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
495 // FIXME: these never get Z_Free'd
496 cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
497 cvar->flags = flags | CVAR_ALLOCATED;
498 alloclen = strlen(name) + 1;
499 cvar->name = (char *)Z_Malloc(alloclen);
500 memcpy(cvar->name, name, alloclen);
501 alloclen = strlen(value) + 1;
502 cvar->string = (char *)Z_Malloc(alloclen);
503 memcpy(cvar->string, value, alloclen);
504 cvar->defstring = (char *)Z_Malloc(alloclen);
505 memcpy(cvar->defstring, value, alloclen);
506 cvar->value = atof (cvar->string);
507 cvar->integer = (int) cvar->value;
509 if(newdescription && *newdescription)
511 alloclen = strlen(newdescription) + 1;
512 cvar->description = (char *)Z_Malloc(alloclen);
513 memcpy(cvar->description, newdescription, alloclen);
516 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
518 // link the variable in
519 // alphanumerical order
520 for( current = NULL, next = cvar_vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next )
523 current->next = cvar;
528 // link to head of list in this hash table index
529 hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name));
530 cvar->nextonhashchain = cvar_hashtable[hashindex];
531 cvar_hashtable[hashindex] = cvar;
541 Handles variable inspection and changing from the console
544 qboolean Cvar_Command (void)
549 v = Cvar_FindVar (Cmd_Argv(0));
553 // perform a variable print or set
556 Con_Printf("\"%s\" is \"%s\" [\"%s\"]\n", v->name, v->string, v->defstring);
560 if (developer.integer >= 100)
561 Con_DPrint("Cvar_Command: ");
563 if (v->flags & CVAR_READONLY)
565 Con_Printf("%s is read-only\n", v->name);
568 Cvar_Set (v->name, Cmd_Argv(1));
569 if (developer.integer >= 100)
575 void Cvar_UnlockDefaults (void)
578 // unlock the default values of all cvars
579 for (var = cvar_vars ; var ; var = var->next)
580 var->flags &= ~CVAR_DEFAULTSET;
584 void Cvar_LockDefaults_f (void)
587 // lock in the default values of all cvars
588 for (var = cvar_vars ; var ; var = var->next)
590 if (!(var->flags & CVAR_DEFAULTSET))
594 //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
595 var->flags |= CVAR_DEFAULTSET;
596 Z_Free(var->defstring);
597 alloclen = strlen(var->string) + 1;
598 var->defstring = (char *)Z_Malloc(alloclen);
599 memcpy(var->defstring, var->string, alloclen);
605 void Cvar_ResetToDefaults_All_f (void)
608 // restore the default values of all cvars
609 for (var = cvar_vars ; var ; var = var->next)
610 Cvar_SetQuick(var, var->defstring);
614 void Cvar_ResetToDefaults_NoSaveOnly_f (void)
617 // restore the default values of all cvars
618 for (var = cvar_vars ; var ; var = var->next)
619 if (!(var->flags & CVAR_SAVE))
620 Cvar_SetQuick(var, var->defstring);
624 void Cvar_ResetToDefaults_SaveOnly_f (void)
627 // restore the default values of all cvars
628 for (var = cvar_vars ; var ; var = var->next)
629 if (var->flags & CVAR_SAVE)
630 Cvar_SetQuick(var, var->defstring);
638 Writes lines containing "set variable value" for all variables
639 with the archive flag set to true.
642 void Cvar_WriteVariables (qfile_t *f)
646 // don't save cvars that match their default value
647 for (var = cvar_vars ; var ; var = var->next)
648 if ((var->flags & CVAR_SAVE) && (strcmp(var->string, var->defstring) || (var->flags & CVAR_ALLOCATED)))
649 FS_Printf(f, "%s%s \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", var->name, var->string);
653 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
654 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
660 void Cvar_List_f (void)
670 partial = Cmd_Argv (1);
671 len = strlen(partial);
679 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
682 for (cvar = cvar_vars; cvar; cvar = cvar->next)
684 if (len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp (partial,cvar->name,len)))
687 Con_Printf("%s is \"%s\" [\"%s\"] %s\n", cvar->name, cvar->string, cvar->defstring, cvar->description);
694 Con_Printf("%i cvar(s) matching \"%s\"\n", count, partial);
696 Con_Printf("%i cvar(s) beginning with \"%s\"\n", count, partial);
699 Con_Printf("%i cvar(s)\n", count);
701 // 2000-01-09 CvarList command by Maddes
703 void Cvar_Set_f (void)
707 // make sure it's the right number of parameters
710 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
714 // check if it's read-only
715 cvar = Cvar_FindVar(Cmd_Argv(1));
716 if (cvar && cvar->flags & CVAR_READONLY)
718 Con_Printf("Set: %s is read-only\n", cvar->name);
722 if (developer.integer >= 100)
725 // all looks ok, create/modify the cvar
726 Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), 0, Cmd_Argc() > 3 ? Cmd_Argv(3) : NULL);
729 void Cvar_SetA_f (void)
733 // make sure it's the right number of parameters
736 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
740 // check if it's read-only
741 cvar = Cvar_FindVar(Cmd_Argv(1));
742 if (cvar && cvar->flags & CVAR_READONLY)
744 Con_Printf("SetA: %s is read-only\n", cvar->name);
748 if (developer.integer >= 100)
749 Con_DPrint("SetA: ");
751 // all looks ok, create/modify the cvar
752 Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), CVAR_SAVE, Cmd_Argc() > 3 ? Cmd_Argv(3) : NULL);