summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMichael Smith <mikesmiffy128@gmail.com>2021-12-29 18:14:01 +0000
committerMichael Smith <mikesmiffy128@gmail.com>2021-12-29 18:18:42 +0000
commitd40d588f6d7cf866f7de41db9efffdd6c1a05135 (patch)
treecc1ff1eaecf3680815d7b30776f8b6264b0242e3
parent8eecc029568bbe8e2f3c0d9af218ad3f957251c9 (diff)
Make convar init macro type-generic
This removes the need to call atof() on each cvar on plugin load; now all that's required is the memory allocations for the string values. The syntax is also a bit nicer since numbers can just be numbers rather than quoted strings. Minor issue: specifying a string value that also happens to be numeric will break this since the numeric representation will be zero, but I can't see a reason this would ever happen. Also, add a DEF_CVAR_MAX just for completeness.
-rw-r--r--src/autojump.c2
-rw-r--r--src/build/cmeta.c2
-rw-r--r--src/con_.c4
-rw-r--r--src/con_.h13
-rw-r--r--src/demorec.c4
5 files changed, 16 insertions, 9 deletions
diff --git a/src/autojump.c b/src/autojump.c
index fa0f4b5..2a884c0 100644
--- a/src/autojump.c
+++ b/src/autojump.c
@@ -25,7 +25,7 @@
#include "os.h"
#include "vcall.h"
-DEF_CVAR(sst_autojump, "Jump upon hitting the group while holding space", "0",
+DEF_CVAR(sst_autojump, "Jump upon hitting the group while holding space", 0,
CON_REPLICATE | CON_DEMO | CON_HIDDEN)
struct vec3f { float x, y, z; };
diff --git a/src/build/cmeta.c b/src/build/cmeta.c
index b895253..157ba92 100644
--- a/src/build/cmeta.c
+++ b/src/build/cmeta.c
@@ -201,7 +201,7 @@ void cmeta_conmacros(const struct cmeta *cm, void (*cb)(const char *, bool)) {
bool isplusminus = false, isvar = false;
if (equal(tp, "DEF_CCMD_PLUSMINUS")) isplusminus = true;
else if (equal(tp, "DEF_CVAR") || equal(tp, "DEF_CVAR_MIN") ||
- equal(tp, "DEF_CVAR_MINMAX")) {
+ equal(tp, "DEF_CVAR_MAX") || equal(tp, "DEF_CVAR_MINMAX")) {
isvar = true;
}
else if (!equal(tp, "DEF_CCMD") && !equal(tp, "DEF_CCMD_HERE")) {
diff --git a/src/con_.c b/src/con_.c
index 54a8e9a..1c9d4a7 100644
--- a/src/con_.c
+++ b/src/con_.c
@@ -78,13 +78,11 @@ DECL_VFUNC_CDECL(void, ConsoleColorPrintf_l4d, 21, const struct con_colour *,
DECL_VFUNC_CDECL(void, ConsoleColorPrintf_p2, 24, const struct con_colour *,
const char *, ...)
-static void initval(struct con_var *v) {
+static inline void initval(struct con_var *v) {
// v->strlen is set to defaultval len in _DEF_CVAR so we don't need to call
// strlen() on each string :)
v->strval = extmalloc(v->strlen);
memcpy(v->strval, v->defaultval, v->strlen);
- // FIXME: technically this can be compile time too, using _Generic. Do that!
- v->fval = atof(v->strval); v->ival = v->fval;
}
// generated by build/codegen.c, defines regcmds() and freevars()
diff --git a/src/con_.h b/src/con_.h
index f6ef161..bfc8c47 100644
--- a/src/con_.h
+++ b/src/con_.h
@@ -225,10 +225,15 @@ extern void *_con_vtab_iconvar[];
}, \
.vtable_iconvar = _con_vtab_iconvar, \
.parent = &_cvar_##name_, /* bizarre, but how the engine does it */ \
- .defaultval = value, .strlen = sizeof("" value), \
+ .defaultval = _Generic(value, char *: value, int: #value, \
+ float: #value), \
+ .strlen = _Generic(value, char *: sizeof(value), \
+ default: sizeof(#value)), \
+ .fval = _Generic(value, char *: 0, int: value, float: value), \
+ .ival = _Generic(value, char *: 0, int: value, float: (int)value), \
.hasmin = hasmin_, .minval = (min), .hasmax = hasmax_, .maxval = (max) \
}; \
- struct con_var *name_ = (struct con_var *)&_cvar_##name_;
+ struct con_var *name_ = &_cvar_##name_;
/* Defines a console variable with no min/max values. */
#define DEF_CVAR(name, desc, value, flags) \
@@ -238,6 +243,10 @@ extern void *_con_vtab_iconvar[];
#define DEF_CVAR_MIN(name_, desc, value, min, flags_) \
_DEF_CVAR(name, desc, value, true, min, false, 0, flags)
+/* Defines a console variable with a given maximum numeric value. */
+#define DEF_CVAR_MAX(name_, desc, value, max, flags_) \
+ _DEF_CVAR(name, desc, value, false, 0, true, max, flags)
+
/* Defines a console variable in the given numeric value range. */
#define DEF_CVAR_MINMAX(name_, desc, value, min, max, flags_) \
_DEF_CVAR(name, desc, value, true, min, true, max, flags)
diff --git a/src/demorec.c b/src/demorec.c
index c4ac504..dda692f 100644
--- a/src/demorec.c
+++ b/src/demorec.c
@@ -49,8 +49,8 @@ static int nbits_datalen;
static int auto_demonum = 1;
static bool auto_recording = false;
-DEF_CVAR(sst_autorecord, "Continue recording demos through map changes",
- "1", CON_ARCHIVE | CON_HIDDEN)
+DEF_CVAR(sst_autorecord, "Continue recording demos through map changes", 1,
+ CON_ARCHIVE | CON_HIDDEN)
static void VCALLCONV hook_StopRecording(void *this) {
// This hook will get called twice per loaded save (in most games/versions,