8 void RegisterCvars(void(string name, string def, string desc, bool archive, string file) f) {}
11 bool cvar_value_issafe(string s)
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;
22 /** escape the string to make it safe for consoles */
24 string MakeConsoleSafe(string input)
26 input = strreplace("\n", "", input);
27 input = strreplace("\\", "\\\\", input);
28 input = strreplace("$", "$$", input);
29 input = strreplace("\"", "\\\"", input);
34 * Evaluate an expression of the form: [+ | -]? [var[op]val | [op]var | val | var] ...
35 * +: all must match. this is the default
36 * -: one must NOT match
48 bool expr_evaluate(string s)
51 if (str2chr(s, 0) == '+') {
52 s = substring(s, 1, -1);
53 } else if (str2chr(s, 0) == '-') {
55 s = substring(s, 1, -1);
57 bool expr_fail = false;
58 for (int i = 0, n = tokenize_console(s); i < n; ++i) {
68 #define BINOP(op, len, expr) \
69 if ((o = strstrofs(s, op, 0)) >= 0) { \
70 k = substring(s, 0, o); \
71 v = substring(s, o + len, -1); \
74 BINOP(">=", 2, cvar(k) >= stof(v));
75 BINOP("<=", 2, cvar(k) <= stof(v));
76 BINOP(">", 1, cvar(k) > stof(v));
77 BINOP("<", 1, cvar(k) < stof(v));
78 BINOP("==", 2, cvar(k) == stof(v));
79 BINOP("!=", 2, cvar(k) != stof(v));
80 BINOP("===", 3, cvar_string(k) == v);
81 BINOP("!==", 3, cvar_string(k) != v);
85 if (str2chr(k, 0) == '!') {
86 k = substring(s, 1, -1);
90 bool isnum = ftos(f) == k;
91 X(boolean(isnum ? f : cvar(k)) == b);
99 // now ret is true if we want to keep the item, and false if we want to get rid of it
104 void RegisterCvars_Set(string name, string def, string desc, bool archive, string file)
106 localcmd(sprintf("\nset %1$s \"$%1$s\" \"%2$s\"\n", name, MakeConsoleSafe(desc)));
108 localcmd(sprintf("\nseta %1$s \"$%1$s\"\n", name));
111 int RegisterCvars_Save_fd;
113 void RegisterCvars_Save(string name, string def, string desc, bool archive, string file)
115 if (!archive) return;
116 fputs(RegisterCvars_Save_fd, sprintf("seta %s \"%s\"\n", name, def));
119 STATIC_INIT_LATE(Cvars)
121 RegisterCvars(RegisterCvars_Set);
122 RegisterCvars_Save_fd = fopen(sprintf("default%s.cfg", PROGNAME), FILE_WRITE);
123 if (RegisterCvars_Save_fd >= 0)
125 RegisterCvars(RegisterCvars_Save);
126 fclose(RegisterCvars_Save_fd);
130 const noref bool default_bool = false;
131 const noref int default_int = 0;
132 const noref float default_float = 0;
133 const noref string default_string = "";
134 const noref vector default_vector = '0 0 0';
136 #define repr_cvar_bool(x) ((x) ? "1" : "0")
137 #define repr_cvar_int(x) (ftos(x))
138 #define repr_cvar_float(x) (ftos(x))
139 #define repr_cvar_string(x) (x)
140 #define repr_cvar_vector(x) (sprintf("%v", x))
143 // void AUTOCVAR(<cvar_name>, <qc_var_type>, default_cvar_value, string desc)
144 // void AUTOCVAR_SAVE(<cvar_name>, <qc_var_type>, default_cvar_value, string desc)
145 // where default_cvar_value has type <qc_var_type>
146 // e.g.: AUTOCVAR(mycvar, float, 2.5, "cvar description")
148 #define __AUTOCVAR(file, archive, var, type, desc, default) \
149 ACCUMULATE void RegisterCvars(void(string, string, string, bool, string) f) \
151 f( #var, repr_cvar_##type(default), desc, archive, file); \
153 type autocvar_##var = default
154 #define AUTOCVAR_5(file, archive, var, type, desc) \
155 __AUTOCVAR(file, archive, var, type, desc, default_##type)
156 #define AUTOCVAR_6(file, archive, var, type, default, desc) \
157 __AUTOCVAR(file, archive, var, type, desc, default)
158 #define _AUTOCVAR(...) EVAL__AUTOCVAR(OVERLOAD(AUTOCVAR, __FILE__, __VA_ARGS__))
159 #define EVAL__AUTOCVAR(...) __VA_ARGS__
160 #define AUTOCVAR_SAVE(...) _AUTOCVAR(true, __VA_ARGS__)
161 #define AUTOCVAR(...) _AUTOCVAR(false, __VA_ARGS__)