2020-02-28 22:12:05 +01:00
|
|
|
-- get resource + dofile
|
|
|
|
function include(modname, file)
|
2020-07-08 20:08:31 +02:00
|
|
|
if not file then
|
|
|
|
file = modname
|
|
|
|
modname = minetest.get_current_modname()
|
|
|
|
end
|
2020-12-20 14:39:01 +01:00
|
|
|
return dofile(get_resource(modname, file))
|
2020-02-28 22:12:05 +01:00
|
|
|
end
|
|
|
|
|
2020-07-08 20:08:31 +02:00
|
|
|
function include_env(file_or_string, env, is_string)
|
|
|
|
setfenv(assert((is_string and loadstring or loadfile)(file_or_string)), env)()
|
|
|
|
end
|
|
|
|
|
|
|
|
function create_namespace(namespace_name, parent_namespace)
|
|
|
|
namespace_name = namespace_name or minetest.get_current_modname()
|
2020-03-23 20:20:43 +01:00
|
|
|
parent_namespace = parent_namespace or _G
|
2020-07-08 20:08:31 +02:00
|
|
|
local namespace = setmetatable({}, {__index = parent_namespace})
|
2020-11-15 11:28:05 +01:00
|
|
|
-- should use rawset if MT's strictness wasn't disabled in init.lua
|
|
|
|
parent_namespace[namespace_name] = namespace
|
2020-07-08 20:08:31 +02:00
|
|
|
return namespace
|
|
|
|
end
|
|
|
|
|
|
|
|
-- formerly extend_mod
|
|
|
|
function extend(modname, file)
|
|
|
|
if not file then
|
|
|
|
file = modname
|
|
|
|
modname = minetest.get_current_modname()
|
|
|
|
end
|
|
|
|
include_env(get_resource(modname, file .. ".lua"), _G[modname])
|
2020-02-28 22:12:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- runs main.lua in table env
|
2020-02-29 12:55:02 +01:00
|
|
|
-- formerly include_mod
|
|
|
|
function init(modname)
|
2020-07-08 20:08:31 +02:00
|
|
|
modname = modname or minetest.get_current_modname()
|
|
|
|
create_namespace(modname)
|
|
|
|
extend(modname, "main")
|
2020-02-28 22:12:05 +01:00
|
|
|
end
|
|
|
|
|
2020-02-29 12:55:02 +01:00
|
|
|
function extend_string(modname, string)
|
2020-07-08 20:08:31 +02:00
|
|
|
if not string then
|
|
|
|
string = modname
|
|
|
|
modname = minetest.get_current_modname()
|
|
|
|
end
|
|
|
|
include_env(string, _G[modname], true)
|
2020-12-20 14:39:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function configuration(modname)
|
|
|
|
modname = modname or minetest.get_current_modname()
|
|
|
|
local schema = modlib.schema.new(assert(include(modname, "schema.lua")))
|
|
|
|
schema.name = schema.name or modname
|
|
|
|
assert(schema.type == "table")
|
|
|
|
local overrides = {}
|
|
|
|
local conf
|
|
|
|
local function add(path)
|
|
|
|
for _, format in ipairs{
|
|
|
|
{extension = "lua", read = function(text)
|
|
|
|
assert(overrides._C == nil)
|
|
|
|
local additions = setfenv(assert(loadstring(text)), setmetatable(overrides, {__index = {_C = overrides}}))()
|
|
|
|
setmetatable(overrides, nil)
|
|
|
|
if additions == nil then
|
|
|
|
return overrides
|
|
|
|
end
|
|
|
|
return additions
|
|
|
|
end},
|
|
|
|
{extension = "luon", read = function(text)
|
|
|
|
local value = {setfenv(assert(loadstring("return " .. text)), setmetatable(overrides, {}))()}
|
|
|
|
assert(#value == 1)
|
|
|
|
value = value[1]
|
|
|
|
local function check_type(value)
|
|
|
|
local type = type(value)
|
|
|
|
if type == "table" then
|
|
|
|
assert(getmetatable(value) == nil)
|
|
|
|
for key, value in pairs(value) do
|
|
|
|
check_type(key)
|
|
|
|
check_type(value)
|
|
|
|
end
|
|
|
|
elseif not (type == "boolean" or type == "number" or type == "string") then
|
|
|
|
error("disallowed type " .. type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
check_type(value)
|
|
|
|
return value
|
|
|
|
end},
|
2020-12-22 11:09:45 +01:00
|
|
|
{extension = "conf", read = function(text) return modlib.conf.build_setting_tree(Settings(text):to_table()) end, convert_strings = true},
|
2020-12-20 14:39:01 +01:00
|
|
|
{extension = "json", read = minetest.parse_json}
|
|
|
|
} do
|
|
|
|
local content = modlib.file.read(path .. "." .. format.extension)
|
|
|
|
if content then
|
|
|
|
overrides = modlib.table.deep_add_all(overrides, format.read(content))
|
|
|
|
conf = schema:load(overrides, {convert_strings = format.convert_strings, error_message = true})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
add(minetest.get_worldpath() .. "/conf/" .. modname)
|
|
|
|
add(get_resource(modname, "conf"))
|
|
|
|
local minetest_conf = modlib.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})
|
|
|
|
end
|
2020-12-22 11:43:50 +01:00
|
|
|
modlib.file.ensure_content(get_resource(modname, "settingtypes.txt"), schema:generate_settingtypes())
|
2020-12-22 12:37:29 +01:00
|
|
|
local readme_path = get_resource(modname, "Readme.md")
|
|
|
|
local readme = modlib.file.read(readme_path)
|
|
|
|
if readme then
|
|
|
|
local modified = false
|
|
|
|
readme = readme:gsub("<!%-%-modlib:conf:(%d)%-%->" .. "(.-)" .. "<!%-%-modlib:conf%-%->", function(level, content)
|
|
|
|
schema._md_level = assert(tonumber(level)) + 1
|
2020-12-22 12:42:28 +01:00
|
|
|
-- HACK: Newline between comment and heading (MD implementations don't handle comments properly)
|
|
|
|
local markdown = "\n" .. schema:generate_markdown()
|
2020-12-22 12:37:29 +01:00
|
|
|
if content ~= markdown then
|
|
|
|
modified = true
|
|
|
|
return "<!--modlib:conf:" .. level .. "-->" .. markdown .. "<!--modlib:conf-->"
|
|
|
|
end
|
|
|
|
end, 1)
|
|
|
|
if modified then
|
|
|
|
assert(modlib.file.write(readme_path, readme))
|
|
|
|
end
|
|
|
|
end
|
2020-12-20 14:39:01 +01:00
|
|
|
if conf == nil then
|
|
|
|
return schema:load({}, {error_message = true})
|
|
|
|
end
|
|
|
|
return conf
|
2020-02-28 22:12:05 +01:00
|
|
|
end
|