2020-11-19 19:46:40 +01:00
|
|
|
local S = minetest.get_translator("travelnet")
|
|
|
|
|
2021-07-02 05:58:54 +01:00
|
|
|
local mod_data_path = minetest.get_worldpath() .. "/mod_travelnet.data"
|
2020-11-19 19:46:40 +01:00
|
|
|
|
|
|
|
-- called whenever a station is added or removed
|
2021-07-03 18:41:00 +01:00
|
|
|
function travelnet.save_data()
|
2021-07-02 05:58:54 +01:00
|
|
|
local data = minetest.serialize(travelnet.targets)
|
2020-11-19 19:46:40 +01:00
|
|
|
|
2021-07-02 05:58:54 +01:00
|
|
|
local success = minetest.safe_file_write(mod_data_path, data)
|
|
|
|
if not success then
|
|
|
|
print(S("[Mod travelnet] Error: Savefile '@1' could not be written.", mod_data_path))
|
|
|
|
end
|
2020-11-19 19:46:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-07-03 18:41:00 +01:00
|
|
|
function travelnet.restore_data()
|
2021-07-02 05:58:54 +01:00
|
|
|
local file = io.open(mod_data_path, "r")
|
|
|
|
if not file then
|
|
|
|
print(S("[Mod travelnet] Error: Savefile '@1' not found.", mod_data_path))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local data = file:read("*all")
|
|
|
|
travelnet.targets = minetest.deserialize(data)
|
|
|
|
|
|
|
|
if not travelnet.targets then
|
|
|
|
local backup_file = mod_data_path .. ".bak"
|
|
|
|
print(S("[Mod travelnet] Error: Savefile '@1' is damaged." .. " " ..
|
|
|
|
"Saved the backup as '@2'.", mod_data_path, backup_file))
|
|
|
|
|
|
|
|
minetest.safe_file_write(backup_file, data)
|
|
|
|
travelnet.targets = {}
|
|
|
|
end
|
|
|
|
file:close()
|
2020-11-19 19:46:40 +01:00
|
|
|
end
|