When using physfs, loading and running a lua script is easy enough by using luaL_dostring and reading the file in myself, but when it comes to using "require", it obviously has trouble.
Has anyone dealt with this problem? Do I need to.... re-write part of lua? I'd really prefer to not do that!
Thanks.
Update, this is how I got it to work
static int MyLoader(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
std::string s = name;
std::replace( s.begin(), s.end(), '.', '/'); // replace all '.' to '/'
s += ".lua"; // apend .lua
File moduleFile = FileManager::ReadFile(s.c_str());
if( luaL_loadbuffer(L, moduleFile.data, moduleFile.fileLength, name) ) {
lua_pop(L, 1);
}
return 1;
}
and then after creating the lua state...
lua_register(L, "my_loader", MyLoader);
const char* str = "table.insert(package.searchers, 2, my_loader) \n";
luaL_dostring(L, str);
The package.searchers
table contains a list of loader functions for require
.
If you want require
to load files from a zip file, you will need to add a function that inspects the zip file. If the path exists in the zipfile, extract it to a memory buffer and call luaL_loadbuffer
and checkload
, like searcher_Lua
does.