-- Preserve empty arrays in H5-compatible JSON and UTF-8 base64 save codes. local Support = require("Game/RuleSupport") local Codec = {} local arrayKeys = {escorts=true,recruits=true,runs=true,news=true,telemetry=true, events=true,goods=true,cities=true,roster=true,escortIds=true,eventAt=true, opts=true,log=true,rfSlots=true,milestones=true,legs=true,gains=true,left=true, cityUnlocks=true,tiers=true,rows=true,names=true,locals=true} ---@param value any ---@return string function Codec.Encode(value) if value == nil then return "null" end if type(value) ~= "table" then assert(type(value) ~= "number" or Support.isFinite(value), "存档包含无效数值") return cjson.encode(value) end local parts = {} if Support.isArray(value) then for i = 1, #value do parts[i] = Codec.Encode(value[i]) end return "[" .. table.concat(parts, ",") .. "]" end local keys = {} for key in pairs(value) do keys[#keys + 1] = key end table.sort(keys) for _, key in ipairs(keys) do assert(type(key) == "string", "存档对象键必须为字符串") parts[#parts + 1] = cjson.encode(key) .. ":" .. Codec.Encode(value[key]) end return "{" .. table.concat(parts, ",") .. "}" end ---@param value any ---@param key? string ---@return any local function hydrate(value, key, parentKey) if value == cjson.null then return nil end if type(value) ~= "table" then return value end local isEmpty = next(value) == nil if #value > 0 or (isEmpty and arrayKeys[key] and not (key == "cities" and parentKey == "cityJourney")) then Support.array(value) end for k, child in pairs(value) do value[k] = hydrate(child, type(k) == "string" and k or nil, key) end return value end ---@param text string ---@return any function Codec.Decode(text) assert(#text <= 2 * 1024 * 1024, "存档内容过大") return hydrate(cjson.decode(text)) end local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ---@param text string ---@return string function Codec.Base64Encode(text) local out = {} for i = 1, #text, 3 do local a, b, c = text:byte(i, i + 2) local n = a * 65536 + (b or 0) * 256 + (c or 0) out[#out + 1] = alphabet:sub((n >> 18) + 1, (n >> 18) + 1) .. alphabet:sub(((n >> 12) & 63) + 1, ((n >> 12) & 63) + 1) .. (b and alphabet:sub(((n >> 6) & 63) + 1, ((n >> 6) & 63) + 1) or "=") .. (c and alphabet:sub((n & 63) + 1, (n & 63) + 1) or "=") end return table.concat(out) end ---@param text string ---@return string function Codec.Base64Decode(text) text = text:gsub("%s", "") assert(#text > 0 and #text % 4 == 0 and not text:find("[^%w+/=]"), "存档码格式不正确") local out = {} for i = 1, #text, 4 do local block, n, padding = text:sub(i, i + 3), 0, 0 for j = 1, 4 do local ch = block:sub(j, j) local k = alphabet:find(ch, 1, true) if ch == "=" then padding = padding + 1 k = 1 else assert(k and padding == 0, "存档码字符顺序不正确") end n = (n << 6) | ((k or 1) - 1) end assert(padding <= 2 and (padding == 0 or i + 3 == #text), "存档码补位不正确") out[#out + 1] = string.char((n >> 16) & 255) if padding < 2 then out[#out + 1] = string.char((n >> 8) & 255) end if padding < 1 then out[#out + 1] = string.char(n & 255) end end return table.concat(out) end return Codec