modlib/conf.lua

101 lines
4.2 KiB
Lua
Raw Normal View History

2020-02-09 01:39:54 +01:00
minetest.mkdir(minetest.get_worldpath().."/config")
function get_path(confname)
return minetest.get_worldpath().."/config/"..confname
end
function load (filename, constraints)
2020-03-25 22:00:51 +01:00
local config = minetest.parse_json(modlib.file.read(filename))
2020-02-09 01:39:54 +01:00
if constraints then
2020-03-25 22:00:51 +01:00
local error_message = check_constraints(config, constraints)
2020-02-09 01:39:54 +01:00
if error_message then
2020-04-05 10:42:44 +02:00
error("Configuration - "..filename.." doesn't satisfy constraints: "..error_message)
2020-02-09 01:39:54 +01:00
end
end
return config
end
function load_or_create(filename, replacement_file, constraints)
modlib.file.create_if_not_exists_from_file(filename, replacement_file)
return load(filename, constraints)
end
2020-03-25 22:00:51 +01:00
function import(modname, constraints)
return load_or_create(get_path(modname)..".json", modlib.mod.get_resource(modname, "default_config.json"), constraints)
2020-02-09 01:39:54 +01:00
end
function check_constraints(value, constraints)
local t=type(value)
if constraints.func then
local possible_errors=constraints.func(value)
if possible_errors then
return possible_errors
end
end
if constraints.type and constraints.type~=t then
2020-04-05 10:42:44 +02:00
return "Wrong type: Expected "..constraints.type..", found "..t
2020-02-09 01:39:54 +01:00
end
if (t == "number" or t == "string") and constraints.range then
if value < constraints.range[1] or (constraints.range[2] and value > constraints.range[2]) then
2020-04-05 10:42:44 +02:00
return "Not inside range: Expected value >= "..constraints.range[1].." and <= "..constraints.range[1]..", found "..minetest.write_json(value)
2020-02-09 01:39:54 +01:00
end
end
if constraints.possible_values and not constraints.possible_values[value] then
2020-04-05 10:42:44 +02:00
return "None of the possible values: Expected one of "..minetest.write_json(modlib.table.keys(constraints.possible_values))..", found "..minetest.write_json(value)
2020-02-09 01:39:54 +01:00
end
if t == "table" then
if constraints.children then
for k, v in pairs(value) do
local child=constraints.children[k]
if not child then
2020-04-05 10:42:44 +02:00
return "Unexpected table entry: Expected one of "..minetest.write_json(modlib.table.keys(constraints.children))..", found "..minetest.write_json(k)
2020-02-09 01:39:54 +01:00
else
local possible_errors=check_constraints(v, child)
if possible_errors then
return possible_errors
end
end
end
for k, _ in pairs(constraints.children) do
if value[k] == nil then
2020-04-05 10:42:44 +02:00
return "Table entry missing: Expected key "..minetest.write_json(k).." to be present in table "..minetest.write_json(value)
2020-02-09 01:39:54 +01:00
end
end
end
if constraints.required_children then
for k,v_constraints in pairs(constraints.required_children) do
local v=value[k]
if v then
local possible_errors=check_constraints(v, v_constraints)
if possible_errors then
return possible_errors
end
else
2020-04-05 10:42:44 +02:00
return "Table entry missing: Expected key "..minetest.write_json(k).." to be present in table "..minetest.write_json(value)
2020-02-09 01:39:54 +01:00
end
end
end
if constraints.possible_children then
for k,v_constraints in pairs(constraints.possible_children) do
local v=value[k]
if v then
local possible_errors=check_constraints(v, v_constraints)
if possible_errors then
return possible_errors
end
end
end
end
if constraints.keys then
for k,_ in pairs(value) do
local possible_errors=check_constraints(k, constraints.keys)
if possible_errors then
return possible_errors
end
end
end
if constraints.values then
for _,v in pairs(value) do
local possible_errors=check_constraints(v, constraints.values)
if possible_errors then
return possible_errors
end
end
end
end
end