extension and serialize constant

This commit is contained in:
Evert Prants 2019-01-12 19:41:38 +02:00
parent 78488b6f39
commit 17deef2f14
Signed by: evert
GPG Key ID: 1688DA83D222D0B5

View File

@ -1,13 +1,22 @@
local extension = "lua"
local serialize = true
function towny.flatfile:write_meta(town,dir,data) function towny.flatfile:write_meta(town,dir,data)
local world = minetest.get_worldpath() local world = minetest.get_worldpath()
local directory = world.."/towny/"..dir local directory = world.."/towny/"..dir
local filepath = town..".json" local filepath = town.."."..extension
minetest.mkdir(directory) minetest.mkdir(directory)
if data.dirty then data.dirty = nil end if data.dirty then data.dirty = nil end
local serialized = minetest.serialize(data) local serialized
if serialize then
serialized = minetest.serialize(data)
else
serialized = minetest.write_json(data)
end
if not serialized then return end if not serialized then return end
minetest.safe_file_write(directory.."/"..filepath, serialized) minetest.safe_file_write(directory.."/"..filepath, serialized)
@ -26,7 +35,15 @@ function towny.flatfile:load_meta(filepath)
end end
file:close() file:close()
return minetest.deserialize(str)
local data
if serialize then
data = minetest.deserialize(str)
else
data = minetest.parse_json(str)
end
return data
end end
function towny.flatfile:save_town_meta(town) function towny.flatfile:save_town_meta(town)
@ -57,8 +74,8 @@ function towny.flatfile:load_all_towns()
local metas = minetest.get_dir_list(metadir, false) local metas = minetest.get_dir_list(metadir, false)
for _,file in pairs(metas) do for _,file in pairs(metas) do
if file:match(".json$") then if file:match("."..extension.."$") then
local town = file:gsub(".json","") local town = file:gsub("."..extension,"")
minetest.after(0.1, function () minetest.after(0.1, function ()
local towndata = towny.flatfile:load_meta(metadir.."/"..file) local towndata = towny.flatfile:load_meta(metadir.."/"..file)
if not towndata then return end if not towndata then return end
@ -72,8 +89,8 @@ function towny.flatfile:load_all_towns()
local regions = minetest.get_dir_list(regiondir, false) local regions = minetest.get_dir_list(regiondir, false)
for _,file in pairs(regions) do for _,file in pairs(regions) do
if file:match(".json$") then if file:match("."..extension.."$") then
local town = file:gsub(".json","") local town = file:gsub("."..extension,"")
minetest.after(0.1, function () minetest.after(0.1, function ()
local regiondata = towny.flatfile:load_meta(regiondir.."/"..file) local regiondata = towny.flatfile:load_meta(regiondir.."/"..file)
if not regiondata then return end if not regiondata then return end
@ -85,7 +102,7 @@ end
function towny.flatfile:delete_all_meta(town) function towny.flatfile:delete_all_meta(town)
local world = minetest.get_worldpath() local world = minetest.get_worldpath()
local file = town..".json" local file = town.."."..extension
for _,d in pairs(ldirs) do for _,d in pairs(ldirs) do
local dir = world.."/towny/"..d local dir = world.."/towny/"..d
@ -95,19 +112,24 @@ function towny.flatfile:delete_all_meta(town)
end end
local clock = 0 local clock = 0
local saving = false
local function carrier_tick() local function carrier_tick()
if not towny.dirty then return end if not towny.dirty or saving then return end
saving = true
for town,data in pairs(towny.towns) do for town,data in pairs(towny.towns) do
if data.dirty then if data.dirty then
towny.flatfile:save_town_meta(town) towny.flatfile:save_town_meta(town)
end end
end end
towny.dirty = false towny.dirty = false
saving = false
end end
-- Register -- Register
minetest.register_globalstep(function (dt) minetest.register_globalstep(function (dt)
clock = clock + ((1 * dt) + 1) clock = clock + (dt + 1)
if clock >= 60 then if clock >= 60 then
carrier_tick() carrier_tick()
clock = 0 clock = 0