From 1dbb75574323086cf7859584c9c4073361cf39a6 Mon Sep 17 00:00:00 2001 From: Louis-Antoine Date: Fri, 29 May 2020 17:35:07 +0200 Subject: [PATCH] Add dofile() to Lua --- src/blua/lbaselib.c | 26 ++++++++++++++++++++++++++ src/lua_script.c | 14 +++++++------- src/lua_script.h | 4 ++-- src/w_wad.c | 20 ++++++++++++++------ 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/blua/lbaselib.c b/src/blua/lbaselib.c index 3c919cb64..644565c28 100644 --- a/src/blua/lbaselib.c +++ b/src/blua/lbaselib.c @@ -11,6 +11,10 @@ #include #include +#include "../doomdef.h" +#include "../lua_script.h" +#include "../w_wad.h" + #define lbaselib_c #define LUA_LIB @@ -263,6 +267,27 @@ static int luaB_ipairs (lua_State *L) { } +// Edited to load PK3 entries instead +static int luaB_dofile (lua_State *L) { + const char *filename = luaL_checkstring(L, 1); + char fullfilename[256]; + UINT16 lumpnum; + int n = lua_gettop(L); + + if (wadfiles[numwadfiles - 1]->type != RET_PK3) + luaL_error(L, "dofile() only works with PK3 files"); + + snprintf(fullfilename, sizeof(fullfilename), "Lua/%s", filename); + lumpnum = W_CheckNumForFullNamePK3(fullfilename, numwadfiles - 1, 0); + if (lumpnum == INT16_MAX) + luaL_error(L, "can't find script " LUA_QS, fullfilename); + + LUA_LoadLump(numwadfiles - 1, lumpnum, false); + + return lua_gettop(L) - n; +} + + static int luaB_assert (lua_State *L) { luaL_checkany(L, 1); if (!lua_toboolean(L, 1)) @@ -380,6 +405,7 @@ static const luaL_Reg base_funcs[] = { {"assert", luaB_assert}, {"collectgarbage", luaB_collectgarbage}, {"error", luaB_error}, + {"dofile", luaB_dofile}, {"gcinfo", luaB_gcinfo}, {"getfenv", luaB_getfenv}, {"getmetatable", luaB_getmetatable}, diff --git a/src/lua_script.c b/src/lua_script.c index 4f94b007e..0e0361535 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -395,10 +395,10 @@ void LUA_ClearExtVars(void) // Use this variable to prevent certain functions from running // if they were not called on lump load // (i.e. they were called in hooks or coroutines etc) -boolean lua_lumploading = false; +INT32 lua_lumploading = 0; // Load a script from a MYFILE -static inline void LUA_LoadFile(MYFILE *f, char *name) +static inline void LUA_LoadFile(MYFILE *f, char *name, boolean noresults) { if (!name) name = wadfiles[f->wad]->filename; @@ -408,19 +408,19 @@ static inline void LUA_LoadFile(MYFILE *f, char *name) lua_pushinteger(gL, f->wad); lua_setfield(gL, LUA_REGISTRYINDEX, "WAD"); - lua_lumploading = true; // turn on loading flag + lua_lumploading++; // turn on loading flag - if (luaL_loadbuffer(gL, f->data, f->size, va("@%s",name)) || lua_pcall(gL, 0, 0, 0)) { + if (luaL_loadbuffer(gL, f->data, f->size, va("@%s",name)) || lua_pcall(gL, 0, noresults ? 0 : LUA_MULTRET, 0)) { CONS_Alert(CONS_WARNING,"%s\n",lua_tostring(gL,-1)); lua_pop(gL,1); } lua_gc(gL, LUA_GCCOLLECT, 0); - lua_lumploading = false; // turn off again + lua_lumploading--; // turn off again } // Load a script from a lump -void LUA_LoadLump(UINT16 wad, UINT16 lump) +void LUA_LoadLump(UINT16 wad, UINT16 lump, boolean noresults) { MYFILE f; char *name; @@ -447,7 +447,7 @@ void LUA_LoadLump(UINT16 wad, UINT16 lump) name[len] = '\0'; } - LUA_LoadFile(&f, name); // actually load file! + LUA_LoadFile(&f, name, noresults); // actually load file! free(name); Z_Free(f.data); diff --git a/src/lua_script.h b/src/lua_script.h index 8d5bed7c7..6815434c5 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -37,9 +37,9 @@ void LUA_ClearExtVars(void); #endif -extern boolean lua_lumploading; // is LUA_LoadLump being called? +extern INT32 lua_lumploading; // is LUA_LoadLump being called? -void LUA_LoadLump(UINT16 wad, UINT16 lump); +void LUA_LoadLump(UINT16 wad, UINT16 lump, boolean noresults); #ifdef LUA_ALLOW_BYTECODE void LUA_DumpFile(const char *filename); #endif diff --git a/src/w_wad.c b/src/w_wad.c index 041222479..af9f9a7c0 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -199,12 +199,20 @@ static inline void W_LoadDehackedLumpsPK3(UINT16 wadnum, boolean mainfile) { UINT16 posStart, posEnd; - posStart = W_CheckNumForFolderStartPK3("Lua/", wadnum, 0); + posStart = W_CheckNumForFullNamePK3("Init.lua", wadnum, 0); if (posStart != INT16_MAX) { - posEnd = W_CheckNumForFolderEndPK3("Lua/", wadnum, posStart); - for (; posStart < posEnd; posStart++) - LUA_LoadLump(wadnum, posStart); + LUA_LoadLump(wadnum, posStart, true); + } + else + { + posStart = W_CheckNumForFolderStartPK3("Lua/", wadnum, 0); + if (posStart != INT16_MAX) + { + posEnd = W_CheckNumForFolderEndPK3("Lua/", wadnum, posStart); + for (; posStart < posEnd; posStart++) + LUA_LoadLump(wadnum, posStart, true); + } } posStart = W_CheckNumForFolderStartPK3("SOC/", wadnum, 0); @@ -236,7 +244,7 @@ static inline void W_LoadDehackedLumps(UINT16 wadnum, boolean mainfile) lumpinfo_t *lump_p = wadfiles[wadnum]->lumpinfo; for (lump = 0; lump < wadfiles[wadnum]->numlumps; lump++, lump_p++) if (memcmp(lump_p->name,"LUA_",4)==0) - LUA_LoadLump(wadnum, lump); + LUA_LoadLump(wadnum, lump, true); } { @@ -854,7 +862,7 @@ UINT16 W_InitFile(const char *filename, boolean mainfile, boolean startup) DEH_LoadDehackedLumpPwad(numwadfiles - 1, 0, mainfile); break; case RET_LUA: - LUA_LoadLump(numwadfiles - 1, 0); + LUA_LoadLump(numwadfiles - 1, 0, true); break; default: break;