diff --git a/init.lua b/init.lua index 8b565b7..413e602 100644 --- a/init.lua +++ b/init.lua @@ -81,6 +81,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 = {}; @@ -99,37 +103,43 @@ minetest.register_privilege("travelnet_remove", { description = S("allows to dig -- read the configuration dofile(travelnet.path.."/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( 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))); + + 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 data = file:read("*all"); + travelnet.targets = minetest.deserialize( data ); + + 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 diff --git a/locale/de.po b/locale/de.po index 74eae03..f39db71 100644 --- a/locale/de.po +++ b/locale/de.po @@ -319,3 +319,8 @@ msgstr "Station entfernen" #, lua-format msgid "You do not have enough room in your inventory." msgstr "Du hast nicht genug Platz in deinem Inventar." + +#: init.lua +#, lua-format +msgid "[Mod travelnet] Error: Savefile '%s' is damaged. Saved the backup as '%s'." +msgstr "[Mod travelnet] Fehler: Sicherungsdatei '%s' ist beschÃĪdigt. Backup wurde unter '%s' gespeichert." diff --git a/locale/template.pot b/locale/template.pot index 177784c..86e0e5c 100644 --- a/locale/template.pot +++ b/locale/template.pot @@ -365,3 +365,8 @@ msgstr "" #, lua-format msgid "You do not have enough room in your inventory." msgstr "" + +#: init.lua +#, lua-format +msgid "[Mod travelnet] Error: Savefile '%s' is damaged. Saved the backup as '%s'." +msgstr ""