forked from Iceman12k/ezquake-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvar.c
1684 lines (1411 loc) · 35.8 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
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
/*
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
#ifdef SERVERONLY
#include "qwsvdef.h"
#define VAR_HASHPOOL_SIZE 32
#else
#include "common.h"
#include "teamplay.h"
#include "rulesets.h"
#include "keys.h"
#include "utils.h"
#include "parser.h"
#include "r_renderer.h"
static void Cvar_ApplyLatchedUpdate(cvar_t* var);
void CL_UserinfoChanged(char *key, char *value);
void SV_ServerinfoChanged(char *key, char *value);
void Help_DescribeCvar(cvar_t *v);
void VID_ReloadCvarChanged(cvar_t* var);
extern cvar_t r_fullbrightSkins;
extern cvar_t cl_fakeshaft;
extern cvar_t allow_scripts;
static cvar_t cvar_viewdefault = {"cvar_viewdefault", "1"};
static cvar_t cvar_viewhelp = {"cvar_viewhelp", "1"};
static cvar_t cvar_viewlatched = {"cvar_viewlatched", "1"};
#define VAR_HASHPOOL_SIZE 1024
static void Cvar_AddCvarToGroup(cvar_t *var);
#endif
static cvar_t *cvar_hash[VAR_HASHPOOL_SIZE];
cvar_t *cvar_vars;
static char *cvar_null_string = "";
// Use this to walk through all vars
cvar_t* Cvar_Next(cvar_t *var)
{
if (var) {
return var->next;
}
else {
return cvar_vars;
}
}
cvar_t *Cvar_Find(const char *var_name)
{
cvar_t *var;
int key = Com_HashKey (var_name) % VAR_HASHPOOL_SIZE;
for (var = cvar_hash[key]; var; var = var->hash_next) {
if (!strcasecmp(var_name, var->name)) {
return var;
}
}
return NULL;
}
float Cvar_Value (const char *var_name)
{
cvar_t *var = Cvar_Find (var_name);
if (!var) {
return 0;
}
return Q_atof (var->string);
}
char *Cvar_String (const char *var_name)
{
cvar_t *var = Cvar_Find (var_name);
if (!var)
return cvar_null_string;
return var->string;
}
void SV_SendServerInfoChange(char *key, char *value);
char* Cvar_ServerInfoValue(char* key, char* value)
{
// force serverinfo "0" vars to be "".
// meag: deathmatch is a special case as clients default 'not in serverinfo' to non-coop
if (!strcmp(value, "0") && strcmp(key, "deathmatch")) {
return "";
}
return value;
}
void Cvar_SetEx(cvar_t *var, char *value, qbool ignore_callback)
{
static qbool changing = false;
char *new_val;
#ifndef SERVERONLY
extern cvar_t cl_warncmd;
extern cvar_t re_subi[10];
float test;
qbool same_value;
#endif
if (!var)
return;
// force serverinfo "0" vars to be "".
if (var->flags & CVAR_SERVERINFO) {
value = Cvar_ServerInfoValue(var->name, value);
}
#ifndef SERVERONLY
// C code may wrongly use Cvar_Set on non registered variable, some 99.99% accurate check
// variables for internal triggers are not registered intentionally
if (var < re_subi || var > re_subi + 9) {
if (!var->next /* this is fast, but a bit flawed logic */ && !Cvar_Find(var->name)) {
Com_Printf("Cvar_Set: on non linked var %s\n", var->name);
return;
}
}
#endif
if (var->flags & CVAR_ROM) {
#ifndef SERVERONLY
Com_Printf ("\"%s\" is write protected\n", var->name);
#endif
return;
}
#ifndef SERVERONLY
if (var->flags & CVAR_RULESET_MIN) {
test = Q_atof (value);
if (test < var->minrulesetvalue) {
Com_Printf ("min \"%s\" is limited to %0.2f\n", var->name,var->minrulesetvalue);
return;
}
}
if (var->flags & CVAR_RULESET_MAX) {
test = Q_atof (value);
if (test > var->maxrulesetvalue) {
Com_Printf ("max \"%s\" is limited to %0.2f\n", var->name,var->maxrulesetvalue);
return;
}
}
if ((var->flags & CVAR_INIT) && host_initialized) {
if (cl_warncmd.value || developer.value)
Com_Printf ("\"%s\" can only be changed with \"+set %s %s\" on the command line.\n", var->name, var->name, value);
return;
}
// We do this before OnChange check, which means no OnChange check for latched cvars.
if (var->flags & CVAR_LATCH)
{
if (var->latchedString)
{
if (strcmp(value, var->latchedString) == 0)
return; // latched string alredy has this value
Q_free(var->latchedString); // switching latching string to other, so free it
}
else
{
if (strcmp(value, var->string) == 0)
return; // we change string value to the same, do no create latched string then
}
// HACK: sometime I need somehow silently change latched cvars.
// So, flag CVAR_SILENT for latched cvars mean no this warning.
// However keep this flag always on latched variable is stupid imo.
// So, do not mix CVAR_LATCHED | CVAR_SILENT in cvar definition.
if ( !(var->flags & CVAR_SILENT) ) {
const char* restartcmd = "vid_restart (video/graphics)";
if (strncmp(var->name, "in_", 3) == 0) {
restartcmd = "in_restart (input)";
}
else if (strncmp(var->name, "s_", 2) == 0) {
restartcmd = "s_restart (sound)";
}
Com_Printf ("%s needs %s to take effect.\n", var->name, restartcmd);
}
var->latchedString = Q_strdup_named(value, var->name);
var->modified = true; // set to true even car->string is not changed yet, that how q3 does
return;
}
#endif
if (!ignore_callback && var->OnChange && !changing) {
qbool cancel = false;
changing = true;
var->flags &= ~(CVAR_AUTOSETRECENT);
var->OnChange(var, value, &cancel);
changing = false;
if (cancel) {
return; // change rejected.
}
}
#ifndef SERVERONLY
same_value = value && var->string && (value == var->string || !strcmp (value, var->string));
#endif
// dup string first (before free) since 'value' and 'var->string' can point at the same memory area.
new_val = Q_strdup_named(value, var->name);
// free the old value string
Q_free(var->string);
// assign new value
var->string = new_val;
var->value = Q_atof (var->string);
#ifndef SERVERONLY
var->integer = Q_atoi (var->string);
if (var->flags & CVAR_TRACKERCOLOR) {
TrackerStringToRGB_W(var->string, var->color);
}
else {
StringToRGB_W(var->string, var->color);
}
if (!same_value && !(var->flags & CVAR_AUTOSETRECENT)) {
Cvar_AutoReset (var);
}
var->flags &= ~(CVAR_AUTOSETRECENT);
var->modified = true;
#endif
#ifndef CLIENTONLY
if (var->flags & CVAR_SERVERINFO) {
SV_ServerinfoChanged(var->name, var->string);
}
#endif
#ifndef SERVERONLY
if (var->flags & CVAR_USERINFO) {
CL_UserinfoChanged(var->name, var->string);
}
if (var->flags & CVAR_RECOMPILE_PROGS) {
renderer.CvarForceRecompile(var);
}
if ((var->flags & CVAR_RELOAD_GFX) && host_everything_loaded && !same_value) {
VID_ReloadCvarChanged(var);
}
#endif
}
void Cvar_SetIgnoreCallback(cvar_t *var, char *value)
{
Cvar_SetEx(var, value, true);
}
void Cvar_Set(cvar_t *var, char *value)
{
Cvar_SetEx(var, value, false);
}
void Cvar_ForceSet (cvar_t *var, char *value)
{
int saved_flags;
if (!var)
return;
saved_flags = var->flags;
var->flags &= ~CVAR_ROM;
Cvar_Set (var, value);
var->flags = saved_flags;
}
void Cvar_SetByName (const char *var_name, char *value)
{
cvar_t *var;
var = Cvar_Find (var_name);
if (!var)
{ // there is an error in C code if this happens
Con_DPrintf ("Cvar_Set: variable %s not found\n", var_name);
return;
}
Cvar_Set (var, value);
}
static void Cvar_ValueToString(char* val, int size, float value)
{
int i;
snprintf(val, size - 1, "%f", value);
for (i = strlen(val) - 1; i > 0 && val[i] == '0'; i--) {
val[i] = 0;
}
if (val[i] == '.') {
val[i] = '\0';
}
}
void Cvar_SetValue (cvar_t *var, const float value)
{
char val[128];
Cvar_ValueToString(val, sizeof(val), value);
Cvar_Set(var, val);
}
void Cvar_SetValueByName(const char *var_name, const float value)
{
char val[32];
snprintf(val, sizeof(val), "%.8g", value);
Cvar_SetByName(var_name, val);
}
void Cvar_Register(cvar_t *var)
{
int key;
cvar_t *old = Cvar_Find(var->name);
// All variables must be named :)
if (var->name[0] == '\0')
return;
#ifdef SERVERONLY
char* value;
// first check to see if it has already been defined
if (old) {
Con_Printf("Can't register variable %s, already defined\n", var->name);
return;
}
// check for overlap with a command
if (Cmd_Exists(var->name)) {
Con_Printf("Cvar_Register: %s is a command\n", var->name);
return;
}
// link the variable in
key = Com_HashKey(var->name);
var->hash_next = cvar_hash[key];
cvar_hash[key] = var;
var->next = cvar_vars;
cvar_vars = var;
// set it through the function to be consistent
value = var->string;
var->string = Q_strdup("");
Cvar_SetROM(var, value);
#else
// we already register cvar, warn about it
if (old && !(old->flags & CVAR_USER_CREATED)) {
// allow re-register latched cvar
if (old->flags & CVAR_LATCH) {
// if we have a latched string, take that value now
Cvar_ApplyLatchedUpdate(old);
Cvar_AutoReset(old);
return;
}
// warn if CVAR_SILENT is not set
if (!(old->flags & CVAR_SILENT)) {
Com_Printf("Can't register variable %s, already defined\n", var->name);
}
return;
}
if (old && old == var) {
Sys_Error("Cvar_Register: something wrong with %s", var->name);
}
if (var->defaultvalue) {
Sys_Error("Cvar_Register: defaultvalue already set for %s", var->name);
}
var->defaultvalue = Q_strdup_named(var->string, var->name);
if (old) {
char string[512];
qbool queued_replace;
// Trigger change later if this is a proper cvar replacing a user one
queued_replace = (old->flags & CVAR_USER_CREATED) && (var->flags & CVAR_QUEUED_TRIGGER) && !(var->flags & CVAR_USER_CREATED);
if (queued_replace) {
// Created by executing config, now we have the 'proper' cvar - store old value to trigger later
var->latchedString = Q_strdup_named(old->string, var->name);
}
var->flags |= old->flags & ~(CVAR_USER_CREATED | CVAR_TEMP);
strlcpy(string, (var->flags & CVAR_ROM) || queued_replace ? var->string : old->string, sizeof(string));
Cvar_Delete(old->name);
var->string = Q_strdup_named(string, var->name);
}
else {
// allocate the string on zone because future sets will Q_free it
var->string = Q_strdup_named(var->string, var->name);
}
var->value = Q_atof(var->string);
if (((var->flags & CVAR_RULESET_MIN) && var->value < var->minrulesetvalue) ||
((var->flags & CVAR_RULESET_MAX) && var->value > var->maxrulesetvalue)) {
char val[128];
var->value = bound(var->minrulesetvalue, var->value, var->maxrulesetvalue);
Cvar_ValueToString(val, sizeof(val), var->value);
Q_free(var->string);
var->string = Q_strdup_named(val, var->name);
}
var->integer = Q_atoi(var->string);
StringToRGB_W(var->string, var->color);
var->modified = true;
// link the variable in
key = Com_HashKey(var->name) % VAR_HASHPOOL_SIZE;
var->hash_next = cvar_hash[key];
cvar_hash[key] = var;
var->next = cvar_vars;
cvar_vars = var;
Cvar_AddCvarToGroup(var);
#ifndef CLIENTONLY
if (var->flags & CVAR_SERVERINFO) {
SV_ServerinfoChanged(var->name, var->string);
}
#endif
if (var->flags & CVAR_USERINFO) {
CL_UserinfoChanged(var->name, var->string);
}
#endif // !SERVERONLY
}
static void Cvar_ApplyLatchedUpdate(cvar_t* var)
{
if (var->latchedString) {
// I did't want bother with all this CVAR_ROM and OnChange handler, just set value
Q_free(var->string);
var->string = var->latchedString;
var->latchedString = NULL;
var->value = Q_atof(var->string);
var->integer = Q_atoi(var->string);
StringToRGB_W(var->string, var->color);
var->modified = true;
}
}
qbool Cvar_Command (void)
{
cvar_t *v;
// check variables
if (!(v = Cvar_Find (Cmd_Argv(0))))
return false;
if (Cmd_Argc() == 1) {
#ifndef SERVERONLY
if (cvar_viewhelp.value)
Help_DescribeCvar (v);
if (cvar_viewdefault.value) {
char *spaces = CreateSpaces(strlen(v->name) + 2);
Con_Printf ("%s : default value is \"%s\"\n", v->name, v->defaultvalue);
Con_Printf ("%s current value is \"%s\"\n", spaces, v->string);
if ((v->flags & CVAR_AUTO) && v->autoString) {
Con_Printf ("%s auto value is \"%s\"\n", spaces, v->autoString);
}
if (cvar_viewlatched.integer && v->latchedString) {
Con_Printf ("%s latched value is \"%s\"\n", spaces, v->latchedString);
}
}
else {
#endif
Con_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
#ifndef SERVERONLY
if ((v->flags & CVAR_AUTO) && v->autoString) {
Con_Printf ("auto: \"%s\"\n", v->autoString);
}
if (cvar_viewlatched.integer && v->latchedString) {
Con_Printf ("latched: \"%s\"\n", v->latchedString);
}
}
#endif
}
else {
#ifndef SERVERONLY
// RestrictTriggers means that advanced (possibly cheaty) scripts are not allowed
// So we will force the usage of user-created variables to go through the set command
if (cbuf_current == &cbuf_server) {
// Keep the mod from changing user cvars that it didn't create
if ((v->flags & CVAR_USER_CREATED) && !(v->flags & CVAR_MOD_CREATED)) {
Con_Printf ("Error: server attempt to \"set\" user created variable [%s]\n", v->name);
return true;
}
}
else if ((v->flags & CVAR_USER_CREATED) && Rulesets_RestrictTriggers()) {
Con_Printf ("Current ruleset requires \"set\" with user created variables [%s]\n", v->name);
return true;
}
#endif
Cvar_Set (v, Cmd_MakeArgs(1));
}
return true;
}
static void Cvar_Toggle_f_base (qbool use_regex)
{
cvar_t *var;
char *name;
int i;
if (Cmd_Argc() < 2) {
Con_Printf("%s <cvar> [<cvar>..]: toggle a cvar on/off\n", Cmd_Argv(0));
return;
}
for (i = 1; i < Cmd_Argc(); i++) {
name = Cmd_Argv(i);
if (use_regex && IsRegexp(name)) {
if (!ReSearchInitEx(name, false)) {
continue;
}
for (var = cvar_vars; var; var = var->next) {
if (ReSearchMatch(var->name)) {
Cvar_Set(var, var->value ? "0" : "1");
}
}
ReSearchDone();
}
else {
var = Cvar_Find(name);
if (!(var)) {
Con_Printf("Unknown variable \"%s\"\n", name);
continue;
}
Cvar_Set(var, var->value ? "0" : "1");
}
}
}
void Cvar_Toggle_f (void)
{
Cvar_Toggle_f_base(false);
}
void Cvar_Toggle_re_f (void)
{
Cvar_Toggle_f_base(true);
}
void Cvar_Toggle (cvar_t *var)
{
Cvar_SetValue (var, (var->value == 0) ? 1 : 0);
}
int Cvar_CvarCompare (const void *p1, const void *p2)
{
return strcmp((*((cvar_t **) p1))->name, (*((cvar_t **) p2))->name);
}
void Cvar_CvarList(qbool use_regex)
{
static cvar_t *sorted_cvars[4096];
int i, c, m = 0, count;
cvar_t *var;
char *pattern;
#define MAX_SORTED_CVARS (sizeof (sorted_cvars) / sizeof (sorted_cvars[0]))
for (var = cvar_vars, count = 0; var && count < MAX_SORTED_CVARS; var = var->next, count++) {
sorted_cvars[count] = var;
}
qsort(sorted_cvars, count, sizeof(cvar_t *), Cvar_CvarCompare);
if (count == MAX_SORTED_CVARS) {
assert(!"count == MAX_SORTED_CVARS");
}
pattern = (Cmd_Argc() > 1) ? Cmd_Argv(1) : NULL;
if (((c = Cmd_Argc()) > 1) && use_regex) {
if (!ReSearchInitEx(Cmd_Argv(1), false)) {
return;
}
}
Con_Printf("List of cvars:\n");
for (i = 0; i < count; i++) {
var = sorted_cvars[i];
if (use_regex) {
if (!(c == 1 || ReSearchMatch(var->name))) {
continue;
}
}
else {
if (pattern && !Q_glob_match(pattern, var->name)) {
continue;
}
}
Con_Printf("%c%c %s\n",
#ifndef SERVERONLY
var->flags & CVAR_USERINFO ? 'u' :
#endif
' ',
#ifndef CLIENTONLY
var->flags & CVAR_SERVERINFO ? 's' :
#endif
' ',
var->name);
m++;
}
if (use_regex && (c > 1)) {
ReSearchDone();
}
Con_Printf("------------\n%i/%i %svariables\n", m, count, (pattern) ? "matching " : "");
}
void Cvar_CvarList_f (void)
{
Cvar_CvarList (false);
}
void Cvar_CvarList_re_f (void)
{
Cvar_CvarList (true);
}
static void Cvar_CvarEdit_f(void)
{
cvar_t* cvar;
char *s, final_string[MAXCMDLINE - 1];
int c;
c = Cmd_Argc();
if (c == 1) {
Com_Printf("%s <name> : modify a cvar\n", Cmd_Argv(0));
Com_Printf("cvarlist : list all cvars\n");
return;
}
cvar = Cvar_Find(Cmd_Argv(1));
if (cvar == NULL) {
Com_Printf("\"%s\" not a valid cvar\n", Cmd_Argv(1));
return;
}
else {
s = Q_strdup(cvar->string);
}
snprintf(final_string, sizeof(final_string), "/%s \"%s\"", Cmd_Argv(1), s);
Key_ClearTyping();
memcpy(key_lines[edit_line] + 1, str2wcs(final_string), strlen(final_string) * sizeof(wchar));
Q_free(s);
}
cvar_t *Cvar_Create(const char *name, const char *string, int cvarflags)
{
cvar_t *v;
int key;
v = Cvar_Find(name);
if (v) {
#ifndef SERVERONLY
v->flags &= ~CVAR_TEMP;
#endif
v->flags |= cvarflags;
return v;
}
v = (cvar_t *)Q_malloc(sizeof(cvar_t));
// Cvar doesn't exist, so we create it
v->next = cvar_vars;
cvar_vars = v;
key = Com_HashKey(name) % VAR_HASHPOOL_SIZE;
v->hash_next = cvar_hash[key];
cvar_hash[key] = v;
v->name = Q_strdup_named(name, name);
v->string = Q_strdup_named(string, name);
v->value = Q_atof(v->string);
v->flags = cvarflags;
#ifndef SERVERONLY
v->flags |= CVAR_USER_CREATED;
v->defaultvalue = Q_strdup_named(string, name);
if (cbuf_current == &cbuf_server) {
v->flags |= CVAR_MOD_CREATED;
}
v->integer = Q_atoi(v->string);
StringToRGB_W(v->string, v->color);
v->modified = true;
#endif
return v;
}
// returns true if the cvar was found (and deleted)
qbool Cvar_Delete(const char *name)
{
cvar_t *var, *prev = NULL;
int key = Com_HashKey(name) % VAR_HASHPOOL_SIZE;
for (var = cvar_hash[key]; var; var = var->hash_next) {
if (!strcasecmp(var->name, name)) {
// unlink from hash
if (prev) {
prev->hash_next = var->hash_next;
}
else {
cvar_hash[key] = var->hash_next;
}
break;
}
prev = var;
}
if (!var) {
return false;
}
prev = NULL;
for (var = cvar_vars; var; var = var->next) {
if (!strcasecmp(var->name, name)) {
// unlink from cvar list
if (prev) {
prev->next = var->next;
}
else {
cvar_vars = var->next;
}
#ifndef SERVERONLY
Q_free(var->defaultvalue);
#endif
// free
Q_free(var->string);
Q_free(var->name);
Q_free(var);
return true;
}
prev = var;
}
Sys_Error("Cvar list broken");
return false;
}
void Cvar_Set_f (void)
{
cvar_t *var;
char *var_name;
if (Cmd_Argc() != 3) {
Con_Printf ("Usage: %s <cvar> <value>\n", Cmd_Argv(0));
return;
}
var_name = Cmd_Argv (1);
var = Cvar_Find (var_name);
if (var) {
Cvar_Set(var, Cmd_Argv(2));
}
else {
if (Cmd_Exists(var_name)) {
Con_Printf("\"%s\" is a command\n", var_name);
return;
}
var = Cvar_Create (var_name, Cmd_Argv(2), cbuf_current == &cbuf_server ? CVAR_MOD_CREATED : CVAR_USER_CREATED);
}
}
void Cvar_Inc_f(void)
{
int c;
cvar_t *var;
float delta;
c = Cmd_Argc();
if (c != 2 && c != 3) {
Con_Printf("Usage: inc <cvar> [value]\n");
return;
}
var = Cvar_Find(Cmd_Argv(1));
if (!var) {
Con_Printf("Unknown variable \"%s\"\n", Cmd_Argv(1));
return;
}
delta = (c == 3) ? atof(Cmd_Argv(2)) : 1;
Cvar_SetValue(var, var->value + delta);
}
int Cvar_GetFlags (cvar_t *var)
{
return var->flags;
}
#ifndef SERVERONLY
qbool Cvar_ForceCallback(cvar_t *var)
{
qbool cancel = false;
if (!var || !var->OnChange)
return cancel;
var->OnChange(var, var->string, &cancel);
return cancel;
}
void Cvar_ResetVar (cvar_t *var)
{
if (var && strcmp(var->string, var->defaultvalue)) {
Cvar_Set(var, var->defaultvalue);
}
}
void Cvar_Reset(qbool use_regex)
{
qbool re_search = false;
cvar_t *var;
char *name;
int i;
if (Cmd_Argc() < 2) {
Com_Printf("%s <cvar> [<cvar2>..]: reset variable to it default value\n", Cmd_Argv(0));
return;
}
for (i = 1; i < Cmd_Argc(); i++) {
name = Cmd_Argv(i);
if (use_regex && (re_search = IsRegexp(name))) {
if (!ReSearchInitEx(name, false)) {
continue;
}
}
if (use_regex && re_search) {
for (var = cvar_vars; var; var = var->next) {
if (ReSearchMatch(var->name)) {
Cvar_ResetVar(var);
}
}
}
else {
if ((var = Cvar_Find(name))) {
Cvar_ResetVar(var);
}
else {
Com_Printf("%s : No variable with name %s\n", Cmd_Argv(0), name);
}
}
if (use_regex && re_search) {
ReSearchDone();
}
}
}
void Cvar_Reset_f (void)
{
Cvar_Reset(false);
}
void Cvar_Reset_re_f (void)
{
Cvar_Reset(true);
}
void Cvar_SetDefaultAndValue(cvar_t *var, float default_value, float actual_value)
{
char val[128];
int i;
snprintf (val, sizeof(val), "%f", default_value);
for (i = strlen(val) - 1; i > 0 && val[i] == '0'; i--)
val[i] = 0;
if (val[i] == '.')
val[i] = 0;
Q_free(var->defaultvalue);
var->defaultvalue = Q_strdup_named(val, var->name);
Cvar_SetValue(var, actual_value);
}
char *Cvar_CompleteVariable (char *partial)
{
cvar_t *cvar;
int len;
if (!(len = strlen(partial)))
return NULL;
// check exact match
for (cvar = cvar_vars; cvar; cvar = cvar->next)
if (!strcasecmp (partial,cvar->name))
return cvar->name;
// check partial match
for (cvar = cvar_vars; cvar; cvar = cvar->next)
if (!strncasecmp (partial,cvar->name, len))
return cvar->name;
return NULL;
}
int Cvar_CompleteCountPossible (char *partial)
{
cvar_t *cvar;
int len, c = 0;
if (!(len = strlen(partial)))
return 0;
// check partial match
for (cvar = cvar_vars; cvar; cvar = cvar->next)
if (!strncasecmp (partial, cvar->name, len))
c++;
return c;
}
void Cvar_RulesetSet(cvar_t *var, char *val, int m)
{
float rulesetval_f = Q_atof (val);
switch (m) {
case 0:
var->minrulesetvalue = rulesetval_f;
break;
case 1:
var->maxrulesetvalue = rulesetval_f;
break;
case 2:
var->minrulesetvalue = rulesetval_f;
var->maxrulesetvalue = rulesetval_f;
break;
default:
return;
}
}
void Cvar_AutoSet(cvar_t *var, char *value)
{
if (!var || !(var->flags & CVAR_AUTO) || !value) {
return;
}
Q_free(var->autoString);
var->autoString = Q_strdup(value);
var->flags |= CVAR_AUTOSETRECENT;
}
void Cvar_AutoSetInt(cvar_t *var, int value)
{
char val[128];
if (!var || !(var->flags & CVAR_AUTO)) {
return;
}
Q_free(var->autoString);
snprintf(&val[0], sizeof(val), "%d", value);