From 5d08bfd7068997edbc435d1e50dfd3ae8a402bc8 Mon Sep 17 00:00:00 2001 From: James R Date: Wed, 15 Mar 2023 11:43:53 -0700 Subject: [PATCH] Rename CV_NOLUA to CV_ALLOWLUA, opt IN to Lua mutability --- src/command.c | 19 ++++++++++++++++++- src/command.h | 2 +- src/deh_tables.c | 2 +- src/lua_consolelib.c | 5 +++-- src/screen.c | 2 +- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/command.c b/src/command.c index de9da232e..e027b45f3 100644 --- a/src/command.c +++ b/src/command.c @@ -57,6 +57,7 @@ static boolean CV_FilterVarByVersion(consvar_t *v, const char *valstr); static boolean CV_Command(void); consvar_t *CV_FindVar(const char *name); static const char *CV_StringValue(const char *var_name); +static boolean CV_Immutable(const consvar_t *var); static consvar_t *consvar_vars; // list of registered console variables static UINT16 consvar_number_of_netids = 0; @@ -2371,7 +2372,7 @@ static boolean CV_Command(void) if (!v) return false; - if (( com_flags & COM_SAFE ) && ( v->flags & CV_NOLUA )) + if (CV_Immutable(v)) { CONS_Alert(CONS_WARNING, "Variable '%s' cannot be changed from Lua.\n", v->name); return true; @@ -2460,6 +2461,22 @@ void CV_SaveVariables(FILE *f) } } +// Returns true if this cvar cannot be modified in current context. +// Such as if the cvar does not have CV_ALLOWLUA. +static boolean CV_Immutable(const consvar_t *var) +{ + // Currently operating from Lua + if (com_flags & COM_SAFE) + { + if (!(var->flags & CV_ALLOWLUA)) + { + return true; + } + } + + return false; +} + //============================================================================ // SCRIPT PARSE //============================================================================ diff --git a/src/command.h b/src/command.h index 30d7e5bbe..48827f99f 100644 --- a/src/command.h +++ b/src/command.h @@ -120,7 +120,7 @@ typedef enum // can only be set when we have the pointer to it // used on menus CV_CHEAT = 2048, // Don't let this be used in multiplayer unless cheats are on. - CV_NOLUA = 4096,/* don't let this be called from Lua */ + CV_ALLOWLUA = 4096,/* Let this be called from Lua */ } cvflags_t; typedef struct CV_PossibleValue_s diff --git a/src/deh_tables.c b/src/deh_tables.c index 4a3467f78..1031f44fa 100644 --- a/src/deh_tables.c +++ b/src/deh_tables.c @@ -5525,7 +5525,7 @@ struct int_const_s const INT_CONST[] = { {"CV_HIDEN",CV_HIDEN}, {"CV_HIDDEN",CV_HIDEN}, {"CV_CHEAT",CV_CHEAT}, - {"CV_NOLUA",CV_NOLUA}, + {"CV_ALLOWLUA",CV_ALLOWLUA}, // v_video flags {"V_NOSCALEPATCH",V_NOSCALEPATCH}, diff --git a/src/lua_consolelib.c b/src/lua_consolelib.c index 816051199..f5e98e920 100644 --- a/src/lua_consolelib.c +++ b/src/lua_consolelib.c @@ -450,6 +450,7 @@ static int lib_cvRegisterVar(lua_State *L) return luaL_error(L, M_GetText("Variable %s has CV_CALL without a function"), cvar->name); } + cvar->flags |= CV_ALLOWLUA; // actually time to register it to the console now! Finally! cvar->flags |= CV_MODIFIED; CV_RegisterVar(cvar); @@ -478,7 +479,7 @@ static int CVarSetFunction ){ consvar_t *cvar = *(consvar_t **)luaL_checkudata(L, 1, META_CVAR); - if (cvar->flags & CV_NOLUA) + if (!(cvar->flags & CV_ALLOWLUA)) return luaL_error(L, "Variable '%s' cannot be set from Lua.", cvar->name); switch (lua_type(L, 2)) @@ -510,7 +511,7 @@ static int lib_cvAddValue(lua_State *L) { consvar_t *cvar = *(consvar_t **)luaL_checkudata(L, 1, META_CVAR); - if (cvar->flags & CV_NOLUA) + if (!(cvar->flags & CV_ALLOWLUA)) return luaL_error(L, "Variable %s cannot be set from Lua.", cvar->name); CV_AddValue(cvar, (INT32)luaL_checknumber(L, 2)); diff --git a/src/screen.c b/src/screen.c index 3842a365d..501ee358d 100644 --- a/src/screen.c +++ b/src/screen.c @@ -82,7 +82,7 @@ CV_PossibleValue_t cv_renderer_t[] = { {0, NULL} }; -consvar_t cv_renderer = CVAR_INIT ("renderer", "Software", CV_SAVE|CV_NOLUA|CV_CALL, cv_renderer_t, SCR_ChangeRenderer); +consvar_t cv_renderer = CVAR_INIT ("renderer", "Software", CV_SAVE|CV_CALL, cv_renderer_t, SCR_ChangeRenderer); static void SCR_ChangeFullscreen(void);