save networks to mod storage

This commit is contained in:
theFox6 2023-10-15 17:18:59 +02:00
parent 2be2eea4d8
commit 0dbe8146e9
No known key found for this signature in database
GPG Key ID: 810803FF29A86CCC
2 changed files with 56 additions and 13 deletions

@ -2,6 +2,7 @@ local me = microexpansion
me.networks = {} me.networks = {}
local networks = me.networks local networks = me.networks
local path = microexpansion.get_module_path("network") local path = microexpansion.get_module_path("network")
local storage = minetest.get_mod_storage()
dofile(path.."/constants.lua") dofile(path.."/constants.lua")
@ -186,17 +187,29 @@ dofile(path.."/security.lua") --Security Terminal
-- load networks -- load networks
function me.load() function me.load()
local f = io.open(me.worldpath.."/microexpansion_networks", "r") local res = storage:get_string("networks")
if f then if res == "" then
local res = minetest.deserialize(f:read("*all")) local f = io.open(me.worldpath.."/microexpansion_networks", "r")
f:close() if f then
if type(res) == "table" then me.log("loading network data from file","action")
for _,n in pairs(res) do res = minetest.deserialize(f:read("*all"))
local net = me.network.new(n) f:close()
net:load() else
table.insert(me.networks,net) me.log("no network data loaded","action")
end return
end end
else
me.log("loading network data from mod storage","action")
res = minetest.deserialize(res)
end
if type(res) == "table" then
for _,n in pairs(res) do
local net = me.network.new(n)
net:load()
table.insert(me.networks,net)
end
else
me.log("network data in unexpected format","error")
end end
end end
@ -209,9 +222,35 @@ function me.save()
for _,v in pairs(me.networks) do for _,v in pairs(me.networks) do
table.insert(data,v:serialize()) table.insert(data,v:serialize())
end end
local f = io.open(me.worldpath.."/microexpansion_networks", "w") if storage then
f:write(minetest.serialize(data)) me.log("saving network data to mod storage","info")
f:close() storage:set_string("networks", minetest.serialize(data))
else
me.log("saving network data to file","info")
local f = io.open(me.worldpath.."/microexpansion_networks", "w")
f:write(minetest.serialize(data))
f:close()
end
end
function me.do_autosave()
me.last_autosave = -1
minetest.after(1, function()
--print("autosaving ME Networks")
me.save()
me.last_autosave = minetest.get_server_uptime()
end)
end
function me.autosave()
--TODO: make max autosave interval settable
if not me.last_autosave then
me.do_autosave()
elseif me.last_autosave == -1 then
return
elseif minetest.get_server_uptime() - me.last_autosave >= 600 then
me.do_autosave()
end
end end
-- save on server shutdown -- save on server shutdown

@ -106,6 +106,8 @@ function network:set_access_level(player, level)
end end
self.access[name] = level self.access[name] = level
self:fallback_access() self:fallback_access()
-- autosave network data
me.autosave()
end end
function network:fallback_access() function network:fallback_access()
@ -254,6 +256,8 @@ function network:set_storage_space(count,listname)
needed = needed + csize needed = needed + csize
remove_slots(inv,ln,needed,csize) remove_slots(inv,ln,needed,csize)
end end
-- autosave network data
me.autosave()
end end
function network:update() function network:update()