Avoid serialisation of references nightmare
Without this, large footpath networks turn into enormous files that take forever to save. With it, they're small and instant.master
parent
361d66e6e5
commit
a68cc6f917
25
footpath.lua
25
footpath.lua
|
@ -8,9 +8,22 @@ local routecache = {}
|
|||
--- Save the current footpath network status.
|
||||
people.footpath_save = function()
|
||||
if not pathnodes then return end
|
||||
|
||||
-- For the purposes of serialisation, we want a copy of the nodes with
|
||||
-- neighbours referenced by node name, not a reference to the actual
|
||||
-- node, otherwise it will serialise horribly!
|
||||
local lnodes = {}
|
||||
for n, nn in pairs(pathnodes) do
|
||||
local nei = {}
|
||||
for _, ne in pairs(nn.neighbours) do
|
||||
table.insert(nei, ne.name)
|
||||
end
|
||||
lnodes[n] = {name=n, pos=nn.pos, neighbours=nei}
|
||||
end
|
||||
|
||||
local f = io.open(minetest.get_worldpath().."/people_footpaths", "w+")
|
||||
if f then
|
||||
f:write(minetest.serialize(pathnodes))
|
||||
f:write(minetest.serialize(lnodes))
|
||||
f:close()
|
||||
end
|
||||
end
|
||||
|
@ -21,6 +34,16 @@ people.footpath_load = function()
|
|||
if f then
|
||||
pathnodes = minetest.deserialize(f:read("*all"))
|
||||
f:close()
|
||||
|
||||
-- Now we need to undo the dereferencing we did when we saved it...
|
||||
for _, nn in pairs(pathnodes) do
|
||||
local nei = {}
|
||||
for _, ne in pairs(nn.neighbours) do
|
||||
table.insert(nei, pathnodes[ne])
|
||||
end
|
||||
nn.neighbours = nei
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue