]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/cvar.qh
Merge branch 'TimePath/unified_weapons' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / cvar.qh
1 #ifndef CVAR_H
2 #define CVAR_H
3
4 #include "nil.qh"
5 #include "static.qh"
6
7 void RegisterCvars(void(string name, string def, string desc, bool archive, string file) f) { }
8
9 void RegisterCvars_Set(string name, string def, string desc, bool archive, string file)
10 {
11     string val = string_null;
12     if (cvar_type(name) & CVAR_TYPEFLAG_EXISTS) {
13         val = cvar_string(name);
14         // Need to unset first to change the default
15         localcmd(sprintf("\nunset %s\n", name));
16     }
17     localcmd(sprintf("\n%s %s \"%s\" \"%s\"\n", (archive ? "seta" : "set"), name, def, desc));
18     if (val) {
19         localcmd(sprintf("\n%s \"%s\"\n", name, val));
20     }
21 }
22
23 #ifndef SVQC
24 STATIC_INIT_LATE(Cvars) { RegisterCvars(RegisterCvars_Set); }
25 #endif
26
27 const noref bool default_bool = false;
28 const noref int default_int = 0;
29 const noref float default_float = 0;
30 const noref string default_string = "";
31 const noref vector default_vector = '0 0 0';
32
33 #define repr_cvar_bool(x)   ((x) ? "1" : "0")
34 #define repr_cvar_int(x)    (ftos(x))
35 #define repr_cvar_float(x)  (ftos(x))
36 #define repr_cvar_string(x) (x)
37 #define repr_cvar_vector(x) (sprintf("%v", x))
38
39 #define __AUTOCVAR(file, archive, var, type, desc, default) \
40     [[accumulate]] void RegisterCvars(void(string, string, string, bool, string) f) { f(#var, repr_cvar_##type(default), desc, archive, file); } \
41     type autocvar_##var = default
42 #define AUTOCVAR_5(file, archive, var, type, desc) \
43     __AUTOCVAR(file, archive, var, type, desc, default_##type)
44 #define AUTOCVAR_6(file, archive, var, type, default, desc) \
45     __AUTOCVAR(file, archive, var, type, desc, default)
46 #define _AUTOCVAR(...) EVAL(OVERLOAD(AUTOCVAR, __FILE__, __VA_ARGS__))
47 #define AUTOCVAR_SAVE(...) _AUTOCVAR(true, __VA_ARGS__)
48 #define AUTOCVAR(...) _AUTOCVAR(false, __VA_ARGS__)
49
50 #endif