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 void RegisterCvars_Set(string name, string def, string desc, bool archive, string file)
36 localcmd(sprintf("\nset %1$s \"$%1$s\" \"%2$s\"\n", name, MakeConsoleSafe(desc)));
38 localcmd(sprintf("\nseta %1$s \"$%1$s\"\n", name));
41 int RegisterCvars_Save_fd;
43 void RegisterCvars_Save(string name, string def, string desc, bool archive, string file)
46 fputs(RegisterCvars_Save_fd, sprintf("seta %s \"%s\"\n", name, def));
49 STATIC_INIT_LATE(Cvars)
51 RegisterCvars(RegisterCvars_Set);
52 RegisterCvars_Save_fd = fopen(sprintf("default%s.cfg", PROGNAME), FILE_WRITE);
53 if (RegisterCvars_Save_fd >= 0)
55 RegisterCvars(RegisterCvars_Save);
56 fclose(RegisterCvars_Save_fd);
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';
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))
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")
78 #define __AUTOCVAR(file, archive, var, type, desc, default) \
79 ACCUMULATE void RegisterCvars(void(string, string, string, bool, string) f) \
81 f( #var, repr_cvar_##type(default), desc, archive, file); \
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__)