Skip to content

Commit

Permalink
FIXUP: Consider that a key can also be a table
Browse files Browse the repository at this point in the history
  • Loading branch information
Gliese852 committed Jan 31, 2024
1 parent f82ef9c commit dff8e9c
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions data/pigui/modules/new-game-window/recovery.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}}

Expand All @@ -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
Expand Down

0 comments on commit dff8e9c

Please sign in to comment.