From 8aef549cfc6c7e63939da5fdb617ee3990fefede Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 3 Aug 2025 16:24:05 +0200 Subject: [PATCH] restore optional freeslot stuff --- src/lua_script.c | 73 ++++++++++++++++++++++++++++++++++++++++-------- src/lua_script.h | 2 ++ 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/lua_script.c b/src/lua_script.c index 332353bfb..406e4c660 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -366,7 +366,18 @@ void LUA_DumpFile(const char *filename) } #endif -fixed_t LUA_EvalMath(const char *word) +static void createMathLibState(void) +{ + mL = lua_newstate(LUA_Alloc, NULL); + lua_atpanic(mL, LUA_Panic); + + // open only enum lib + lua_pushcfunction(mL, LUA_EnumLib); + lua_pushboolean(mL, true); + lua_call(mL, 1, 0); +} + +fixed_t LUA_EvalMathEx(const char *word, const char **error) { char buf[1024], *b; const char *p; @@ -375,15 +386,7 @@ fixed_t LUA_EvalMath(const char *word) // make a new state so SOC can't interefere with scripts // allocate state if (mL == NULL) - { - mL = lua_newstate(LUA_Alloc, NULL); - lua_atpanic(mL, LUA_Panic); - - // open only enum lib - lua_pushcfunction(mL, LUA_EnumLib); - lua_pushboolean(mL, true); - lua_call(mL, 1, 0); - } + createMathLibState(); // change ^ into ^^ for Lua. strcpy(buf, "return "); @@ -394,6 +397,9 @@ fixed_t LUA_EvalMath(const char *word) if (*p == '^') *b++ = '^'; } + // length of word is zero!? + if (p == word) + return 0; *b = '\0'; // eval string. @@ -409,7 +415,7 @@ fixed_t LUA_EvalMath(const char *word) p += 3; // "1: " } - CONS_Alert(CONS_WARNING, "%s\n", p); + if (error) *error = p; } else res = lua_tointeger(mL, -1); @@ -417,6 +423,51 @@ fixed_t LUA_EvalMath(const char *word) return res; } +fixed_t LUA_EvalMath(const char *word) +{ + const char *error = NULL; + fixed_t res = LUA_EvalMathEx(word, &error); + + if (error) + CONS_Alert(CONS_WARNING, "%s\n", error); + + return res; +} + +// A hack to prevent lua from crashing game if constant doesn't exist +static int lua_get_constant(lua_State *L) +{ + lua_pushvalue(mL, LUA_GLOBALSINDEX); + lua_pushvalue(L, 1); + lua_gettable(L, -2); + + return 1; +} + +fixed_t LUA_GetConstant(const char *word) +{ + if (mL == NULL) + createMathLibState(); + + fixed_t res = 0; + + lua_pushcfunction(mL, lua_get_constant); + lua_pushstring(mL, word); + + if (lua_pcall(mL, 1, 1, 0) == 0) + { + if (lua_isnumber(mL, -1)) + res = lua_tointeger(mL, -1); + } + else + { + // Pop error message + lua_pop(mL, 1); + } + + return res; +} + void LUA_InvalidateMathlibCache(const char *name) { // No state => no cache => nothing to invalidate :3 diff --git a/src/lua_script.h b/src/lua_script.h index ccbef2b45..a8f102522 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -48,7 +48,9 @@ void LUA_LoadLump(UINT16 wad, UINT16 lump); #ifdef LUA_ALLOW_BYTECODE void LUA_DumpFile(const char *filename); #endif +fixed_t LUA_EvalMathEx(const char *word, const char **error); fixed_t LUA_EvalMath(const char *word); +fixed_t LUA_GetConstant(const char *word); // Need better name for this ;-; void LUA_InvalidateMathlibCache(const char *name);