From d60089839cea44827db5c7f72647becd9faa61ed Mon Sep 17 00:00:00 2001 From: TimePath Date: Mon, 31 Aug 2015 20:02:21 +1000 Subject: [PATCH] Make custom autocvar enumeration possible --- qcsrc/dpdefs/menudefs.qh | 2 ++ qcsrc/lib/Cvar.qh | 23 ++++++++++++++++------- qcsrc/lib/Registry.qh | 13 ------------- qcsrc/lib/Static.qh | 17 +++++++++++++++++ qcsrc/lib/_all.inc | 1 + qcsrc/menu/menu.qh | 2 -- 6 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 qcsrc/lib/Static.qh diff --git a/qcsrc/dpdefs/menudefs.qh b/qcsrc/dpdefs/menudefs.qh index 7d4dcc6af..c0e6d3ba7 100644 --- a/qcsrc/dpdefs/menudefs.qh +++ b/qcsrc/dpdefs/menudefs.qh @@ -19,6 +19,8 @@ #undef spawn +#define localcmd cmd + int(string str, string sub, int startpos) _strstrofs = #221; #define strstrofs _strstrofs int(string str, int ofs) _str2chr = #222; diff --git a/qcsrc/lib/Cvar.qh b/qcsrc/lib/Cvar.qh index 5eeafddce..914bed97a 100644 --- a/qcsrc/lib/Cvar.qh +++ b/qcsrc/lib/Cvar.qh @@ -1,15 +1,24 @@ #ifndef CVAR_H #define CVAR_H -#define CVAR_DESCRIBE(set, var, desc) localcmd(sprintf("\n"set" %1$s \"$%1$s\" \"%2$s\"\n", #var, desc)) +#include "Static.qh" -#define AUTOCVAR_4(set, var, type, desc) \ - STATIC_INIT(autocvar_##var) { CVAR_DESCRIBE(set, var, desc); } \ +void RegisterCvars(void(string name, string desc, bool archive) f) { } + +void RegisterCvars_Set(string name, string desc, bool archive) +{ + localcmd(sprintf("\n%1$s %2$s \"$%2$s\" \"%3$s\"\n", (archive ? "seta" : "set"), name, desc)); +} + +STATIC_INIT_LATE(Cvars) { RegisterCvars(RegisterCvars_Set); } + +#define AUTOCVAR_4(archive, var, type, desc) \ + [[accumulate]] void RegisterCvars(void(string, string, bool) f) { f(#var, desc, archive); } \ type autocvar_##var -#define AUTOCVAR_5(set, var, type, default, desc) \ - AUTOCVAR_4(set, var, type, desc) = default +#define AUTOCVAR_5(archive, var, type, default, desc) \ + AUTOCVAR_4(archive, var, type, desc) = default #define _AUTOCVAR(...) OVERLOAD(AUTOCVAR, __VA_ARGS__) -#define AUTOCVAR_SAVE(...) _AUTOCVAR("seta", __VA_ARGS__) -#define AUTOCVAR(...) _AUTOCVAR("set", __VA_ARGS__) +#define AUTOCVAR_SAVE(...) _AUTOCVAR(true, __VA_ARGS__) +#define AUTOCVAR(...) _AUTOCVAR(false, __VA_ARGS__) #endif diff --git a/qcsrc/lib/Registry.qh b/qcsrc/lib/Registry.qh index b9e2fd653..ca0beae9d 100644 --- a/qcsrc/lib/Registry.qh +++ b/qcsrc/lib/Registry.qh @@ -25,17 +25,4 @@ ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id) \ REGISTER_INIT(ns, id) -void __static_init_early() { } -void __static_init() { CALL_ACCUMULATED_FUNCTION(__static_init_early); } -#define static_init() CALL_ACCUMULATED_FUNCTION(__static_init) -#define REGISTER_REGISTRY(func) ACCUMULATE_FUNCTION(__static_init_early, func) - -#define _STATIC_INIT(where, func) \ - void _static_##func(); \ - ACCUMULATE_FUNCTION(where, _static_##func) \ - void _static_##func() - -#define STATIC_INIT(func) _STATIC_INIT(__static_init_early, func##_early) -#define STATIC_INIT_LATE(func) _STATIC_INIT(__static_init, func) - #endif diff --git a/qcsrc/lib/Static.qh b/qcsrc/lib/Static.qh new file mode 100644 index 000000000..d51a87175 --- /dev/null +++ b/qcsrc/lib/Static.qh @@ -0,0 +1,17 @@ +#ifndef STATIC_H +#define STATIC_H + +void __static_init_early() { } +void __static_init() { CALL_ACCUMULATED_FUNCTION(__static_init_early); } +#define static_init() CALL_ACCUMULATED_FUNCTION(__static_init) +#define REGISTER_REGISTRY(func) ACCUMULATE_FUNCTION(__static_init_early, func) + +#define _STATIC_INIT(where, func) \ + void _static_##func(); \ + ACCUMULATE_FUNCTION(where, _static_##func) \ + void _static_##func() + +#define STATIC_INIT(func) _STATIC_INIT(__static_init_early, func##_early) +#define STATIC_INIT_LATE(func) _STATIC_INIT(__static_init, func) + +#endif diff --git a/qcsrc/lib/_all.inc b/qcsrc/lib/_all.inc index 4e0b12f25..1b7e61e9a 100644 --- a/qcsrc/lib/_all.inc +++ b/qcsrc/lib/_all.inc @@ -20,6 +20,7 @@ #include "Progname.qh" #include "Registry.qh" #include "sortlist.qc" +#include "Static.qh" #include "String.qh" #include "Struct.qh" #include "test.qc" diff --git a/qcsrc/menu/menu.qh b/qcsrc/menu/menu.qh index e4f594f71..0ece40c88 100644 --- a/qcsrc/menu/menu.qh +++ b/qcsrc/menu/menu.qh @@ -11,8 +11,6 @@ #include "../common/constants.qh" #include "../common/util.qh" -#define localcmd cmd - // constants const int GAME_ISSERVER = 1; -- 2.39.2