From 1244f1eba0824c612b464e3c6c989fdc8f524a31 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Wed, 28 Sep 2022 20:45:00 +0200 Subject: [PATCH] Move setting tree building from conf to mod module --- init.lua | 2 -- minetest/conf.lua | 29 ++--------------------------- minetest/mod.lua | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/init.lua b/init.lua index 9e592ae..fd99ba2 100644 --- a/init.lua +++ b/init.lua @@ -139,8 +139,6 @@ if minetest then local ml_mt = modlib.minetest ml_mt.mod.get_resource = get_resource 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 dofile(get_resource(modlib.modname, "minetest", "gametime.lua")) local ie = minetest.request_insecure_environment() diff --git a/minetest/conf.lua b/minetest/conf.lua index e673dbd..a06fba0 100644 --- a/minetest/conf.lua +++ b/minetest/conf.lua @@ -1,36 +1,11 @@ -- 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 local _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 function build_setting_tree() settings = build_tree(minetest.settings:to_table()) diff --git a/minetest/mod.lua b/minetest/mod.lua index b233349..5651292 100644 --- a/minetest/mod.lua +++ b/minetest/mod.lua @@ -1,5 +1,6 @@ -- 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 local _ENV = {} @@ -57,6 +58,33 @@ function init(modname) extend(modname, "main") 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 function configuration(modname) modname = modname or minetest.get_current_modname() @@ -96,7 +124,9 @@ function configuration(modname) check_type(value) return value 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} } do local content = modlib.file.read(path .. "." .. format.extension) @@ -108,7 +138,7 @@ function configuration(modname) end add(minetest.get_worldpath() .. "/conf/" .. modname) add(get_resource(modname, "conf")) - local minetest_conf = modlib.conf.settings[schema.name] + local minetest_conf = settings[schema.name] if minetest_conf then overrides = modlib.table.deep_add_all(overrides, minetest_conf) conf = schema:load(overrides, {convert_strings = true, error_message = true})