223 lines
6.9 KiB
Lua
223 lines
6.9 KiB
Lua
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
|