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 res = storage:get_string("networks")
if res == "" then
local f = io.open(me.worldpath.."/microexpansion_networks", "r") local f = io.open(me.worldpath.."/microexpansion_networks", "r")
if f then if f then
local res = minetest.deserialize(f:read("*all")) me.log("loading network data from file","action")
res = minetest.deserialize(f:read("*all"))
f:close() f:close()
else
me.log("no network data loaded","action")
return
end
else
me.log("loading network data from mod storage","action")
res = minetest.deserialize(res)
end
if type(res) == "table" then if type(res) == "table" then
for _,n in pairs(res) do for _,n in pairs(res) do
local net = me.network.new(n) local net = me.network.new(n)
net:load() net:load()
table.insert(me.networks,net) table.insert(me.networks,net)
end end
end else
me.log("network data in unexpected format","error")
end end
end end
@ -209,10 +222,36 @@ 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
if storage then
me.log("saving network data to mod storage","info")
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") local f = io.open(me.worldpath.."/microexpansion_networks", "w")
f:write(minetest.serialize(data)) f:write(minetest.serialize(data))
f:close() f:close()
end 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
-- save on server shutdown -- save on server shutdown
minetest.register_on_shutdown(me.save) minetest.register_on_shutdown(me.save)

@ -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()