]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/cvar.qh
Merge branch 'terencehill/menu_optimization' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / cvar.qh
1 #pragma once
2
3 #include "nil.qh"
4 #include "progname.qh"
5 #include "static.qh"
6
7 void RegisterCvars(void(string name, string def, string desc, bool archive, string file) f) {}
8
9 bool cvar_value_issafe(string s)
10 {
11         if (strstrofs(s, "\"", 0) >= 0) return false;
12         if (strstrofs(s, "\\", 0) >= 0) return false;
13         if (strstrofs(s, ";", 0) >= 0) return false;
14         if (strstrofs(s, "$", 0) >= 0) return false;
15         if (strstrofs(s, "\r", 0) >= 0) return false;
16         if (strstrofs(s, "\n", 0) >= 0) return false;
17         return true;
18 }
19
20 /** escape the string to make it safe for consoles */
21 string MakeConsoleSafe(string input)
22 {
23         input = strreplace("\n", "", input);
24         input = strreplace("\\", "\\\\", input);
25         input = strreplace("$", "$$", input);
26         input = strreplace("\"", "\\\"", input);
27         return input;
28 }
29
30 void cvar_describe(string name, string desc)
31 {
32         localcmd(sprintf("\nset %1$s \"$%1$s\" \"%2$s\"\n", name, MakeConsoleSafe(desc)));
33 }
34
35 void cvar_archive(string name)
36 {
37         localcmd(sprintf("\nseta %1$s \"$%1$s\"\n", name));
38 }
39
40 void RegisterCvars_Set(string name, string def, string desc, bool archive, string file)
41 {
42         cvar_describe(name, desc);
43         if (archive) cvar_archive(name);
44 }
45
46 int RegisterCvars_Save_fd;
47 void RegisterCvars_Save(string name, string def, string desc, bool archive, string file)
48 {
49         if (!archive) return;
50         fputs(RegisterCvars_Save_fd, sprintf("seta %s \"%s\"\n", name, def));
51 }
52
53 STATIC_INIT_LATE(Cvars)
54 {
55         RegisterCvars(RegisterCvars_Set);
56         RegisterCvars_Save_fd = fopen(sprintf("default%s.cfg", PROGNAME), FILE_WRITE);
57         if (RegisterCvars_Save_fd >= 0)
58         {
59                 RegisterCvars(RegisterCvars_Save);
60                 fclose(RegisterCvars_Save_fd);
61         }
62 }
63
64 const noref bool default_bool = false;
65 const noref int default_int = 0;
66 const noref float default_float = 0;
67 const noref string default_string = "";
68 const noref vector default_vector = '0 0 0';
69
70 #define repr_cvar_bool(x) ((x) ? "1" : "0")
71 #define repr_cvar_int(x) (ftos(x))
72 #define repr_cvar_float(x) (ftos(x))
73 #define repr_cvar_string(x) (x)
74 #define repr_cvar_vector(x) (sprintf("%v", x))
75
76 #define __AUTOCVAR(file, archive, var, type, desc, default) \
77         [[accumulate]] void RegisterCvars(void(string, string, string, bool, string) f) \
78         { \
79                 f( #var, repr_cvar_##type(default), desc, archive, file); \
80         } \
81         type autocvar_##var = default
82 #define AUTOCVAR_5(file, archive, var, type, desc) \
83         __AUTOCVAR(file, archive, var, type, desc, default_##type)
84 #define AUTOCVAR_6(file, archive, var, type, default, desc) \
85         __AUTOCVAR(file, archive, var, type, desc, default)
86 #define _AUTOCVAR(...) EVAL__AUTOCVAR(OVERLOAD(AUTOCVAR, __FILE__, __VA_ARGS__))
87 #define EVAL__AUTOCVAR(...) __VA_ARGS__
88 #define AUTOCVAR_SAVE(...) _AUTOCVAR(true, __VA_ARGS__)
89 #define AUTOCVAR(...) _AUTOCVAR(false, __VA_ARGS__)