Add dofile() to Lua

This commit is contained in:
Louis-Antoine 2020-05-29 17:35:07 +02:00
parent 755e9d659d
commit 1dbb755743
4 changed files with 49 additions and 15 deletions

View File

@ -11,6 +11,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "../doomdef.h"
#include "../lua_script.h"
#include "../w_wad.h"
#define lbaselib_c #define lbaselib_c
#define LUA_LIB #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) { static int luaB_assert (lua_State *L) {
luaL_checkany(L, 1); luaL_checkany(L, 1);
if (!lua_toboolean(L, 1)) if (!lua_toboolean(L, 1))
@ -380,6 +405,7 @@ static const luaL_Reg base_funcs[] = {
{"assert", luaB_assert}, {"assert", luaB_assert},
{"collectgarbage", luaB_collectgarbage}, {"collectgarbage", luaB_collectgarbage},
{"error", luaB_error}, {"error", luaB_error},
{"dofile", luaB_dofile},
{"gcinfo", luaB_gcinfo}, {"gcinfo", luaB_gcinfo},
{"getfenv", luaB_getfenv}, {"getfenv", luaB_getfenv},
{"getmetatable", luaB_getmetatable}, {"getmetatable", luaB_getmetatable},

View File

@ -395,10 +395,10 @@ void LUA_ClearExtVars(void)
// Use this variable to prevent certain functions from running // Use this variable to prevent certain functions from running
// if they were not called on lump load // if they were not called on lump load
// (i.e. they were called in hooks or coroutines etc) // (i.e. they were called in hooks or coroutines etc)
boolean lua_lumploading = false; INT32 lua_lumploading = 0;
// Load a script from a MYFILE // 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) if (!name)
name = wadfiles[f->wad]->filename; name = wadfiles[f->wad]->filename;
@ -408,19 +408,19 @@ static inline void LUA_LoadFile(MYFILE *f, char *name)
lua_pushinteger(gL, f->wad); lua_pushinteger(gL, f->wad);
lua_setfield(gL, LUA_REGISTRYINDEX, "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)); CONS_Alert(CONS_WARNING,"%s\n",lua_tostring(gL,-1));
lua_pop(gL,1); lua_pop(gL,1);
} }
lua_gc(gL, LUA_GCCOLLECT, 0); lua_gc(gL, LUA_GCCOLLECT, 0);
lua_lumploading = false; // turn off again lua_lumploading--; // turn off again
} }
// Load a script from a lump // Load a script from a lump
void LUA_LoadLump(UINT16 wad, UINT16 lump) void LUA_LoadLump(UINT16 wad, UINT16 lump, boolean noresults)
{ {
MYFILE f; MYFILE f;
char *name; char *name;
@ -447,7 +447,7 @@ void LUA_LoadLump(UINT16 wad, UINT16 lump)
name[len] = '\0'; name[len] = '\0';
} }
LUA_LoadFile(&f, name); // actually load file! LUA_LoadFile(&f, name, noresults); // actually load file!
free(name); free(name);
Z_Free(f.data); Z_Free(f.data);

View File

@ -37,9 +37,9 @@
void LUA_ClearExtVars(void); void LUA_ClearExtVars(void);
#endif #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 #ifdef LUA_ALLOW_BYTECODE
void LUA_DumpFile(const char *filename); void LUA_DumpFile(const char *filename);
#endif #endif

View File

@ -199,12 +199,20 @@ static inline void W_LoadDehackedLumpsPK3(UINT16 wadnum, boolean mainfile)
{ {
UINT16 posStart, posEnd; UINT16 posStart, posEnd;
posStart = W_CheckNumForFolderStartPK3("Lua/", wadnum, 0); posStart = W_CheckNumForFullNamePK3("Init.lua", wadnum, 0);
if (posStart != INT16_MAX) if (posStart != INT16_MAX)
{ {
posEnd = W_CheckNumForFolderEndPK3("Lua/", wadnum, posStart); LUA_LoadLump(wadnum, posStart, true);
for (; posStart < posEnd; posStart++) }
LUA_LoadLump(wadnum, posStart); 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); 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; lumpinfo_t *lump_p = wadfiles[wadnum]->lumpinfo;
for (lump = 0; lump < wadfiles[wadnum]->numlumps; lump++, lump_p++) for (lump = 0; lump < wadfiles[wadnum]->numlumps; lump++, lump_p++)
if (memcmp(lump_p->name,"LUA_",4)==0) 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); DEH_LoadDehackedLumpPwad(numwadfiles - 1, 0, mainfile);
break; break;
case RET_LUA: case RET_LUA:
LUA_LoadLump(numwadfiles - 1, 0); LUA_LoadLump(numwadfiles - 1, 0, true);
break; break;
default: default:
break; break;