---@class Framework.HotReload ---Coordinates Lua module hot-reloading with UnLua and notifies listeners. local Log = require("Framework.Log") local EventBus = require("Framework.EventBus") local HotReload = {} HotReload.EVENT_BEFORE_RELOAD = "Framework.BeforeHotReload" HotReload.EVENT_AFTER_RELOAD = "Framework.AfterHotReload" ---Reloads a specific Lua module (clears package.loaded and re-requires it). ---@param moduleName string Dot-separated Lua module name, e.g. "Game.LuaGameMode" ---@return boolean ok ---@return any resultOrErr function HotReload.ReloadModule(moduleName) if type(moduleName) ~= "string" or moduleName == "" then return false, "Invalid module name" end Log.Info("Hot-reloading Lua module: %s", moduleName) EventBus.Emit(HotReload.EVENT_BEFORE_RELOAD, moduleName) package.loaded[moduleName] = nil local ok, result = pcall(require, moduleName) if not ok then Log.Error("Failed to hot-reload module '%s': %s", moduleName, tostring(result)) return false, result end EventBus.Emit(HotReload.EVENT_AFTER_RELOAD, moduleName, result) Log.Info("Successfully hot-reloaded Lua module: %s", moduleName) return true, result end ---Triggers UnLua's built-in hot reload across all loaded modules. ---@return boolean ok function HotReload.ReloadAll() Log.Info("Triggering full UnLua HotReload...") EventBus.Emit(HotReload.EVENT_BEFORE_RELOAD, "*") if UnLua and type(UnLua.HotReload) == "function" then local ok, err = pcall(UnLua.HotReload) if not ok then Log.Error("UnLua.HotReload() failed: %s", tostring(err)) return false end else Log.Warn("UnLua.HotReload is unavailable; skipping global hot reload.") return false end EventBus.Emit(HotReload.EVENT_AFTER_RELOAD, "*") Log.Info("UnLua HotReload completed.") return true end return HotReload