-1

使用 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);
4

1 回答 1

2

package.searchers包含require.

如果require要从 zip 文件加载文件,则需要添加一个检查 zip 文件的函数。如果该路径存在于 zipfile 中,请将其解压缩到内存缓冲区并调用luaL_loadbufferand checkload,就像这样searcher_Lua做一样。

于 2019-12-16T11:09:13.243 回答