Use safe_write_file, backup damaged network files
This commit is contained in:
parent
c295395ae8
commit
c1a4ca0741
38
init.lua
38
init.lua
@ -78,6 +78,10 @@
|
||||
- target list is now centered if there are less than 9 targets
|
||||
--]]
|
||||
|
||||
-- Required to save the travelnet data properly in all cases
|
||||
if not minetest.safe_file_write then
|
||||
error("[Mod travelnet] Your Minetest version is no longer supported. (version < 0.4.17)")
|
||||
end
|
||||
|
||||
travelnet = {};
|
||||
|
||||
@ -98,37 +102,43 @@ minetest.register_privilege("travelnet_remove", { description = S("allows to dig
|
||||
-- read the configuration
|
||||
dofile(minetest.get_modpath("travelnet").."/config.lua"); -- the normal, default travelnet
|
||||
|
||||
|
||||
travelnet.mod_data_path = minetest.get_worldpath().."/mod_travelnet.data"
|
||||
|
||||
-- TODO: save and restore ought to be library functions and not implemented in each individual mod!
|
||||
-- called whenever a station is added or removed
|
||||
travelnet.save_data = function()
|
||||
|
||||
local data = minetest.serialize( travelnet.targets );
|
||||
local path = minetest.get_worldpath().."/mod_travelnet.data";
|
||||
|
||||
local file = io.open( path, "w" );
|
||||
if( file ) then
|
||||
file:write( data );
|
||||
file:close();
|
||||
else
|
||||
print(S("[Mod travelnet] Error: Savefile '%s' could not be written."):format(tostring(path)));
|
||||
local success = minetest.safe_file_write( travelnet.mod_data_path, data );
|
||||
if( not success ) then
|
||||
print(S("[Mod travelnet] Error: Savefile '%s' could not be written.")
|
||||
:format(travelnet.mod_data_path));
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
travelnet.restore_data = function()
|
||||
|
||||
local path = minetest.get_worldpath().."/mod_travelnet.data";
|
||||
local file = io.open( travelnet.mod_data_path, "r" );
|
||||
if( not file ) then
|
||||
print(S("[Mod travelnet] Error: Savefile '%s' not found.")
|
||||
:format(travelnet.mod_data_path));
|
||||
return;
|
||||
end
|
||||
|
||||
local file = io.open( path, "r" );
|
||||
if( file ) then
|
||||
local data = file:read("*all");
|
||||
travelnet.targets = minetest.deserialize( data );
|
||||
file:close();
|
||||
else
|
||||
print(S("[Mod travelnet] Error: Savefile '%s' not found."):format(tostring(path)));
|
||||
|
||||
if( not travelnet.targets ) then
|
||||
local backup_file = travelnet.mod_data_path..".bak"
|
||||
print(S("[Mod travelnet] Error: Savefile '%s' is damaged. Saved the backup as '%s'.")
|
||||
:format(travelnet.mod_data_path, backup_file));
|
||||
|
||||
minetest.safe_file_write( backup_file, data );
|
||||
travelnet.targets = {};
|
||||
end
|
||||
file:close();
|
||||
end
|
||||
|
||||
|
||||
|
@ -9,6 +9,7 @@ allows to dig travelnet boxes which belog to nets of other players = erlaubt es,
|
||||
|
||||
[Mod travelnet] Error: Savefile '%s' could not be written. = [Mod travelnet] Fehler: Sicherungsdatei '%s' konnte nicht geschrieben werden.
|
||||
[Mod travelnet] Error: Savefile '%s' not found. = [Mod travelnet] Fehler: Sicherungsdatei '%s' nicht gefunden.
|
||||
[Mod travelnet] Error: Savefile '%s' is damaged. Saved the backup as '%s'. = [Mod travelnet] Fehler: Sicherungsdatei '%s' ist beschädigt. Backup wurde unter '%s' gespeichert.
|
||||
|
||||
Back = Zurück
|
||||
Exit = Ende
|
||||
|
@ -9,6 +9,7 @@ allows to dig travelnet boxes which belog to nets of other players =
|
||||
|
||||
[Mod travelnet] Error: Savefile '%s' could not be written. =
|
||||
[Mod travelnet] Error: Savefile '%s' not found. =
|
||||
[Mod travelnet] Error: Savefile '%s' is damaged. Saved the backup as '%s'. =
|
||||
|
||||
Back =
|
||||
Exit =
|
||||
|
Loading…
x
Reference in New Issue
Block a user