-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvar.c
527 lines (449 loc) · 10.4 KB
/
cvar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// cvar.c -- dynamic variable tracking
#include "quakedef.h"
cvar_t *cvar_vars;
char *cvar_null_string = "";
//==============================================================================
//
// USER COMMANDS
//
//==============================================================================
/*
============
Cvar_List_f
============
*/
void Cvar_List_f (void)
{
cvar_t *cvar;
char *partial;
int len, count;
if (Cmd_Argc() > 1)
{
partial = Cmd_Argv (1);
len = strlen(partial);
}
else
{
partial = NULL;
len = 0;
}
count=0;
for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
{
if (partial && strncmp (partial,cvar->name, len))
{
continue;
}
Con_SafePrintf ("%s%s %s \"%s\"\n",
cvar->archive ? "*" : " ",
cvar->server ? "s" : " ",
cvar->name,
cvar->string);
count++;
}
Con_SafePrintf ("%i cvars", count);
if (partial)
{
Con_SafePrintf (" beginning with \"%s\"", partial);
}
Con_SafePrintf ("\n");
}
/*
============
Cvar_Inc_f
============
*/
void Cvar_Inc_f (void)
{
switch (Cmd_Argc())
{
default:
case 1:
Con_Printf("inc <cvar> [amount] : increment cvar\n");
break;
case 2:
Cvar_SetValue (Cmd_Argv(1), Cvar_VariableValue(Cmd_Argv(1)) + 1);
break;
case 3:
Cvar_SetValue (Cmd_Argv(1), Cvar_VariableValue(Cmd_Argv(1)) + atof(Cmd_Argv(2)));
break;
}
}
/*
============
Cvar_Dec_f
============
*/
void Cvar_Dec_f (void)
{
switch (Cmd_Argc())
{
default:
case 1:
Con_Printf("dec <cvar> [amount] : decrement cvar\n");
break;
case 2:
Cvar_SetValue (Cmd_Argv(1), Cvar_VariableValue(Cmd_Argv(1)) - 1);
break;
case 3:
Cvar_SetValue (Cmd_Argv(1), Cvar_VariableValue(Cmd_Argv(1)) - atof(Cmd_Argv(2)));
break;
}
}
/*
============
Cvar_Toggle_f
============
*/
void Cvar_Toggle_f (void)
{
switch (Cmd_Argc())
{
default:
case 1:
Con_Printf("toggle <cvar> : toggle cvar\n");
break;
case 2:
if (Cvar_VariableValue(Cmd_Argv(1)))
Cvar_Set (Cmd_Argv(1), "0");
else
Cvar_Set (Cmd_Argv(1), "1");
break;
}
}
/*
============
Cvar_Cycle_f
============
*/
void Cvar_Cycle_f (void)
{
int i;
if (Cmd_Argc() < 3)
{
Con_Printf("cycle <cvar> <value list>: cycle cvar through a list of values\n");
return;
}
//loop through the args until you find one that matches the current cvar value.
//yes, this will get stuck on a list that contains the same value twice.
//it's not worth dealing with, and i'm not even sure it can be dealt with.
for (i=2;i<Cmd_Argc();i++)
{
//zero is assumed to be a string, even though it could actually be zero. The worst case
//is that the first time you call this command, it won't match on zero when it should, but after that,
//it will be comparing strings that all had the same source (the user) so it will work.
if (atof(Cmd_Argv(i)) == 0)
{
if (!strcmp(Cmd_Argv(i), Cvar_VariableString(Cmd_Argv(1))))
break;
}
else
{
if (atof(Cmd_Argv(i)) == Cvar_VariableValue(Cmd_Argv(1)))
break;
}
}
if (i == Cmd_Argc())
Cvar_Set (Cmd_Argv(1), Cmd_Argv(2)); // no match
else if (i + 1 == Cmd_Argc())
Cvar_Set (Cmd_Argv(1), Cmd_Argv(2)); // matched last value in list
else
Cvar_Set (Cmd_Argv(1), Cmd_Argv(i+1)); // matched earlier in list
}
/*
============
Cvar_Reset_f -- johnfitz
============
*/
void Cvar_Reset_f (void)
{
switch (Cmd_Argc())
{
default:
case 1:
Con_Printf ("reset <cvar> : reset cvar to default\n");
break;
case 2:
Cvar_Reset (Cmd_Argv(1));
break;
}
}
/*
============
Cvar_ResetAll_f -- johnfitz
============
*/
void Cvar_ResetAll_f (void)
{
cvar_t *var;
for (var = cvar_vars; var; var = var->next)
Cvar_Reset (var->name);
}
/*
============
Cvar_ResetCfg_f -- QuakeSpasm
============
*/
void Cvar_ResetCfg_f (void)
{
cvar_t *var;
for (var = cvar_vars ; var ; var = var->next)
if (var->archive)
Cvar_Reset (var->name);
}
//==============================================================================
//
// INIT
//
//==============================================================================
/*
============
Cvar_Init
============
*/
void Cvar_Init (void)
{
Cmd_AddCommand ("cvarlist", Cvar_List_f);
Cmd_AddCommand ("toggle", Cvar_Toggle_f);
Cmd_AddCommand ("cycle", Cvar_Cycle_f);
Cmd_AddCommand ("inc", Cvar_Inc_f);
Cmd_AddCommand ("dec", Cvar_Dec_f);
Cmd_AddCommand ("reset", Cvar_Reset_f);
Cmd_AddCommand ("resetall", Cvar_ResetAll_f);
Cmd_AddCommand ("resetcfg", Cvar_ResetCfg_f);
}
//==============================================================================
//
// CVAR FUNCTIONS
//
//==============================================================================
/*
============
Cvar_FindVar
============
*/
cvar_t *Cvar_FindVar (char *var_name)
{
cvar_t *var;
for (var=cvar_vars ; var ; var=var->next)
if (!strcmp (var_name, var->name))
return var;
return NULL;
}
/*
============
Cvar_VariableValue
============
*/
float Cvar_VariableValue (char *var_name)
{
cvar_t *var;
var = Cvar_FindVar (var_name);
if (!var)
return 0;
return atof (var->string);
}
/*
============
Cvar_VariableString
============
*/
char *Cvar_VariableString (char *var_name)
{
cvar_t *var;
var = Cvar_FindVar (var_name);
if (!var)
return cvar_null_string;
return var->string;
}
/*
============
Cvar_CompleteVariable
============
*/
char *Cvar_CompleteVariable (char *partial)
{
cvar_t *cvar;
int len;
len = strlen(partial);
if (!len)
return NULL;
// check functions
for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
if (!strncmp (partial,cvar->name, len))
return cvar->name;
return NULL;
}
/*
============
Cvar_Reset -- johnfitz
============
*/
void Cvar_Reset (char *var_name)
{
cvar_t *var;
var = Cvar_FindVar (var_name);
if (!var)
Con_Printf ("Cvar_Reset: variable \"%s\" not found\n", var_name);
else
Cvar_Set (var->name, var->default_string);
}
/*
============
Cvar_Set
============
*/
void Cvar_Set (char *var_name, char *value)
{
cvar_t *var;
qboolean changed;
var = Cvar_FindVar (var_name);
if (!var)
{ // there is an error in C code if this happens
Con_Printf ("Cvar_Set: variable \"%s\" not found\n", var_name);
return;
}
changed = strcmp(var->string, value);
Z_Free (var->string); // free the old value string
var->string = Z_Malloc (strlen(value)+1);
strcpy (var->string, value);
var->value = atof (var->string);
//johnfitz -- during initialization, update default too
if (!host_initialized)
{
Z_Free (var->default_string);
var->default_string = Z_Malloc (strlen(value)+1);
strcpy (var->default_string, value);
}
//johnfitz
if (var->server && changed)
{
if (sv.active)
SV_BroadcastPrintf ("\"%s\" changed to \"%s\"\n", var->name, var->string);
}
//johnfitz
if(var->callback && changed)
var->callback();
//johnfitz
}
/*
============
Cvar_SetValue
============
*/
void Cvar_SetValue (char *var_name, float value)
{
static char str[32];
int i;
sprintf (str, "%f", value);
// Strip off ending zeros
for (i = strlen(str) - 1 ; i > 0 && str[i] == '0' ; i--)
str[i] = 0;
// Strip off ending period
if (str[i] == '.')
str[i] = 0;
Cvar_Set (var_name, str);
}
/*
============
Cvar_RegisterVariable
Adds a freestanding variable to the variable list.
============
*/
void Cvar_RegisterVariable (cvar_t *variable, void *function)
{
char *oldstr;
cvar_t *cursor,*prev; //johnfitz -- sorted list insert
// first check to see if it has already been defined
if (Cvar_FindVar (variable->name))
{
Con_Printf ("Can't register variable %s, already defined\n", variable->name);
return;
}
// check for overlap with a command
if (Cmd_Exists (variable->name))
{
Con_Printf (" %s is a command\n", variable->name);
return;
}
// copy the value off, because future sets will Z_Free it
oldstr = variable->string;
variable->string = Z_Malloc (strlen(variable->string)+1);
strcpy (variable->string, oldstr);
variable->value = atof (variable->string);
//johnfitz -- save initial value for "reset" command
variable->default_string = Z_Malloc (strlen(variable->string)+1);
strcpy (variable->default_string, oldstr);
//johnfitz
// link the variable in
//johnfitz -- insert each entry in alphabetical order
if (cvar_vars == NULL || strcmp(variable->name, cvar_vars->name) < 0) // insert at front
{
variable->next = cvar_vars;
cvar_vars = variable;
}
else //insert later
{
prev = cvar_vars;
cursor = cvar_vars->next;
while (cursor && (strcmp(variable->name, cursor->name) > 0))
{
prev = cursor;
cursor = cursor->next;
}
variable->next = prev->next;
prev->next = variable;
}
//johnfitz
variable->callback = function; //johnfitz
}
/*
============
Cvar_Command
Handles variable inspection and changing from the console
============
*/
qboolean Cvar_Command (void)
{
cvar_t *v;
// check variables
v = Cvar_FindVar (Cmd_Argv(0));
if (!v)
return false;
// perform a variable print or set
if (Cmd_Argc() == 1)
{
Con_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
return true;
}
Cvar_Set (v->name, Cmd_Argv(1));
return true;
}
/*
============
Cvar_WriteVariables
Writes lines containing "set variable value" for all variables
with the archive flag set to true.
============
*/
void Cvar_WriteVariables (FILE *f)
{
cvar_t *var;
for (var = cvar_vars ; var ; var = var->next)
if (var->archive)
fprintf (f, "%s \"%s\"\n", var->name, var->string);
}