-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.lua
More file actions
82 lines (67 loc) · 2.5 KB
/
Copy pathutil.lua
File metadata and controls
82 lines (67 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
-- ui/util.lua
local M = {}
-- Validates that props only contains keys from `allowed_keys`.
-- Logs all props and raises a clear error if a key is unexpected.
function M.validate_props(widget_name, props, schema)
assert(type(widget_name) == "string", "validate_props: widget_name must be string")
assert(type(props) == "table", "validate_props: props must be table")
assert(type(schema) == "table", "validate_props: schema must be table")
for key, rule in pairs(schema) do
local val = props[key]
local required = rule.required or false
local types = rule.type
-- Normalize to array if not already
if type(types) == "string" then types = { types } end
-- Required check
if required and val == nil then
error(("[validate_props:%s] missing required prop: %s"):format(widget_name, key))
end
-- Type check (if value is present)
if val ~= nil then
local actual = type(val)
local match = false
for _, t in ipairs(types) do
if actual == t then match = true break end
end
if not match then
error(("[validate_props:%s] invalid type for prop '%s': expected %s, got %s"):format(
widget_name, key, table.concat(types, "|"), actual
))
end
end
end
-- Detect extra/invalid props
for k, _ in pairs(props) do
if schema[k] == nil then
error(("[validate_props:%s] unexpected prop: %s"):format(widget_name, k))
end
end
end
-- Loads a file relative to the current mod's path, with log output.
-- Use like: ui.checkbox = ui.util.doModfile("widgets/checkbox.lua")
function M.doModfile(file)
assert(type(file) == "string", "doModfile(file) requires a filename string")
file = file:gsub("^/", "") -- Normalize leading slash
local modname = minetest.get_current_modname()
local path = minetest.get_modpath(modname) .. "/" .. file
minetest.log("action", ("[ui.util] Loading modfile: %s (mod=%s)"):format(file, modname))
local ok, result = pcall(dofile, path)
if not ok then
error("[ui.util] doModfile ERROR loading " .. file .. ": " .. tostring(result))
end
minetest.log("action", ("[ui.util] Loaded successfully: %s"):format(file))
return result
end
function M.get_label(options, value)
for _, opt in ipairs(options or {}) do
if opt.value == value then
return opt.label or opt.value
end
end
return value
end
function M.formname(name)
assert(type(name) == "string", "ui.formname: name must be a string")
return name:sub(1, 3) == "ui:" and name or "ui:" .. name
end
return M