2022-12-07 14:26:35 -07:00
|
|
|
local _ENV = mkmodule('script-manager')
|
2022-12-05 18:08:08 -07:00
|
|
|
|
|
|
|
local utils = require('utils')
|
|
|
|
|
|
|
|
-- for each script that can be loaded as a module, calls cb(script_name, env)
|
|
|
|
function foreach_module_script(cb)
|
|
|
|
for _,script_path in ipairs(dfhack.internal.getScriptPaths()) do
|
|
|
|
local files = dfhack.filesystem.listdir_recursive(
|
|
|
|
script_path, nil, false)
|
|
|
|
if not files then goto skip_path end
|
|
|
|
for _,f in ipairs(files) do
|
|
|
|
if not f.isdir and
|
|
|
|
f.path:endswith('.lua') and
|
|
|
|
not f.path:startswith('test/') and
|
|
|
|
not f.path:startswith('internal/') then
|
|
|
|
local script_name = f.path:sub(1, #f.path - 4) -- remove '.lua'
|
|
|
|
local ok, script_env = pcall(reqscript, script_name)
|
|
|
|
if ok then
|
|
|
|
cb(script_name, script_env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
::skip_path::
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local enabled_map = {}
|
|
|
|
|
2022-12-06 15:42:27 -07:00
|
|
|
local function process_script(env_name, env)
|
|
|
|
local global_name = 'isEnabled'
|
2022-12-05 18:08:08 -07:00
|
|
|
local fn = env[global_name]
|
|
|
|
if not fn then return end
|
|
|
|
if type(fn) ~= 'function' then
|
|
|
|
dfhack.printerr(
|
|
|
|
('error registering %s() from "%s": global' ..
|
|
|
|
' value is not a function'):format(global_name, env_name))
|
|
|
|
return
|
|
|
|
end
|
2022-12-06 15:42:27 -07:00
|
|
|
enabled_map[env_name] = fn
|
2022-12-05 18:08:08 -07:00
|
|
|
end
|
|
|
|
|
2022-12-06 17:45:17 -07:00
|
|
|
function reload()
|
2022-12-05 18:08:08 -07:00
|
|
|
enabled_map = utils.OrderedTable()
|
|
|
|
foreach_module_script(process_script)
|
|
|
|
end
|
|
|
|
|
|
|
|
function list()
|
2022-12-10 23:13:38 -07:00
|
|
|
-- call reload every time we list to make sure we get scripts that have
|
|
|
|
-- just been added
|
|
|
|
reload()
|
2022-12-05 18:08:08 -07:00
|
|
|
for name,fn in pairs(enabled_map) do
|
2023-01-24 00:50:08 -07:00
|
|
|
print(('%21s %-3s'):format(name..':', fn() and 'on' or 'off'))
|
2022-12-05 18:08:08 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return _ENV
|