From 63ed12fe2225d0de1c1939fe2da33557852fb98f Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Mon, 12 Aug 2024 07:24:28 +0200 Subject: [PATCH] save / load --- data.lua | 17 +++++++++-- integrations/advtrains.lua | 58 ++++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/data.lua b/data.lua index e75ec8f..13a793a 100644 --- a/data.lua +++ b/data.lua @@ -31,16 +31,27 @@ function mapsync.load_data(key) return value end +-- returns the path of the data file, nil if not available +function mapsync.get_data_file_path(key) + local data_backend_def = mapsync.get_data_backend() + if not data_backend_def then + -- no data backend defined + return + end + + return data_backend_def.path .. "/" .. key +end + -- returns a file to write to in the data-storage, nil if not available function mapsync.get_data_file(key, mode) -- default to read mode = mode or "r" - local data_backend_def = mapsync.get_data_backend() - if not data_backend_def then + local path = mapsync.get_data_file_path(key) + if not path then -- no data backend defined return end - return global_env.io.open(data_backend_def.path .. "/" .. key, mode) + return global_env.io.open(path, mode) end \ No newline at end of file diff --git a/integrations/advtrains.lua b/integrations/advtrains.lua index af8abd7..d5ad79c 100644 --- a/integrations/advtrains.lua +++ b/integrations/advtrains.lua @@ -1,3 +1,7 @@ +-- advtrains compat +-- saves a snapshot of the advtrains-data with the `/mapsync_save_advtrains` command +-- loads the snapshot if available on startup, defaults to the worldfolder if no snapshot found + -- sanity checks assert(type(advtrains.load_version_4) == "function") assert(type(advtrains.ndb.save_callback) == "function") @@ -11,34 +15,36 @@ assert(type(advtrains.fpath) == "string") -- local old_advtrains_read_component = advtrains.read_component function advtrains.read_component(name) assert(name == "version") + -- currently supported version return 4 end -- local old_advtrains_save_component = advtrains.save_component -function advtrains.save_component() - -- no-op +function advtrains.save_component(name) + assert(name == "version") end +-- load from data- or world-path local old_load_atomic = serialize_lib.load_atomic function serialize_lib.load_atomic(filename, load_callback) + local relpath = string.sub(filename, #advtrains.fpath + 2) print(dump({ fn = "serialize_lib.load_atomic", filename = filename, fpath = advtrains.fpath, - relpath = string.sub(filename, #advtrains.fpath + 2) + relpath = relpath })) - return old_load_atomic(filename, load_callback) -end -local old_save_atomic_multiple = serialize_lib.save_atomic_multiple -function serialize_lib.save_atomic_multiple(parts_table, filename_prefix, callback_table, config) - print(dump({ - fn = "serialize_lib.save_atomic_multiple", - parts_table = parts_table, - filename_prefix = filename_prefix, - config = config - })) - return old_save_atomic_multiple(parts_table, filename_prefix, callback_table, config) + local data_file = mapsync.get_data_file("advtrains_" .. relpath) + if data_file then + -- data-snapshot available, load it + -- TODO: create a timestamp and load only if a newer snapshot if found + local data_path = mapsync.get_data_file_path("advtrains_" .. relpath) + return old_load_atomic(data_path, load_callback) + else + -- no data snapshot available, load default from world-folder + return old_load_atomic(filename, load_callback) + end end local advtrains_parts = {"atlatc.ls", "interlocking.ls", "core.ls", "lines.ls", "ndb4.ls"} @@ -46,9 +52,31 @@ local advtrains_parts = {"atlatc.ls", "interlocking.ls", "core.ls", "lines.ls", minetest.register_chatcommand("mapsync_save_advtrains", { privs = { mapsync = true }, func = function() + if not mapsync.get_data_backend() then + return false, "no data-backend configured" + end + + -- save advtrains data first and remove players from wagons + local remove_players_from_wagons = true + advtrains.save(remove_players_from_wagons) + + -- copy files to data-directory + local count = 0 for _, part in ipairs(advtrains_parts) do local path = advtrains.fpath .. "_" .. part - print(path) + local src = io.open(path, "rb") + if not src then + return false, "open failed for '" .. path .. "'" + end + + local dst = mapsync.get_data_file("advtrains_" .. part, "wb") + local data = src:read("*all") + count = count + #data + dst:write(data) + dst:close() + src:close() end + + return true, "saved " .. count .. " bytes of advtrains data" end }) \ No newline at end of file