local Codec = require("Game/Codec") local Support = require("Game/RuleSupport") local Save = {} ---@param state any ---@param canonical? boolean ---@return boolean, string? function Save.Validate(state,canonical) if type(state) ~= "table" or type(state.escorts) ~= "table" then return false, "存档缺少镖师名册" end if state.version and (not Support.isFinite(state.version) or state.version > 8) then return false, "存档版本暂不兼容" end for _, key in ipairs({"silver", "prestige"}) do if not Support.isFinite(state[key]) or state[key] < 0 then return false, "存档资源数值无效" end end if state.offline and (type(state.offline)~="table" or not Support.isFinite(state.offline.silver) or state.offline.silver<0 or not Support.isFinite(state.offline.hours) or state.offline.hours<0) then return false,"离线收益记录无效" end if state.offline and state.offline.accruedSilver~=nil and (not Support.isFinite(state.offline.accruedSilver) or state.offline.accruedSilver<0) then return false,"离线收益明细无效" end if state.offlineRateAtSave~=nil and (not Support.isFinite(state.offlineRateAtSave) or state.offlineRateAtSave<0) then return false,"离线产出记录无效" end if #state.escorts < 1 then return false, "存档名册不能为空" end if state.businessRequirements~=nil then if type(state.businessRequirements)~="table" then return false,"存档业务资质无效" end for _,need in pairs(state.businessRequirements) do if not Support.isFinite(need) or need<1 or need%1~=0 then return false,"存档业务战力要求无效" end end end local seen = {} for _, e in ipairs(state.escorts) do if type(e) ~= "table" or type(e.id) ~= "string" or e.id=="" or seen[e.id] or type(e.at) ~= "table" or type(e.name)~="string" or e.name=="" then return false, "存档镖师数据无效" end seen[e.id] = true local classes={biaotou=true,youxia=true,hangshang=true,laojianghu=true,xuetu=true} if not classes[e.cls] then return false,"存档镖师职业无效" end if not Support.isFinite(e.q) or e.q < 0 or e.q > 11 or e.q % 1 ~= 0 then return false, "存档品阶无效" end if not Support.isFinite(e.level) or e.level < 1 then return false, "存档等级无效" end for _, attr in ipairs({"force", "agility", "insight", "courage"}) do if not Support.isFinite(e.at[attr]) or e.at[attr] < 0 then return false, "存档属性无效" end end for _,key in ipairs({"equip","train","reforge","reforgePity"}) do if e[key] then if type(e[key])~="table" then return false,"存档养成记录无效" end for _,value in pairs(e[key]) do if not Support.isFinite(value) or value<0 then return false,"存档养成数值无效" end end end end end if state.runs and type(state.runs) ~= "table" then return false, "存档行程无效" end for _, run in ipairs(state.runs or {}) do if type(run) ~= "table" or type(run.id) ~= "string" or type(run.effects) ~= "table" or not Support.isFinite(run.duration) or run.duration <= 0 or not Support.isFinite(run.elapsed) or run.elapsed < 0 or type(run.escortIds) ~= "table" then return false, "存档在途记录无效" end if run.battleNotice then local d=run.battleNotice if type(d)~="table" or type(d.choice)~="string" or type(d.failed)~="boolean" then return false,"存档交战结果无效" end for _,key in ipairs({"power","need","silverAfter","silverDelta","paidSilver","loyaltyMin","loyaltyMax"}) do if not Support.isFinite(d[key]) then return false,"存档交战明细无效" end end if d.power<0 or d.need<1 or d.silverAfter<0 or d.paidSilver<0 then return false,"存档交战数值无效" end end if run.journeyDecision then local j = run.journeyDecision if type(j) ~= "table" or type(j.log) ~= "table" then return false,"存档际遇记录无效" end if j.version~=nil and not Support.isFinite(j.version) then return false,"存档际遇版本无效" end if (j.version or 1)>=2 and (not Support.isFinite(j.randomState) or j.randomState < 0 or j.randomState > 0xffffffff) then return false, "存档随机际遇记录无效" end end end if canonical then for _,key in ipairs({"stats","codex","facilities","runs","recruits","news","cityJourney"}) do if type(state[key])~="table" then return false,"存档缺少必要记录:"..key end end for _,key in ipairs({"events","escorts","goods","cities","roster"}) do if type(state.codex[key])~="table" then return false,"存档图鉴记录无效" end end for _,key in ipairs({"runs","silverEarned","left","battles","battleWins"}) do if not Support.isFinite(state.stats[key]) or state.stats[key]<0 then return false,"存档经营统计无效" end end for _,key in ipairs({"wuguan","male","zhangfang","biaoqi","fenhao"}) do if not Support.isFinite(state.facilities[key]) or state.facilities[key]<1 then return false,"存档设施等级无效" end end end return true end ---@param name string ---@return string?, string? local function readText(name) if not fileSystem:FileExists(name) then return nil end local file = File(name, FILE_READ) if not file or not file:IsOpen() then return nil, "无法读取存档文件" end local text = file:ReadString() file:Close() return text end ---@param name string ---@param text string ---@return boolean local function writeText(name, text) local file = File(name, FILE_WRITE) if not file or not file:IsOpen() then return false end local ok = file:WriteString(text) file:Flush() file:Close() return ok end ---@return string function Save.LoadMode() local text=readText("changfeng_preferences.json") if text then local ok,prefs=pcall(Codec.Decode,text) if ok and type(prefs)=="table" and (prefs.mode=="normal" or prefs.mode=="city") then return prefs.mode end end return "city" end ---@param mode string function Save.RememberMode(mode) writeText("changfeng_preferences.json",Codec.Encode({mode=mode})) end ---@param engine table ---@param raw string ---@return any, string? function Save.Decode(engine, raw) local ok, state = pcall(Codec.Decode, raw) if not ok then return nil, "存档内容无法解析" end local valid, reason = Save.Validate(state) if not valid then return nil, reason end local migrated, result = pcall(engine.migrate, state) if not migrated or type(result) ~= "table" then return nil, "旧存档升级失败,原文件已保留" end local canonical,canonicalReason=Save.Validate(result,true) if not canonical then return nil,canonicalReason end return result end ---@param engine table ---@return any, string?, boolean function Save.Load(engine) local name = engine.SAVE_KEY .. ".json" local raw, reason = readText(name) if not raw then if reason then return nil, reason, false end local backup = readText(name .. ".bak") if backup then local state, backupReason = Save.Decode(engine, backup) return state, state and "已从备份恢复进度" or backupReason, false end return engine.initState(), nil, true end local state, decodeReason = Save.Decode(engine, raw) if not state then return nil, decodeReason .. "。请导入有效存档,或选择恢复备份。", false end -- Append only the interval after the last save; pending earnings are never repriced. local now=engine.support.now() local anchor=state.lastSeen or now local elapsed=math.max(0,(now-anchor)/1000) if elapsed>5 and engine.unlocked(state,"offline") then local pending=state.offline local oldHours=pending and pending.hours or 0 local addedHours=math.min(math.max(0,8+2*state.facilities.zhangfang-oldHours),elapsed/3600) local rate=state.offlineRateAtSave or engine.offlineRate(state) local accrued=(pending and (pending.accruedSilver or pending.silver) or 0)+rate*addedHours if accrued>0 then state.offline={hours=oldHours+addedHours,silver=math.floor(accrued+0.5), accruedSilver=accrued,since=pending and pending.since or anchor} end end state.lastSeen = math.max(anchor,now) state.offlineRateAtSave=engine.offlineRate(state) return state, nil, false end ---@param engine table ---@param state table ---@return boolean, string? function Save.Write(engine, state) local valid, reason = Save.Validate(state,true) if not valid then return false, reason end state.lastSeen = math.max(state.lastSeen or 0,engine.support.now()) state.offlineRateAtSave=engine.offlineRate(state) local encoded, text = pcall(Codec.Encode, state) if not encoded then return false, "存档编码失败,旧进度已保留" end local name = engine.SAVE_KEY .. ".json" if not writeText(name .. ".tmp", text) or readText(name .. ".tmp") ~= text then return false, "存档写入失败,旧进度已保留" end if fileSystem:FileExists(name) and not fileSystem:Copy(name, name .. ".bak") then return false, "无法备份旧存档" end if not fileSystem:Rename(name .. ".tmp", name, false) then return false, "无法更新存档,旧进度已保留" end return true end ---@param engine table ---@return any, string? function Save.RestoreBackup(engine) local raw, reason = readText(engine.SAVE_KEY .. ".json.bak") if not raw then return nil, reason or "没有可恢复的备份" end return Save.Decode(engine, raw) end ---@param state table ---@return string function Save.Export(state) return Codec.Base64Encode(Codec.Encode(state)) end ---@param engine table ---@param code string ---@return any, string? function Save.Import(engine, code) local ok, raw = pcall(Codec.Base64Decode, code) if not ok then return nil, "存档码不完整或格式不正确" end return Save.Decode(engine, raw) end return Save