使用 physfs 时,通过使用 luaL_dostring 并在我自己中读取文件,加载和运行 lua 脚本很容易,但是当涉及到使用“require”时,它显然有麻烦。
有没有人处理过这个问题?我需要....重写部分lua吗?我真的不想那样做!
谢谢。
更新,这就是我让它工作的方式
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;
}
然后在创建lua状态之后......
lua_register(L, "my_loader", MyLoader);
const char* str = "table.insert(package.searchers, 2, my_loader) \n";
luaL_dostring(L, str);