【发布时间】:2020-04-08 17:45:36
【问题描述】:
在使用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);
【问题讨论】: