mirror of
				https://github.com/luanti-org/luanti.git
				synced 2025-11-04 09:15:29 +01: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:
		@@ -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");
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user