Move setting tree building from conf to mod module

This commit is contained in:
Lars Mueller 2022-09-28 20:45:00 +02:00
parent a0d092090d
commit 1244f1eba0
3 changed files with 35 additions and 32 deletions

@ -139,8 +139,6 @@ if minetest then
local ml_mt = modlib.minetest local ml_mt = modlib.minetest
ml_mt.mod.get_resource = get_resource ml_mt.mod.get_resource = get_resource
modlib.mod = ml_mt.mod modlib.mod = ml_mt.mod
ml_mt.conf.build_setting_tree()
modlib.conf = ml_mt.conf
-- HACK force load minetest/gametime.lua to ensure that the globalstep is registered earlier than globalsteps of mods depending on modlib -- HACK force load minetest/gametime.lua to ensure that the globalstep is registered earlier than globalsteps of mods depending on modlib
dofile(get_resource(modlib.modname, "minetest", "gametime.lua")) dofile(get_resource(modlib.modname, "minetest", "gametime.lua"))
local ie = minetest.request_insecure_environment() local ie = minetest.request_insecure_environment()

@ -1,36 +1,11 @@
-- Localize globals -- Localize globals
local assert, dump, error, ipairs, minetest, modlib, pairs, pcall, table, tonumber, type, unpack = assert, dump, error, ipairs, minetest, modlib, pairs, pcall, table, tonumber, type, unpack local assert, dump, error, ipairs, minetest, modlib, pairs, pcall, table, tonumber, type
= assert, dump, error, ipairs, minetest, modlib, pairs, pcall, table, tonumber, type
-- Set environment -- Set environment
local _ENV = {} local _ENV = {}
setfenv(1, _ENV) setfenv(1, _ENV)
-- not deprecated
-- TODO find a better way to deal with settings used as both parent & leaf, ideally preserving the data of both (store in boolean field?)
local warn_parent_leaf = "modlib.minetest.conf.build_tree: setting %s used both as parent setting and as leaf, ignoring children"
function build_tree(dict)
local tree = {}
for key, value in pairs(dict) do
local path = modlib.text.split_unlimited(key, ".")
local subtree = tree
for i = 1, #path - 1 do
local index = tonumber(path[i]) or path[i]
subtree[index] = subtree[index] or {}
subtree = subtree[index]
if type(subtree) ~= "table" then
minetest.log("warning", warn_parent_leaf:format(table.concat({unpack(path, 1, i)}, ".")))
break
end
end
if type(subtree) == "table" then
if type(subtree[path[#path]]) == "table" then
minetest.log("warning", warn_parent_leaf:format(key))
end
subtree[path[#path]] = value
end
end
return tree
end
if minetest then if minetest then
function build_setting_tree() function build_setting_tree()
settings = build_tree(minetest.settings:to_table()) settings = build_tree(minetest.settings:to_table())

@ -1,5 +1,6 @@
-- Localize globals -- Localize globals
local Settings, _G, assert, dofile, error, getmetatable, ipairs, loadfile, loadstring, minetest, modlib, pairs, rawget, rawset, setfenv, setmetatable, tonumber, type = Settings, _G, assert, dofile, error, getmetatable, ipairs, loadfile, loadstring, minetest, modlib, pairs, rawget, rawset, setfenv, setmetatable, tonumber, type local Settings, _G, assert, dofile, error, getmetatable, ipairs, loadfile, loadstring, minetest, modlib, pairs, rawget, rawset, setfenv, setmetatable, tonumber, type, table_concat, unpack
= Settings, _G, assert, dofile, error, getmetatable, ipairs, loadfile, loadstring, minetest, modlib, pairs, rawget, rawset, setfenv, setmetatable, tonumber, type, table.concat, unpack
-- Set environment -- Set environment
local _ENV = {} local _ENV = {}
@ -57,6 +58,33 @@ function init(modname)
extend(modname, "main") extend(modname, "main")
end end
local warn_parent_leaf = "modlib: setting %s used both as parent setting and as leaf, ignoring children"
local function build_tree(dict)
local tree = {}
for key, value in pairs(dict) do
local path = modlib.text.split_unlimited(key, ".")
local subtree = tree
for i = 1, #path - 1 do
local index = tonumber(path[i]) or path[i]
subtree[index] = subtree[index] or {}
subtree = subtree[index]
if type(subtree) ~= "table" then
minetest.log("warning", warn_parent_leaf:format(table_concat({unpack(path, 1, i)}, ".")))
break
end
end
if type(subtree) == "table" then
if type(subtree[path[#path]]) == "table" then
minetest.log("warning", warn_parent_leaf:format(key))
end
subtree[path[#path]] = value
end
end
return tree
end
settings = build_tree(minetest.settings:to_table())
--> conf, schema --> conf, schema
function configuration(modname) function configuration(modname)
modname = modname or minetest.get_current_modname() modname = modname or minetest.get_current_modname()
@ -96,7 +124,9 @@ function configuration(modname)
check_type(value) check_type(value)
return value return value
end}, end},
{extension = "conf", read = function(text) return modlib.conf.build_setting_tree(Settings(text):to_table()) end, convert_strings = true}, {extension = "conf", read = function(text)
return build_tree(Settings(text):to_table())
end, convert_strings = true},
{extension = "json", read = minetest.parse_json} {extension = "json", read = minetest.parse_json}
} do } do
local content = modlib.file.read(path .. "." .. format.extension) local content = modlib.file.read(path .. "." .. format.extension)
@ -108,7 +138,7 @@ function configuration(modname)
end end
add(minetest.get_worldpath() .. "/conf/" .. modname) add(minetest.get_worldpath() .. "/conf/" .. modname)
add(get_resource(modname, "conf")) add(get_resource(modname, "conf"))
local minetest_conf = modlib.conf.settings[schema.name] local minetest_conf = settings[schema.name]
if minetest_conf then if minetest_conf then
overrides = modlib.table.deep_add_all(overrides, minetest_conf) overrides = modlib.table.deep_add_all(overrides, minetest_conf)
conf = schema:load(overrides, {convert_strings = true, error_message = true}) conf = schema:load(overrides, {convert_strings = true, error_message = true})