Restructure the code to be able to possibly support loadfile.

This commit is contained in:
GoldenTails 2021-09-05 14:38:51 -05:00
parent cf30fd1bd7
commit 9441bafb09
4 changed files with 76 additions and 25 deletions

View file

@ -282,7 +282,7 @@ static int luaB_dofile (lua_State *L) {
if (lumpnum == INT16_MAX)
luaL_error(L, "can't find script " LUA_QS, fullfilename);
LUA_LoadLump(numwadfiles - 1, lumpnum, false);
LUA_DoLump(numwadfiles - 1, lumpnum, false);
return lua_gettop(L) - n;
}

View file

@ -554,64 +554,114 @@ void LUA_ClearExtVars(void)
INT32 lua_lumploading = 0;
// Load a script from a MYFILE
static inline void LUA_LoadFile(MYFILE *f, char *name, boolean noresults)
static inline boolean LUA_LoadFile(MYFILE *f, char *name)
{
int errorhandlerindex;
boolean success;
if (!name)
name = wadfiles[f->wad]->filename;
CONS_Printf("Loading Lua script from %s\n", name);
if (!gL) // Lua needs to be initialized
LUA_ClearState();
lua_pushinteger(gL, f->wad);
lua_setfield(gL, LUA_REGISTRYINDEX, "WAD");
lua_pushcfunction(gL, LUA_GetErrorMessage);
errorhandlerindex = lua_gettop(gL);
success = !luaL_loadbuffer(gL, f->data, f->size, va("@%s",name));
if (!success) {
CONS_Alert(CONS_WARNING,"%s\n",lua_tostring(gL,-1));
lua_pop(gL,1);
}
lua_gc(gL, LUA_GCCOLLECT, 0);
lua_remove(gL, errorhandlerindex);
return success;
}
// Runs a script loaded by LUA_LoadFile.
static inline void LUA_DoFile(boolean noresults)
{
int errorhandlerindex;
if (!gL) // LUA_LoadFile should've allocated gL for us!
return;
lua_lumploading++; // turn on loading flag
lua_pushcfunction(gL, LUA_GetErrorMessage);
errorhandlerindex = lua_gettop(gL);
if (luaL_loadbuffer(gL, f->data, f->size, va("@%s",name)) || lua_pcall(gL, 0, noresults ? 0 : LUA_MULTRET, lua_gettop(gL) - 1)) {
lua_insert(gL, -2); // move the function we're calling to the top.
errorhandlerindex = lua_gettop(gL) - 1;
if (lua_pcall(gL, 0, noresults ? 0 : LUA_MULTRET, lua_gettop(gL) - 1)) {
CONS_Alert(CONS_WARNING,"%s\n",lua_tostring(gL,-1));
lua_pop(gL,1);
}
lua_gc(gL, LUA_GCCOLLECT, 0);
lua_remove(gL, errorhandlerindex);
lua_lumploading--; // turn off again
}
// Load a script from a lump
void LUA_LoadLump(UINT16 wad, UINT16 lump, boolean noresults)
static inline MYFILE *LUA_GetFile(UINT16 wad, UINT16 lump, char **name)
{
MYFILE f;
char *name;
MYFILE *f = Z_Malloc(sizeof(MYFILE), PU_LUA, NULL);
size_t len;
f.wad = wad;
f.size = W_LumpLengthPwad(wad, lump);
f.data = Z_Malloc(f.size, PU_LUA, NULL);
W_ReadLumpPwad(wad, lump, f.data);
f.curpos = f.data;
f->wad = wad;
f->size = W_LumpLengthPwad(wad, lump);
f->data = Z_Malloc(f->size, PU_LUA, NULL);
W_ReadLumpPwad(wad, lump, f->data);
f->curpos = f->data;
len = strlen(wadfiles[wad]->filename); // length of file name
if (wadfiles[wad]->type == RET_LUA)
{
name = malloc(len+1);
strcpy(name, wadfiles[wad]->filename);
*name = malloc(len+1);
strcpy(*name, wadfiles[wad]->filename);
}
else // If it's not a .lua file, copy the lump name in too.
{
lumpinfo_t *lump_p = &wadfiles[wad]->lumpinfo[lump];
len += 1 + strlen(lump_p->fullname); // length of file name, '|', and lump name
name = malloc(len+1);
sprintf(name, "%s|%s", wadfiles[wad]->filename, lump_p->fullname);
name[len] = '\0';
*name = malloc(len+1);
sprintf(*name, "%s|%s", wadfiles[wad]->filename, lump_p->fullname);
(*name)[len] = '\0'; // annoying that index takes priority over dereference, but w/e
}
LUA_LoadFile(&f, name, noresults); // actually load file!
return f;
}
// Load a script from a lump
boolean LUA_LoadLump(UINT16 wad, UINT16 lump)
{
char *name = NULL;
MYFILE *f = LUA_GetFile(wad, lump, &name);
boolean success = LUA_LoadFile(f, name); // actually load file!
free(name);
Z_Free(f.data);
Z_Free(f->data);
Z_Free(f);
return success;
}
void LUA_DoLump(UINT16 wad, UINT16 lump, boolean noresults)
{
boolean success = LUA_LoadLump(wad, lump);
if (success)
LUA_DoFile(noresults); // run it
}
#ifdef LUA_ALLOW_BYTECODE

View file

@ -45,7 +45,8 @@ extern INT32 lua_lumploading; // is LUA_LoadLump being called?
int LUA_GetErrorMessage(lua_State *L);
int LUA_Call(lua_State *L, int nargs, int nresults, int errorhandlerindex);
void LUA_LoadLump(UINT16 wad, UINT16 lump, boolean noresults);
boolean LUA_LoadLump(UINT16 wad, UINT16 lump);
void LUA_DoLump(UINT16 wad, UINT16 lump, boolean noresults);
#ifdef LUA_ALLOW_BYTECODE
void LUA_DumpFile(const char *filename);
#endif

View file

@ -199,7 +199,7 @@ static inline void W_LoadDehackedLumpsPK3(UINT16 wadnum, boolean mainfile)
posStart = W_CheckNumForFullNamePK3("Init.lua", wadnum, 0);
if (posStart != INT16_MAX)
{
LUA_LoadLump(wadnum, posStart, true);
LUA_DoLump(wadnum, posStart, true);
}
else
{
@ -208,7 +208,7 @@ static inline void W_LoadDehackedLumpsPK3(UINT16 wadnum, boolean mainfile)
{
posEnd = W_CheckNumForFolderEndPK3("Lua/", wadnum, posStart);
for (; posStart < posEnd; posStart++)
LUA_LoadLump(wadnum, posStart, true);
LUA_DoLump(wadnum, posStart, true);
}
}
@ -241,7 +241,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, true);
LUA_DoLump(wadnum, lump, true);
}
{
@ -879,7 +879,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, true);
LUA_DoLump(numwadfiles - 1, 0, true);
break;
default:
break;