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 cvar_describe(string name, string desc)
36 localcmd(sprintf("\nset %1$s \"$%1$s\" \"%2$s\"\n", name, MakeConsoleSafe(desc)));
40 void cvar_archive(string name)
42 localcmd(sprintf("\nseta %1$s \"$%1$s\"\n", name));
46 void RegisterCvars_Set(string name, string def, string desc, bool archive, string file)
48 cvar_describe(name, desc);
49 if (archive) cvar_archive(name);
52 int RegisterCvars_Save_fd;
54 void RegisterCvars_Save(string name, string def, string desc, bool archive, string file)
57 fputs(RegisterCvars_Save_fd, sprintf("seta %s \"%s\"\n", name, def));
60 STATIC_INIT_LATE(Cvars)
62 RegisterCvars(RegisterCvars_Set);
63 RegisterCvars_Save_fd = fopen(sprintf("default%s.cfg", PROGNAME), FILE_WRITE);
64 if (RegisterCvars_Save_fd >= 0)
66 RegisterCvars(RegisterCvars_Save);
67 fclose(RegisterCvars_Save_fd);
71 const noref bool default_bool = false;
72 const noref int default_int = 0;
73 const noref float default_float = 0;
74 const noref string default_string = "";
75 const noref vector default_vector = '0 0 0';
77 #define repr_cvar_bool(x) ((x) ? "1" : "0")
78 #define repr_cvar_int(x) (ftos(x))
79 #define repr_cvar_float(x) (ftos(x))
80 #define repr_cvar_string(x) (x)
81 #define repr_cvar_vector(x) (sprintf("%v", x))
84 // void AUTOCVAR(<cvar_name>, <qc_var_type>, default_cvar_value, string desc)
85 // void AUTOCVAR_SAVE(<cvar_name>, <qc_var_type>, default_cvar_value, string desc)
86 // where default_cvar_value has type <qc_var_type>
87 // e.g.: AUTOCVAR(mycvar, float, 2.5, "cvar description")
89 #define __AUTOCVAR(file, archive, var, type, desc, default) \
90 ACCUMULATE void RegisterCvars(void(string, string, string, bool, string) f) \
92 f( #var, repr_cvar_##type(default), desc, archive, file); \
94 type autocvar_##var = default
95 #define AUTOCVAR_5(file, archive, var, type, desc) \
96 __AUTOCVAR(file, archive, var, type, desc, default_##type)
97 #define AUTOCVAR_6(file, archive, var, type, default, desc) \
98 __AUTOCVAR(file, archive, var, type, desc, default)
99 #define _AUTOCVAR(...) EVAL__AUTOCVAR(OVERLOAD(AUTOCVAR, __FILE__, __VA_ARGS__))
100 #define EVAL__AUTOCVAR(...) __VA_ARGS__
101 #define AUTOCVAR_SAVE(...) _AUTOCVAR(true, __VA_ARGS__)
102 #define AUTOCVAR(...) _AUTOCVAR(false, __VA_ARGS__)