feat: upgrade to ts2lua-unreal v0.3.0 and refactor Game classes to pure C++ callback + TS composition hot-reload
This commit is contained in:
1 parent
4ad4a8f36f
commit
783dccf53d
64 files changed
+2368
-714
No files matched your search
@@ -1,7 +1,7 @@
|
||||
local ____exports = {}
|
||||
local Log = require("Framework.Log")
|
||||
local EventBus = require("Framework.EventBus")
|
||||
local HotReload = require("Framework.HotReload")
|
||||
local ____exports = __module.env
|
||||
local Log = __MODULE__.Import("Framework.Log", __module)
|
||||
local EventBus = __MODULE__.Import("Framework.EventBus", __module)
|
||||
local HotReload = __MODULE__.Import("Framework.HotReload", __module)
|
||||
function ____exports.Shutdown()
|
||||
Log.Info("Lua GameFramework shutting down...")
|
||||
EventBus.Clear()
|
||||
@@ -9,8 +9,12 @@ function ____exports.Shutdown()
|
||||
____exports.IsInitialized = false
|
||||
_G.GameFramework = nil
|
||||
end
|
||||
____exports.Version = "0.1.0"
|
||||
____exports.Version = "0.2.0"
|
||||
---
|
||||
-- @module_data
|
||||
____exports.IsInitialized = false
|
||||
---
|
||||
-- @module_data
|
||||
____exports.GameInstance = nil
|
||||
function ____exports.Init(gameInstance)
|
||||
____exports.GameInstance = gameInstance
|
||||
@@ -24,4 +28,28 @@ function ____exports.Init(gameInstance)
|
||||
}
|
||||
Log.Info("Lua GameFramework initialized (version=%s, Lua=%s)", ____exports.Version, _VERSION)
|
||||
end
|
||||
return ____exports
|
||||
function __module.StashData(env, to)
|
||||
local ____has = to.__has__
|
||||
if not ____has then
|
||||
____has = {}
|
||||
to.__has__ = ____has
|
||||
end
|
||||
to.IsInitialized = ____exports.IsInitialized
|
||||
____has.IsInitialized = true
|
||||
to.GameInstance = ____exports.GameInstance
|
||||
____has.GameInstance = true
|
||||
end
|
||||
function __module.RestoreData(env, from)
|
||||
local ____has = from.__has__
|
||||
if not ____has then
|
||||
return
|
||||
end
|
||||
if ____has.IsInitialized then
|
||||
____exports.IsInitialized = from.IsInitialized
|
||||
end
|
||||
if ____has.GameInstance then
|
||||
____exports.GameInstance = from.GameInstance
|
||||
end
|
||||
end
|
||||
function __module.UpdateImports()
|
||||
end
|
||||
@@ -1,4 +1,6 @@
|
||||
local ____exports = {}
|
||||
local ____exports = __module.env
|
||||
---
|
||||
-- @module_data
|
||||
local Listeners = {}
|
||||
function ____exports.On(eventName, callback, owner)
|
||||
assert(
|
||||
@@ -98,4 +100,23 @@ end
|
||||
function ____exports.Clear()
|
||||
Listeners = {}
|
||||
end
|
||||
return ____exports
|
||||
function __module.StashData(env, to)
|
||||
local ____has = to.__has__
|
||||
if not ____has then
|
||||
____has = {}
|
||||
to.__has__ = ____has
|
||||
end
|
||||
to.Listeners = Listeners
|
||||
____has.Listeners = true
|
||||
end
|
||||
function __module.RestoreData(env, from)
|
||||
local ____has = from.__has__
|
||||
if not ____has then
|
||||
return
|
||||
end
|
||||
if ____has.Listeners then
|
||||
Listeners = from.Listeners
|
||||
end
|
||||
end
|
||||
function __module.UpdateImports()
|
||||
end
|
||||
@@ -1,97 +1,76 @@
|
||||
local ____exports = {}
|
||||
local Log = require("Framework.Log")
|
||||
local EventBus = require("Framework.EventBus")
|
||||
local ____exports = __module.env
|
||||
local Log = __MODULE__.Import("Framework.Log", __module)
|
||||
local EventBus = __MODULE__.Import("Framework.EventBus", __module)
|
||||
____exports.EVENT_BEFORE_RELOAD = "Framework.BeforeHotReload"
|
||||
____exports.EVENT_AFTER_RELOAD = "Framework.AfterHotReload"
|
||||
local function GetLuaPackage()
|
||||
return _G.package
|
||||
end
|
||||
local function CollectReloadableGameModules()
|
||||
local function CollectLoadedHotReloadModules()
|
||||
local modules = {}
|
||||
for name in pairs(GetLuaPackage().loaded) do
|
||||
if type(name) == "string" and (string.match(name, "^Game%.")) ~= nil then
|
||||
if __MODULE__ == nil then
|
||||
return modules
|
||||
end
|
||||
for name in pairs(__MODULE__.AllLoadedModules()) do
|
||||
if type(name) == "string" and name ~= "Framework.HotReload" and ((string.match(name, "^Game%.")) ~= nil or (string.match(name, "^Framework%.")) ~= nil) then
|
||||
modules[#modules + 1] = name
|
||||
end
|
||||
end
|
||||
table.sort(modules)
|
||||
return modules
|
||||
end
|
||||
--- Reloads a specific Lua module using UnLua's hot-reload sandbox (falls back to package.loaded reset).
|
||||
--- 单文件热更指定模块(完全基于 Lib/Module.lua 的 __MODULE__.ReimportSafely,零入侵 UnLua)。
|
||||
function ____exports.ReloadModule(moduleName)
|
||||
if type(moduleName) ~= "string" or moduleName == "" then
|
||||
return false, "Invalid module name"
|
||||
end
|
||||
Log.Info("Hot-reloading Lua module: %s", moduleName)
|
||||
if __MODULE__ == nil then
|
||||
return false, "__MODULE__ runtime is not initialized"
|
||||
end
|
||||
if __MODULE__.GetModuleByName(moduleName) == nil then
|
||||
Log.Warn("Module '%s' is not loaded yet, skipping hot-reload.", moduleName)
|
||||
return false, "Module not loaded: " .. moduleName
|
||||
end
|
||||
Log.Info("Hot-reloading module via __MODULE__: %s", moduleName)
|
||||
EventBus.Emit(____exports.EVENT_BEFORE_RELOAD, moduleName)
|
||||
local okHr, unluaHr = pcall(require, "UnLua.HotReload")
|
||||
local hrMod = unluaHr
|
||||
if okHr and hrMod ~= nil and type(hrMod.reload) == "function" then
|
||||
local ok, err = pcall(hrMod.reload, {moduleName})
|
||||
if not ok then
|
||||
Log.Error(
|
||||
"UnLua.HotReload.reload('%s') failed: %s",
|
||||
moduleName,
|
||||
tostring(err)
|
||||
)
|
||||
return false, err
|
||||
end
|
||||
else
|
||||
GetLuaPackage().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
|
||||
local ok, err = __MODULE__.ReimportSafely(moduleName)
|
||||
if not ok then
|
||||
Log.Error(
|
||||
"__MODULE__.ReimportSafely('%s') failed: %s",
|
||||
moduleName,
|
||||
tostring(err)
|
||||
)
|
||||
return false, err
|
||||
end
|
||||
EventBus.Emit(____exports.EVENT_AFTER_RELOAD, moduleName)
|
||||
Log.Info("Successfully hot-reloaded Lua module: %s", moduleName)
|
||||
Log.Info("Successfully hot-reloaded module: %s", moduleName)
|
||||
return true
|
||||
end
|
||||
--- Triggers UnLua hot-reload across all loaded Game.* modules (supports Saved/HotUpdate/Script overrides).
|
||||
--- 批量热更所有已加载的业务/框架模块(Game.* / Framework.*)。
|
||||
function ____exports.ReloadAll()
|
||||
local gameModules = CollectReloadableGameModules()
|
||||
Log.Info("Triggering UnLua HotReload (modules=%d)...", #gameModules)
|
||||
local managedModules = CollectLoadedHotReloadModules()
|
||||
Log.Info("Triggering HotReload via __MODULE__ (modules=%d)...", #managedModules)
|
||||
EventBus.Emit(____exports.EVENT_BEFORE_RELOAD, "*")
|
||||
local okHr, unluaHr = pcall(require, "UnLua.HotReload")
|
||||
local hrMod = unluaHr
|
||||
if okHr and hrMod ~= nil and type(hrMod.reload) == "function" then
|
||||
if #gameModules > 0 then
|
||||
local ok, err = pcall(hrMod.reload, gameModules)
|
||||
local allOk = true
|
||||
do
|
||||
local i = 0
|
||||
while i < #managedModules do
|
||||
local modName = managedModules[i + 1]
|
||||
local ok, err = __MODULE__.ReimportSafely(modName)
|
||||
if not ok then
|
||||
Log.Error(
|
||||
"UnLua.HotReload.reload(gameModules) failed: %s",
|
||||
"__MODULE__.ReimportSafely('%s') failed: %s",
|
||||
modName,
|
||||
tostring(err)
|
||||
)
|
||||
return false
|
||||
allOk = false
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
local ok2, err2 = pcall(hrMod.reload)
|
||||
if not ok2 then
|
||||
Log.Error(
|
||||
"UnLua.HotReload.reload() failed: %s",
|
||||
tostring(err2)
|
||||
)
|
||||
return false
|
||||
end
|
||||
elseif UnLua ~= nil 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(____exports.EVENT_AFTER_RELOAD, "*")
|
||||
Log.Info("UnLua HotReload completed.")
|
||||
return true
|
||||
Log.Info(
|
||||
"HotReload completed (ok=%s).",
|
||||
tostring(allOk)
|
||||
)
|
||||
return allOk
|
||||
end
|
||||
function __module.UpdateImports()
|
||||
end
|
||||
return ____exports
|
||||
@@ -1,4 +1,4 @@
|
||||
local ____exports = {}
|
||||
local ____exports = __module.env
|
||||
---
|
||||
-- @noSelfInFile
|
||||
local TAG = "[UnrealLuaDemo]"
|
||||
@@ -27,4 +27,5 @@ end
|
||||
function ____exports.Error(fmt, ...)
|
||||
print(FormatMessage("ERROR", fmt, ...))
|
||||
end
|
||||
return ____exports
|
||||
function __module.UpdateImports()
|
||||
end
|
||||
Reference in new issue
Block a user