better save file handling

This commit is contained in:
theFox6 2018-04-29 17:25:20 +02:00
parent 1b4d5d477f
commit 1c47f180fc
1 changed files with 18 additions and 16 deletions

View File

@ -9,24 +9,26 @@ weather_mod={
modpath=minetest.get_modpath("weather"), modpath=minetest.get_modpath("weather"),
} }
save_weather = function () weather = (function()
local file = io.open(minetest.get_worldpath().."/weather", "w+") local file_name = minetest.get_worldpath() .. "/weather"
file:write(minetest.serialize(weather))
file:close()
end
read_weather = function () minetest.register_on_shutdown(function()
local file = io.open(minetest.get_worldpath().."/weather", "r") local file = io.open(file_name, "w")
if not file then return {type = "none", wind = 0} end file:write(minetest.serialize(weather))
local readweather = minetest.deserialize(file:read()) file:close()
file:close() end)
if type(readweather)~="table" then
return {type = "none", wind = 0} local file = io.open(file_name, "r")
if file ~= nil then
local readweather = minetest.deserialize(file:read("*a"))
file:close()
if type(readweather)~="table" then
return {type = "none", wind = 0}
end
return readweather
end end
return readweather return {type = "none", wind = vector.new(0,0,0)}
end end) ()
weather = read_weather()
dofile(weather_mod.modpath.."/api.lua") dofile(weather_mod.modpath.."/api.lua")
dofile(weather_mod.modpath.."/rain.lua") dofile(weather_mod.modpath.."/rain.lua")