]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cvar.c
only give shareware warning if running GAME_NORMAL (quake)
[xonotic/darkplaces.git] / cvar.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // cvar.c -- dynamic variable tracking
21
22 #include "quakedef.h"
23
24 cvar_t *cvar_vars = NULL;
25 char *cvar_null_string = "";
26
27 /*
28 ============
29 Cvar_FindVar
30 ============
31 */
32 cvar_t *Cvar_FindVar (const char *var_name)
33 {
34         cvar_t *var;
35
36         for (var = cvar_vars;var;var = var->next)
37                 if (!strcasecmp (var_name, var->name))
38                         return var;
39
40         return NULL;
41 }
42
43 cvar_t *Cvar_FindVarAfter (const char *prev_var_name, int neededflags)
44 {
45         cvar_t *var;
46
47         if (*prev_var_name)
48         {
49                 var = Cvar_FindVar (prev_var_name);
50                 if (!var)
51                         return NULL;
52                 var = var->next;
53         }
54         else
55                 var = cvar_vars;
56
57         // search for the next cvar matching the needed flags
58         while (var)
59         {
60                 if ((var->flags & neededflags) || !neededflags)
61                         break;
62                 var = var->next;
63         }
64         return var;
65 }
66
67 /*
68 ============
69 Cvar_VariableValue
70 ============
71 */
72 float Cvar_VariableValue (const char *var_name)
73 {
74         cvar_t *var;
75
76         var = Cvar_FindVar (var_name);
77         if (!var)
78                 return 0;
79         return atof (var->string);
80 }
81
82
83 /*
84 ============
85 Cvar_VariableString
86 ============
87 */
88 const char *Cvar_VariableString (const char *var_name)
89 {
90         cvar_t *var;
91
92         var = Cvar_FindVar (var_name);
93         if (!var)
94                 return cvar_null_string;
95         return var->string;
96 }
97
98
99 /*
100 ============
101 Cvar_CompleteVariable
102 ============
103 */
104 const char *Cvar_CompleteVariable (const char *partial)
105 {
106         cvar_t          *cvar;
107         int                     len;
108
109         len = strlen(partial);
110
111         if (!len)
112                 return NULL;
113
114 // check functions
115         for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
116                 if (!strncasecmp (partial,cvar->name, len))
117                         return cvar->name;
118
119         return NULL;
120 }
121
122
123 /*
124         CVar_CompleteCountPossible
125
126         New function for tab-completion system
127         Added by EvilTypeGuy
128         Thanks to Fett erich@heintz.com
129
130 */
131 int Cvar_CompleteCountPossible (const char *partial)
132 {
133         cvar_t  *cvar;
134         int             len;
135         int             h;
136
137         h = 0;
138         len = strlen(partial);
139
140         if (!len)
141                 return  0;
142
143         // Loop through the cvars and count all possible matches
144         for (cvar = cvar_vars; cvar; cvar = cvar->next)
145                 if (!strncasecmp(partial, cvar->name, len))
146                         h++;
147
148         return h;
149 }
150
151 /*
152         CVar_CompleteBuildList
153
154         New function for tab-completion system
155         Added by EvilTypeGuy
156         Thanks to Fett erich@heintz.com
157         Thanks to taniwha
158
159 */
160 const char **Cvar_CompleteBuildList (const char *partial)
161 {
162         const cvar_t *cvar;
163         int len = 0;
164         int bpos = 0;
165         int sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) * sizeof (const char *);
166         const char **buf;
167
168         len = strlen(partial);
169         buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (const char *));
170         // Loop through the alias list and print all matches
171         for (cvar = cvar_vars; cvar; cvar = cvar->next)
172                 if (!strncasecmp(partial, cvar->name, len))
173                         buf[bpos++] = cvar->name;
174
175         buf[bpos] = NULL;
176         return buf;
177 }
178
179 /*
180 ============
181 Cvar_Set
182 ============
183 */
184 void Cvar_SetQuick_Internal (cvar_t *var, const char *value)
185 {
186         qboolean changed;
187
188         changed = strcmp(var->string, value);
189         // LordHavoc: don't reallocate when there is no change
190         if (!changed)
191                 return;
192
193         // LordHavoc: don't reallocate when the buffer is the same size
194         if (!var->string || strlen(var->string) != strlen(value))
195         {
196                 Z_Free (var->string);   // free the old value string
197
198                 var->string = Z_Malloc (strlen(value)+1);
199         }
200         strcpy (var->string, value);
201         var->value = atof (var->string);
202         var->integer = (int) var->value;
203         if ((var->flags & CVAR_NOTIFY) && changed && sv.active)
204                 SV_BroadcastPrintf("\"%s\" changed to \"%s\"\n", var->name, var->string);
205 }
206
207 void Cvar_SetQuick (cvar_t *var, const char *value)
208 {
209         if (var == NULL)
210         {
211                 Con_Print("Cvar_SetQuick: var == NULL\n");
212                 return;
213         }
214
215         if (developer.integer)
216                 Con_Printf("Cvar_SetQuick({\"%s\", \"%s\", %i}, \"%s\");\n", var->name, var->string, var->flags, value);
217
218         Cvar_SetQuick_Internal(var, value);
219 }
220
221 void Cvar_Set (const char *var_name, const char *value)
222 {
223         cvar_t *var;
224         var = Cvar_FindVar (var_name);
225         if (var == NULL)
226         {
227                 Con_Printf("Cvar_Set: variable %s not found\n", var_name);
228                 return;
229         }
230         Cvar_SetQuick(var, value);
231 }
232
233 /*
234 ============
235 Cvar_SetValue
236 ============
237 */
238 void Cvar_SetValueQuick(cvar_t *var, float value)
239 {
240         char val[256];
241
242         if ((float)((int)value) == value)
243                 sprintf(val, "%i", (int)value);
244         else
245                 sprintf(val, "%f", value);
246         Cvar_SetQuick(var, val);
247 }
248
249 void Cvar_SetValue(const char *var_name, float value)
250 {
251         char val[256];
252
253         if ((float)((int)value) == value)
254                 sprintf(val, "%i", (int)value);
255         else
256                 sprintf(val, "%f", value);
257         Cvar_Set(var_name, val);
258 }
259
260 /*
261 ============
262 Cvar_RegisterVariable
263
264 Adds a freestanding variable to the variable list.
265 ============
266 */
267 void Cvar_RegisterVariable (cvar_t *variable)
268 {
269         cvar_t *cvar, *cvar2;
270         char *oldstr;
271
272         if (developer.integer)
273                 Con_Printf("Cvar_RegisterVariable({\"%s\", \"%s\", %i});\n", variable->name, variable->string, variable->flags);
274
275 // first check to see if it has already been defined
276         cvar = Cvar_FindVar (variable->name);
277         if (cvar)
278         {
279                 if (cvar->flags & CVAR_ALLOCATED)
280                 {
281                         if (developer.integer)
282                                 Con_Printf("...  replacing existing allocated cvar {\"%s\", \"%s\", %i}", cvar->name, cvar->string, cvar->flags);
283                         // fixed variables replace allocated ones
284                         // (because the engine directly accesses fixed variables)
285                         // NOTE: this isn't actually used currently
286                         // (all cvars are registered before config parsing)
287                         variable->flags |= (cvar->flags & ~CVAR_ALLOCATED);
288                         // cvar->string is now owned by variable instead
289                         variable->string = cvar->string;
290                         variable->value = atof (variable->string);
291                         variable->integer = (int) variable->value;
292                         // replace cvar with this one...
293                         variable->next = cvar->next;
294                         if (cvar_vars == cvar)
295                         {
296                                 // head of the list is easy to change
297                                 cvar_vars = variable;
298                         }
299                         else
300                         {
301                                 // otherwise find it somewhere in the list
302                                 for (cvar2 = cvar_vars;cvar2->next != cvar;cvar2 = cvar2->next);
303                                 if (cvar2->next == cvar)
304                                         cvar2->next = variable;
305                         }
306
307                         // get rid of old allocated cvar
308                         // (but not the cvar->string, because we kept that)
309                         Z_Free(cvar->name);
310                         Z_Free(cvar);
311                 }
312                 else
313                         Con_Printf("Can't register variable %s, already defined\n", variable->name);
314                 return;
315         }
316
317 // check for overlap with a command
318         if (Cmd_Exists (variable->name))
319         {
320                 Con_Printf("Cvar_RegisterVariable: %s is a command\n", variable->name);
321                 return;
322         }
323
324 // copy the value off, because future sets will Z_Free it
325         oldstr = variable->string;
326         variable->string = Z_Malloc (strlen(variable->string)+1);
327         strcpy (variable->string, oldstr);
328         variable->value = atof (variable->string);
329         variable->integer = (int) variable->value;
330
331 // link the variable in
332 // alphanumerical order
333         for( cvar = NULL, cvar2 = cvar_vars ; cvar2 && strcmp( cvar2->name, variable->name ) < 0 ; cvar = cvar2, cvar2 = cvar->next )
334                 ;
335         if( cvar ) {
336                 cvar->next = variable;
337         } else {
338                 cvar_vars = variable;
339         }
340         variable->next = cvar2;
341 }
342
343 /*
344 ============
345 Cvar_Get
346
347 Adds a newly allocated variable to the variable list or sets its value.
348 ============
349 */
350 cvar_t *Cvar_Get (const char *name, const char *value, int flags)
351 {
352         cvar_t *cvar;
353
354         if (developer.integer)
355                 Con_Printf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags);
356
357 // first check to see if it has already been defined
358         cvar = Cvar_FindVar (name);
359         if (cvar)
360         {
361                 cvar->flags |= flags;
362                 Cvar_SetQuick_Internal (cvar, value);
363                 return cvar;
364         }
365
366 // check for overlap with a command
367         if (Cmd_Exists (name))
368         {
369                 Con_Printf("Cvar_Get: %s is a command\n", name);
370                 return NULL;
371         }
372
373 // allocate a new cvar, cvar name, and cvar string
374 // FIXME: these never get Z_Free'd
375         cvar = Z_Malloc(sizeof(cvar_t));
376         cvar->flags = flags | CVAR_ALLOCATED;
377         cvar->name = Z_Malloc(strlen(name)+1);
378         strcpy(cvar->name, name);
379         cvar->string = Z_Malloc(strlen(value)+1);
380         strcpy(cvar->string, value);
381         cvar->value = atof (cvar->string);
382         cvar->integer = (int) cvar->value;
383
384 // link the variable in
385         cvar->next = cvar_vars;
386         cvar_vars = cvar;
387         return cvar;
388 }
389
390
391 /*
392 ============
393 Cvar_Command
394
395 Handles variable inspection and changing from the console
396 ============
397 */
398 qboolean        Cvar_Command (void)
399 {
400         cvar_t                  *v;
401
402 // check variables
403         v = Cvar_FindVar (Cmd_Argv(0));
404         if (!v)
405                 return false;
406
407 // perform a variable print or set
408         if (Cmd_Argc() == 1)
409         {
410                 Con_Printf("\"%s\" is \"%s\"\n", v->name, v->string);
411                 return true;
412         }
413
414         Con_DPrint("Cvar_Command: ");
415
416         if (v->flags & CVAR_READONLY)
417         {
418                 Con_Printf("%s is read-only\n", v->name);
419                 return true;
420         }
421         Cvar_Set (v->name, Cmd_Argv(1));
422         return true;
423 }
424
425
426 /*
427 ============
428 Cvar_WriteVariables
429
430 Writes lines containing "set variable value" for all variables
431 with the archive flag set to true.
432 ============
433 */
434 void Cvar_WriteVariables (qfile_t *f)
435 {
436         cvar_t  *var;
437
438         for (var = cvar_vars ; var ; var = var->next)
439                 if (var->flags & CVAR_SAVE)
440                         FS_Printf(f, "%s%s \"%s\"\n", var->flags & CVAR_ALLOCATED ? "seta " : "", var->name, var->string);
441 }
442
443
444 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
445 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
446 /*
447 =========
448 Cvar_List
449 =========
450 */
451 void Cvar_List_f (void)
452 {
453         cvar_t *cvar;
454         const char *partial;
455         int len, count;
456
457         if (Cmd_Argc() > 1)
458         {
459                 partial = Cmd_Argv (1);
460                 len = strlen(partial);
461         }
462         else
463         {
464                 partial = NULL;
465                 len = 0;
466         }
467
468         count = 0;
469         for (cvar = cvar_vars; cvar; cvar = cvar->next)
470         {
471                 if (partial && strncasecmp (partial,cvar->name,len))
472                         continue;
473
474                 Con_Printf("%s is \"%s\"\n", cvar->name, cvar->string);
475                 count++;
476         }
477
478         Con_Printf("%i cvar(s)", count);
479         if (partial)
480                 Con_Printf(" beginning with \"%s\"", partial);
481         Con_Print("\n");
482 }
483 // 2000-01-09 CvarList command by Maddes
484
485 void Cvar_Set_f (void)
486 {
487         cvar_t *cvar;
488
489         // make sure it's the right number of parameters
490         if (Cmd_Argc() < 3)
491         {
492                 Con_Printf("Set: wrong number of parameters, usage: set <variablename> <value>\n");
493                 return;
494         }
495
496         // check if it's read-only
497         cvar = Cvar_FindVar(Cmd_Argv(1));
498         if (cvar && cvar->flags & CVAR_READONLY)
499         {
500                 Con_Printf("Set: %s is read-only\n", cvar->name);
501                 return;
502         }
503
504         Con_DPrint("Set: ");
505
506         // all looks ok, create/modify the cvar
507         Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), 0);
508 }
509
510 void Cvar_SetA_f (void)
511 {
512         cvar_t *cvar;
513
514         // make sure it's the right number of parameters
515         if (Cmd_Argc() < 3)
516         {
517                 Con_Printf("SetA: wrong number of parameters, usage: seta <variablename> <value>\n");
518                 return;
519         }
520
521         // check if it's read-only
522         cvar = Cvar_FindVar(Cmd_Argv(1));
523         if (cvar && cvar->flags & CVAR_READONLY)
524         {
525                 Con_Printf("SetA: %s is read-only\n", cvar->name);
526                 return;
527         }
528
529         Con_DPrint("SetA: ");
530
531         // all looks ok, create/modify the cvar
532         Cvar_Get(Cmd_Argv(1), Cmd_Argv(2), CVAR_SAVE);
533 }
534
535