]> git.xonotic.org Git - xonotic/darkplaces.git/blob - cvar.c
vid: move sound start/restart out of video start/restart
[xonotic/darkplaces.git] / cvar.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3 Copyright (C) 2000-2021 DarkPlaces contributors
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 */
21 // cvar.c -- dynamic variable tracking
22
23 #include "quakedef.h"
24
25 const char *cvar_dummy_description = "custom cvar";
26 static const char *cvar_null_string = "";
27
28 cvar_state_t cvars_all;
29 cvar_state_t cvars_null;
30
31 /*
32 ============
33 Cvar_FindVar
34 ============
35 */
36 cvar_t *Cvar_FindVar(cvar_state_t *cvars, const char *var_name, int neededflags)
37 {
38         int hashindex;
39         cvar_hash_t *hash;
40
41         // use hash lookup to minimize search time
42         hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)) % CVAR_HASHSIZE;
43         for (hash = cvars->hashtable[hashindex];hash;hash = hash->next)
44                 if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
45                         return hash->cvar;
46                 else
47                         for (char **alias = hash->cvar->aliases; alias && *alias; alias++)
48                                 if (!strcmp (var_name, *alias) && (hash->cvar->flags & neededflags))
49                                         return hash->cvar;
50         return NULL;
51 }
52
53 cvar_t *Cvar_FindVarAfter(cvar_state_t *cvars, const char *prev_var_name, int neededflags)
54 {
55         cvar_t *var;
56
57         if (*prev_var_name)
58         {
59                 var = Cvar_FindVar(cvars, prev_var_name, neededflags);
60                 if (!var)
61                         return NULL;
62                 var = var->next;
63         }
64         else
65                 var = cvars->vars;
66
67         // search for the next cvar matching the needed flags
68         while (var)
69         {
70                 if (var->flags & neededflags)
71                         break;
72                 var = var->next;
73         }
74         return var;
75 }
76
77 static cvar_t *Cvar_FindVarLink(cvar_state_t *cvars, const char *var_name, cvar_t **parent, cvar_t ***link, cvar_t **prev_alpha, int neededflags)
78 {
79         int hashindex;
80         cvar_hash_t *hash;
81
82         // use hash lookup to minimize search time
83         hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)) % CVAR_HASHSIZE;
84         if(parent) *parent = NULL;
85         if(prev_alpha) *prev_alpha = NULL;
86         if(link) *link = &cvars->hashtable[hashindex]->cvar;
87         for (hash = cvars->hashtable[hashindex];hash;hash = hash->next)
88         {
89                 if (!strcmp (var_name, hash->cvar->name) && (hash->cvar->flags & neededflags))
90                         goto match;
91                 else
92                         for (char **alias = hash->cvar->aliases; alias && *alias; alias++)
93                                 if (!strcmp (var_name, *alias) && (hash->cvar->flags & neededflags))
94                                         goto match;
95                 if(parent) *parent = hash->cvar;
96         }
97         return NULL;
98 match:
99         if(!prev_alpha || hash->cvar == cvars->vars)
100                 return hash->cvar;
101
102         *prev_alpha = cvars->vars;
103         // if prev_alpha happens to become NULL then there has been some inconsistency elsewhere
104         // already - should I still insert '*prev_alpha &&' in the loop?
105         while((*prev_alpha)->next != hash->cvar)
106                 *prev_alpha = (*prev_alpha)->next;
107         return hash->cvar;
108 }
109
110 /*
111 ============
112 Cvar_VariableValue
113 ============
114 */
115 float Cvar_VariableValueOr(cvar_state_t *cvars, const char *var_name, float def, int neededflags)
116 {
117         cvar_t *var;
118
119         var = Cvar_FindVar(cvars, var_name, neededflags);
120         if (!var)
121                 return def;
122         return atof (var->string);
123 }
124
125 float Cvar_VariableValue(cvar_state_t *cvars, const char *var_name, int neededflags)
126 {
127         return Cvar_VariableValueOr(cvars, var_name, 0, neededflags);
128 }
129
130 /*
131 ============
132 Cvar_VariableString
133 ============
134 */
135 const char *Cvar_VariableStringOr(cvar_state_t *cvars, const char *var_name, const char *def, int neededflags)
136 {
137         cvar_t *var;
138
139         var = Cvar_FindVar(cvars, var_name, neededflags);
140         if (!var)
141                 return def;
142         return var->string;
143 }
144
145 const char *Cvar_VariableString(cvar_state_t *cvars, const char *var_name, int neededflags)
146 {
147         return Cvar_VariableStringOr(cvars, var_name, cvar_null_string, neededflags);
148 }
149
150 /*
151 ============
152 Cvar_VariableDefString
153 ============
154 */
155 const char *Cvar_VariableDefString(cvar_state_t *cvars, const char *var_name, int neededflags)
156 {
157         cvar_t *var;
158
159         var = Cvar_FindVar(cvars, var_name, neededflags);
160         if (!var)
161                 return cvar_null_string;
162         return var->defstring;
163 }
164
165 /*
166 ============
167 Cvar_VariableDescription
168 ============
169 */
170 const char *Cvar_VariableDescription(cvar_state_t *cvars, const char *var_name, int neededflags)
171 {
172         cvar_t *var;
173
174         var = Cvar_FindVar(cvars, var_name, neededflags);
175         if (!var)
176                 return cvar_null_string;
177         return var->description;
178 }
179
180
181 /*
182 ============
183 Cvar_CompleteVariable
184 ============
185 */
186 const char *Cvar_CompleteVariable(cvar_state_t *cvars, const char *partial, int neededflags)
187 {
188         cvar_t          *cvar;
189         size_t          len;
190
191         len = strlen(partial);
192
193         if (!len)
194                 return NULL;
195
196 // check functions
197         for (cvar=cvars->vars ; cvar ; cvar=cvar->next)
198                 if (!strncasecmp (partial,cvar->name, len) && (cvar->flags & neededflags))
199                         return cvar->name;
200
201         return NULL;
202 }
203
204
205 /*
206         CVar_CompleteCountPossible
207
208         New function for tab-completion system
209         Added by EvilTypeGuy
210         Thanks to Fett erich@heintz.com
211
212 */
213 int Cvar_CompleteCountPossible(cvar_state_t *cvars, const char *partial, int neededflags)
214 {
215         cvar_t  *cvar;
216         size_t  len;
217         int             h;
218
219         h = 0;
220         len = strlen(partial);
221
222         if (!len)
223                 return  0;
224
225         // Loop through the cvars and count all possible matches
226         for (cvar = cvars->vars; cvar; cvar = cvar->next)
227                 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
228                         h++;
229                 else
230                         for (char **alias = cvar->aliases; alias && *alias; alias++)
231                                 if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
232                                         h++;
233                 
234         return h;
235 }
236
237 /*
238         CVar_CompleteBuildList
239
240         New function for tab-completion system
241         Added by EvilTypeGuy
242         Thanks to Fett erich@heintz.com
243         Thanks to taniwha
244
245 */
246 const char **Cvar_CompleteBuildList(cvar_state_t *cvars, const char *partial, int neededflags)
247 {
248         const cvar_t *cvar;
249         size_t len = 0;
250         size_t bpos = 0;
251         size_t sizeofbuf = (Cvar_CompleteCountPossible(cvars, partial, neededflags) + 1) * sizeof(const char *);
252         const char **buf;
253
254         len = strlen(partial);
255         buf = (const char **)Mem_Alloc(tempmempool, sizeofbuf + sizeof(const char *));
256         // Loop through the alias list and print all matches
257         for (cvar = cvars->vars; cvar; cvar = cvar->next)
258                 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
259                         buf[bpos++] = cvar->name;
260                 else
261                         for (char **alias = cvar->aliases; alias && *alias; alias++)
262                                 if (!strncasecmp(partial, *alias, len) && (cvar->flags & neededflags))
263                                         buf[bpos++] = *alias;
264                 
265
266         buf[bpos] = NULL;
267         return buf;
268 }
269
270 void Cvar_PrintHelp(cvar_t *cvar, const char *name, qbool full)
271 {
272         // Aliases are purple, cvars are yellow
273         if (strcmp(cvar->name, name))
274                 Con_Printf("^6%s^7 (alias of ^3%s^7)", name, cvar->name);
275         else
276                 Con_Printf("^3%s^7", name);
277         Con_Printf(" is \"%s^7\" [\"%s^7\"]", ((cvar->flags & CF_PRIVATE) ? "********"/*hunter2*/ : cvar->string), cvar->defstring);
278         if (full)
279                 Con_Printf(" %s", cvar->description);
280         Con_Print("\n");
281 }
282
283 // written by LadyHavoc
284 void Cvar_CompleteCvarPrint(cvar_state_t *cvars, const char *partial, int neededflags)
285 {
286         cvar_t *cvar;
287         size_t len = strlen(partial);
288         // Loop through the command list and print all matches
289         for (cvar = cvars->vars; cvar; cvar = cvar->next)
290                 if (!strncasecmp(partial, cvar->name, len) && (cvar->flags & neededflags))
291                         Cvar_PrintHelp(cvar, cvar->name, true);
292                 else
293                         for (char **alias = cvar->aliases; alias && *alias; alias++)
294                                 if (!strncasecmp (partial, *alias, len) && (cvar->flags & neededflags))
295                                         Cvar_PrintHelp(cvar, *alias, true);
296
297                 
298 }
299
300 // check if a cvar is held by some progs
301 static qbool Cvar_IsAutoCvar(cvar_t *var)
302 {
303         int i;
304         prvm_prog_t *prog;
305         for (i = 0;i < PRVM_PROG_MAX;i++)
306         {
307                 prog = &prvm_prog_list[i];
308                 if (prog->loaded && var->globaldefindex[i] >= 0)
309                         return true;
310         }
311         return false;
312 }
313
314 // we assume that prog is already set to the target progs
315 static void Cvar_UpdateAutoCvar(cvar_t *var)
316 {
317         int i;
318         int j;
319         const char *s;
320         vec3_t v;
321         prvm_prog_t *prog;
322         for (i = 0;i < PRVM_PROG_MAX;i++)
323         {
324                 prog = &prvm_prog_list[i];
325                 if (prog->loaded && var->globaldefindex[i] >= 0)
326                 {
327                         // MUST BE SYNCED WITH prvm_edict.c PRVM_LoadProgs
328                         switch(prog->globaldefs[var->globaldefindex[i]].type & ~DEF_SAVEGLOBAL)
329                         {
330                         case ev_float:
331                                 PRVM_GLOBALFIELDFLOAT(prog->globaldefs[var->globaldefindex[i]].ofs) = var->value;
332                                 break;
333                         case ev_vector:
334                                 s = var->string;
335                                 VectorClear(v);
336                                 for (j = 0;j < 3;j++)
337                                 {
338                                         while (*s && ISWHITESPACE(*s))
339                                                 s++;
340                                         if (!*s)
341                                                 break;
342                                         v[j] = atof(s);
343                                         while (!ISWHITESPACE(*s))
344                                                 s++;
345                                         if (!*s)
346                                                 break;
347                                 }
348                                 VectorCopy(v, PRVM_GLOBALFIELDVECTOR(prog->globaldefs[var->globaldefindex[i]].ofs));
349                                 break;
350                         case ev_string:
351                                 PRVM_ChangeEngineString(prog, var->globaldefindex_stringno[i], var->string);
352                                 PRVM_GLOBALFIELDSTRING(prog->globaldefs[var->globaldefindex[i]].ofs) = var->globaldefindex_stringno[i];
353                                 break;
354                         }
355                 }
356         }
357 }
358
359 // called after loading a savegame
360 void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars)
361 {
362         cvar_t *var;
363         for (var = cvars->vars ; var ; var = var->next)
364                 Cvar_UpdateAutoCvar(var);
365 }
366
367 void Cvar_Callback(cvar_t *var)
368 {
369         if (var == NULL)
370         {
371                 Con_Print("Cvar_Callback: var == NULL\n");
372                 return;
373         }
374
375         if(var->callback)
376                 var->callback(var);
377 }
378
379 /*
380 ============
381 Cvar_Set
382 ============
383 */
384 extern cvar_t sv_disablenotify;
385 static void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
386 {
387         qbool changed;
388         size_t valuelen;
389
390         changed = strcmp(var->string, value) != 0;
391         // LadyHavoc: don't reallocate when there is no change
392         if (!changed)
393                 goto cvar_callback;
394
395         // LadyHavoc: don't reallocate when the buffer is the same size
396         valuelen = strlen(value);
397         if (!var->string || strlen(var->string) != valuelen)
398         {
399                 Z_Free ((char *)var->string);   // free the old value string
400
401                 var->string = (char *)Z_Malloc (valuelen + 1);
402         }
403         memcpy ((char *)var->string, value, valuelen + 1);
404         var->value = atof (var->string);
405         var->integer = (int) var->value;
406         if ((var->flags & CF_NOTIFY) && sv.active && !sv_disablenotify.integer)
407                 SV_BroadcastPrintf("\003^3Server cvar \"%s\" changed to \"%s\"\n", var->name, var->string);
408 #if 0
409         // TODO: add infostring support to the server?
410         if ((var->flags & CF_SERVERINFO) && changed && sv.active)
411         {
412                 InfoString_SetValue(svs.serverinfo, sizeof(svs.serverinfo), var->name, var->string);
413                 if (sv.active)
414                 {
415                         MSG_WriteByte (&sv.reliable_datagram, svc_serverinfostring);
416                         MSG_WriteString (&sv.reliable_datagram, var->name);
417                         MSG_WriteString (&sv.reliable_datagram, var->string);
418                 }
419         }
420 #endif
421         if (var->flags & CF_USERINFO)
422                 CL_SetInfo(var->name, var->string, true, false, false, false);
423
424         Cvar_UpdateAutoCvar(var);
425
426 cvar_callback:
427         // Call the function stored in the cvar for bounds checking, cleanup, etc
428         Cvar_Callback(var);
429 }
430
431 void Cvar_SetQuick (cvar_t *var, const char *value)
432 {
433         if (var == NULL)
434         {
435                 Con_Print("Cvar_SetQuick: var == NULL\n");
436                 return;
437         }
438
439         if (!(var->flags & CF_REGISTERED) && !(var->flags & CF_ALLOCATED))
440         {
441                 Con_Printf(CON_WARN "Warning: Cvar_SetQuick() cannot set unregistered cvar \"%s\"\n", var->name);
442                 return; // setting an unregistered engine cvar crashes
443         }
444
445         if (developer_extra.integer)
446                 Con_DPrintf("Cvar_SetQuick({\"%s\", \"%s\", %i, \"%s\"}, \"%s\");\n", var->name, var->string, var->flags, var->defstring, value);
447
448         Cvar_SetQuick_Internal(var, value);
449 }
450
451 void Cvar_Set(cvar_state_t *cvars, const char *var_name, const char *value)
452 {
453         cvar_t *var;
454         var = Cvar_FindVar(cvars, var_name, ~0);
455         if (var == NULL)
456         {
457                 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
458                 return;
459         }
460         Cvar_SetQuick(var, value);
461 }
462
463 /*
464 ============
465 Cvar_SetValue
466 ============
467 */
468 void Cvar_SetValueQuick(cvar_t *var, float value)
469 {
470         char val[MAX_INPUTLINE];
471
472         if ((float)((int)value) == value)
473                 dpsnprintf(val, sizeof(val), "%i", (int)value);
474         else
475                 dpsnprintf(val, sizeof(val), "%f", value);
476         Cvar_SetQuick(var, val);
477 }
478
479 void Cvar_SetValue(cvar_state_t *cvars, const char *var_name, float value)
480 {
481         char val[MAX_INPUTLINE];
482
483         if ((float)((int)value) == value)
484                 dpsnprintf(val, sizeof(val), "%i", (int)value);
485         else
486                 dpsnprintf(val, sizeof(val), "%f", value);
487         Cvar_Set(cvars, var_name, val);
488 }
489
490 void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *))
491 {
492         if (variable == NULL)
493         {
494                 Con_Print("Cvar_RegisterCallback: var == NULL\n");
495                 return;
496         }
497         variable->callback = callback;
498 }
499
500 void Cvar_RegisterVirtual(cvar_t *variable, const char *name )
501 {
502         cvar_state_t *cvars = &cvars_all;
503         cvar_hash_t *hash;
504         int hashindex;
505
506         if (cls.state == ca_dedicated && !(variable->flags & CF_SERVER))
507                 return;
508
509         if(!*name)
510         {
511                 Con_Printf(CON_WARN "Cvar_RegisterVirtual: invalid virtual cvar name\n");
512                 return;
513         }
514
515         // check for overlap with a command
516         if (Cmd_Exists(cmd_local, name))
517         {
518                 Con_Printf(CON_WARN "Cvar_RegisterVirtual: %s is a command\n", name);
519                 return;
520         }
521
522         if(Cvar_FindVar(&cvars_all, name, 0))
523         {
524                 Con_Printf(CON_WARN "Cvar_RegisterVirtual: %s is a cvar\n", name);
525                 return;
526         }
527
528         // Resize the variable->aliases list to have room for another entry and a null terminator.
529         // This zero-pads when resizing, so we don't need to write the NULL terminator manually here.
530         // Also if aliases is NULL this allocates fresh for the correct size, so it's fine to just do this.
531         variable->aliases = (char **)Z_Realloc(variable->aliases, sizeof(char *) * (variable->aliases_size + 2));
532         // Add the new alias, and increment the number of aliases in the list
533         variable->aliases[variable->aliases_size++] = (char *)Z_strdup(name);
534
535         // link to head of list in this hash table index
536         hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
537         hashindex = CRC_Block((const unsigned char *)name, strlen(name)) % CVAR_HASHSIZE;
538         hash->next = cvars->hashtable[hashindex];
539         cvars->hashtable[hashindex] = hash;
540         hash->cvar = variable;
541 }
542
543 /*
544 ============
545 Cvar_Link
546
547 Links a variable to the variable list and hashtable
548 ============
549 */
550 static void Cvar_Link(cvar_t *variable, cvar_state_t *cvars)
551 {
552         cvar_t *current, *next;
553         cvar_hash_t *hash;
554         int hashindex;
555         /*
556          * Link the variable in
557          * alphanumerical order
558          */
559         for( current = NULL, next = cvars->vars ; next && strcmp( next->name, variable->name ) < 0 ; current = next, next = next->next )
560                 ;
561         if(current)
562                 current->next = variable;
563         else
564                 cvars->vars = variable;
565         variable->next = next;
566
567         // link to head of list in this hash table index
568         hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t));
569         hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)) % CVAR_HASHSIZE;
570         hash->next = cvars->hashtable[hashindex];
571         hash->cvar = variable;
572         cvars->hashtable[hashindex] = hash;
573 }
574
575 /*
576 ============
577 Cvar_RegisterVariable
578
579 Adds a freestanding variable to the variable list.
580 ============
581 */
582 void Cvar_RegisterVariable (cvar_t *variable)
583 {
584         cvar_state_t *cvars = NULL;
585         cvar_t *current, *cvar;
586         int i;
587
588         switch (variable->flags & (CF_CLIENT | CF_SERVER))
589         {
590         case CF_CLIENT: // client-only cvar
591                 if (cls.state == ca_dedicated)
592                         return;
593         case CF_SERVER:
594         case CF_CLIENT | CF_SERVER:
595                 cvars = &cvars_all;
596                 break;
597         case 0:
598                 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with no CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
599                 break;
600         default:
601                 Sys_Error("Cvar_RegisterVariable({\"%s\", \"%s\", %i}) with weird CF_CLIENT | CF_SERVER flags\n", variable->name, variable->string, variable->flags);
602                 break;
603         }
604
605         if (developer_extra.integer)
606                 Con_DPrintf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
607
608         // first check to see if it has already been defined
609         cvar = Cvar_FindVar(cvars, variable->name, ~0);
610         if (cvar)
611         {
612                 if (cvar->flags & CF_ALLOCATED)
613                 {
614                         if (developer_extra.integer)
615                                 Con_DPrintf("...  replacing existing allocated cvar {\"%s\", \"%s\", %i}\n", cvar->name, cvar->string, cvar->flags);
616                         // fixed variables replace allocated ones
617                         // (because the engine directly accesses fixed variables)
618                         // NOTE: this isn't actually used currently
619                         // (all cvars are registered before config parsing)
620                         variable->flags &= ~CF_ALLOCATED;
621                         // cvar->string is now owned by variable instead
622                         variable->string = cvar->string;
623                         variable->defstring = cvar->defstring;
624                         variable->value = atof (variable->string);
625                         variable->integer = (int) variable->value;
626                         // Preserve autocvar status.
627                         memcpy(variable->globaldefindex, cvar->globaldefindex, sizeof(variable->globaldefindex));
628                         memcpy(variable->globaldefindex_stringno, cvar->globaldefindex_stringno, sizeof(variable->globaldefindex_stringno));
629                         // replace cvar with this one...
630                         variable->next = cvar->next;
631                         if (cvars->vars == cvar)
632                         {
633                                 // head of the list is easy to change
634                                 cvars->vars = variable;
635                         }
636                         else
637                         {
638                                 // otherwise find it somewhere in the list
639                                 for (current = cvars->vars;current->next != cvar;current = current->next)
640                                         ;
641                                 current->next = variable;
642                         }
643
644                         // get rid of old allocated cvar
645                         // (but not cvar->string and cvar->defstring, because we kept those)
646                         Z_Free((char *)cvar->name);
647                         Z_Free(cvar);
648                 }
649                 else
650                         Con_DPrintf("Can't register variable %s, already defined\n", variable->name);
651                 return;
652         }
653
654         // check for overlap with a command
655         if (Cmd_Exists(cmd_local, variable->name))
656         {
657                 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
658                 return;
659         }
660
661         // copy the value off, because future sets will Z_Free it
662         variable->name = (char *)Mem_strdup(zonemempool, variable->name);
663         variable->string = (char *)Mem_strdup(zonemempool, variable->string);
664         variable->defstring = (char *)Mem_strdup(zonemempool, variable->string);
665         variable->value = atof (variable->string);
666         variable->integer = (int) variable->value;
667         variable->aliases = NULL;
668         variable->aliases_size = 0;
669         variable->initstate = NULL;
670
671         // Mark it as not an autocvar.
672         for (i = 0;i < PRVM_PROG_MAX;i++)
673                 variable->globaldefindex[i] = -1;
674
675         // Safe for Cvar_SetQuick()
676         variable->flags |= CF_REGISTERED;
677
678         Cvar_Link(variable, cvars);
679 }
680
681 /*
682 ============
683 Cvar_Get
684
685 Adds a newly allocated variable to the variable list or sets its value.
686 ============
687 */
688 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int flags, const char *newdescription)
689 {
690         cvar_t *cvar;
691         int i;
692
693         if (developer_extra.integer)
694                 Con_DPrintf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
695
696         // first check to see if it has already been defined
697         cvar = Cvar_FindVar(cvars, name, ~0);
698         if (cvar)
699         {
700                 cvar->flags |= flags;
701                 Cvar_SetQuick_Internal (cvar, value);
702                 if(newdescription && (cvar->flags & CF_ALLOCATED))
703                 {
704                         if(cvar->description != cvar_dummy_description)
705                                 Z_Free((char *)cvar->description);
706
707                         if(*newdescription)
708                                 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
709                         else
710                                 cvar->description = cvar_dummy_description;
711                 }
712                 return cvar;
713         }
714
715         // check for pure evil
716         if (!*name)
717         {
718                 Con_Printf("Cvar_Get: invalid variable name\n");
719                 return NULL;
720         }
721
722         // check for overlap with a command
723         if (Cmd_Exists(cmd_local, name))
724         {
725                 Con_Printf("Cvar_Get: %s is a command\n", name);
726                 return NULL;
727         }
728
729         // allocate a new cvar, cvar name, and cvar string
730         // TODO: factorize the following code with the one at the end of Cvar_RegisterVariable()
731         // FIXME: these never get Z_Free'd
732         cvar = (cvar_t *)Z_Malloc(sizeof(cvar_t));
733         cvar->flags = flags | CF_ALLOCATED;
734         cvar->name = (char *)Mem_strdup(zonemempool, name);
735         cvar->string = (char *)Mem_strdup(zonemempool, value);
736         cvar->defstring = (char *)Mem_strdup(zonemempool, value);
737         cvar->value = atof (cvar->string);
738         cvar->integer = (int) cvar->value;
739         cvar->aliases = NULL;
740         cvar->aliases_size = 0;
741         cvar->initstate = NULL;
742
743         if(newdescription && *newdescription)
744                 cvar->description = (char *)Mem_strdup(zonemempool, newdescription);
745         else
746                 cvar->description = cvar_dummy_description; // actually checked by VM_cvar_type
747
748         // Mark it as not an autocvar.
749         for (i = 0;i < PRVM_PROG_MAX;i++)
750                 cvar->globaldefindex[i] = -1;
751
752         Cvar_Link(cvar, cvars);
753
754         return cvar;
755 }
756
757 qbool Cvar_Readonly (cvar_t *var, const char *cmd_name)
758 {
759         if (var->flags & CF_READONLY)
760         {
761                 if(cmd_name)
762                         Con_Printf("%s: ",cmd_name);
763                 Con_Printf("%s", var->name);
764                 Con_Printf(" is read-only\n");
765                 return true;
766         }
767         return false;
768 }
769
770 /*
771 ============
772 Cvar_Command
773
774 Handles variable inspection and changing from the console
775 ============
776 */
777 qbool   Cvar_Command (cmd_state_t *cmd)
778 {
779         cvar_state_t    *cvars = cmd->cvars;
780         cvar_t                  *v;
781
782         // check variables
783         v = Cvar_FindVar(cvars, Cmd_Argv(cmd, 0), (cmd->cvars_flagsmask));
784         if (!v)
785                 return false;
786
787         // perform a variable print or set
788         if (Cmd_Argc(cmd) == 1)
789         {
790                 Cvar_PrintHelp(v, Cmd_Argv(cmd, 0), true);
791                 return true;
792         }
793
794         if (developer_extra.integer)
795                 Con_DPrint("Cvar_Command: ");
796
797         if(Cvar_Readonly(v, NULL))
798                 return true;
799
800         Cvar_SetQuick(v, Cmd_Argv(cmd, 1));
801         if (developer_extra.integer)
802                 Con_DPrint("\n");
803         return true;
804 }
805
806 void Cvar_UnlockDefaults(cmd_state_t *cmd)
807 {
808         cvar_state_t *cvars = cmd->cvars;
809         cvar_t *var;
810         // unlock the default values of all cvars
811         for (var = cvars->vars ; var ; var = var->next)
812                 var->flags &= ~CF_DEFAULTSET;
813 }
814
815 void Cvar_LockDefaults_f(cmd_state_t *cmd)
816 {
817         cvar_state_t *cvars = cmd->cvars;
818         cvar_t *var;
819         // lock in the default values of all cvars
820         for (var = cvars->vars ; var ; var = var->next)
821         {
822                 if (!(var->flags & CF_DEFAULTSET))
823                 {
824                         size_t alloclen;
825
826                         //Con_Printf("locking cvar %s (%s -> %s)\n", var->name, var->string, var->defstring);
827                         var->flags |= CF_DEFAULTSET;
828                         Z_Free((char *)var->defstring);
829                         alloclen = strlen(var->string) + 1;
830                         var->defstring = (char *)Z_Malloc(alloclen);
831                         memcpy((char *)var->defstring, var->string, alloclen);
832                 }
833         }
834 }
835
836 void Cvar_SaveInitState(cvar_state_t *cvars)
837 {
838         cvar_t *c;
839         for (c = cvars->vars;c;c = c->next)
840         {
841                 c->initstate = (cvar_t *)Z_Malloc(sizeof(cvar_t));
842                 memcpy(c->initstate, c, sizeof(cvar_t));
843         }
844 }
845
846 void Cvar_RestoreInitState(cvar_state_t *cvars)
847 {
848         int hashindex;
849         cvar_t *c, **cp;
850         cvar_t *c2, **cp2;
851         for (cp = &cvars->vars;(c = *cp);)
852         {
853                 if (c->initstate)
854                 {
855                         // restore this cvar, it existed at init
856                         if (((c->flags ^ c->initstate->flags) & CF_MAXFLAGSVAL)
857                          || strcmp(c->defstring ? c->defstring : "", c->initstate->defstring ? c->initstate->defstring : "")
858                          || strcmp(c->string ? c->string : "", c->initstate->string ? c->initstate->string : ""))
859                         {
860                                 Con_DPrintf("Cvar_RestoreInitState: Restoring cvar \"%s\"\n", c->name);
861                                 if (c->defstring)
862                                         Z_Free((char *)c->defstring);
863                                 c->defstring = Mem_strdup(zonemempool, c->initstate->defstring);
864                                 if (c->string)
865                                         Z_Free((char *)c->string);
866                                 c->string = Mem_strdup(zonemempool, c->initstate->string);
867                         }
868                         c->flags = c->initstate->flags;
869                         c->value = c->initstate->value;
870                         c->integer = c->initstate->integer;
871                         VectorCopy(c->initstate->vector, c->vector);
872                         cp = &c->next;
873                 }
874                 else
875                 {
876                         if (!(c->flags & CF_ALLOCATED))
877                         {
878                                 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it was registered after init!\n", c->name);
879                                 // In this case, at least reset it to the default.
880                                 if((c->flags & CF_PERSISTENT) == 0)
881                                         Cvar_SetQuick(c, c->defstring);
882                                 cp = &c->next;
883                                 continue;
884                         }
885                         if (Cvar_IsAutoCvar(c))
886                         {
887                                 Con_DPrintf("Cvar_RestoreInitState: Unable to destroy cvar \"%s\", it is an autocvar used by running progs!\n", c->name);
888                                 // In this case, at least reset it to the default.
889                                 if((c->flags & CF_PERSISTENT) == 0)
890                                         Cvar_SetQuick(c, c->defstring);
891                                 cp = &c->next;
892                                 continue;
893                         }
894                         // remove this cvar, it did not exist at init
895                         Con_DPrintf("Cvar_RestoreInitState: Destroying cvar \"%s\"\n", c->name);
896                         // unlink struct from hash
897                         hashindex = CRC_Block((const unsigned char *)c->name, strlen(c->name)) % CVAR_HASHSIZE;
898                         for (cp2 = &cvars->hashtable[hashindex]->cvar;(c2 = *cp2);)
899                         {
900                                 if (c2 == c)
901                                 {
902                                         *cp2 = cvars->hashtable[hashindex]->next->cvar;
903                                         break;
904                                 }
905                                 else
906                                         cp2 = &cvars->hashtable[hashindex]->next->cvar;
907                         }
908                         // unlink struct from main list
909                         *cp = c->next;
910                         // free strings
911                         if (c->defstring)
912                                 Z_Free((char *)c->defstring);
913                         if (c->string)
914                                 Z_Free((char *)c->string);
915                         if (c->description && c->description != cvar_dummy_description)
916                                 Z_Free((char *)c->description);
917                         // free struct
918                         Z_Free(c);
919                 }
920         }
921 }
922
923 void Cvar_ResetToDefaults_All_f(cmd_state_t *cmd)
924 {
925         cvar_state_t *cvars = cmd->cvars;
926         cvar_t *var;
927         // restore the default values of all cvars
928         for (var = cvars->vars ; var ; var = var->next)
929         {
930                 if((var->flags & CF_PERSISTENT) == 0)
931                         Cvar_SetQuick(var, var->defstring);
932         }
933 }
934
935 void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd)
936 {
937         cvar_state_t *cvars = cmd->cvars;
938         cvar_t *var;
939         // restore the default values of all cvars
940         for (var = cvars->vars ; var ; var = var->next)
941         {
942                 if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == 0)
943                         Cvar_SetQuick(var, var->defstring);
944         }
945 }
946
947
948 void Cvar_ResetToDefaults_SaveOnly_f(cmd_state_t *cmd)
949 {
950         cvar_state_t *cvars = cmd->cvars;
951         cvar_t *var;
952         // restore the default values of all cvars
953         for (var = cvars->vars ; var ; var = var->next)
954         {
955                 if ((var->flags & (CF_PERSISTENT | CF_ARCHIVE)) == CF_ARCHIVE)
956                         Cvar_SetQuick(var, var->defstring);
957         }
958 }
959
960 /*
961 ============
962 Cvar_WriteVariables
963
964 Writes lines containing "set variable value" for all variables
965 with the archive flag set to true.
966 ============
967 */
968 void Cvar_WriteVariables (cvar_state_t *cvars, qfile_t *f)
969 {
970         cvar_t  *var;
971         char buf1[MAX_INPUTLINE], buf2[MAX_INPUTLINE];
972
973         // don't save cvars that match their default value
974         for (var = cvars->vars ; var ; var = var->next) {
975                 if ((var->flags & CF_ARCHIVE) && (strcmp(var->string, var->defstring) || ((var->flags & CF_ALLOCATED) && !(var->flags & CF_DEFAULTSET))))
976                 {
977                         Cmd_QuoteString(buf1, sizeof(buf1), var->name, "\"\\$", false);
978                         Cmd_QuoteString(buf2, sizeof(buf2), var->string, "\"\\$", false);
979                         FS_Printf(f, "%s\"%s\" \"%s\"\n", var->flags & CF_ALLOCATED ? "seta " : "", buf1, buf2);
980                 }
981         }
982 }
983
984 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
985 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
986 /*
987 =========
988 Cvar_List
989 =========
990 */
991 void Cvar_List_f(cmd_state_t *cmd)
992 {
993         cvar_state_t *cvars = cmd->cvars;
994         cvar_t *cvar;
995         const char *partial;
996         int count;
997         qbool ispattern;
998         char vabuf[1024];
999
1000         if (Cmd_Argc(cmd) > 1)
1001         {
1002                 partial = Cmd_Argv(cmd, 1);
1003                 ispattern = (strchr(partial, '*') || strchr(partial, '?'));
1004                 if(!ispattern)
1005                         partial = va(vabuf, sizeof(vabuf), "%s*", partial);
1006         }
1007         else
1008         {
1009                 partial = va(vabuf, sizeof(vabuf), "*");
1010                 ispattern = false;
1011         }
1012
1013         count = 0;
1014         for (cvar = cvars->vars; cvar; cvar = cvar->next)
1015         {
1016                 if (matchpattern_with_separator(cvar->name, partial, false, "", false))
1017                 {
1018                         Cvar_PrintHelp(cvar, cvar->name, true);
1019                         count++;
1020                 }
1021                 for (char **alias = cvar->aliases; alias && *alias; alias++)
1022                 {
1023                         if (matchpattern_with_separator(*alias, partial, false, "", false))
1024                         {
1025                                 Cvar_PrintHelp(cvar, *alias, true);
1026                                 count++;
1027                         }
1028                 }
1029         }
1030
1031         if (Cmd_Argc(cmd) > 1)
1032         {
1033                 if(ispattern)
1034                         Con_Printf("%i cvar%s matching \"%s\"\n", count, (count > 1) ? "s" : "", partial);
1035                 else
1036                         Con_Printf("%i cvar%s beginning with \"%s\"\n", count, (count > 1) ? "s" : "", Cmd_Argv(cmd,1));
1037         }
1038         else
1039                 Con_Printf("%i cvar(s)\n", count);
1040 }
1041 // 2000-01-09 CvarList command by Maddes
1042
1043 void Cvar_Set_f(cmd_state_t *cmd)
1044 {
1045         cvar_state_t *cvars = cmd->cvars;
1046         cvar_t *cvar;
1047
1048         // make sure it's the right number of parameters
1049         if (Cmd_Argc(cmd) < 3)
1050         {
1051                 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value> [<description>]\n");
1052                 return;
1053         }
1054
1055         // check if it's read-only
1056         cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1057         if (cvar)
1058                 if(Cvar_Readonly(cvar,"Set"))
1059                         return;
1060
1061         if (developer_extra.integer)
1062                 Con_DPrint("Set: ");
1063
1064         // all looks ok, create/modify the cvar
1065         Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1066 }
1067
1068 void Cvar_SetA_f(cmd_state_t *cmd)
1069 {
1070         cvar_state_t *cvars = cmd->cvars;
1071         cvar_t *cvar;
1072
1073         // make sure it's the right number of parameters
1074         if (Cmd_Argc(cmd) < 3)
1075         {
1076                 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value> [<description>]\n");
1077                 return;
1078         }
1079
1080         // check if it's read-only
1081         cvar = Cvar_FindVar(cvars, Cmd_Argv(cmd, 1), ~0);
1082         if (cvar)
1083                 if(Cvar_Readonly(cvar,"SetA"))
1084                         return;
1085
1086         if (developer_extra.integer)
1087                 Con_DPrint("SetA: ");
1088
1089         // all looks ok, create/modify the cvar
1090         Cvar_Get(cvars, Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), cmd->cvars_flagsmask | CF_ARCHIVE, Cmd_Argc(cmd) > 3 ? Cmd_Argv(cmd, 3) : NULL);
1091 }
1092
1093 void Cvar_Del_f(cmd_state_t *cmd)
1094 {
1095         cvar_state_t *cvars = cmd->cvars;
1096         int neededflags = ~0;
1097         int i;
1098         cvar_t *parent, **link;
1099         cvar_t *cvar, *prev;
1100
1101         if(Cmd_Argc(cmd) < 2)
1102         {
1103                 Con_Printf("%s: wrong number of parameters, usage: unset <variablename1> [<variablename2> ...]\n", Cmd_Argv(cmd, 0));
1104                 return;
1105         }
1106         for(i = 1; i < Cmd_Argc(cmd); ++i)
1107         {
1108                 cvar = Cvar_FindVarLink(cvars, Cmd_Argv(cmd, i), &parent, &link, &prev, neededflags);
1109
1110                 if(!cvar)
1111                 {
1112                         Con_Printf("%s: %s is not defined\n", Cmd_Argv(cmd, 0), Cmd_Argv(cmd, i));
1113                         continue;
1114                 }
1115                 if(Cvar_Readonly(cvar, Cmd_Argv(cmd, 0)))
1116                         continue;
1117                 if(!(cvar->flags & CF_ALLOCATED))
1118                 {
1119                         Con_Printf("%s: %s is static and cannot be deleted\n", Cmd_Argv(cmd, 0), cvar->name);
1120                         continue;
1121                 }
1122                 if(cvar == cvars->vars)
1123                 {
1124                         cvars->vars = cvar->next;
1125                 }
1126                 else
1127                 {
1128                         // in this case, prev must be set, otherwise there has been some inconsistensy
1129                         // elsewhere already... should I still check for prev != NULL?
1130                         prev->next = cvar->next;
1131                 }
1132
1133                 if(parent)
1134                         parent->next = cvar->next;
1135                 else if(link)
1136                         *link = cvar->next;
1137                 if(cvar->description != cvar_dummy_description)
1138                         Z_Free((char *)cvar->description);
1139
1140                 Z_Free((char *)cvar->name);
1141                 Z_Free((char *)cvar->string);
1142                 Z_Free((char *)cvar->defstring);
1143                 Z_Free(cvar);
1144         }
1145 }
1146
1147 #ifdef FILLALLCVARSWITHRUBBISH
1148 void Cvar_FillAll_f(cmd_state_t *cmd)
1149 {
1150         char *buf, *p, *q;
1151         int n, i;
1152         cvar_t *var;
1153         qbool verify;
1154         if(Cmd_Argc(cmd) != 2)
1155         {
1156                 Con_Printf("Usage: %s length to plant rubbish\n", Cmd_Argv(cmd, 0));
1157                 Con_Printf("Usage: %s -length to verify that the rubbish is still there\n", Cmd_Argv(cmd, 0));
1158                 return;
1159         }
1160         n = atoi(Cmd_Argv(cmd, 1));
1161         verify = (n < 0);
1162         if(verify)
1163                 n = -n;
1164         buf = Z_Malloc(n + 1);
1165         buf[n] = 0;
1166         for(var = cvars->vars; var; var = var->next)
1167         {
1168                 for(i = 0, p = buf, q = var->name; i < n; ++i)
1169                 {
1170                         *p++ = *q++;
1171                         if(!*q)
1172                                 q = var->name;
1173                 }
1174                 if(verify && strcmp(var->string, buf))
1175                 {
1176                         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);
1177                 }
1178                 Cvar_SetQuick(var, buf);
1179         }
1180         Z_Free(buf);
1181 }
1182 #endif /* FILLALLCVARSWITHRUBBISH */