From dff8e9ce00fd147794ef36ca0d4188c1f94c594f Mon Sep 17 00:00:00 2001 From: Gliese852 Date: Wed, 31 Jan 2024 22:42:44 +0300 Subject: [PATCH] FIXUP: Consider that a key can also be a table --- .../modules/new-game-window/recovery.lua | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/data/pigui/modules/new-game-window/recovery.lua b/data/pigui/modules/new-game-window/recovery.lua index 80f4a3672b2..73c01222966 100644 --- a/data/pigui/modules/new-game-window/recovery.lua +++ b/data/pigui/modules/new-game-window/recovery.lua @@ -45,23 +45,30 @@ local fixupDoc = Helpers.versioned {{ if tbl.ref and tbl.table and not refs[tbl.ref] then refs[tbl.ref] = unpickle(tbl) end - for _, v in pairs(tbl) do + for k, v in pairs(tbl) do + getRefs(k) getRefs(v) end end local function putRefs(tbl, cache) - if type(tbl) ~= 'table' then return end - if cache[tbl] then return end - cache[tbl] = true + if type(tbl) ~= 'table' then return tbl end + + if cache[tbl] then return cache[tbl] end + if cache[tbl.ref] then return cache[tbl.ref] end + + local result = {} + if tbl.ref then + cache[tbl.ref] = result + tbl = refs[tbl.ref] + else + cache[tbl] = result + end + for k, v in pairs(tbl) do - if type(v) == 'table' then - if v.ref then - tbl[k] = refs[v.ref] - end - putRefs(tbl[k], cache) - end + result[ putRefs(k, cache) ] = putRefs(v, cache) end + return result end -- circular references are possible after putRefs @@ -73,21 +80,23 @@ local fixupDoc = Helpers.versioned {{ if tbl.ref and not tbl.table then counter = counter + 1 end - for _, v in pairs(tbl) do - counter = counter + countRefs(v, cache) + for k, v in pairs(tbl) do + counter = counter + countRefs(v, cache) + countRefs(k, cache) end return counter end getRefs(gameDoc) local countBefore = countRefs(gameDoc, {}) - putRefs(gameDoc, {}) + gameDoc = putRefs(gameDoc, {}) local countAfter = countRefs(gameDoc, {}) - if countAfter ~= 0 then return false end + if countAfter ~= 0 then + print("fixupDoc failed: " .. tostring(countAfter) .. " of " .. tostring(countBefore) .. " refs were not resolved.") + return nil + end - print("fixupDoc: " .. tostring(countBefore) .. " refs were resolved.") - return true + return gameDoc end }} @@ -111,7 +120,8 @@ function Recovery.loadDoc(saveName) end end Helpers.implicitVersion = gameDoc.version - if not fixupDoc(gameDoc) then + gameDoc = fixupDoc(gameDoc) + if not gameDoc then return lui.COULD_NOT_LOAD_GAME .. " (" .. tostring(saveName) .. ")" end return gameDoc