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");
}

View File

@ -51,6 +51,9 @@ public:
// use ScriptApiBase::loadMod() to load mods
// Save globals that are copied into other Lua envs
void saveGlobals();
// Initialize async engine, call this AFTER loading all mods
void initAsync();

View File

@ -460,6 +460,8 @@ void Server::init()
m_gamespec.checkAndLog();
m_modmgr->loadMods(m_script);
m_script->saveGlobals();
// Read Textures and calculate sha1 sums
fillMediaCache();

View File

@ -383,8 +383,8 @@ public:
// Lua files registered for init of async env, pair of modname + path
std::vector<std::pair<std::string, std::string>> m_async_init_files;
// Data transferred into async envs at init time
std::unique_ptr<PackedValue> m_async_globals_data;
// Data transferred into other Lua envs at init time
std::unique_ptr<PackedValue> m_lua_globals_data;
// Bind address
Address m_bind_addr;