1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Save Lua globals after mod loading

These are used for the async env currently and will be needed elsewhere soon.
This commit is contained in:
sfan5 2022-12-30 15:04:46 +01:00
parent 4fdd2dec59
commit 32ff832108
4 changed files with 26 additions and 18 deletions

View file

@ -89,23 +89,25 @@ ServerScripting::ServerScripting(Server* server):
infostream << "SCRIPTAPI: Initialized game modules" << std::endl;
}
void ServerScripting::saveGlobals()
{
SCRIPTAPI_PRECHECKHEADER
lua_getglobal(L, "core");
luaL_checktype(L, -1, LUA_TTABLE);
lua_getfield(L, -1, "get_globals_to_transfer");
lua_call(L, 0, 1);
auto *data = script_pack(L, -1);
assert(!data->contains_userdata);
getServer()->m_lua_globals_data.reset(data);
// unset the function
lua_pushnil(L);
lua_setfield(L, -3, "get_globals_to_transfer");
lua_pop(L, 2); // pop 'core', return value
}
void ServerScripting::initAsync()
{
// Save globals to transfer
{
lua_State *L = getStack();
lua_getglobal(L, "core");
luaL_checktype(L, -1, LUA_TTABLE);
lua_getfield(L, -1, "get_globals_to_transfer");
lua_call(L, 0, 1);
auto *data = script_pack(L, -1);
assert(!data->contains_userdata);
getServer()->m_async_globals_data.reset(data);
lua_pushnil(L);
lua_setfield(L, -3, "get_globals_to_transfer"); // unset function too
lua_pop(L, 2); // pop 'core', return value
}
infostream << "SCRIPTAPI: Initializing async engine" << std::endl;
asyncEngine.registerStateInitializer(InitializeAsync);
asyncEngine.registerStateInitializer(ModApiUtil::InitializeAsync);
@ -182,7 +184,8 @@ void ServerScripting::InitializeAsync(lua_State *L, int top)
LuaSettings::Register(L);
// globals data
auto *data = ModApiBase::getServer(L)->m_async_globals_data.get();
auto *data = ModApiBase::getServer(L)->m_lua_globals_data.get();
assert(data);
script_unpack(L, data);
lua_setfield(L, top, "transferred_globals");
}