feat: integrate ts2lua-unreal v0.2.1 and UnLua TypeScript (.d.ts) generator

This commit is contained in:
szy73711 committed 2026-11-29 03:08:03 +08:00
1 parent ebce41f347
commit ee2e62bb34
282 files changed
+141794 -261

No files matched your search

+18 -28
View File
@@ -1,37 +1,27 @@
---@class Framework.Bootstrap
---Central Lua game framework bootstrapper initialized by ULuaGameInstance.
local ____exports = {}
local Log = require("Framework.Log")
local EventBus = require("Framework.EventBus")
local HotReload = require("Framework.HotReload")
local Bootstrap = {
Version = "0.1.0",
IsInitialized = false,
GameInstance = nil,
}
---@param gameInstance ULuaGameInstance
function Bootstrap.Init(gameInstance)
Bootstrap.GameInstance = gameInstance
Bootstrap.IsInitialized = true
function ____exports.Shutdown()
Log.Info("Lua GameFramework shutting down...")
EventBus.Clear()
____exports.GameInstance = nil
____exports.IsInitialized = false
_G.GameFramework = nil
end
____exports.Version = "0.1.0"
____exports.IsInitialized = false
____exports.GameInstance = nil
function ____exports.Init(gameInstance)
____exports.GameInstance = gameInstance
____exports.IsInitialized = true
_G.GameFramework = {
Log = Log,
EventBus = EventBus,
HotReload = HotReload,
Bootstrap = Bootstrap,
GameInstance = gameInstance,
Bootstrap = {Version = ____exports.Version, Init = ____exports.Init, Shutdown = ____exports.Shutdown},
GameInstance = gameInstance
}
Log.Info("Lua GameFramework initialized (version=%s, Lua=%s)", Bootstrap.Version, _VERSION)
Log.Info("Lua GameFramework initialized (version=%s, Lua=%s)", ____exports.Version, _VERSION)
end
function Bootstrap.Shutdown()
Log.Info("Lua GameFramework shutting down...")
EventBus.Clear()
Bootstrap.GameInstance = nil
Bootstrap.IsInitialized = false
_G.GameFramework = nil
end
return Bootstrap
return ____exports
+66 -59
View File
@@ -1,94 +1,101 @@
---@class Framework.EventBus
---Lightweight publish/subscribe event bus for decoupled Lua game systems.
local EventBus = {}
local ____exports = {}
local Listeners = {}
---@param eventName string
---@param callback function
---@param owner any|nil
function EventBus.On(eventName, callback, owner)
assert(type(eventName) == "string", "EventBus.On: eventName must be a string")
assert(type(callback) == "function", "EventBus.On: callback must be a function")
function ____exports.On(eventName, callback, owner)
assert(
type(eventName) == "string",
"EventBus.On: eventName must be a string"
)
assert(
type(callback) == "function",
"EventBus.On: callback must be a function"
)
local list = Listeners[eventName]
if not list then
list = {}
Listeners[eventName] = list
end
for _, entry in ipairs(list) do
if entry.callback == callback and entry.owner == owner then
return
do
local i = 0
while i < #list do
local entry = list[i + 1]
if entry.callback == callback and entry.owner == owner then
return
end
i = i + 1
end
end
list[#list + 1] = {
callback = callback,
owner = owner,
}
list[#list + 1] = {callback = callback, owner = owner}
end
---@param eventName string
---@param callback function
---@param owner any|nil
function EventBus.Off(eventName, callback, owner)
function ____exports.Off(eventName, callback, owner)
local list = Listeners[eventName]
if not list then
return
end
for i = #list, 1, -1 do
local entry = list[i]
if entry.callback == callback and (owner == nil or entry.owner == owner) then
table.remove(list, i)
do
local i = #list - 1
while i >= 0 do
local entry = list[i + 1]
if entry.callback == callback and (owner == nil or entry.owner == owner) then
table.remove(list, i + 1)
end
i = i - 1
end
end
end
---@param owner any
function EventBus.OffAllForOwner(owner)
function ____exports.OffAllForOwner(owner)
if owner == nil then
return
end
for _, list in pairs(Listeners) do
for i = #list, 1, -1 do
if list[i].owner == owner then
table.remove(list, i)
if list ~= nil then
do
local i = #list - 1
while i >= 0 do
if list[i + 1].owner == owner then
table.remove(list, i + 1)
end
i = i - 1
end
end
end
end
end
---@param eventName string
function EventBus.Emit(eventName, ...)
function ____exports.Emit(eventName, ...)
local list = Listeners[eventName]
if not list then
return
end
-- Snapshot to allow listeners to unsubscribe during dispatch
local snapshot = {}
for i = 1, #list do
snapshot[i] = list[i]
end
for i = 1, #snapshot do
local entry = snapshot[i]
local ok, err
if entry.owner ~= nil then
ok, err = pcall(entry.callback, entry.owner, ...)
else
ok, err = pcall(entry.callback, ...)
do
local i = 0
while i < #list do
snapshot[i + 1] = list[i + 1]
i = i + 1
end
if not ok then
print(string.format("[UnrealLuaDemo][ERROR] EventBus(%s): %s", eventName, tostring(err)))
end
do
local i = 0
while i < #snapshot do
local entry = snapshot[i + 1]
local ok
local err
if entry.owner ~= nil then
ok, err = pcall(entry.callback, entry.owner, ...)
else
ok, err = pcall(entry.callback, ...)
end
if not ok then
print(string.format(
"[UnrealLuaDemo][ERROR] EventBus(%s): %s",
eventName,
tostring(err)
))
end
i = i + 1
end
end
end
function EventBus.Clear()
function ____exports.Clear()
Listeners = {}
end
return EventBus
return ____exports
+48 -44
View File
@@ -1,93 +1,97 @@
---@class Framework.HotReload
---Coordinates Lua module hot-reloading with UnLua and notifies listeners.
local ____exports = {}
local Log = require("Framework.Log")
local EventBus = require("Framework.EventBus")
local HotReload = {}
HotReload.EVENT_BEFORE_RELOAD = "Framework.BeforeHotReload"
HotReload.EVENT_AFTER_RELOAD = "Framework.AfterHotReload"
____exports.EVENT_BEFORE_RELOAD = "Framework.BeforeHotReload"
____exports.EVENT_AFTER_RELOAD = "Framework.AfterHotReload"
local function GetLuaPackage()
return _G.package
end
local function CollectReloadableGameModules()
local modules = {}
for name, _ in pairs(package.loaded) do
if type(name) == "string" and name:match("^Game%.") then
for name in pairs(GetLuaPackage().loaded) do
if type(name) == "string" and (string.match(name, "^Game%.")) ~= 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).
---@param moduleName string Dot-separated Lua module name, e.g. "Game.LuaDemoActor"
---@return boolean ok
---@return any resultOrErr
function HotReload.ReloadModule(moduleName)
--- Reloads a specific Lua module using UnLua's hot-reload sandbox (falls back to package.loaded reset).
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)
EventBus.Emit(HotReload.EVENT_BEFORE_RELOAD, moduleName)
EventBus.Emit(____exports.EVENT_BEFORE_RELOAD, moduleName)
local okHr, unluaHr = pcall(require, "UnLua.HotReload")
if okHr and unluaHr and type(unluaHr.reload) == "function" then
local ok, err = pcall(unluaHr.reload, { moduleName })
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))
Log.Error(
"UnLua.HotReload.reload('%s') failed: %s",
moduleName,
tostring(err)
)
return false, err
end
else
package.loaded[moduleName] = nil
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))
Log.Error(
"Failed to hot-reload module '%s': %s",
moduleName,
tostring(result)
)
return false, result
end
end
EventBus.Emit(HotReload.EVENT_AFTER_RELOAD, moduleName)
EventBus.Emit(____exports.EVENT_AFTER_RELOAD, moduleName)
Log.Info("Successfully hot-reloaded Lua module: %s", moduleName)
return true
end
---Triggers UnLua hot-reload across all loaded Game.* modules (supports Saved/HotUpdate/Script overrides).
---@return boolean ok
function HotReload.ReloadAll()
--- Triggers UnLua hot-reload across all loaded Game.* modules (supports Saved/HotUpdate/Script overrides).
function ____exports.ReloadAll()
local gameModules = CollectReloadableGameModules()
Log.Info("Triggering UnLua HotReload (modules=%d)...", #gameModules)
EventBus.Emit(HotReload.EVENT_BEFORE_RELOAD, "*")
EventBus.Emit(____exports.EVENT_BEFORE_RELOAD, "*")
local okHr, unluaHr = pcall(require, "UnLua.HotReload")
if okHr and unluaHr and type(unluaHr.reload) == "function" then
local hrMod = unluaHr
if okHr and hrMod ~= nil and type(hrMod.reload) == "function" then
if #gameModules > 0 then
local ok, err = pcall(unluaHr.reload, gameModules)
local ok, err = pcall(hrMod.reload, gameModules)
if not ok then
Log.Error("UnLua.HotReload.reload(gameModules) failed: %s", tostring(err))
Log.Error(
"UnLua.HotReload.reload(gameModules) failed: %s",
tostring(err)
)
return false
end
end
-- Also run timestamp-based check for any other modified modules
local ok2, err2 = pcall(unluaHr.reload)
local ok2, err2 = pcall(hrMod.reload)
if not ok2 then
Log.Error("UnLua.HotReload.reload() failed: %s", tostring(err2))
Log.Error(
"UnLua.HotReload.reload() failed: %s",
tostring(err2)
)
return false
end
elseif UnLua and type(UnLua.HotReload) == "function" then
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))
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, "*")
EventBus.Emit(____exports.EVENT_AFTER_RELOAD, "*")
Log.Info("UnLua HotReload completed.")
return true
end
return HotReload
return ____exports
+19 -15
View File
@@ -1,26 +1,30 @@
---@class Framework.Log
local Log = {}
local ____exports = {}
---
-- @noSelfInFile
local TAG = "[UnrealLuaDemo]"
local function FormatMessage(level, fmt, ...)
local ok, msg = pcall(string.format, tostring(fmt), ...)
if not ok then
msg = tostring(fmt)
local args = {...}
local fmtStr = tostring(fmt)
local msg
if #args > 0 then
local ok, formatted = pcall(
string.format,
fmtStr,
table.unpack(args)
)
msg = ok and formatted or fmtStr
else
msg = fmtStr
end
return string.format("%s[%s] %s", TAG, level, msg)
end
function Log.Info(fmt, ...)
function ____exports.Info(fmt, ...)
print(FormatMessage("INFO", fmt, ...))
end
function Log.Warn(fmt, ...)
function ____exports.Warn(fmt, ...)
print(FormatMessage("WARN", fmt, ...))
end
function Log.Error(fmt, ...)
function ____exports.Error(fmt, ...)
print(FormatMessage("ERROR", fmt, ...))
end
return Log
return ____exports
+8 -10
View File
@@ -1,14 +1,12 @@
---@type LuaCharacter
local M = UnLua.Class()
local Log = require("Framework.Log")
function M:ReceiveBeginPlay()
local LuaCharacter = UnLua.Class()
function LuaCharacter.ReceiveBeginPlay(self)
Log.Info("LuaCharacter:ReceiveBeginPlay()")
end
function M:ReceiveEndPlay(EndPlayReason)
Log.Info("LuaCharacter:ReceiveEndPlay(reason=%s)", tostring(EndPlayReason))
function LuaCharacter.ReceiveEndPlay(self, EndPlayReason)
Log.Info(
"LuaCharacter:ReceiveEndPlay(reason=%s)",
tostring(EndPlayReason)
)
end
return M
return LuaCharacter
+102 -55
View File
@@ -1,109 +1,156 @@
---@type LuaDemoActor
local M = UnLua.Class()
local Log = require("Framework.Log")
local EventBus = require("Framework.EventBus")
local HotReload = require("Framework.HotReload")
local DEMO_COLORS = {
{ Name = "Cyan", R = 0.08, G = 0.78, B = 0.95, A = 1.0 },
{ Name = "Gold", R = 0.98, G = 0.72, B = 0.12, A = 1.0 },
{ Name = "Magenta", R = 0.92, G = 0.22, B = 0.65, A = 1.0 },
{ Name = "Lime", R = 0.25, G = 0.90, B = 0.35, A = 1.0 },
{ Name = "Orange", R = 0.98, G = 0.42, B = 0.14, A = 1.0 },
{
Name = "Cyan",
R = 0.08,
G = 0.78,
B = 0.95,
A = 1
},
{
Name = "Gold",
R = 0.98,
G = 0.72,
B = 0.12,
A = 1
},
{
Name = "Magenta",
R = 0.92,
G = 0.22,
B = 0.65,
A = 1
},
{
Name = "Lime",
R = 0.25,
G = 0.9,
B = 0.35,
A = 1
},
{
Name = "Orange",
R = 0.98,
G = 0.42,
B = 0.14,
A = 1
}
}
---
-- @noSelf
local function MakeLinearColor(c)
return UE.FLinearColor(c.R, c.G, c.B, c.A)
end
function M:ReceiveBeginPlay()
self.ElapsedTime = 0.0
self.TextRefreshTimer = 0.0
local LuaDemoActor = UnLua.Class()
function LuaDemoActor.ReceiveBeginPlay(self)
self.ElapsedTime = 0
self.TextRefreshTimer = 0
self.TickCount = 0
self.PingCount = 0
self.ReloadCount = 0
self.ColorIndex = 1
self.ColorIndex = 0
self.HasLoggedTick = false
self._tmpDeltaRot = UE.FRotator(0, 0, 0)
EventBus.On("Demo.Ping", self.OnDemoPing, self)
EventBus.On(HotReload.EVENT_AFTER_RELOAD, self.OnAfterHotReload, self)
self:ApplyCurrentColor()
self:RefreshStatusText()
Log.Info("LuaDemoActor:ReceiveBeginPlay() bound successfully! Lua=%s", _VERSION)
local msg = string.format("[UnLua Demo] LuaDemoActor active (%s) - Press [Space] to Ping, [R] to HotReload", _VERSION)
UE.UKismetSystemLibrary.PrintString(self, msg, true, true, UE.FLinearColor(0.2, 1.0, 0.4, 1.0), 8.0)
UE.UKismetSystemLibrary.PrintString(
self,
msg,
true,
true,
UE.FLinearColor(0.2, 1, 0.4, 1),
8
)
end
function M:ReceiveTick(DeltaSeconds)
self.ElapsedTime = (self.ElapsedTime or 0.0) + DeltaSeconds
function LuaDemoActor.ReceiveTick(self, DeltaSeconds)
self.ElapsedTime = (self.ElapsedTime or 0) + DeltaSeconds
self.TickCount = (self.TickCount or 0) + 1
if not self.HasLoggedTick then
self.HasLoggedTick = true
Log.Info("LuaDemoActor:ReceiveTick() first tick verified (dt=%.4f)", DeltaSeconds)
end
-- Rotate cube mesh & apply vertical sine-wave bobbing entirely from Lua
local yawDelta = (self.SpinSpeed or 60.0) * DeltaSeconds
local yawDelta = (self.SpinSpeed or 60) * DeltaSeconds
local pitchDelta = yawDelta * 0.35
self:AddCubeLocalRotation(UE.FRotator(pitchDelta, yawDelta, 0.0))
local zOffset = math.sin(self.ElapsedTime * (self.BobFrequency or 2.0)) * (self.BobAmplitude or 25.0)
if not self._tmpDeltaRot then
self._tmpDeltaRot = UE.FRotator(pitchDelta, yawDelta, 0)
else
self._tmpDeltaRot.Pitch = pitchDelta
self._tmpDeltaRot.Yaw = yawDelta
self._tmpDeltaRot.Roll = 0
end
self:AddCubeLocalRotation(self._tmpDeltaRot)
local zOffset = math.sin(self.ElapsedTime * (self.BobFrequency or 2)) * (self.BobAmplitude or 25)
self:SetCubeVerticalOffset(zOffset)
self.TextRefreshTimer = (self.TextRefreshTimer or 0.0) + DeltaSeconds
self.TextRefreshTimer = (self.TextRefreshTimer or 0) + DeltaSeconds
if self.TextRefreshTimer >= 0.2 then
self.TextRefreshTimer = 0.0
self.TextRefreshTimer = 0
self:RefreshStatusText()
end
end
function M:OnDemoPing(sourceName)
function LuaDemoActor.OnDemoPing(self, sourceName)
self.PingCount = (self.PingCount or 0) + 1
self.ColorIndex = (self.ColorIndex % #DEMO_COLORS) + 1
self.ColorIndex = ((self.ColorIndex or 0) + 1) % #DEMO_COLORS
local colorEntry = self:ApplyCurrentColor()
self:RefreshStatusText()
local msg = string.format("[UnLua Demo] Ping #%d from %s -> Cube color: %s", self.PingCount, tostring(sourceName), colorEntry.Name)
local msg = string.format(
"[UnLua Demo] Ping #%d from %s -> Cube color: %s",
self.PingCount,
tostring(sourceName),
colorEntry.Name
)
Log.Info(msg)
UE.UKismetSystemLibrary.PrintString(self, msg, true, true, MakeLinearColor(colorEntry), 3.0)
UE.UKismetSystemLibrary.PrintString(
self,
msg,
true,
true,
MakeLinearColor(colorEntry),
3
)
end
function M:OnAfterHotReload(moduleName)
function LuaDemoActor.OnAfterHotReload(self, moduleName)
self.ReloadCount = (self.ReloadCount or 0) + 1
self:RefreshStatusText()
local msg = string.format("[UnLua Demo] HotReload #%d completed (target=%s)", self.ReloadCount, tostring(moduleName))
local msg = string.format(
"[UnLua Demo] HotReload #%d completed (target=%s)",
self.ReloadCount,
tostring(moduleName)
)
Log.Info(msg)
UE.UKismetSystemLibrary.PrintString(self, msg, true, true, UE.FLinearColor(1.0, 0.85, 0.2, 1.0), 4.0)
UE.UKismetSystemLibrary.PrintString(
self,
msg,
true,
true,
UE.FLinearColor(1, 0.85, 0.2, 1),
4
)
end
function M:ApplyCurrentColor()
local colorEntry = DEMO_COLORS[self.ColorIndex or 1] or DEMO_COLORS[1]
function LuaDemoActor.ApplyCurrentColor(self)
local colorEntry = DEMO_COLORS[(self.ColorIndex or 0) + 1] or DEMO_COLORS[1]
self:SetCubeColor(MakeLinearColor(colorEntry))
return colorEntry
end
function M:RefreshStatusText()
local colorEntry = DEMO_COLORS[self.ColorIndex or 1] or DEMO_COLORS[1]
function LuaDemoActor.RefreshStatusText(self)
local colorEntry = DEMO_COLORS[(self.ColorIndex or 0) + 1] or DEMO_COLORS[1]
local text = string.format(
"UnLua Active (%s)\nTime: %.1fs | Pings: %d | Reloads: %d | Color: %s\n[Space] Ping Lua | [R] HotReload",
_VERSION,
self.ElapsedTime or 0.0,
self.ElapsedTime or 0,
self.PingCount or 0,
self.ReloadCount or 0,
colorEntry.Name
)
self:SetStatusText(text)
end
function M:ReceiveEndPlay(EndPlayReason)
function LuaDemoActor.ReceiveEndPlay(self, EndPlayReason)
EventBus.OffAllForOwner(self)
Log.Info("LuaDemoActor:ReceiveEndPlay(ticks=%d, pings=%d)", self.TickCount or 0, self.PingCount or 0)
end
return M
return LuaDemoActor
+4 -9
View File
@@ -1,17 +1,12 @@
---@type LuaGameInstance
local M = UnLua.Class()
local Bootstrap = require("Framework.Bootstrap")
local Log = require("Framework.Log")
function M:ReceiveInit()
local LuaGameInstance = UnLua.Class()
function LuaGameInstance.ReceiveInit(self)
Bootstrap.Init(self)
Log.Info("LuaGameInstance:ReceiveInit()")
end
function M:ReceiveShutdown()
function LuaGameInstance.ReceiveShutdown(self)
Log.Info("LuaGameInstance:ReceiveShutdown()")
Bootstrap.Shutdown()
end
return M
return LuaGameInstance
+10 -12
View File
@@ -1,22 +1,20 @@
---@type LuaGameModeBase
local M = UnLua.Class()
local Log = require("Framework.Log")
function M:ReceiveBeginPlay()
local LuaGameMode = UnLua.Class()
function LuaGameMode.ReceiveBeginPlay(self)
Log.Info("LuaGameMode:ReceiveBeginPlay() - Demo scene started")
UE.UKismetSystemLibrary.PrintString(
self,
"[UnLua Demo] LuaGameMode initialized!",
true,
true,
UE.FLinearColor(0.3, 0.85, 1.0, 1.0),
5.0
UE.FLinearColor(0.3, 0.85, 1, 1),
5
)
end
function M:ReceiveEndPlay(EndPlayReason)
Log.Info("LuaGameMode:ReceiveEndPlay(reason=%s)", tostring(EndPlayReason))
function LuaGameMode.ReceiveEndPlay(self, EndPlayReason)
Log.Info(
"LuaGameMode:ReceiveEndPlay(reason=%s)",
tostring(EndPlayReason)
)
end
return M
return LuaGameMode
+10 -14
View File
@@ -1,26 +1,22 @@
---@type LuaPlayerController
local M = UnLua.Class()
local Log = require("Framework.Log")
local EventBus = require("Framework.EventBus")
local HotReload = require("Framework.HotReload")
function M:ReceiveBeginPlay()
local LuaPlayerController = UnLua.Class()
function LuaPlayerController.ReceiveBeginPlay(self)
Log.Info("LuaPlayerController:ReceiveBeginPlay() - Ready for [Space]=Ping, [R]=HotReload")
end
function M:OnDemoPingPressed()
function LuaPlayerController.OnDemoPingPressed(self)
Log.Info("LuaPlayerController:OnDemoPingPressed() -> Emitting Demo.Ping")
EventBus.Emit("Demo.Ping", "LuaPlayerController")
end
function M:OnDemoHotReloadPressed()
function LuaPlayerController.OnDemoHotReloadPressed(self)
Log.Info("LuaPlayerController:OnDemoHotReloadPressed() -> Triggering HotReload.ReloadAll()")
HotReload.ReloadAll()
end
function M:ReceiveEndPlay(EndPlayReason)
Log.Info("LuaPlayerController:ReceiveEndPlay(reason=%s)", tostring(EndPlayReason))
function LuaPlayerController.ReceiveEndPlay(self, EndPlayReason)
Log.Info(
"LuaPlayerController:ReceiveEndPlay(reason=%s)",
tostring(EndPlayReason)
)
end
return M
return LuaPlayerController