chore: upgrade ts2lua-unreal to v0.3.1 with lowercase lib/module.lua & lib/class.lua and commit framework refactor

This commit is contained in:
szy73711 committed 2026-11-30 03:43:42 +08:00
1 parent 783dccf53d
commit 3572f2da43
206 files changed
+14442 -13186

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
local ____exports = __module.env
function __module.UpdateImports()
end
+228
View File
@@ -0,0 +1,228 @@
local ____lualib = require("lualib_bundle")
local __TS__New = ____lualib.__TS__New
local ____exports = __module.env
____exports.LogLevel = {
TRACE = 0,
DEBUG = 1,
INFO = 2,
WARN = 3,
ERROR = 4
}
---
-- @noSelf
function ____exports.StringToLogLevel(lv)
if lv == "trace" then
return ____exports.LogLevel.TRACE
end
if lv == "debug" then
return ____exports.LogLevel.DEBUG
end
if lv == "warn" then
return ____exports.LogLevel.WARN
end
if lv == "error" then
return ____exports.LogLevel.ERROR
end
return ____exports.LogLevel.INFO
end
---
-- @noSelf
local function LevelLabel(lv)
if lv == ____exports.LogLevel.TRACE then
return "TRACE"
end
if lv == ____exports.LogLevel.DEBUG then
return "DEBUG"
end
if lv == ____exports.LogLevel.INFO then
return "INFO"
end
if lv == ____exports.LogLevel.WARN then
return "WARN"
end
return "ERROR"
end
---
-- @noSelf
function ____exports.SerializeLogValue(value, depth)
if depth == nil then
depth = 0
end
local t = type(value)
if t == "nil" then
return "nil"
end
if t == "string" then
return string.format("%q", value)
end
if t == "number" or t == "boolean" then
return tostring(value)
end
if t ~= "table" then
return tostring(value)
end
if depth >= 3 then
return "{...}"
end
local parts = {}
for k, v in pairs(value) do
parts[#parts + 1] = string.format(
"%s=%s",
tostring(k),
____exports.SerializeLogValue(v, depth + 1)
)
end
table.sort(parts)
return ("{" .. table.concat(parts, ", ")) .. "}"
end
---
-- @module_data
local defaultLogLevel = ____exports.LogLevel.INFO
---
-- @module_data
local customSink = nil
---
-- @module_data
local allLoggers = {}
---
-- @noSelf
local function DefaultSink(level, moduleName, message)
local line = string.format(
"[%s][%s] %s",
LevelLabel(level),
moduleName,
message
)
if UnLua ~= nil then
if level >= ____exports.LogLevel.ERROR and UnLua.LogError ~= nil then
UnLua.LogError(line)
return
end
if level == ____exports.LogLevel.WARN and UnLua.LogWarn ~= nil then
UnLua.LogWarn(line)
return
end
if UnLua.Log ~= nil then
UnLua.Log(line)
return
end
end
print(line)
end
____exports.LoggerObject = __TS__DefineClass("LoggerObject", __module)
local LoggerObject = ____exports.LoggerObject
LoggerObject.name = "LoggerObject"
function LoggerObject.prototype.____constructor(self, name)
self.debugEnabled = false
self.infoEnabled = false
self.warnEnabled = false
self.errorEnabled = false
self.name = name
self.level = defaultLogLevel
self:UpdateLevelFlags()
end
function LoggerObject.prototype.UpdateLevelFlags(self)
self.debugEnabled = self.level <= ____exports.LogLevel.DEBUG
self.infoEnabled = self.level <= ____exports.LogLevel.INFO
self.warnEnabled = self.level <= ____exports.LogLevel.WARN
self.errorEnabled = self.level <= ____exports.LogLevel.ERROR
end
function LoggerObject.prototype.EmitLog(self, level, msg, data)
if self.level > level then
return
end
local payload = data ~= nil and (msg .. ", ") .. ____exports.SerializeLogValue(data, 0) or msg
local sink = customSink or DefaultSink
sink(level, self.name, payload)
end
function LoggerObject.prototype.Debug(self, msg, data)
self:EmitLog(____exports.LogLevel.DEBUG, msg, data)
end
function LoggerObject.prototype.Info(self, msg, data)
self:EmitLog(____exports.LogLevel.INFO, msg, data)
end
function LoggerObject.prototype.Warn(self, msg, data)
self:EmitLog(____exports.LogLevel.WARN, msg, data)
end
function LoggerObject.prototype.Error(self, msg, data)
self:EmitLog(____exports.LogLevel.ERROR, msg, data)
end
function LoggerObject.prototype.d(self, msg, data)
self:Debug(msg, data)
end
function LoggerObject.prototype.i(self, msg, data)
self:Info(msg, data)
end
function LoggerObject.prototype.w(self, msg, data)
self:Warn(msg, data)
end
function LoggerObject.prototype.e(self, msg, data)
self:Error(msg, data)
end
function LoggerObject.prototype.GetLevel(self)
return self.level
end
function LoggerObject.prototype.SetLevel(self, lv)
self.level = type(lv) == "string" and ____exports.StringToLogLevel(lv) or lv
self:UpdateLevelFlags()
return self
end
---
-- @noSelf
function ____exports.SetLogSink(sink)
customSink = sink
end
---
-- @noSelf
function ____exports.SetDefaultLogLevel(lv)
defaultLogLevel = lv
end
---
-- @noSelf
function ____exports.SetAllLoggersLevel(lv)
for _, logger in pairs(allLoggers) do
if logger ~= nil then
logger:SetLevel(lv)
end
end
end
---
-- @noSelf
function ____exports.Logger(name)
local logger = allLoggers[name]
if logger == nil then
logger = __TS__New(____exports.LoggerObject, name)
allLoggers[name] = logger
end
return logger
end
function __module.StashData(env, to)
local ____has = to.__has__
if not ____has then
____has = {}
to.__has__ = ____has
end
to.defaultLogLevel = defaultLogLevel
____has.defaultLogLevel = true
to.customSink = customSink
____has.customSink = true
to.allLoggers = allLoggers
____has.allLoggers = true
end
function __module.RestoreData(env, from)
local ____has = from.__has__
if not ____has then
return
end
if ____has.defaultLogLevel then
defaultLogLevel = from.defaultLogLevel
end
if ____has.customSink then
customSink = from.customSink
end
if ____has.allLoggers then
allLoggers = from.allLoggers
end
end
function __module.UpdateImports()
end
+63
View File
@@ -0,0 +1,63 @@
local ____lualib = require("lualib_bundle")
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
local ____exports = __module.env
local M__share_event_event_hub = __MODULE__.Import("share.event.event_hub", __module)
local EventListenerContainer = M__share_event_event_hub.EventListenerContainer
____exports.ScopedObject = __TS__DefineClass("ScopedObject", __module)
local ScopedObject = ____exports.ScopedObject
ScopedObject.name = "ScopedObject"
__TS__ClassExtends(ScopedObject, EventListenerContainer)
function ScopedObject.prototype.____constructor(self, ...)
EventListenerContainer.prototype.____constructor(self, ...)
self.scopedObjects = {}
self.disposed = false
end
function ScopedObject.prototype.IsDisposed(self)
return self.disposed
end
function ScopedObject.prototype.AddScopedObject(self, obj)
if self.disposed then
obj:Dispose()
return
end
self.scopedObjects[obj] = true
end
function ScopedObject.prototype.RemoveScopedObject(self, obj)
self.scopedObjects[obj] = nil
end
function ScopedObject.prototype.GetScopedObjectCount(self)
local count = 0
for _, active in pairs(self.scopedObjects) do
if active == true then
count = count + 1
end
end
return count
end
function ScopedObject.prototype.DisposeAllScopedObjects(self)
local list = {}
for obj, active in pairs(self.scopedObjects) do
if active == true then
list[#list + 1] = obj
end
end
self.scopedObjects = {}
do
local i = 0
while i < #list do
list[i + 1]:Dispose()
i = i + 1
end
end
end
function ScopedObject.prototype.Dispose(self)
if self.disposed then
return
end
self.disposed = true
self:DisposeAllScopedObjects()
EventListenerContainer.prototype.Dispose(self)
end
function __module.UpdateImports()
EventListenerContainer = M__share_event_event_hub.EventListenerContainer
end
+62
View File
@@ -0,0 +1,62 @@
local ____exports = __module.env
---
-- @module_data
local activeHandler = nil
function ____exports.SetupTracebackHandler(handler)
activeHandler = handler
end
local function DefaultDump(msg)
if UnLua ~= nil and UnLua.LogError ~= nil then
UnLua.LogError(msg)
else
print("[ERROR] " .. msg)
end
end
local function OnError(err)
local errText = tostring(err)
local fullStack = debug.traceback(errText, 2)
if activeHandler ~= nil then
if activeHandler.LogTraceback ~= nil then
pcall(activeHandler.LogTraceback, errText, fullStack)
end
if activeHandler.DumpTraceback ~= nil then
pcall(activeHandler.DumpTraceback, fullStack)
else
DefaultDump(fullStack)
end
else
DefaultDump(fullStack)
end
return fullStack
end
--- 带完整堆栈上报的安全调用(对应 gamelogic 的 tbpcall),保留全部多返回值。
function ____exports.tbpcall(fn, ...)
return xpcall(fn, OnError, ...)
end
--- 调用带 self 的对象方法并捕获异常上报堆栈。
function ____exports.tbpcallMethod(fn, selfObj, ...)
return xpcall(fn, OnError, selfObj, ...)
end
function ____exports.ReportError(message)
OnError(message)
end
function __module.StashData(env, to)
local ____has = to.__has__
if not ____has then
____has = {}
to.__has__ = ____has
end
to.activeHandler = activeHandler
____has.activeHandler = true
end
function __module.RestoreData(env, from)
local ____has = from.__has__
if not ____has then
return
end
if ____has.activeHandler then
activeHandler = from.activeHandler
end
end
function __module.UpdateImports()
end
+166
View File
@@ -0,0 +1,166 @@
local ____lualib = require("lualib_bundle")
local __TS__New = ____lualib.__TS__New
local ____exports = __module.env
local M__share_event_events = __MODULE__.Import("share.event.events", __module)
local EvEmitter = M__share_event_events.EvEmitter
____exports.EventListenerContainer = __TS__DefineClass("EventListenerContainer", __module)
local EventListenerContainer = ____exports.EventListenerContainer
EventListenerContainer.name = "EventListenerContainer"
function EventListenerContainer.prototype.____constructor(self)
end
function EventListenerContainer.prototype.GetAllEventListenerTable(self)
return rawget(self, "__events__")
end
function EventListenerContainer.prototype.EnableAllEventListeners(self)
local tbl = self:GetAllEventListenerTable()
if tbl ~= nil then
for _, listener in pairs(tbl) do
if listener ~= nil then
listener:Enable()
end
end
end
end
function EventListenerContainer.prototype.DisableAllEventListeners(self)
local tbl = self:GetAllEventListenerTable()
if tbl ~= nil then
for _, listener in pairs(tbl) do
if listener ~= nil then
listener:Disable()
end
end
end
end
function EventListenerContainer.prototype.OffAllEventListeners(self)
local tbl = self:GetAllEventListenerTable()
if tbl ~= nil then
local keys = {}
for ekey in pairs(tbl) do
keys[#keys + 1] = ekey
end
do
local i = 0
while i < #keys do
local key = keys[i + 1]
local listener = tbl[key]
if listener ~= nil then
listener:Off()
tbl[key] = nil
end
i = i + 1
end
end
end
end
function EventListenerContainer.prototype.GetActiveEventListenerCount(self)
local tbl = self:GetAllEventListenerTable()
if tbl == nil then
return 0
end
local count = 0
for _, listener in pairs(tbl) do
if listener ~= nil and listener:IsActive() then
count = count + 1
end
end
return count
end
function EventListenerContainer.prototype.Dispose(self)
self:OffAllEventListeners()
end
____exports.EventHub = __TS__DefineClass("EventHub", __module)
local EventHub = ____exports.EventHub
EventHub.name = "EventHub"
function EventHub.prototype.____constructor(self, name, options)
self.emit = {}
self.on = {}
self.disposed = false
self.ev = __TS__New(EvEmitter, name)
if options ~= nil then
self.preEmit = options.preEmit
self.asyncEmit = options.asyncEmit
end
self:SetupEmit()
self:SetupOn()
end
function EventHub.prototype.Dispose(self)
if self.disposed then
return
end
self.disposed = true
self.ev:Dispose()
end
function EventHub.prototype.IsDisposed(self)
return self.disposed
end
function EventHub.prototype.HasOnEvent(self, name)
return self.ev:HasOnEvent(name)
end
function EventHub.prototype.GetListenerCount(self, name)
return self.ev:GetListenerCount(name)
end
function EventHub.prototype.Emit(self, name, ...)
if self.disposed then
return
end
if self.preEmit ~= nil then
self.preEmit(name)
end
self.ev:Emit(name, ...)
end
function EventHub.prototype.EmitWithArgs(self, name, packed)
if self.disposed then
return
end
if self.preEmit ~= nil then
self.preEmit(name)
end
self.ev:Emit(
name,
table.unpack(packed, 1, packed.n)
)
end
function EventHub.prototype.SetupEmit(self)
local hub = self
local mt = {
__name = "EventHubEmit",
__index = function(tbl, name)
local function dispatch(...)
if hub.disposed then
return
end
if hub.asyncEmit ~= nil then
local packed = table.pack(...)
hub.asyncEmit(
name,
function()
hub:EmitWithArgs(name, packed)
end
)
else
hub:Emit(name, ...)
end
end
rawset(tbl, name, dispatch)
return dispatch
end
}
setmetatable(self.emit, mt)
end
function EventHub.prototype.SetupOn(self)
local ev = self.ev
local mt = {
__name = "EventHubOn",
__index = function(tbl, name)
local function subscribe(handle, ctx)
return ev:On(name, handle, ctx)
end
rawset(tbl, name, subscribe)
return subscribe
end
}
setmetatable(self.on, mt)
end
function __module.UpdateImports()
EvEmitter = M__share_event_events.EvEmitter
end
+282
View File
@@ -0,0 +1,282 @@
local ____lualib = require("lualib_bundle")
local __TS__New = ____lualib.__TS__New
local ____exports = __module.env
local M__share_base_traceback = __MODULE__.Import("share.base.traceback", __module)
local tbpcall = M__share_base_traceback.tbpcall
local EMPTY_SLOT = -999999
---
-- @module_data
local nextEmitterId = 0
---
-- @module_data
local globalOwnerFilter = nil
---
-- @noSelf
local function AllocateEmitterId()
nextEmitterId = nextEmitterId + 1
return nextEmitterId
end
---
-- @noSelf
local function IsOwnerDisposed(owner)
if owner == nil then
return false
end
if globalOwnerFilter ~= nil and globalOwnerFilter(owner) then
return true
end
local checkFn = owner.IsDisposed
if checkFn ~= nil and type(checkFn) == "function" then
return checkFn(owner) == true
end
return false
end
---
-- @noSelf
function ____exports.SetGlobalEventFilter(filter)
globalOwnerFilter = filter
end
____exports.EvListener = __TS__DefineClass("EvListener", __module)
local EvListener = ____exports.EvListener
EvListener.name = "EvListener"
function EvListener.prototype.____constructor(self, channel, callable, target, ekey)
self.enabled = true
self.channel = channel
self.callable = callable
self.target = target
self.ekey = ekey
self.enabled = true
end
function EvListener.prototype.Off(self)
if self.channel == nil then
return
end
local ch = self.channel
self.channel = nil
ch:Off(self)
local owner = self.target
local key = self.ekey
if owner ~= nil and key ~= nil then
local ownerEvents = rawget(owner, "__events__")
if ownerEvents ~= nil and ownerEvents[key] == self then
ownerEvents[key] = nil
end
end
self.callable = nil
self.target = nil
self.ekey = nil
self.enabled = false
end
function EvListener.prototype.Enable(self)
self.enabled = true
end
function EvListener.prototype.Disable(self)
self.enabled = false
end
function EvListener.prototype.IsActive(self)
return self.channel ~= nil and self.enabled
end
function EvListener.prototype.Dispose(self)
self:Off()
end
____exports.EvChannel = __TS__DefineClass("EvChannel", __module)
local EvChannel = ____exports.EvChannel
EvChannel.name = "EvChannel"
function EvChannel.prototype.____constructor(self, emitter, event)
self.listeners = {}
self.liveCount = 0
self.indexes = {}
self.dispatchDepth = 0
self.emitter = emitter
self.event = event
end
function EvChannel.prototype.On(self, callable, target, ekey)
assert(
type(callable) == "function",
"EvChannel.On: callable must be a function"
)
local listener = __TS__New(
____exports.EvListener,
self,
callable,
target,
ekey
)
local idx = #self.listeners
self.listeners[idx + 1] = listener
self.liveCount = self.liveCount + 1
self.indexes[listener] = idx
return listener
end
function EvChannel.prototype.Off(self, listener)
local idx = self.indexes[listener]
if idx == nil then
return
end
self.listeners[idx + 1] = EMPTY_SLOT
self.liveCount = self.liveCount - 1
self.indexes[listener] = nil
if self.dispatchDepth == 0 then
self:TryPurge()
end
end
function EvChannel.prototype.TryPurge(self)
if #self.listeners - self.liveCount <= 4 then
return
end
local compacted = {}
local newIndexes = {}
do
local i = 0
while i < #self.listeners do
local item = self.listeners[i + 1]
if item ~= EMPTY_SLOT then
local newIdx = #compacted
compacted[newIdx + 1] = item
newIndexes[item] = newIdx
end
i = i + 1
end
end
self.listeners = compacted
self.liveCount = #compacted
self.indexes = newIndexes
end
function EvChannel.prototype.Emit(self, unsafeCall, ...)
self.dispatchDepth = self.dispatchDepth + 1
local list = self.listeners
do
local i = 0
while i < #list do
local listener = list[i + 1]
if listener ~= EMPTY_SLOT and listener.enabled then
local owner = listener.target
if not IsOwnerDisposed(owner) then
local callable = listener.callable
if callable ~= nil then
if unsafeCall then
callable(...)
else
tbpcall(callable, ...)
end
end
end
end
i = i + 1
end
end
self.dispatchDepth = self.dispatchDepth - 1
if self.dispatchDepth == 0 then
self:TryPurge()
end
end
____exports.EvEmitter = __TS__DefineClass("EvEmitter", __module)
local EvEmitter = ____exports.EvEmitter
EvEmitter.name = "EvEmitter"
function EvEmitter.prototype.____constructor(self, name)
self.channels = {}
self.enableUnsafeCall = false
self.name = name
self.id = AllocateEmitterId()
self.key = (name .. ":") .. tostring(self.id)
end
function EvEmitter.prototype.On(self, event, callable, target)
assert(
type(target) == "table",
"EvEmitter.On: owner(target) must be a table/object"
)
assert(
type(callable) == "function",
"EvEmitter.On: callable must be a function"
)
local ekey = (self.key .. ":") .. event
local targetEvents = rawget(target, "__events__")
if targetEvents == nil then
targetEvents = {}
setmetatable(targetEvents, {__mode = "v"})
rawset(target, "__events__", targetEvents)
end
local existing = targetEvents[ekey]
if existing ~= nil then
existing:Off()
end
local channel = self.channels[event]
if channel == nil then
channel = __TS__New(____exports.EvChannel, self, event)
self.channels[event] = channel
end
local listener = channel:On(callable, target, ekey)
targetEvents[ekey] = listener
return listener
end
function EvEmitter.prototype.Emit(self, event, ...)
local channel = self.channels[event]
if channel == nil then
return
end
channel:Emit(self.enableUnsafeCall, ...)
end
function EvEmitter.prototype.HasOnEvent(self, event)
local channel = self.channels[event]
return channel ~= nil and channel.liveCount > 0
end
function EvEmitter.prototype.GetListenerCount(self, event)
if event ~= nil then
local ch = self.channels[event]
return ch ~= nil and ch.liveCount or 0
end
local total = 0
for _, ch in pairs(self.channels) do
if ch ~= nil then
total = total + ch.liveCount
end
end
return total
end
function EvEmitter.prototype.ClearAll(self)
for evName, ch in pairs(self.channels) do
if ch ~= nil then
local list = ch.listeners
do
local i = 0
while i < #list do
local listener = list[i + 1]
if listener ~= EMPTY_SLOT then
listener:Off()
end
i = i + 1
end
end
self.channels[evName] = nil
end
end
end
function EvEmitter.prototype.Dispose(self)
self:ClearAll()
end
function __module.StashData(env, to)
local ____has = to.__has__
if not ____has then
____has = {}
to.__has__ = ____has
end
to.nextEmitterId = nextEmitterId
____has.nextEmitterId = true
to.globalOwnerFilter = globalOwnerFilter
____has.globalOwnerFilter = true
end
function __module.RestoreData(env, from)
local ____has = from.__has__
if not ____has then
return
end
if ____has.nextEmitterId then
nextEmitterId = from.nextEmitterId
end
if ____has.globalOwnerFilter then
globalOwnerFilter = from.globalOwnerFilter
end
end
function __module.UpdateImports()
tbpcall = M__share_base_traceback.tbpcall
end