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