Change the way metadata are saved

This commit is contained in:
Jean-Patrick Guerrero 2021-05-31 20:48:12 +02:00
parent d612f8697d
commit 978c2b23a2

View File

@ -1,7 +1,10 @@
i3 = {} i3 = {}
local storage = core.get_mod_storage()
local slz, dslz = core.serialize, core.deserialize
local pdata = dslz(storage:get_string "pdata") or {}
-- Caches -- Caches
local pdata = {}
local init_items = {} local init_items = {}
local searches = {} local searches = {}
local recipes_cache = {} local recipes_cache = {}
@ -46,9 +49,7 @@ local get_craft_result = core.get_craft_result
local translate = minetest.get_translated_string local translate = minetest.get_translated_string
local on_joinplayer = core.register_on_joinplayer local on_joinplayer = core.register_on_joinplayer
local get_all_recipes = core.get_all_craft_recipes local get_all_recipes = core.get_all_craft_recipes
local slz, dslz = core.serialize, core.deserialize
local on_mods_loaded = core.register_on_mods_loaded local on_mods_loaded = core.register_on_mods_loaded
local on_leaveplayer = core.register_on_leaveplayer
local get_player_info = core.get_player_information local get_player_info = core.get_player_information
local create_inventory = core.create_detached_inventory local create_inventory = core.create_detached_inventory
local on_receive_fields = core.register_on_player_receive_fields local on_receive_fields = core.register_on_player_receive_fields
@ -88,8 +89,6 @@ local HUD_TIMER_MAX = 1.5
local MIN_FORMSPEC_VERSION = 4 local MIN_FORMSPEC_VERSION = 4
local META_SAVES = {"bag_size", "waypoints"}
local BAG_SIZES = { local BAG_SIZES = {
small = INV_SIZE + 3, small = INV_SIZE + 3,
medium = INV_SIZE + 6, medium = INV_SIZE + 6,
@ -469,18 +468,6 @@ local function table_replace(t, val, new)
end end
end end
local function save_meta(player, entries)
local name = player:get_player_name()
local data = pdata[name]
if not data then return end
local meta = player:get_meta()
for _, entry in ipairs(entries) do
meta:set_string(entry, slz(data[entry]))
end
end
local craft_types = {} local craft_types = {}
function i3.register_craft_type(name, def) function i3.register_craft_type(name, def)
@ -2345,27 +2332,19 @@ end
local function init_data(player, info) local function init_data(player, info)
local name = player:get_player_name() local name = player:get_player_name()
pdata[name] = pdata[name] or {}
pdata[name] = {
filter = "",
pagenum = 1,
items = init_items,
items_raw = init_items,
favs = {},
export_counts = {},
current_tab = 1,
subcat = 1,
scrbar_inv = 0,
lang_code = get_lang_code(info),
}
local data = pdata[name] local data = pdata[name]
local meta = player:get_meta()
for i = 1, #META_SAVES do data.filter = ""
local recover = META_SAVES[i] data.pagenum = 1
data[recover] = dslz(meta:get_string(recover)) data.items = init_items
end data.items_raw = init_items
data.favs = {}
data.export_counts = {}
data.current_tab = 1
data.subcat = 1
data.scrbar_inv = 0
data.lang_code = get_lang_code(info)
after(0, set_fs, player) after(0, set_fs, player)
end end
@ -2750,7 +2729,6 @@ end
if rawget(_G, "skins") then if rawget(_G, "skins") then
__skinsdb = true __skinsdb = true
insert(META_SAVES, "skin_id")
end end
if rawget(_G, "awards") then if rawget(_G, "awards") then
@ -3089,20 +3067,24 @@ core.register_on_dieplayer(function(player)
set_fs(player) set_fs(player)
end) end)
on_leaveplayer(function(player) local META_SAVES = {
save_meta(player, META_SAVES) bag_size = true,
waypoints = true,
local name = player:get_player_name() skin_id = true,
pdata[name] = nil inv_items = true,
end) known_recipes = true,
}
on_shutdown(function() on_shutdown(function()
local players = get_players() for name, v in pairs(pdata) do
for dat in pairs(v) do
for i = 1, #players do if not META_SAVES[dat] then
local player = players[i] pdata[name][dat] = nil
save_meta(player, META_SAVES) end
end end
end
storage:set_string("pdata", slz(pdata))
end) end)
on_receive_fields(function(player, formname, fields) on_receive_fields(function(player, formname, fields)
@ -3353,11 +3335,10 @@ if progressive_mode then
on_joinplayer(function(player) on_joinplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
local meta = player:get_meta()
local data = pdata[name] local data = pdata[name]
data.inv_items = dslz(meta:get_string "inv_items") or {} data.inv_items = data.inv_items or {}
data.known_recipes = dslz(meta:get_string "known_recipes") or 0 data.known_recipes = data.known_recipes or 0
local items = get_filtered_items(player, data) local items = get_filtered_items(player, data)
data.items_raw = items data.items_raw = items
@ -3367,8 +3348,6 @@ if progressive_mode then
init_hud(player, data) init_hud(player, data)
end end
end) end)
table_merge(META_SAVES, {"inv_items", "known_recipes"})
end end
local bag_recipes = { local bag_recipes = {