From 63eadaf83c53699490d025fcc16b15622d334c76 Mon Sep 17 00:00:00 2001 From: Latapostrophe Date: Tue, 10 Mar 2020 18:12:20 +0100 Subject: [PATCH] Expose CV_FindVar to Lua --- src/command.c | 4 ++-- src/command.h | 3 +++ src/lua_consolelib.c | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/command.c b/src/command.c index 6d9c86d3..dff83c2c 100644 --- a/src/command.c +++ b/src/command.c @@ -56,7 +56,7 @@ static void CV_EnforceExecVersion(void); static boolean CV_FilterVarByVersion(consvar_t *v, const char *valstr); static boolean CV_Command(void); -static consvar_t *CV_FindVar(const char *name); +consvar_t *CV_FindVar(const char *name); static const char *CV_StringValue(const char *var_name); static consvar_t *consvar_vars; // list of registered console variables @@ -1027,7 +1027,7 @@ static const char *cv_null_string = ""; * \return Pointer to the variable if found, or NULL. * \sa CV_FindNetVar */ -static consvar_t *CV_FindVar(const char *name) +consvar_t *CV_FindVar(const char *name) { consvar_t *cvar; diff --git a/src/command.h b/src/command.h index 6b5d513e..484cd426 100644 --- a/src/command.h +++ b/src/command.h @@ -173,4 +173,7 @@ void CV_ResetCheatNetVars(void); boolean CV_IsSetToDefault(consvar_t *v); UINT8 CV_CheatsEnabled(void); +// Returns cvar by name. Exposed here for Lua. +consvar_t *CV_FindVar(const char *name); + #endif // __COMMAND_H__ diff --git a/src/lua_consolelib.c b/src/lua_consolelib.c index 299870e0..0c73459c 100644 --- a/src/lua_consolelib.c +++ b/src/lua_consolelib.c @@ -413,6 +413,30 @@ static int lib_cvRegisterVar(lua_State *L) return 1; } +// For some reason I couldn't cherry pick this. +// Credits for this function go to james. All hail birb. -Lat' + +static int lib_cvFindVar(lua_State *L) +{ + consvar_t *cv; + if (( cv = CV_FindVar(luaL_checkstring(L,1)) )) + { + lua_settop(L,1);/* We only want one argument in the stack. */ + lua_pushlightuserdata(L, cv);/* Now the second value on stack. */ + luaL_getmetatable(L, META_CVAR); + /* + The metatable is the last value on the stack, so this + applies it to the second value, which is the cvar. + */ + lua_setmetatable(L,2); + lua_pushvalue(L,2); + return 1; + } + else + return 0; +} + + // CONS_Printf for a single player // Use 'print' in baselib for a global message. static int lib_consPrintf(lua_State *L) @@ -452,6 +476,7 @@ static luaL_Reg lib[] = { {"COM_BufInsertText", lib_comBufInsertText}, {"CV_RegisterVar", lib_cvRegisterVar}, {"CONS_Printf", lib_consPrintf}, + {"CV_FindVar", lib_cvFindVar}, {NULL, NULL} };