7 void RegisterCvars(void(string name, string def, string desc, bool archive, string file) f) {}
9 bool cvar_value_issafe(string s)
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;
20 /** escape the string to make it safe for consoles */
21 string MakeConsoleSafe(string input)
23 input = strreplace("\n", "", input);
24 input = strreplace("\\", "\\\\", input);
25 input = strreplace("$", "$$", input);
26 input = strreplace("\"", "\\\"", input);
30 void cvar_describe(string name, string desc)
32 localcmd(sprintf("\nset %1$s \"$%1$s\" \"%2$s\"\n", name, MakeConsoleSafe(desc)));
35 void cvar_archive(string name)
37 localcmd(sprintf("\nseta %1$s \"$%1$s\"\n", name));
40 void RegisterCvars_Set(string name, string def, string desc, bool archive, string file)
42 cvar_describe(name, desc);
43 if (archive) cvar_archive(name);
46 int RegisterCvars_Save_fd;
47 void RegisterCvars_Save(string name, string def, string desc, bool archive, string file)
50 fputs(RegisterCvars_Save_fd, sprintf("seta %s \"%s\"\n", name, def));
53 STATIC_INIT_LATE(Cvars)
55 RegisterCvars(RegisterCvars_Set);
56 RegisterCvars_Save_fd = fopen(sprintf("default%s.cfg", PROGNAME), FILE_WRITE);
57 if (RegisterCvars_Save_fd >= 0)
59 RegisterCvars(RegisterCvars_Save);
60 fclose(RegisterCvars_Save_fd);
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';
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))
76 #define __AUTOCVAR(file, archive, var, type, desc, default) \
77 [[accumulate]] void RegisterCvars(void(string, string, string, bool, string) f) \
79 f( #var, repr_cvar_##type(default), desc, archive, file); \
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__)