Unite all save data (except the track database) into a single save file

master
orwell96 2017-01-12 17:33:43 +01:00
parent 04d14dc1bf
commit dc8b47a066
4 changed files with 115 additions and 118 deletions

View File

@ -2,37 +2,10 @@
--registers and controls the ATC system
local atc={}
-- ATC persistence table
atc.controllers = {}
-- ATC persistence table. advtrains.atc is created by init.lua when it loads the save file.
atc.controllers = advtrains.atc.controllers
--contents: {command="...", arrowconn=0-15 where arrow points}
advtrains.fpath_atc=minetest.get_worldpath().."/advtrains_atc"
local file, err = io.open(advtrains.fpath_atc, "r")
if not file then
local er=err or "Unknown Error"
atprint("Failed loading advtrains atc save file "..er)
else
local tbl = minetest.deserialize(file:read("*a"))
if type(tbl) == "table" then
atc.controllers=tbl.controllers
end
file:close()
end
function atc.save()
--leave space for more save data.
local datastr = minetest.serialize({controllers = atc.controllers})
if not datastr then
minetest.log("error", " Failed to serialize trackdb data!")
return
end
local file, err = io.open(advtrains.fpath_atc, "w")
if err then
return err
end
file:write(datastr)
file:close()
end
--call from advtrains.detector subprogram
function atc.trigger_controller_train_enter(pos, train_id)
@ -290,5 +263,7 @@ function atc.execute_atc_command(id, train)
atc.train_reset_command(id)
end
--move table to desired place
advtrains.atc=atc

View File

@ -1,6 +1,7 @@
--advtrains
advtrains={}
--create the base table structure.
advtrains={atc={controllers={}}, detector={}, trackplacer={}, trains={}, trackdb={}, wagon_save={}}
advtrains.modpath = minetest.get_modpath("advtrains")
@ -34,8 +35,8 @@ local function print_concat_table(a)
end
return str
end
atprint=function() end
--atprint=function(t, ...) minetest.log("action", "[advtrains]"..print_concat_table({t, ...})) minetest.chat_send_all("[advtrains]"..print_concat_table({t, ...})) end
--atprint=function() end
atprint=function(t, ...) minetest.log("action", "[advtrains]"..print_concat_table({t, ...})) minetest.chat_send_all("[advtrains]"..print_concat_table({t, ...})) end
sid=function(id) return string.sub(id, -4) end
dofile(advtrains.modpath.."/helpers.lua");
@ -55,6 +56,110 @@ advtrains.meseconrules =
{x=0, y=1, z=-1},
{x=0, y=-1, z=-1},
{x=0, y=-2, z=0}}
--load/save
advtrains.fpath=minetest.get_worldpath().."/advtrains"
local file, err = io.open(advtrains.fpath, "r")
if not file then
minetest.log("error", " Failed to read advtrains save data from file "..advtrains.fpath..": "..(err or "Unknown Error"))
else
local tbl = minetest.deserialize(file:read("*a"))
if type(tbl) == "table" then
if tbl.version then
--congrats, we have the new save format.
advtrains.atc.controllers = tbl.atc_controllers
advtrains.trains = tbl.trains
advtrains.wagon_save = tbl.wagon_save
else
--oh no, its the old one...
advtrains.trains=tbl
--load ATC
advtrains.fpath_atc=minetest.get_worldpath().."/advtrains_atc"
local file, err = io.open(advtrains.fpath_atc, "r")
if not file then
local er=err or "Unknown Error"
atprint("Failed loading advtrains atc save file "..er)
else
local tbl = minetest.deserialize(file:read("*a"))
if type(tbl) == "table" then
advtrains.atc.controllers=tbl.controllers
end
file:close()
end
--load wagon saves
advtrains.fpath_ws=minetest.get_worldpath().."/advtrains_wagon_save"
local file, err = io.open(advtrains.fpath_ws, "r")
if not file then
local er=err or "Unknown Error"
atprint("Failed loading advtrains save file "..er)
else
local tbl = minetest.deserialize(file:read("*a"))
if type(tbl) == "table" then
advtrains.wagon_save=tbl
end
file:close()
end
end
else
minetest.log("error", " Failed to deserialize advtrains save data: Not a table!")
end
file:close()
end
advtrains.save = function()
atprint("saving")
advtrains.invalidate_all_paths()
-- update wagon saves
for _,wagon in pairs(minetest.luaentities) do
if wagon.is_wagon and wagon.initialized then
wagon:get_staticdata()
end
end
--cross out userdata
for w_id, data in pairs(advtrains.wagon_save) do
data.name=nil
data.object=nil
if data.driver then
data.driver_name=data.driver:get_player_name()
data.driver=nil
else
data.driver_name=nil
end
if data.discouple then
data.discouple.object:remove()
data.discouple=nil
end
end
--atprint(dump(advtrains.wagon_save))
--versions:
-- 1 - Initial new save format.
local save_tbl={
trains = advtrains.trains,
wagon_save = advtrains.wagon_save,
atc_controllers = advtrains.atc.controllers,
version = 1,
}
local datastr = minetest.serialize(save_tbl)
if not datastr then
minetest.log("error", " Failed to serialize advtrains save data!")
return
end
local file, err = io.open(advtrains.fpath, "w")
if err then
minetest.log("error", " Failed to write advtrains save data to file "..advtrains.fpath..": "..(err or "Unknown Error"))
return
end
file:write(datastr)
file:close()
end
minetest.register_on_shutdown(advtrains.save)
dofile(advtrains.modpath.."/trainlogic.lua")
dofile(advtrains.modpath.."/trainhud.lua")
dofile(advtrains.modpath.."/trackplacer.lua")
@ -69,3 +174,4 @@ dofile(advtrains.modpath.."/damage.lua")
dofile(advtrains.modpath.."/signals.lua")
dofile(advtrains.modpath.."/misc_nodes.lua")
dofile(advtrains.modpath.."/crafting.lua")

