]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/cvar.qh
Merge branch 'master' into terencehill/menu_gametype_tooltips_2
[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 /** escape the string to make it safe for consoles */
10 string MakeConsoleSafe(string input)
11 {
12         input = strreplace("\n", "", input);
13         input = strreplace("\\", "\\\\", input);
14         input = strreplace("$", "$$", input);
15         input = strreplace("\"", "\\\"", input);
16         return input;
17 }
18
19 void RegisterCvars_Set(string name, string def, string desc, bool archive, string file)
20 {
21     string val = string_null;
22     if (cvar_type(name) & CVAR_TYPEFLAG_EXISTS) {
23         val = cvar_string(name);
24         // Need to unset first to change the default
25         localcmd(sprintf("\nunset %s\n", name));
26     }
27     localcmd(sprintf("\n%s %s \"%s\" \"%s\"\n", (archive ? "seta" : "set"), name, MakeConsoleSafe(def), MakeConsoleSafe(desc)));
28     if (val) {
29         localcmd(sprintf("\n%s \"%s\"\n", name, MakeConsoleSafe(val)));
30     }
31 }
32
33 #ifndef SVQC
34 STATIC_INIT_LATE(Cvars) { RegisterCvars(RegisterCvars_Set); }
35 #endif
36
37 const noref bool default_bool = false;
38 const noref int default_int = 0;
39 const noref float default_float = 0;
40 const noref string default_string = "";
41 const noref vector default_vector = '0 0 0';
42
43 #define repr_cvar_bool(x)   ((x) ? "1" : "0")
44 #define repr_cvar_int(x)    (ftos(x))
45 #define repr_cvar_float(x)  (ftos(x))
46 #define repr_cvar_string(x) (x)
47 #define repr_cvar_vector(x) (sprintf("%v", x))
48
49 #define __AUTOCVAR(file, archive, var, type, desc, default) \
50     [[accumulate]] void RegisterCvars(void(string, string, string, bool, string) f) { f(#var, repr_cvar_##type(default), desc, archive, file); } \
51     type autocvar_##var = default
52 #define AUTOCVAR_5(file, archive, var, type, desc) \
53     __AUTOCVAR(file, archive, var, type, desc, default_##type)
54 #define AUTOCVAR_6(file, archive, var, type, default, desc) \
55     __AUTOCVAR(file, archive, var, type, desc, default)
56 #define _AUTOCVAR(...) EVAL(OVERLOAD(AUTOCVAR, __FILE__, __VA_ARGS__))
57 #define AUTOCVAR_SAVE(...) _AUTOCVAR(true, __VA_ARGS__)
58 #define AUTOCVAR(...) _AUTOCVAR(false, __VA_ARGS__)
59
60 #endif