Use: safe_file_write

This commit is contained in:
Coder12a 2019-01-22 16:16:56 -06:00
parent 3e63a1c657
commit c65bde260e
3 changed files with 18 additions and 37 deletions

View File

@ -341,6 +341,8 @@ end
function factions.Faction.remove_player(self, player)
self.players[player] = nil
self.onlineplayers[player] = nil
self.offlineplayers[player] = nil
factions.players[player] = nil
self:on_player_leave(player)
self:check_players_in_faction(self)
@ -368,7 +370,7 @@ function factions.Faction.remove_player(self, player)
self.onlineplayers[player] = nil
factions.bulk_save()
end
local parcel_size = factions_config.parcel_size
--! @param parcelpos position of the wanted parcel
--! @return whether this faction can claim a parcelpos
function factions.Faction.can_claim_parcel(self, parcelpos)
@ -599,7 +601,7 @@ end
function factions.Faction.tp_spawn(self, playername)
player = minetest.get_player_by_name(playername)
if player then
player:moveto(self.spawn, false)
player:set_pos(self.spawn)
end
end
@ -926,7 +928,6 @@ function factions.Faction.on_revoke_invite(self, player)
minetest.chat_send_player(player, "You are no longer invited to faction "..self.name)
end
local parcel_size = factions_config.parcel_size
function factions.get_parcel_pos(pos)
if factions_config.protection_style == "2d" then
return math.floor(pos.x / parcel_size) * parcel_size .. "," .. math.floor(pos.z / parcel_size) * parcel_size
@ -1023,17 +1024,12 @@ function factions.save()
--due to figuring out which data to save and which is temporary only
--all data is saved here
--this implies data needs to be cleant up on load
local file,error = io.open(factions_worldid .. "/" .. "factions.conf","w")
if file ~= nil then
file:write(minetest.serialize(factions.factions))
file:close()
else
saving = false
if not minetest.safe_file_write(factions_worldid .. "/" .. "factions.conf",minetest.serialize(factions.factions)) then
minetest.log("error","MOD factions: unable to save factions world specific data!: " .. error)
factions.bulk_save()
end
factions_ip.save()
saving = false
end
function factions.bulk_save()
@ -1239,11 +1235,8 @@ function factions.faction_tick()
end
end
local player_count = 0
minetest.register_on_joinplayer(
function(player)
player_count = player_count + 1
local name = player:get_player_name()
minetest.after(5,createHudfactionLand,player)
local faction = factions.get_player_faction(name)
@ -1252,7 +1245,7 @@ function(player)
minetest.after(5,createHudFactionName,player,faction.name)
minetest.after(5,createHudPower,player,faction)
faction.offlineplayers[name] = nil
faction.onlineplayers[name] = 1
faction.onlineplayers[name] = true
if faction.no_parcel ~= -1 then
local now = os.time() - faction.no_parcel
local l = factions_config.maximum_parcelless_faction_time
@ -1282,7 +1275,6 @@ end
minetest.register_on_leaveplayer(
function(player)
player_count = player_count - 1
local name = player:get_player_name()
local faction = factions.get_player_faction(name)
local id_name1 = name .. "factionLand"
@ -1298,14 +1290,9 @@ minetest.register_on_leaveplayer(
if hud_ids[id_name3] then
hud_ids[id_name3] = nil
end
faction.offlineplayers[name] = 1
faction.offlineplayers[name] = true
faction.onlineplayers[name] = nil
end
if player_count > 0 then
factions.bulk_save()
else
factions.save()
end
end
)
@ -1325,7 +1312,11 @@ minetest.register_on_respawnplayer(
end
)
minetest.register_on_shutdown(
function()
factions.save()
saving = false
end)
local default_is_protected = minetest.is_protected
minetest.is_protected = function(pos, player)

11
ip.lua
View File

@ -5,14 +5,9 @@ factions_ip.player_ips = {}
local factions_worldid = minetest.get_worldpath()
function factions_ip.save()
local file,error = io.open(factions_worldid .. "/" .. "factions_iplist.txt","w")
if file ~= nil then
file:write(minetest.serialize(factions_ip.player_ips))
file:close()
else
minetest.log("error","MOD factions: unable to save faction player ips!: " .. error)
end
if not minetest.safe_file_write(factions_worldid .. "/" .. "factions_iplist.txt", minetest.serialize(factions_ip.player_ips)) then
minetest.log("error","MOD factions: unable to save faction player ips!: " .. error)
end
end
function factions_ip.load()

View File

@ -5,12 +5,7 @@ misc_mod_data.data = {factions_version = "0.8.1",config = factions_config}
local factions_worldid = minetest.get_worldpath()
function misc_mod_data.save()
local file,error = io.open(factions_worldid .. "/" .. "factions_misc_mod_data.txt","w")
if file ~= nil then
file:write(minetest.serialize(misc_mod_data.data))
file:close()
else
if not minetest.safe_file_write(factions_worldid .. "/" .. "factions_misc_mod_data.txt", minetest.serialize(misc_mod_data.data)) then
minetest.log("error","MOD factions: unable to save factions misc mod data!: " .. error)
end
end