View File

@ -98,7 +98,8 @@ function advtrains.save_trackdb()
end
end
]]--end temp outcomment
advtrains.trackdb={}
--trackdb keeps its own save file.
advtrains.fpath_tdb=minetest.get_worldpath().."/advtrains_trackdb2"
local file, err = io.open(advtrains.fpath_tdb, "r")
if not file then

View File

@ -39,96 +39,11 @@ advtrains.train_emerg_force=10--for emergency brakes(when going off track)
advtrains.audit_interval=10
advtrains.trains={}
advtrains.wagon_save={}
--load initially
advtrains.fpath=minetest.get_worldpath().."/advtrains"
local file, err = io.open(advtrains.fpath, "r")
if not file then
local er=err or "Unknown Error"
atprint("Failed loading advtrains save file "..er)
else
local tbl = minetest.deserialize(file:read("*a"))
if type(tbl) == "table" then
advtrains.trains=tbl
end
file:close()
end
advtrains.fpath_ws=minetest.get_worldpath().."/advtrains_wagon_save"
local file, err = io.open(advtrains.fpath_ws, "r")
if not file then
local er=err or "Unknown Error"
atprint("Failed loading advtrains save file "..er)
else
local tbl = minetest.deserialize(file:read("*a"))
if type(tbl) == "table" then
advtrains.wagon_save=tbl
end
file:close()
end
advtrains.save = function()
atprint("saving")
advtrains.invalidate_all_paths()
local datastr = minetest.serialize(advtrains.trains)
if not datastr then
minetest.log("error", " Failed to serialize train data!")
return
end
local file, err = io.open(advtrains.fpath, "w")
if err then
return err
end
file:write(datastr)
file:close()
-- update wagon saves
for _,wagon in pairs(minetest.luaentities) do
if wagon.is_wagon and wagon.initialized then
wagon:get_staticdata()
end
end
--cross out userdata
for w_id, data in pairs(advtrains.wagon_save) do
data.name=nil
data.object=nil
if data.driver then
data.driver_name=data.driver:get_player_name()
data.driver=nil
else
data.driver_name=nil
end
if data.discouple then
data.discouple.object:remove()
data.discouple=nil
end
end
--atprint(dump(advtrains.wagon_save))
datastr = minetest.serialize(advtrains.wagon_save)
if not datastr then
minetest.log("error", " Failed to serialize train data!")
return
end
file, err = io.open(advtrains.fpath_ws, "w")
if err then
return err
end
file:write(datastr)
file:close()
advtrains.save_trackdb()
advtrains.atc.save()
end
minetest.register_on_shutdown(advtrains.save)
advtrains.save_and_audit_timer=advtrains.audit_interval
minetest.register_globalstep(function(dtime)
advtrains.save_and_audit_timer=advtrains.save_and_audit_timer-dtime
if advtrains.save_and_audit_timer<=0 then
local t=os.clock()
--save
advtrains.save()
advtrains.save_and_audit_timer=advtrains.audit_interval