use storage backend instead of Settings() for max global / total pads

master
entuland 2022-06-19 14:01:35 +02:00
parent a5a591241c
commit d62fb59884
2 changed files with 66 additions and 49 deletions

View File

@ -5,7 +5,6 @@ tpad.texture = "tpad-texture.png"
tpad.mesh = "tpad-mesh.obj"
tpad.nodename = "tpad:tpad"
tpad.mod_path = minetest.get_modpath(tpad.mod_name)
tpad.settings_file = minetest.get_worldpath() .. "/mod_storage/" .. tpad.mod_name .. ".custom.conf"
local PRIVATE_PAD_STRING = "Private (only owner)"
local PUBLIC_PAD_STRING = "Public (only owner's network)"
@ -215,42 +214,6 @@ end
local submit = {}
function tpad.set_max_total_pads(max)
if not max then max = 0 end
local settings = Settings(tpad.settings_file)
settings:set("max_total_pads_per_player", max)
settings:write()
end
function tpad.get_max_total_pads()
local settings = Settings(tpad.settings_file)
local max = tonumber(settings:get("max_total_pads_per_player"))
if not max then
tpad.set_max_total_pads(100)
return 100
end
return max
end
tpad.get_max_total_pads()
function tpad.set_max_global_pads(max)
if not max then max = 0 end
local settings = Settings(tpad.settings_file)
settings:set("max_global_pads_per_player", max)
settings:write()
end
function tpad.get_max_global_pads()
local settings = Settings(tpad.settings_file)
local max = tonumber(settings:get("max_global_pads_per_player"))
if not max then
tpad.set_max_global_pads(4)
return 4
end
return max
end
tpad.get_max_global_pads()
function tpad.max_total_pads_reached(placer)
local placername = placer:get_player_name()
if minetest.get_player_privs(placername).tpad_admin then

View File

@ -26,19 +26,57 @@ function tpad._set_stored_pads(ownername, pads)
storage:set_string("pads:" .. ownername, minetest.serialize(pads))
end
function tpad._storage_sanity_check()
local storage_version = storage:get_string("_version")
local storage_path = minetest.get_worldpath() .. "/mod_storage/"
if storage_version == "1.1" then
tpad._copy_file(storage_path .. tpad.mod_name, storage_path .. tpad.mod_name .. ".1.1.backup")
tpad._convert_storage_1_1()
elseif storage_version ~= "" and storage_version ~= tpad.version then
error("Mod storage version not supported, aborting to prevent data corruption")
end
storage:set_string("_version", tpad.version)
function tpad.set_max_total_pads(max)
if not max then max = 0 end
storage:set_string("max_total_pads_per_player", max)
end
function tpad._convert_storage_1_1()
function tpad.get_max_total_pads()
local max = tonumber(storage:get_string("max_total_pads_per_player"))
if not max then
tpad.set_max_total_pads(100)
return 100
end
return max
end
function tpad.set_max_global_pads(max)
if not max then max = 0 end
storage:set_string("max_global_pads_per_player", max)
end
function tpad.get_max_global_pads()
local max = tonumber(storage:get_string("max_global_pads_per_player"))
if not max then
tpad.set_max_global_pads(4)
return 4
end
return max
end
local function _convert_legacy_settings()
local legacy_settings_file = minetest.get_worldpath() .. "/mod_storage/" .. tpad.mod_name .. ".custom.conf"
local file = io.open(legacy_settings_file, "r")
if file then
file:close()
local settings = Settings(legacy_settings_file)
local max_global = tonumber(settings:get("max_global_pads_per_player"))
if max_global then
tpad.set_max_global_pads(max_global)
end
local max_total = tonumber(settings:get("max_total_pads_per_player"))
if max_total then
tpad.set_max_total_pads(max_total)
end
os.remove(legacy_settings_file)
end
end
_convert_legacy_settings()
tpad.get_max_total_pads()
tpad.get_max_global_pads()
local function _convert_storage_1_1()
local storage_table = storage:to_table()
for field, value in pairs(storage_table.fields) do
local parts = field:split(":")
@ -53,4 +91,20 @@ function tpad._convert_storage_1_1()
storage:from_table(storage_table)
end
tpad._storage_sanity_check()
local function _storage_version_check()
local storage_version = storage:get_string("_version")
local storage_path = minetest.get_worldpath() .. "/mod_storage/"
if storage_version == "1.1" then
local file = io.open(storage_path .. tpad.mod_name, "r")
if file then
file:close()
tpad._copy_file(storage_path .. tpad.mod_name, storage_path .. tpad.mod_name .. ".1.1.backup")
end
_convert_storage_1_1()
elseif storage_version ~= "" and storage_version ~= tpad.version then
error("Mod storage version not supported, aborting to prevent data corruption")
end
storage:set_string("_version", tpad.version)
end
_storage_version_check()