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

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

@ -51,6 +51,9 @@ public:
// use ScriptApiBase::loadMod() to load mods // 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 // Initialize async engine, call this AFTER loading all mods
void initAsync(); void initAsync();

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

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