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

+222
View File
@@ -0,0 +1,222 @@
local ____lualib = require("lualib_bundle")
local __TS__New = ____lualib.__TS__New
local ____exports = __module.env
local M__share_base_logger = __MODULE__.Import("share.base.logger", __module)
local Logger = M__share_base_logger.Logger
local Json = __MODULE__.Import("framework.core.json", __module)
local logger = Logger("LocalSave")
local SCOPE_PREFIX = {global = "gdata", role = "rdata", chat = "cdata", setting = "sdata"}
local SAVE_DEBOUNCE_SECONDS = 0.1
---
-- @noSelf
function ____exports.BuildSlotName(scope, identity)
local prefix = SCOPE_PREFIX[scope]
if identity == nil or identity == "" then
return prefix
end
return (prefix .. "_") .. identity
end
____exports.LocalSaveStore = __TS__DefineClass("LocalSaveStore", __module)
local LocalSaveStore = ____exports.LocalSaveStore
LocalSaveStore.name = "LocalSaveStore"
function LocalSaveStore.prototype.____constructor(self, slotName, backend, clock, schemaVersion, userIndex)
if schemaVersion == nil then
schemaVersion = 1
end
if userIndex == nil then
userIndex = 0
end
self.data = {}
self.loadedVersion = 0
self.lastLoadStatus = "not_found"
self.lastWriteStatus = "ok"
self.recoveredFromBackup = false
self.dirty = false
self.disposed = false
self.slotName = slotName
self.backend = backend
self.clock = clock
self.schemaVersion = schemaVersion
self.userIndex = userIndex
self:Reload()
end
function LocalSaveStore.prototype.Reload(self)
local readResult = self.backend:LoadSlotUtf8(self.slotName, self.userIndex)
self.lastLoadStatus = readResult.status
self.recoveredFromBackup = readResult.recoveredFromBackup
if readResult.status == "not_found" then
self.data = {}
self.loadedVersion = self.schemaVersion
return "not_found"
end
if readResult.status ~= "ok" then
logger:Warn("Failed to load save slot", {slot = self.slotName, status = readResult.status, err = readResult.errorMessage})
self.data = {}
self.loadedVersion = 0
return readResult.status
end
local decoded, decodeErr = Json.Decode(readResult.payloadUtf8)
if decodeErr ~= nil or type(decoded) ~= "table" then
logger:Error("Corrupted JSON payload in save slot", {slot = self.slotName, err = decodeErr or "non-table root"})
self.lastLoadStatus = "corrupted"
self.data = {}
self.loadedVersion = 0
return "corrupted"
end
local env = decoded
if type(env.version) ~= "number" or type(env.data) ~= "table" then
logger:Error("Invalid LocalSaveEnvelope schema in save slot", {slot = self.slotName})
self.lastLoadStatus = "corrupted"
self.data = {}
self.loadedVersion = 0
return "corrupted"
end
self.loadedVersion = env.version
self.data = env.data
return "ok"
end
function LocalSaveStore.prototype.GetLastLoadStatus(self)
return self.lastLoadStatus
end
function LocalSaveStore.prototype.GetLastWriteStatus(self)
return self.lastWriteStatus
end
function LocalSaveStore.prototype.WasRecoveredFromBackup(self)
return self.recoveredFromBackup
end
function LocalSaveStore.prototype.GetLoadedSchemaVersion(self)
return self.loadedVersion
end
function LocalSaveStore.prototype.IsDirty(self)
return self.dirty
end
function LocalSaveStore.prototype.Has(self, key)
return self.data[key] ~= nil
end
function LocalSaveStore.prototype.Get(self, key)
return self.data[key]
end
function LocalSaveStore.prototype.GetDefault(self, key, defaultValue)
local val = self.data[key]
if val ~= nil then
return val
end
return defaultValue
end
function LocalSaveStore.prototype.Set(self, key, value)
if self.disposed then
return
end
self.data[key] = value
self.dirty = true
self:ScheduleSave()
end
function LocalSaveStore.prototype.DeleteKey(self, key)
if self.disposed then
return
end
self.data[key] = nil
self.dirty = true
self:ScheduleSave()
end
function LocalSaveStore.prototype.DeleteAll(self)
if self.disposed then
return
end
self.data = {}
self.dirty = true
self:ScheduleSave()
end
function LocalSaveStore.prototype.ScheduleSave(self)
if self.delayTimer ~= nil and self.delayTimer:IsActive() then
return
end
self.delayTimer = self.clock:AddTimer(
SAVE_DEBOUNCE_SECONDS,
function()
self.delayTimer = nil
self:Flush()
end
)
end
function LocalSaveStore.prototype.Flush(self)
if self.delayTimer ~= nil then
self.delayTimer:Cancel()
self.delayTimer = nil
end
if not self.dirty then
return "ok"
end
local envelope = {version = self.schemaVersion, data = self.data}
local encoded, encodeErr = Json.Encode(envelope)
if encodeErr ~= nil or encoded == nil then
logger:Error("JSON encode failed for save slot", {slot = self.slotName, err = encodeErr})
self.lastWriteStatus = "write_failed"
return "write_failed"
end
local status = self.backend:SaveSlotUtf8(self.slotName, encoded, self.userIndex)
self.lastWriteStatus = status
if status == "ok" then
self.dirty = false
self.loadedVersion = self.schemaVersion
else
logger:Error("Failed to save slot", {slot = self.slotName, status = status})
end
return status
end
function LocalSaveStore.prototype.Dispose(self)
if self.disposed then
return
end
self:Flush()
self.disposed = true
end
____exports.LocalSaveManager = __TS__DefineClass("LocalSaveManager", __module)
local LocalSaveManager = ____exports.LocalSaveManager
LocalSaveManager.name = "LocalSaveManager"
function LocalSaveManager.prototype.____constructor(self, backend, clock)
self.storesBySlot = {}
self.disposed = false
self.backend = backend
self.clock = clock
end
function LocalSaveManager.prototype.GetOrOpenStore(self, scope, identity, schemaVersion)
if schemaVersion == nil then
schemaVersion = 1
end
local slotName = ____exports.BuildSlotName(scope, identity)
local store = self.storesBySlot[slotName]
if store == nil then
store = __TS__New(
____exports.LocalSaveStore,
slotName,
self.backend,
self.clock,
schemaVersion
)
self.storesBySlot[slotName] = store
end
return store
end
function LocalSaveManager.prototype.FlushAll(self)
for _, store in pairs(self.storesBySlot) do
if store ~= nil then
store:Flush()
end
end
end
function LocalSaveManager.prototype.Dispose(self)
if self.disposed then
return
end
self.disposed = true
for slot, store in pairs(self.storesBySlot) do
if store ~= nil then
self.storesBySlot[slot] = nil
store:Dispose()
end
end
end
function __module.UpdateImports()
Logger = M__share_base_logger.Logger
end
@@ -0,0 +1,152 @@
local ____exports = __module.env
---
-- @noSelf
local function MapNativeStatus(nativeStatus)
if nativeStatus == UE.ELuaStorageStatus.Ok then
return "ok"
end
if nativeStatus == UE.ELuaStorageStatus.NotFound then
return "not_found"
end
if nativeStatus == UE.ELuaStorageStatus.Corrupted then
return "corrupted"
end
if nativeStatus == UE.ELuaStorageStatus.WriteFailed then
return "write_failed"
end
return "invalid_argument"
end
____exports.UEStorageBackend = __TS__DefineClass("UEStorageBackend", __module)
local UEStorageBackend = ____exports.UEStorageBackend
UEStorageBackend.name = "UEStorageBackend"
function UEStorageBackend.prototype.____constructor(self)
end
function UEStorageBackend.prototype.LoadSlotUtf8(self, slotName, userIndex)
if userIndex == nil then
userIndex = 0
end
local res = UE.ULuaStorageLibrary.LoadSlotUtf8(slotName, userIndex)
return {
status = MapNativeStatus(res.Status),
payloadUtf8 = res.PayloadUtf8 or "",
containerVersion = res.ContainerVersion or 0,
recoveredFromBackup = res.bRecoveredFromBackup == true,
errorMessage = res.ErrorMessage or ""
}
end
function UEStorageBackend.prototype.SaveSlotUtf8(self, slotName, payloadUtf8, userIndex)
if userIndex == nil then
userIndex = 0
end
return MapNativeStatus(UE.ULuaStorageLibrary.SaveSlotUtf8(slotName, payloadUtf8, userIndex))
end
function UEStorageBackend.prototype.DeleteSlot(self, slotName, userIndex)
if userIndex == nil then
userIndex = 0
end
return UE.ULuaStorageLibrary.DeleteSlot(slotName, userIndex, true)
end
function UEStorageBackend.prototype.DoesSlotExist(self, slotName, userIndex)
if userIndex == nil then
userIndex = 0
end
return UE.ULuaStorageLibrary.DoesSlotExist(slotName, userIndex)
end
____exports.MemoryStorageBackend = __TS__DefineClass("MemoryStorageBackend", __module)
local MemoryStorageBackend = ____exports.MemoryStorageBackend
MemoryStorageBackend.name = "MemoryStorageBackend"
function MemoryStorageBackend.prototype.____constructor(self)
self.slots = {}
self.backups = {}
self.corruptCopies = {}
self.corruptedPrimaryFlags = {}
self.writeCount = 0
self.failNextWrite = false
end
function MemoryStorageBackend.prototype.InjectCorruptPrimary(self, slotName, rawGarbage)
self.slots[slotName] = rawGarbage
self.corruptedPrimaryFlags[slotName] = true
end
function MemoryStorageBackend.prototype.GetCorruptCopy(self, slotName)
return self.corruptCopies[slotName]
end
function MemoryStorageBackend.prototype.LoadSlotUtf8(self, slotName, _userIndex)
if _userIndex == nil then
_userIndex = 0
end
local primary = self.slots[slotName]
local backup = self.backups[slotName]
if primary == nil and backup == nil then
return {
status = "not_found",
payloadUtf8 = "",
containerVersion = 0,
recoveredFromBackup = false,
errorMessage = ""
}
end
if primary ~= nil and not self.corruptedPrimaryFlags[slotName] then
return {
status = "ok",
payloadUtf8 = primary,
containerVersion = 1,
recoveredFromBackup = false,
errorMessage = ""
}
end
if primary ~= nil and self.corruptedPrimaryFlags[slotName] then
self.corruptCopies[slotName] = primary
end
if backup ~= nil then
return {
status = "ok",
payloadUtf8 = backup,
containerVersion = 1,
recoveredFromBackup = true,
errorMessage = "Primary slot corrupted; recovered from backup"
}
end
return {
status = "corrupted",
payloadUtf8 = "",
containerVersion = 0,
recoveredFromBackup = false,
errorMessage = "Primary slot corrupted and no valid backup exists"
}
end
function MemoryStorageBackend.prototype.SaveSlotUtf8(self, slotName, payloadUtf8, _userIndex)
if _userIndex == nil then
_userIndex = 0
end
self.writeCount = self.writeCount + 1
if self.failNextWrite then
self.failNextWrite = false
return "write_failed"
end
local prev = self.slots[slotName]
if prev ~= nil and not self.corruptedPrimaryFlags[slotName] then
self.backups[slotName] = prev
end
self.slots[slotName] = payloadUtf8
self.corruptedPrimaryFlags[slotName] = nil
return "ok"
end
function MemoryStorageBackend.prototype.DeleteSlot(self, slotName, _userIndex)
if _userIndex == nil then
_userIndex = 0
end
local existed = self.slots[slotName] ~= nil or self.backups[slotName] ~= nil
self.slots[slotName] = nil
self.backups[slotName] = nil
self.corruptCopies[slotName] = nil
self.corruptedPrimaryFlags[slotName] = nil
return existed
end
function MemoryStorageBackend.prototype.DoesSlotExist(self, slotName, _userIndex)
if _userIndex == nil then
_userIndex = 0
end
return self.slots[slotName] ~= nil
end
function __module.UpdateImports()
end