[Change] No longer calling luaopen directly.

This commit is contained in:
Allanis 2013-06-30 17:07:06 +01:00
parent d692035039
commit cad5bd932c
3 changed files with 77 additions and 8 deletions

View File

@ -8,9 +8,12 @@
#include "space.h" #include "space.h"
#include "land.h" #include "land.h"
#include "map.h" #include "map.h"
#include "pack.h"
#include "lluadef.h" #include "lluadef.h"
#include "llua.h" #include "llua.h"
static int llua_packfileLoader(lua_State* L);
/* -- Libraries. -- */ /* -- Libraries. -- */
/* Lephisto. */ /* Lephisto. */
@ -77,12 +80,76 @@ lua_State* llua_newState(void) {
return L; return L;
} }
/* Load a specially modified version of base. */ /* Open a lua library. */
int llua_loadBase(lua_State* L) { int llua_load(lua_State* L, lua_CFunction f) {
luaopen_base(L); /* Open base. */ 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; return 0;
} }

View File

@ -3,7 +3,8 @@
/* Standard lua stuff wrappers. */ /* Standard lua stuff wrappers. */
lua_State* llua_newState (void); /* Creates a new state */ 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. */ /* Individual custom libraries. */
int lua_loadLephisto(lua_State* L); /* Always read only. */ int lua_loadLephisto(lua_State* L); /* Always read only. */

View File

@ -107,10 +107,11 @@ static int mission_init(Mission* mission, MissionData* misn, int load) {
return -1; return -1;
} }
llua_loadBase(mission->L); /* Pairs and such. */ llua_loadBasic(mission->L); /* Pairs and such. */
luaopen_string(mission->L); /* string.format can be very useful. */ llua_load(mission->L, luaopen_string); /* string.format can be very useful. */
misn_loadLibs(mission->L); /* Load our custom libraries. */ misn_loadLibs(mission->L); /* Load our custom libraries. */
/* Load the file. */
buf = pack_readfile(DATA, misn->lua, &bufsize); buf = pack_readfile(DATA, misn->lua, &bufsize);
if(luaL_dobuffer(mission->L, buf, bufsize, misn->lua) != 0) { if(luaL_dobuffer(mission->L, buf, bufsize, misn->lua) != 0) {
ERR("Error loading mission file: %s", misn->lua); ERR("Error loading mission file: %s", misn->lua);