From cad5bd932cb92f8d817102c34ec68773b291f473 Mon Sep 17 00:00:00 2001 From: Allanis Date: Sun, 30 Jun 2013 17:07:06 +0100 Subject: [PATCH] [Change] No longer calling luaopen directly. --- src/llua.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/llua.h | 3 ++- src/mission.c | 7 ++--- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/src/llua.c b/src/llua.c index 7bdec63..b44e309 100644 --- a/src/llua.c +++ b/src/llua.c @@ -8,9 +8,12 @@ #include "space.h" #include "land.h" #include "map.h" +#include "pack.h" #include "lluadef.h" #include "llua.h" +static int llua_packfileLoader(lua_State* L); + /* -- Libraries. -- */ /* Lephisto. */ @@ -77,12 +80,76 @@ lua_State* llua_newState(void) { return L; } -/* Load a specially modified version of base. */ -int llua_loadBase(lua_State* L) { - luaopen_base(L); /* Open base. */ +/* Open a lua library. */ +int llua_load(lua_State* L, lua_CFunction f) { + lua_pushcfunction(L, f); + lua_pcall(L, 0, 0, 0); - /* replace package.loaders with custom one. */ + return 0; +} +/* Load specially modified basic stuff. */ +int llua_loadBasic(lua_State* L) { + int i; + /* Unsafe functions. */ + char* override[] = { + "collectgarbage", + "dofile", + "getfenv", + "getmetatable", + "load", + "loadfile", + "loadstring", + "rawequal", + "rawget", + "rawset", + "setfenv", + "setmetatable", + "END" + }; + + llua_load(L, luaopen_base); /* Open base. */ + + /* Replace non-safe functions. */ + for(i = 0; strcmp(override[i], "END") != 0; i++) { + lua_pushnil(L); + lua_setglobal(L, override[i]); + } + + /* Add our own. */ + lua_register(L, "include", llua_packfileLoader); + + return 0; +} + +static int llua_packfileLoader(lua_State* L) { + char* buf, *filename; + uint32_t bufsize; + + LLUA_MIN_ARGS(1); + + if(!lua_isstring(L, -1)) { + LLUA_INVALID_PARAMETER(); + return 0; + } + + filename = (char*) lua_tostring(L, -1); + + /* Try to locate the data. */ + buf = pack_readfile(DATA, filename, &bufsize); + if(buf == NULL) { + lua_pushfstring(L, "%s not found in packfile %s", filename, DATA); + return 1; + } + + /* Run the buffer. */ + if(luaL_dobuffer(L, buf, bufsize, filename) != 0) { + /* Will push the current error from the dobuffer. */ + return 1; + } + + /* Cleanup. */ + free(buf); return 0; } diff --git a/src/llua.h b/src/llua.h index 916ab6c..f99061c 100644 --- a/src/llua.h +++ b/src/llua.h @@ -3,7 +3,8 @@ /* Standard lua stuff wrappers. */ lua_State* llua_newState (void); /* Creates a new state */ -int llua_loadBase(lua_State* L); +int llua_load(lua_State* L, lua_CFunction f); +int llua_loadBasic(lua_State* L); /* Individual custom libraries. */ int lua_loadLephisto(lua_State* L); /* Always read only. */ diff --git a/src/mission.c b/src/mission.c index 027753e..04c5e94 100644 --- a/src/mission.c +++ b/src/mission.c @@ -107,10 +107,11 @@ static int mission_init(Mission* mission, MissionData* misn, int load) { return -1; } - llua_loadBase(mission->L); /* Pairs and such. */ - luaopen_string(mission->L); /* string.format can be very useful. */ - misn_loadLibs(mission->L); /* Load our custom libraries. */ + llua_loadBasic(mission->L); /* Pairs and such. */ + llua_load(mission->L, luaopen_string); /* string.format can be very useful. */ + misn_loadLibs(mission->L); /* Load our custom libraries. */ + /* Load the file. */ buf = pack_readfile(DATA, misn->lua, &bufsize); if(luaL_dobuffer(mission->L, buf, bufsize, misn->lua) != 0) { ERR("Error loading mission file: %s", misn->lua);