Skip to content

Commit

Permalink
save / load
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Aug 12, 2024
1 parent d73e19e commit 63ed12f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
17 changes: 14 additions & 3 deletions data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 43 additions & 15 deletions integrations/advtrains.lua
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -11,44 +15,68 @@ 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"}

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
})

0 comments on commit 63ed12f

Please sign in to comment.