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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
|
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code 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.
Quake III Arena source code 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 Quake III Arena source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// cvar.c -- dynamic variable tracking
#include "q_shared.h"
#include "qcommon.h"
cvar_t *cvar_vars;
cvar_t *cvar_cheats;
int cvar_modifiedFlags;
#define MAX_CVARS 1024
cvar_t cvar_indexes[MAX_CVARS];
int cvar_numIndexes;
#define FILE_HASH_SIZE 256
static cvar_t* hashTable[FILE_HASH_SIZE];
cvar_t *Cvar_Set2( const char *var_name, const char *value, qboolean force);
/*
================
return a hash value for the filename
================
*/
static long generateHashValue( const char *fname ) {
int i;
long hash;
char letter;
hash = 0;
i = 0;
while (fname[i] != '\0') {
letter = tolower(fname[i]);
hash+=(long)(letter)*(i+119);
i++;
}
hash &= (FILE_HASH_SIZE-1);
return hash;
}
/*
============
Cvar_ValidateString
============
*/
static qboolean Cvar_ValidateString( const char *s ) {
if ( !s ) {
return qfalse;
}
if ( strchr( s, '\\' ) ) {
return qfalse;
}
if ( strchr( s, '\"' ) ) {
return qfalse;
}
if ( strchr( s, ';' ) ) {
return qfalse;
}
return qtrue;
}
/*
============
Cvar_FindVar
============
*/
static cvar_t *Cvar_FindVar( const char *var_name ) {
cvar_t *var;
long hash;
hash = generateHashValue(var_name);
for (var=hashTable[hash] ; var ; var=var->hashNext) {
if (!Q_stricmp(var_name, var->name)) {
return var;
}
}
return NULL;
}
/*
============
Cvar_VariableValue
============
*/
float Cvar_VariableValue( const char *var_name ) {
cvar_t *var;
var = Cvar_FindVar (var_name);
if (!var)
return 0;
return var->value;
}
/*
============
Cvar_VariableIntegerValue
============
*/
int Cvar_VariableIntegerValue( const char *var_name ) {
cvar_t *var;
var = Cvar_FindVar (var_name);
if (!var)
return 0;
return var->integer;
}
/*
============
Cvar_VariableString
============
*/
char *Cvar_VariableString( const char *var_name ) {
cvar_t *var;
var = Cvar_FindVar (var_name);
if (!var)
return "";
return var->string;
}
/*
============
Cvar_VariableStringBuffer
============
*/
void Cvar_VariableStringBuffer( const char *var_name, char *buffer, int bufsize ) {
cvar_t *var;
var = Cvar_FindVar (var_name);
if (!var) {
*buffer = 0;
}
else {
Q_strncpyz( buffer, var->string, bufsize );
}
}
/*
============
Cvar_Flags
============
*/
int Cvar_Flags(const char *var_name)
{
cvar_t *var;
if(! (var = Cvar_FindVar(var_name)) )
return CVAR_NONEXISTENT;
else
return var->flags;
}
/*
============
Cvar_CommandCompletion
============
*/
void Cvar_CommandCompletion( void(*callback)(const char *s) ) {
cvar_t *cvar;
for ( cvar = cvar_vars ; cvar ; cvar = cvar->next ) {
callback( cvar->name );
}
}
/*
============
Cvar_Validate
============
*/
static const char *Cvar_Validate( cvar_t *var,
const char *value, qboolean warn )
{
static char s[ MAX_CVAR_VALUE_STRING ];
float valuef;
qboolean changed = qfalse;
if( !var->validate )
return value;
if( !value )
return value;
if( Q_isanumber( value ) )
{
valuef = atof( value );
if( var->integral )
{
if( !Q_isintegral( valuef ) )
{
if( warn )
Com_Printf( "WARNING: cvar '%s' must be integral", var->name );
valuef = (int)valuef;
changed = qtrue;
}
}
}
else
{
if( warn )
Com_Printf( "WARNING: cvar '%s' must be numeric", var->name );
valuef = atof( var->resetString );
changed = qtrue;
}
if( valuef < var->min )
{
if( warn )
{
if( changed )
Com_Printf( " and is" );
else
Com_Printf( "WARNING: cvar '%s'", var->name );
if( Q_isintegral( var->min ) )
Com_Printf( " out of range (min %d)", (int)var->min );
else
Com_Printf( " out of range (min %f)", var->min );
}
valuef = var->min;
changed = qtrue;
}
else if( valuef > var->max )
{
if( warn )
{
if( changed )
Com_Printf( " and is" );
else
Com_Printf( "WARNING: cvar '%s'", var->name );
if( Q_isintegral( var->max ) )
Com_Printf( " out of range (max %d)", (int)var->max );
else
Com_Printf( " out of range (max %f)", var->max );
}
valuef = var->max;
changed = qtrue;
}
if( changed )
{
if( Q_isintegral( valuef ) )
{
Com_sprintf( s, sizeof( s ), "%d", (int)valuef );
if( warn )
Com_Printf( ", setting to %d\n", (int)valuef );
}
else
{
Com_sprintf( s, sizeof( s ), "%f", valuef );
if( warn )
Com_Printf( ", setting to %f\n", valuef );
}
return s;
}
else
return value;
}
/*
============
Cvar_Get
If the variable already exists, the value will not be set unless CVAR_ROM
The flags will be or'ed in if the variable exists.
============
*/
cvar_t *Cvar_Get( const char *var_name, const char *var_value, int flags ) {
cvar_t *var;
long hash;
if ( !var_name || ! var_value ) {
Com_Error( ERR_FATAL, "Cvar_Get: NULL parameter" );
}
if ( !Cvar_ValidateString( var_name ) ) {
Com_Printf("invalid cvar name string: %s\n", var_name );
var_name = "BADNAME";
}
#if 0 // FIXME: values with backslash happen
if ( !Cvar_ValidateString( var_value ) ) {
Com_Printf("invalid cvar value string: %s\n", var_value );
var_value = "BADVALUE";
}
#endif
var = Cvar_FindVar (var_name);
if ( var ) {
var_value = Cvar_Validate( var, var_value, qfalse );
// if the C code is now specifying a variable that the user already
// set a value for, take the new value as the reset value
if ( ( var->flags & CVAR_USER_CREATED ) && !( flags & CVAR_USER_CREATED )
&& var_value[0] ) {
var->flags &= ~CVAR_USER_CREATED;
Z_Free( var->resetString );
var->resetString = CopyString( var_value );
if(flags & CVAR_ROM)
{
// this variable was set by the user,
// so force it to value given by the engine.
if(var->latchedString)
Z_Free(var->latchedString);
var->latchedString = CopyString(var_value);
}
// ZOID--needs to be set so that cvars the game sets as
// SERVERINFO get sent to clients
cvar_modifiedFlags |= flags;
}
var->flags |= flags;
// only allow one non-empty reset string without a warning
if ( !var->resetString[0] ) {
// we don't have a reset string yet
Z_Free( var->resetString );
var->resetString = CopyString( var_value );
} else if ( var_value[0] && strcmp( var->resetString, var_value ) ) {
Com_DPrintf( "Warning: cvar \"%s\" given initial values: \"%s\" and \"%s\"\n",
var_name, var->resetString, var_value );
}
// if we have a latched string, take that value now
if ( var->latchedString ) {
char *s;
s = var->latchedString;
var->latchedString = NULL; // otherwise cvar_set2 would free it
Cvar_Set2( var_name, s, qtrue );
Z_Free( s );
}
return var;
}
//
// allocate a new cvar
//
if ( cvar_numIndexes >= MAX_CVARS ) {
Com_Error( ERR_FATAL, "MAX_CVARS" );
}
var = &cvar_indexes[cvar_numIndexes];
cvar_numIndexes++;
var->name = CopyString (var_name);
var->string = CopyString (var_value);
var->modified = qtrue;
var->modificationCount = 1;
var->value = atof (var->string);
var->integer = atoi(var->string);
var->resetString = CopyString( var_value );
var->validate = qfalse;
// link the variable in
var->next = cvar_vars;
cvar_vars = var;
var->flags = flags;
// note what types of cvars have been modified (userinfo, archive, serverinfo, systeminfo)
cvar_modifiedFlags |= var->flags;
hash = generateHashValue(var_name);
var->hashNext = hashTable[hash];
hashTable[hash] = var;
return var;
}
/*
============
Cvar_Print
Prints the value, default, and latched string of the given variable
============
*/
void Cvar_Print( cvar_t *v ) {
Com_Printf ("\"%s\" is:\"%s" S_COLOR_WHITE "\"",
v->name, v->string );
if ( !( v->flags & CVAR_ROM ) ) {
if ( !Q_stricmp( v->string, v->resetString ) ) {
Com_Printf (", the default" );
} else {
Com_Printf (" default:\"%s" S_COLOR_WHITE "\"",
v->resetString );
}
}
Com_Printf ("\n");
if ( v->latchedString ) {
Com_Printf( "latched: \"%s\"\n", v->latchedString );
}
}
/*
============
Cvar_Set2
============
*/
cvar_t *Cvar_Set2( const char *var_name, const char *value, qboolean force ) {
cvar_t *var;
// Com_DPrintf( "Cvar_Set2: %s %s\n", var_name, value );
if ( !Cvar_ValidateString( var_name ) ) {
Com_Printf("invalid cvar name string: %s\n", var_name );
var_name = "BADNAME";
}
#if 0 // FIXME
if ( value && !Cvar_ValidateString( value ) ) {
Com_Printf("invalid cvar value string: %s\n", value );
var_value = "BADVALUE";
}
#endif
var = Cvar_FindVar (var_name);
if (!var) {
if ( !value ) {
return NULL;
}
// create it
if ( !force ) {
return Cvar_Get( var_name, value, CVAR_USER_CREATED );
} else {
return Cvar_Get (var_name, value, 0);
}
}
if (!value ) {
value = var->resetString;
}
value = Cvar_Validate( var, value, qtrue );
if((var->flags & CVAR_LATCH) && var->latchedString) {
if(!strcmp(value,var->latchedString))
return var;
}
else if (!strcmp(value,var->string)) {
return var;
}
// note what types of cvars have been modified (userinfo, archive, serverinfo, systeminfo)
cvar_modifiedFlags |= var->flags;
if (!force)
{
if (var->flags & CVAR_ROM)
{
Com_Printf ("%s is read only.\n", var_name);
return var;
}
if (var->flags & CVAR_INIT)
{
Com_Printf ("%s is write protected.\n", var_name);
return var;
}
if (var->flags & CVAR_LATCH)
{
if (var->latchedString)
{
if (strcmp(value, var->latchedString) == 0)
return var;
Z_Free (var->latchedString);
}
else
{
if (strcmp(value, var->string) == 0)
return var;
}
Com_Printf ("%s will be changed upon restarting.\n", var_name);
var->latchedString = CopyString(value);
var->modified = qtrue;
var->modificationCount++;
return var;
}
if ( (var->flags & CVAR_CHEAT) && !cvar_cheats->integer )
{
Com_Printf ("%s is cheat protected.\n", var_name);
return var;
}
}
else
{
if (var->latchedString)
{
Z_Free (var->latchedString);
var->latchedString = NULL;
}
}
if (!strcmp(value, var->string))
return var; // not changed
var->modified = qtrue;
var->modificationCount++;
Z_Free (var->string); // free the old value string
var->string = CopyString(value);
var->value = atof (var->string);
var->integer = atoi (var->string);
return var;
}
/*
============
Cvar_Set
============
*/
void Cvar_Set( const char *var_name, const char *value) {
Cvar_Set2 (var_name, value, qtrue);
}
/*
============
Cvar_SetLatched
============
*/
void Cvar_SetLatched( const char *var_name, const char *value) {
Cvar_Set2 (var_name, value, qfalse);
}
/*
============
Cvar_SetValue
============
*/
void Cvar_SetValue( const char *var_name, float value) {
char val[32];
if ( value == (int)value ) {
Com_sprintf (val, sizeof(val), "%i",(int)value);
} else {
Com_sprintf (val, sizeof(val), "%f",value);
}
Cvar_Set (var_name, val);
}
/*
============
Cvar_Reset
============
*/
void Cvar_Reset( const char *var_name ) {
Cvar_Set2( var_name, NULL, qfalse );
}
/*
============
Cvar_ForceReset
============
*/
void Cvar_ForceReset(const char *var_name)
{
Cvar_Set2(var_name, NULL, qtrue);
}
/*
============
Cvar_SetCheatState
Any testing variables will be reset to the safe values
============
*/
void Cvar_SetCheatState( void ) {
cvar_t *var;
// set all default vars to the safe value
for ( var = cvar_vars ; var ; var = var->next ) {
if ( var->flags & CVAR_CHEAT ) {
// the CVAR_LATCHED|CVAR_CHEAT vars might escape the reset here
// because of a different var->latchedString
if (var->latchedString)
{
Z_Free(var->latchedString);
var->latchedString = NULL;
}
if (strcmp(var->resetString,var->string)) {
Cvar_Set( var->name, var->resetString );
}
}
}
}
/*
============
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 qfalse;
}
// perform a variable print or set
if ( Cmd_Argc() == 1 ) {
Cvar_Print( v );
return qtrue;
}
// set the value if forcing isn't required
Cvar_Set2 (v->name, Cmd_Args(), qfalse);
return qtrue;
}
/*
============
Cvar_Print_f
Prints the contents of a cvar
(preferred over Cvar_Command where cvar names and commands conflict)
============
*/
void Cvar_Print_f(void)
{
char *name;
cvar_t *cv;
if(Cmd_Argc() != 2)
{
Com_Printf ("usage: print <variable>\n");
return;
}
name = Cmd_Argv(1);
cv = Cvar_FindVar(name);
if(cv)
Cvar_Print(cv);
else
Com_Printf ("Cvar %s does not exist.\n", name);
}
/*
============
Cvar_Toggle_f
Toggles a cvar for easy single key binding
============
*/
void Cvar_Toggle_f( void ) {
int v;
if ( Cmd_Argc() != 2 ) {
Com_Printf ("usage: toggle <variable>\n");
return;
}
v = Cvar_VariableValue( Cmd_Argv( 1 ) );
v = !v;
Cvar_Set2 (Cmd_Argv(1), va("%i", v), qfalse);
}
/*
============
Cvar_Set_f
Allows setting and defining of arbitrary cvars from console, even if they
weren't declared in C code.
============
*/
void Cvar_Set_f( void ) {
int i, c, l, len;
char cmd[5], combined[MAX_STRING_TOKENS];
cvar_t *v;
c = Cmd_Argc();
Q_strncpyz( cmd, Cmd_Argv(0), sizeof( cmd ) );
if ( c < 2 ) {
Com_Printf ("usage: %s <variable> <value>\n", cmd);
return;
}
if ( c == 2 ) {
Cvar_Print_f();
return;
}
combined[0] = 0;
l = 0;
for ( i = 2 ; i < c ; i++ ) {
len = strlen ( Cmd_Argv( i ) + 1 );
if ( l + len >= MAX_STRING_TOKENS - 2 ) {
break;
}
strcat( combined, Cmd_Argv( i ) );
if ( i != c-1 ) {
strcat( combined, " " );
}
l += len;
}
v = Cvar_Set2 (Cmd_Argv(1), combined, qfalse);
if( !v ) {
return;
}
switch( cmd[3] ) {
default:
case '\0':
break;
case 'u':
v->flags |= CVAR_USERINFO;
break;
case 's':
v->flags |= CVAR_SERVERINFO;
break;
case 'a':
v->flags |= CVAR_ARCHIVE;
break;
}
}
/*
============
Cvar_Reset_f
============
*/
void Cvar_Reset_f( void ) {
if ( Cmd_Argc() != 2 ) {
Com_Printf ("usage: reset <variable>\n");
return;
}
Cvar_Reset( Cmd_Argv( 1 ) );
}
/*
============
Cvar_WriteVariables
Appends lines containing "set variable value" for all variables
with the archive flag set to qtrue.
============
*/
void Cvar_WriteVariables( fileHandle_t f ) {
cvar_t *var;
char buffer[1024];
for (var = cvar_vars ; var ; var = var->next) {
if( Q_stricmp( var->name, "cl_cdkey" ) == 0 ) {
continue;
}
if( var->flags & CVAR_ARCHIVE ) {
// write the latched value, even if it hasn't taken effect yet
if ( var->latchedString ) {
if( strlen( var->name ) + strlen( var->latchedString ) + 10 > sizeof( buffer ) ) {
Com_Printf( S_COLOR_YELLOW "WARNING: value of variable "
"\"%s\" too long to write to file\n", var->name );
continue;
}
Com_sprintf (buffer, sizeof(buffer), "seta %s \"%s\"\n", var->name, var->latchedString);
} else {
if( strlen( var->name ) + strlen( var->string ) + 10 > sizeof( buffer ) ) {
Com_Printf( S_COLOR_YELLOW "WARNING: value of variable "
"\"%s\" too long to write to file\n", var->name );
continue;
}
Com_sprintf (buffer, sizeof(buffer), "seta %s \"%s\"\n", var->name, var->string);
}
FS_Write( buffer, strlen( buffer ), f );
}
}
}
/*
============
Cvar_List_f
============
*/
void Cvar_List_f( void ) {
cvar_t *var;
int i;
char *match;
if ( Cmd_Argc() > 1 ) {
match = Cmd_Argv( 1 );
} else {
match = NULL;
}
i = 0;
for (var = cvar_vars ; var ; var = var->next, i++)
{
if (match && !Com_Filter(match, var->name, qfalse)) continue;
if (var->flags & CVAR_SERVERINFO) {
Com_Printf("S");
} else {
Com_Printf(" ");
}
if (var->flags & CVAR_SYSTEMINFO) {
Com_Printf("s");
} else {
Com_Printf(" ");
}
if (var->flags & CVAR_USERINFO) {
Com_Printf("U");
} else {
Com_Printf(" ");
}
if (var->flags & CVAR_ROM) {
Com_Printf("R");
} else {
Com_Printf(" ");
}
if (var->flags & CVAR_INIT) {
Com_Printf("I");
} else {
Com_Printf(" ");
}
if (var->flags & CVAR_ARCHIVE) {
Com_Printf("A");
} else {
Com_Printf(" ");
}
if (var->flags & CVAR_LATCH) {
Com_Printf("L");
} else {
Com_Printf(" ");
}
if (var->flags & CVAR_CHEAT) {
Com_Printf("C");
} else {
Com_Printf(" ");
}
if (var->flags & CVAR_USER_CREATED) {
Com_Printf("?");
} else {
Com_Printf(" ");
}
Com_Printf (" %s \"%s\"\n", var->name, var->string);
}
Com_Printf ("\n%i total cvars\n", i);
Com_Printf ("%i cvar indexes\n", cvar_numIndexes);
}
/*
============
Cvar_Restart_f
Resets all cvars to their hardcoded values
============
*/
void Cvar_Restart_f( void ) {
cvar_t *var;
cvar_t **prev;
prev = &cvar_vars;
while ( 1 ) {
var = *prev;
if ( !var ) {
break;
}
// don't mess with rom values, or some inter-module
// communication will get broken (com_cl_running, etc)
if ( var->flags & ( CVAR_ROM | CVAR_INIT | CVAR_NORESTART ) ) {
prev = &var->next;
continue;
}
// throw out any variables the user created
if ( var->flags & CVAR_USER_CREATED ) {
*prev = var->next;
if ( var->name ) {
Z_Free( var->name );
}
if ( var->string ) {
Z_Free( var->string );
}
if ( var->latchedString ) {
Z_Free( var->latchedString );
}
if ( var->resetString ) {
Z_Free( var->resetString );
}
// clear the var completely, since we
// can't remove the index from the list
Com_Memset( var, 0, sizeof( var ) );
continue;
}
Cvar_Set( var->name, var->resetString );
prev = &var->next;
}
}
/*
=====================
Cvar_InfoString
=====================
*/
char *Cvar_InfoString( int bit ) {
static char info[MAX_INFO_STRING];
cvar_t *var;
info[0] = 0;
for (var = cvar_vars ; var ; var = var->next) {
if (var->flags & bit) {
Info_SetValueForKey (info, var->name, var->string);
}
}
return info;
}
/*
=====================
Cvar_InfoString_Big
handles large info strings ( CS_SYSTEMINFO )
=====================
*/
char *Cvar_InfoString_Big( int bit ) {
static char info[BIG_INFO_STRING];
cvar_t *var;
info[0] = 0;
for (var = cvar_vars ; var ; var = var->next) {
if (var->flags & bit) {
Info_SetValueForKey_Big (info, var->name, var->string);
}
}
return info;
}
/*
=====================
Cvar_InfoStringBuffer
=====================
*/
void Cvar_InfoStringBuffer( int bit, char* buff, int buffsize ) {
Q_strncpyz(buff,Cvar_InfoString(bit),buffsize);
}
/*
=====================
Cvar_CheckRange
=====================
*/
void Cvar_CheckRange( cvar_t *var, float min, float max, qboolean integral )
{
var->validate = qtrue;
var->min = min;
var->max = max;
var->integral = integral;
// Force an initial range check
Cvar_Set( var->name, var->string );
}
/*
=====================
Cvar_Register
basically a slightly modified Cvar_Get for the interpreted modules
=====================
*/
void Cvar_Register( vmCvar_t *vmCvar, const char *varName, const char *defaultValue, int flags ) {
cvar_t *cv;
cv = Cvar_Get( varName, defaultValue, flags );
if ( !vmCvar ) {
return;
}
vmCvar->handle = cv - cvar_indexes;
vmCvar->modificationCount = -1;
Cvar_Update( vmCvar );
}
/*
=====================
Cvar_Update
updates an interpreted modules' version of a cvar
=====================
*/
void Cvar_Update( vmCvar_t *vmCvar ) {
cvar_t *cv = NULL;
assert(vmCvar);
if ( (unsigned)vmCvar->handle >= cvar_numIndexes ) {
Com_Error( ERR_DROP, "Cvar_Update: handle out of range" );
}
cv = cvar_indexes + vmCvar->handle;
if ( cv->modificationCount == vmCvar->modificationCount ) {
return;
}
if ( !cv->string ) {
return; // variable might have been cleared by a cvar_restart
}
vmCvar->modificationCount = cv->modificationCount;
if ( strlen(cv->string)+1 > MAX_CVAR_VALUE_STRING )
Com_Error( ERR_DROP, "Cvar_Update: src %s length %zd exceeds MAX_CVAR_VALUE_STRING",
cv->string,
strlen(cv->string));
Q_strncpyz( vmCvar->string, cv->string, MAX_CVAR_VALUE_STRING );
vmCvar->value = cv->value;
vmCvar->integer = cv->integer;
}
/*
==================
Cvar_CompleteCvarName
==================
*/
void Cvar_CompleteCvarName( char *args, int argNum )
{
if( argNum == 2 )
{
// Skip "<cmd> "
char *p = Com_SkipTokens( args, 1, " " );
if( p > args )
Field_CompleteCommand( p, qfalse, qtrue );
}
}
/*
============
Cvar_Init
Reads in all archived cvars
============
*/
void Cvar_Init (void) {
cvar_cheats = Cvar_Get("sv_cheats", "1", CVAR_ROM | CVAR_SYSTEMINFO );
Cmd_AddCommand ("print", Cvar_Print_f);
Cmd_AddCommand ("toggle", Cvar_Toggle_f);
Cmd_SetCommandCompletionFunc( "toggle", Cvar_CompleteCvarName );
Cmd_AddCommand ("set", Cvar_Set_f);
Cmd_SetCommandCompletionFunc( "set", Cvar_CompleteCvarName );
Cmd_AddCommand ("sets", Cvar_Set_f);
Cmd_SetCommandCompletionFunc( "sets", Cvar_CompleteCvarName );
Cmd_AddCommand ("setu", Cvar_Set_f);
Cmd_SetCommandCompletionFunc( "setu", Cvar_CompleteCvarName );
Cmd_AddCommand ("seta", Cvar_Set_f);
Cmd_SetCommandCompletionFunc( "seta", Cvar_CompleteCvarName );
Cmd_AddCommand ("reset", Cvar_Reset_f);
Cmd_SetCommandCompletionFunc( "reset", Cvar_CompleteCvarName );
Cmd_AddCommand ("cvarlist", Cvar_List_f);
Cmd_AddCommand ("cvar_restart", Cvar_Restart_f);
}
|