restore optional freeslot stuff

This commit is contained in:
Alug committed 2025-08-03 16:24:35 +02:00
1 parent 8dd268b83e
commit 8aef549cfc
2 files changed
+64 -11

No files matched your search

+62 -11
View File
@@ -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
+2
View File
@@ -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);