From 71ab72c85e726639f288a987e267f005afb18b99 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Fri, 24 Jan 2025 18:25:04 +0100 Subject: [PATCH] Add lua_compatmode --- src/lua_hooklib.c | 12 ++++++++++++ src/lua_script.c | 7 +++++++ src/lua_script.h | 1 + src/w_wad.c | 26 +++++++++++++------------- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index a11aac517..b8e05b8b0 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -65,6 +65,9 @@ static int nextid; // After a hook errors once, don't print the error again. static UINT8 * hooksErrored; +// compat mode for each hook +static UINT8 * hookCompat; + static int errorRef; static boolean mobj_hook_available(int hook_type, mobjtype_t mobj_type) @@ -187,10 +190,17 @@ static void add_hook_ref(lua_State *L, int idx) BIT_ARRAY_SIZE (nextid + 1) * sizeof *hooksErrored, PU_STATIC, &hooksErrored); hooksErrored[nextid >> 3] = 0; + Z_Realloc(hookCompat, + BIT_ARRAY_SIZE (nextid + 1) * sizeof *hookCompat, + PU_STATIC, &hookCompat); + hookCompat[nextid >> 3] = 0; } Z_Realloc(hookRefs, (nextid + 1) * sizeof *hookRefs, PU_STATIC, &hookRefs); + if (lua_compatmode) + set_bit_array(hookCompat, nextid); + // set the hook function in the registry. lua_pushvalue(L, idx); hookRefs[nextid++] = luaL_ref(L, LUA_REGISTRYINDEX); @@ -388,6 +398,7 @@ static void init_hook_call static void get_hook(Hook_State *hook, const int *ids, int n) { hook->id = ids[n]; + lua_compatmode = in_bit_array(hookCompat, hook->id); lua_getref(gL, hookRefs[hook->id]); } @@ -396,6 +407,7 @@ static void get_hook_from_table(Hook_State *hook, int n) lua_rawgeti(gL, -1, n); hook->id = lua_tonumber(gL, -1); lua_pop(gL, 1); + lua_compatmode = in_bit_array(hookCompat, hook->id); lua_getref(gL, hookRefs[hook->id]); } diff --git a/src/lua_script.c b/src/lua_script.c index ec2b15dfa..4833e5d9f 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -150,6 +150,8 @@ int LUA_Call(lua_State *L, int nargs, int nresults, int errorhandlerindex) return err; } +boolean lua_compatmode = false; + // Moved here from lib_getenum. int LUA_PushGlobals(lua_State *L, const char *word) { @@ -408,6 +410,9 @@ int LUA_PushGlobals(lua_State *L, const char *word) } else if (fastcmp(word, "gamestate")) { lua_pushinteger(L, gamestate); return 1; + } else if (fastcmp(word, "compatmode")) { + lua_pushboolean(L, lua_compatmode); + return 1; } return 0; @@ -640,6 +645,8 @@ void LUA_LoadLump(UINT16 wad, UINT16 lump, boolean noresults) name[len] = '\0'; } + lua_compatmode = wadfiles[wad]->compatmode; + LUA_LoadFile(&f, name, noresults); // actually load file! // Okay, we've modified the game beyond the point of no return. diff --git a/src/lua_script.h b/src/lua_script.h index 9bfddc875..179171eec 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -48,6 +48,7 @@ void LUA_ClearExtVars(void); void LUA_ClearState(void); extern INT32 lua_lumploading; // is LUA_LoadLump being called? +extern boolean lua_compatmode; // compatmode enabled for this lump/hook? int LUA_GetErrorMessage(lua_State *L); int LUA_Call(lua_State *L, int nargs, int nresults, int errorhandlerindex); diff --git a/src/w_wad.c b/src/w_wad.c index 49efb9cf2..9843958d1 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -879,6 +879,19 @@ UINT16 W_InitFile(const char *filename, boolean mainfile, boolean startup) } #endif // HWRENDER + // check if compatmode is needed + switch (wadfile->type) + { + case RET_WAD: + wadfile->compatmode = CheckCompatWad(numwadfiles - 1); + break; + case RET_PK3: + wadfile->compatmode = CheckCompatZip(numwadfiles - 1); + break; + default: + break; + } + // TODO: HACK ALERT - Load Lua & SOC stuff right here. I feel like this should be out of this place, but... Let's stick with this for now. switch (wadfile->type) { @@ -899,19 +912,6 @@ UINT16 W_InitFile(const char *filename, boolean mainfile, boolean startup) break; } - // check if compatmode is needed - switch (wadfile->type) - { - case RET_WAD: - wadfile->compatmode = CheckCompatWad(numwadfiles - 1); - break; - case RET_PK3: - wadfile->compatmode = CheckCompatZip(numwadfiles - 1); - break; - default: - break; - } - if (refreshdirmenu & REFRESHDIR_GAMEDATA) G_LoadGameData(); DEH_UpdateMaxFreeslots();