InsaneProtestor/mods/ip_elevator/persistence.lua

36 lines
998 B
Lua
Raw Normal View History

2022-11-21 22:12:22 +01:00
local S = minetest.get_translator("elevator")
local mod_data_path = minetest.get_worldpath() .. "/mod_elevator.data"
-- called whenever a station is added or removed
function elevator.save_data()
local data = minetest.serialize(elevator.targets)
local success = minetest.safe_file_write(mod_data_path, data)
if not success then
print(S("[Mod elevator] Error: Savefile '@1' could not be written.", mod_data_path))
end
end
function elevator.restore_data()
local file = io.open(mod_data_path, "r")
if not file then
print(S("[Mod elevator] Error: Savefile '@1' not found.", mod_data_path))
return
end
local data = file:read("*all")
elevator.targets = minetest.deserialize(data)
if not elevator.targets then
local backup_file = mod_data_path .. ".bak"
print(S("[Mod elevator] Error: Savefile '@1' is damaged." .. " " ..
"Saved the backup as '@2'.", mod_data_path, backup_file))
minetest.safe_file_write(backup_file, data)
elevator.targets = {}
end
file:close()
end