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

+365
View File
@@ -0,0 +1,365 @@
local ____exports = __module.env
local FALLBACK_NULL = {__json_null__ = true}
local FALLBACK_EMPTY_ARRAY = {__json_empty_array__ = true}
---
-- @module_data
local cachedNative = nil
local function TryLoadNativeJson()
if cachedNative ~= nil then
local ____temp_0
if cachedNative == false then
____temp_0 = nil
else
____temp_0 = cachedNative
end
return ____temp_0
end
local ok, mod = pcall(require, "ue_json")
if ok and type(mod) == "table" and type(mod.encode) == "function" and type(mod.decode) == "function" then
cachedNative = mod
return cachedNative
end
cachedNative = false
return nil
end
local function EscapeJsonString(str)
local escaped = string.gsub(
str,
"[%z-\\\"]",
function(ch)
if ch == "\"" then
return "\\\""
end
if ch == "\\" then
return "\\\\"
end
if ch == "\b" then
return "\\b"
end
if ch == "\f" then
return "\\f"
end
if ch == "\n" then
return "\\n"
end
if ch == "\r" then
return "\\r"
end
if ch == "\t" then
return "\\t"
end
return string.format(
"\\u%04x",
string.byte(ch)
)
end
)
return ("\"" .. escaped) .. "\""
end
local function IsPureArray(tbl)
local count = 0
local maxKey = 0
for k in pairs(tbl) do
count = count + 1
if type(k) ~= "number" or math.type(k) ~= "integer" or k <= 0 then
return false, 0
end
if k > maxKey then
maxKey = k
end
end
return count > 0 and maxKey == count, maxKey
end
local function FallbackEncodeValue(val, depth)
if depth > 64 then
return nil, "JSON encode exceeded maximum depth (64)"
end
if val == nil or val == FALLBACK_NULL then
return "null", nil
end
if val == FALLBACK_EMPTY_ARRAY then
return "[]", nil
end
local t = type(val)
if t == "boolean" then
return val and "true" or "false", nil
end
if t == "number" then
local num = val
if num ~= num or num == math.huge or num == -math.huge then
return nil, "Cannot encode NaN or Infinity to JSON"
end
if math.type(num) == "integer" then
return string.format("%d", num), nil
end
return string.format("%.17g", num), nil
end
if t == "string" then
return EscapeJsonString(val), nil
end
if t == "table" then
local tbl = val
local isArr, len = IsPureArray(tbl)
if isArr then
local items = {}
do
local i = 1
while i <= len do
local encoded, err = FallbackEncodeValue(tbl[i], depth + 1)
if err ~= nil then
return nil, err
end
items[#items + 1] = encoded
i = i + 1
end
end
return ("[" .. table.concat(items, ",")) .. "]", nil
end
local pairsOut = {}
for k, v in pairs(tbl) do
local kt = type(k)
if kt ~= "string" and kt ~= "number" then
return nil, "Unsupported JSON object key type: " .. kt
end
local encodedVal, err = FallbackEncodeValue(v, depth + 1)
if err ~= nil then
return nil, err
end
pairsOut[#pairsOut + 1] = (EscapeJsonString(tostring(k)) .. ":") .. encodedVal
end
return ("{" .. table.concat(pairsOut, ",")) .. "}", nil
end
return nil, "Unsupported Lua type for JSON encode: " .. t
end
local function SkipWs(st)
while st.pos <= st.len do
local b = string.byte(st.src, st.pos)
if b == 32 or b == 9 or b == 10 or b == 13 then
st.pos = st.pos + 1
else
break
end
end
end
local function ParseString(st)
st.pos = st.pos + 1
local out = {}
while st.pos <= st.len do
local ch = string.sub(st.src, st.pos, st.pos)
if ch == "\"" then
st.pos = st.pos + 1
return table.concat(out), nil
end
if ch == "\\" then
st.pos = st.pos + 1
if st.pos > st.len then
return nil, "Unterminated escape in JSON string"
end
local esc = string.sub(st.src, st.pos, st.pos)
st.pos = st.pos + 1
if esc == "\"" or esc == "\\" or esc == "/" then
out[#out + 1] = esc
elseif esc == "b" then
out[#out + 1] = "\b"
elseif esc == "f" then
out[#out + 1] = "\f"
elseif esc == "n" then
out[#out + 1] = "\n"
elseif esc == "r" then
out[#out + 1] = "\r"
elseif esc == "t" then
out[#out + 1] = "\t"
elseif esc == "u" then
local hex = string.sub(st.src, st.pos, st.pos + 3)
local code = tonumber(hex, 16)
if code == nil or #hex < 4 then
return nil, "Invalid \\u escape"
end
st.pos = st.pos + 4
out[#out + 1] = utf8.char(code)
else
return nil, "Invalid escape char: \\" .. esc
end
else
out[#out + 1] = ch
st.pos = st.pos + 1
end
end
return nil, "Unterminated JSON string"
end
local function ParseValue(st, depth)
if depth > 64 then
return nil, "JSON decode exceeded maximum depth (64)"
end
SkipWs(st)
if st.pos > st.len then
return nil, "Unexpected EOF"
end
local ch = string.sub(st.src, st.pos, st.pos)
if ch == "\"" then
return ParseString(st)
end
if ch == "{" then
st.pos = st.pos + 1
local obj = {}
SkipWs(st)
if string.sub(st.src, st.pos, st.pos) == "}" then
st.pos = st.pos + 1
return obj, nil
end
while true do
SkipWs(st)
if string.sub(st.src, st.pos, st.pos) ~= "\"" then
return nil, "Expected string key in JSON object"
end
local key, keyErr = ParseString(st)
if keyErr ~= nil then
return nil, keyErr
end
SkipWs(st)
if string.sub(st.src, st.pos, st.pos) ~= ":" then
return nil, "Expected ':' in JSON object"
end
st.pos = st.pos + 1
local val, valErr = ParseValue(st, depth + 1)
if valErr ~= nil then
return nil, valErr
end
obj[key] = val
SkipWs(st)
local sep = string.sub(st.src, st.pos, st.pos)
if sep == "}" then
st.pos = st.pos + 1
return obj, nil
end
if sep ~= "," then
return nil, "Expected ',' or '}' in JSON object"
end
st.pos = st.pos + 1
end
end
if ch == "[" then
st.pos = st.pos + 1
local arr = {}
SkipWs(st)
if string.sub(st.src, st.pos, st.pos) == "]" then
st.pos = st.pos + 1
return arr, nil
end
local idx = 1
while true do
local val, valErr = ParseValue(st, depth + 1)
if valErr ~= nil then
return nil, valErr
end
arr[idx] = val
idx = idx + 1
SkipWs(st)
local sep = string.sub(st.src, st.pos, st.pos)
if sep == "]" then
st.pos = st.pos + 1
return arr, nil
end
if sep ~= "," then
return nil, "Expected ',' or ']' in JSON array"
end
st.pos = st.pos + 1
end
end
if string.sub(st.src, st.pos, st.pos + 3) == "true" then
st.pos = st.pos + 4
return true, nil
end
if string.sub(st.src, st.pos, st.pos + 4) == "false" then
st.pos = st.pos + 5
return false, nil
end
if string.sub(st.src, st.pos, st.pos + 3) == "null" then
st.pos = st.pos + 4
return st.keepNull and FALLBACK_NULL or nil, nil
end
local numStr = string.match(st.src, "^%-?%d+%.?%d*[eE]?[%+%-]?%d*", st.pos)
if numStr ~= nil and numStr ~= "" then
local parsed = tonumber(numStr)
if parsed ~= nil then
st.pos = st.pos + #numStr
return parsed, nil
end
end
return nil, "Unexpected token at pos " .. tostring(st.pos)
end
local function FallbackDecode(raw, keepNull)
local st = {src = raw, len = #raw, pos = 1, keepNull = keepNull}
local val, err = ParseValue(st, 0)
if err ~= nil then
return nil, err
end
SkipWs(st)
if st.pos <= st.len then
return nil, "Trailing characters after JSON value"
end
return val, nil
end
function ____exports.IsNativeBackend()
return TryLoadNativeJson() ~= nil
end
function ____exports.GetNull()
local native = TryLoadNativeJson()
local ____temp_1
if native ~= nil then
____temp_1 = native.null
else
____temp_1 = FALLBACK_NULL
end
return ____temp_1
end
function ____exports.GetEmptyArray()
local native = TryLoadNativeJson()
local ____temp_2
if native ~= nil then
____temp_2 = native.empty_array
else
____temp_2 = FALLBACK_EMPTY_ARRAY
end
return ____temp_2
end
function ____exports.Encode(value)
local native = TryLoadNativeJson()
if native ~= nil then
return native.encode(value)
end
return FallbackEncodeValue(value, 0)
end
function ____exports.Decode(raw, keepNull)
if keepNull == nil then
keepNull = false
end
if type(raw) ~= "string" or raw == "" then
return nil, "Empty JSON input"
end
local native = TryLoadNativeJson()
if native ~= nil then
return native.decode(raw, keepNull)
end
return FallbackDecode(raw, keepNull)
end
function __module.StashData(env, to)
local ____has = to.__has__
if not ____has then
____has = {}
to.__has__ = ____has
end
to.cachedNative = cachedNative
____has.cachedNative = true
end
function __module.RestoreData(env, from)
local ____has = from.__has__
if not ____has then
return
end
if ____has.cachedNative then
cachedNative = from.cachedNative
end
end
function __module.UpdateImports()
end
+404
View File
@@ -0,0 +1,404 @@
local ____lualib = require("lualib_bundle")
local __TS__New = ____lualib.__TS__New
local __TS__InstanceOf = ____lualib.__TS__InstanceOf
local __TS__ArraySetLength = ____lualib.__TS__ArraySetLength
local ____exports = __module.env
local M__share_base_scoped_object = __MODULE__.Import("share.base.scoped_object", __module)
local ScopedObject = M__share_base_scoped_object.ScopedObject
local M__share_base_traceback = __MODULE__.Import("share.base.traceback", __module)
local tbpcall = M__share_base_traceback.tbpcall
____exports.TimerHandle = __TS__DefineClass("TimerHandle", __module)
local TimerHandle = ____exports.TimerHandle
TimerHandle.name = "TimerHandle"
function TimerHandle.prototype.____constructor(self, scheduler, entryId)
self.disposed = false
self.scheduler = scheduler
self.entryId = entryId
end
function TimerHandle.prototype.GetId(self)
return self.entryId
end
function TimerHandle.prototype.IsDisposed(self)
return self.disposed
end
function TimerHandle.prototype.IsActive(self)
return not self.disposed and self.scheduler ~= nil and self.scheduler:IsEntryActive(self.entryId)
end
function TimerHandle.prototype.Cancel(self)
self:Dispose()
end
function TimerHandle.prototype.Dispose(self)
if self.disposed then
return
end
self.disposed = true
if self.scheduler ~= nil then
local s = self.scheduler
self.scheduler = nil
s:CancelById(self.entryId)
end
end
function TimerHandle.prototype.MarkCompletedInternal(self)
self.disposed = true
self.scheduler = nil
end
____exports.FrameScheduler = __TS__DefineClass("FrameScheduler", __module)
local FrameScheduler = ____exports.FrameScheduler
FrameScheduler.name = "FrameScheduler"
function FrameScheduler.prototype.____constructor(self)
self.nextId = 0
self.monotonicTime = 0
self.worldTime = 0
self.currentWorldGen = 1
self.frameCounter = 0
self.disposed = false
self.entriesById = {}
self.frameIds = {}
self.worldIds = {}
self.monotonicIds = {}
end
function FrameScheduler.prototype.IsDisposed(self)
return self.disposed
end
function FrameScheduler.prototype.NowSeconds(self)
return self.monotonicTime
end
function FrameScheduler.prototype.GetWorldTimeSeconds(self)
return self.worldTime
end
function FrameScheduler.prototype.GetFrameCount(self)
return self.frameCounter
end
function FrameScheduler.prototype.GetCurrentWorldGen(self)
return self.currentWorldGen
end
function FrameScheduler.prototype.IsEntryActive(self, id)
local entry = self.entriesById[id]
return entry ~= nil and not entry.cancelled
end
function FrameScheduler.prototype.AddFrameTimer(self, frames, callback, owner)
local count = math.max(
1,
math.floor(frames)
)
return self:CreateEntry(
"frame",
count,
0,
false,
callback,
owner
)
end
function FrameScheduler.prototype.RunNextFrame(self, callback, owner)
return self:AddFrameTimer(1, callback, owner)
end
function FrameScheduler.prototype.AddWorldTimer(self, delaySeconds, callback, owner)
local delay = math.max(0, delaySeconds)
return self:CreateEntry(
"world",
0,
delay,
false,
callback,
owner
)
end
function FrameScheduler.prototype.AddWorldRepeatTimer(self, intervalSeconds, callback, owner)
if intervalSeconds <= 0 then
return self:CreateEntry(
"frame",
1,
0,
true,
callback,
owner
)
end
return self:CreateEntry(
"world",
0,
intervalSeconds,
true,
callback,
owner
)
end
function FrameScheduler.prototype.AddTimer(self, delaySeconds, callback, owner)
return self:AddMonotonicTimer(delaySeconds, callback, owner)
end
function FrameScheduler.prototype.AddRepeatTimer(self, intervalSeconds, callback, owner)
return self:AddMonotonicRepeatTimer(intervalSeconds, callback, owner)
end
function FrameScheduler.prototype.AddMonotonicTimer(self, delaySeconds, callback, owner)
local delay = math.max(0, delaySeconds)
return self:CreateEntry(
"monotonic",
0,
delay,
false,
callback,
owner
)
end
function FrameScheduler.prototype.AddMonotonicRepeatTimer(self, intervalSeconds, callback, owner)
local interval = math.max(0.001, intervalSeconds)
return self:CreateEntry(
"monotonic",
0,
interval,
true,
callback,
owner
)
end
function FrameScheduler.prototype.CreateEntry(self, domain, remainingFrames, intervalSec, ____repeat, callback, owner)
self.nextId = self.nextId + 1
local id = self.nextId
local handle = __TS__New(____exports.TimerHandle, self, id)
if self.disposed or owner ~= nil and owner:IsDisposed() then
handle:MarkCompletedInternal()
return handle
end
local baseTime = domain == "world" and self.worldTime or self.monotonicTime
local entry = {
id = id,
domain = domain,
callback = callback,
owner = owner,
["repeat"] = ____repeat,
cancelled = false,
remainingFrames = remainingFrames,
intervalSec = intervalSec,
nextTriggerTime = baseTime + intervalSec,
worldGen = self.currentWorldGen,
handle = handle
}
self.entriesById[id] = entry
if domain == "frame" then
self.frameIds[#self.frameIds + 1] = id
elseif domain == "world" then
self.worldIds[#self.worldIds + 1] = id
else
self.monotonicIds[#self.monotonicIds + 1] = id
end
if owner ~= nil and __TS__InstanceOf(owner, ScopedObject) then
owner:AddScopedObject(handle)
end
return handle
end
function FrameScheduler.prototype.CancelById(self, id)
local entry = self.entriesById[id]
if entry == nil then
return
end
entry.cancelled = true
self.entriesById[id] = nil
end
function FrameScheduler.prototype.InvalidateWorld(self)
local prevGen = self.currentWorldGen
self.currentWorldGen = self.currentWorldGen + 1
self.worldTime = 0
self:CancelEntriesForWorldGen(self.worldIds, prevGen)
self:CancelEntriesForWorldGen(self.frameIds, prevGen)
return self.currentWorldGen
end
function FrameScheduler.prototype.CancelEntriesForWorldGen(self, idList, worldGen)
local kept = {}
do
local i = 0
while i < #idList do
do
local id = idList[i + 1]
local entry = self.entriesById[id]
if entry == nil or entry.cancelled then
goto __continue38
end
if entry.worldGen == worldGen then
entry.cancelled = true
self.entriesById[id] = nil
entry.handle:MarkCompletedInternal()
else
kept[#kept + 1] = id
end
end
::__continue38::
i = i + 1
end
end
__TS__ArraySetLength(idList, 0)
do
local i = 0
while i < #kept do
idList[i + 1] = kept[i + 1]
i = i + 1
end
end
end
function FrameScheduler.prototype.Tick(self, unscaledDeltaSeconds, worldDeltaSeconds, isWorldPaused)
if isWorldPaused == nil then
isWorldPaused = false
end
if self.disposed then
return
end
local safeUnscaled = math.max(0, unscaledDeltaSeconds)
self.monotonicTime = self.monotonicTime + safeUnscaled
self.frameCounter = self.frameCounter + 1
self:ProcessTimedQueue(self.monotonicIds, self.monotonicTime)
if not isWorldPaused then
local safeWorldDt = math.max(0, worldDeltaSeconds or safeUnscaled)
self.worldTime = self.worldTime + safeWorldDt
self:ProcessTimedQueue(self.worldIds, self.worldTime)
end
self:ProcessFrameQueue()
end
function FrameScheduler.prototype.ProcessFrameQueue(self)
local snapshotLen = #self.frameIds
local nextFrameIds = {}
do
local i = 0
while i < snapshotLen do
do
local id = self.frameIds[i + 1]
local entry = self.entriesById[id]
if entry == nil or entry.cancelled then
goto __continue49
end
if entry.owner ~= nil and entry.owner:IsDisposed() then
entry.cancelled = true
self.entriesById[id] = nil
entry.handle:MarkCompletedInternal()
goto __continue49
end
entry.remainingFrames = entry.remainingFrames - 1
if entry.remainingFrames <= 0 then
if not entry["repeat"] then
entry.cancelled = true
self.entriesById[id] = nil
entry.handle:MarkCompletedInternal()
else
entry.remainingFrames = 1
nextFrameIds[#nextFrameIds + 1] = id
end
tbpcall(entry.callback)
else
nextFrameIds[#nextFrameIds + 1] = id
end
end
::__continue49::
i = i + 1
end
end
do
local i = snapshotLen
while i < #self.frameIds do
local id = self.frameIds[i + 1]
local entry = self.entriesById[id]
if entry ~= nil and not entry.cancelled then
nextFrameIds[#nextFrameIds + 1] = id
end
i = i + 1
end
end
self.frameIds = nextFrameIds
end
function FrameScheduler.prototype.ProcessTimedQueue(self, idList, currentTime)
local snapshotLen = #idList
local kept = {}
do
local i = 0
while i < snapshotLen do
do
local id = idList[i + 1]
local entry = self.entriesById[id]
if entry == nil or entry.cancelled then
goto __continue61
end
if entry.owner ~= nil and entry.owner:IsDisposed() then
entry.cancelled = true
self.entriesById[id] = nil
entry.handle:MarkCompletedInternal()
goto __continue61
end
if currentTime + 1e-9 >= entry.nextTriggerTime then
if not entry["repeat"] then
entry.cancelled = true
self.entriesById[id] = nil
entry.handle:MarkCompletedInternal()
tbpcall(entry.callback)
else
entry.nextTriggerTime = currentTime + entry.intervalSec
tbpcall(entry.callback)
if not entry.cancelled and self.entriesById[id] ~= nil then
kept[#kept + 1] = id
end
end
else
kept[#kept + 1] = id
end
end
::__continue61::
i = i + 1
end
end
do
local i = snapshotLen
while i < #idList do
local id = idList[i + 1]
local entry = self.entriesById[id]
if entry ~= nil and not entry.cancelled then
kept[#kept + 1] = id
end
i = i + 1
end
end
__TS__ArraySetLength(idList, 0)
do
local i = 0
while i < #kept do
idList[i + 1] = kept[i + 1]
i = i + 1
end
end
end
function FrameScheduler.prototype.GetStats(self)
local frameTasks = self:CountActive(self.frameIds)
local worldTasks = self:CountActive(self.worldIds)
local monotonicTasks = self:CountActive(self.monotonicIds)
return {frameTasks = frameTasks, worldTasks = worldTasks, monotonicTasks = monotonicTasks, totalTasks = frameTasks + worldTasks + monotonicTasks}
end
function FrameScheduler.prototype.CountActive(self, idList)
local count = 0
do
local i = 0
while i < #idList do
local entry = self.entriesById[idList[i + 1]]
if entry ~= nil and not entry.cancelled then
count = count + 1
end
i = i + 1
end
end
return count
end
function FrameScheduler.prototype.Dispose(self)
if self.disposed then
return
end
self.disposed = true
for id, entry in pairs(self.entriesById) do
if entry ~= nil then
entry.cancelled = true
entry.handle:MarkCompletedInternal()
self.entriesById[id] = nil
end
end
self.frameIds = {}
self.worldIds = {}
self.monotonicIds = {}
end
function __module.UpdateImports()
ScopedObject = M__share_base_scoped_object.ScopedObject
tbpcall = M__share_base_traceback.tbpcall
end