refactor overlay to use script-manager code

develop
myk002 2022-12-05 17:09:08 -08:00 committed by Myk Taylor
parent 0362d76b39
commit 426a538e30
No known key found for this signature in database
1 changed files with 11 additions and 21 deletions

@ -5,6 +5,8 @@ local json = require('json')
local utils = require('utils')
local widgets = require('gui.widgets')
local scriptmanager = require('plugins.script-manager')
local OVERLAY_CONFIG_FILE = 'dfhack-config/overlay.json'
local OVERLAY_WIDGETS_VAR = 'OVERLAY_WIDGETS'
@ -250,11 +252,9 @@ local function load_widget(name, widget_class)
end
end
local function load_widgets(env_prefix, provider, env_fn)
local env_name = env_prefix .. provider
local ok, provider_env = pcall(env_fn, env_name)
if not ok or not provider_env[OVERLAY_WIDGETS_VAR] then return end
local overlay_widgets = provider_env[OVERLAY_WIDGETS_VAR]
local function load_widgets(env_name, env)
local overlay_widgets = env[OVERLAY_WIDGETS_VAR]
if not overlay_widgets then return end
if type(overlay_widgets) ~= 'table' then
dfhack.printerr(
('error loading overlay widgets from "%s": %s map is malformed')
@ -262,7 +262,7 @@ local function load_widgets(env_prefix, provider, env_fn)
return
end
for widget_name,widget_class in pairs(overlay_widgets) do
local name = provider .. '.' .. widget_name
local name = env_name .. '.' .. widget_name
if not safecall(load_widget, name, widget_class) then
dfhack.printerr(('error loading overlay widget "%s"'):format(name))
end
@ -274,23 +274,13 @@ function reload()
reset()
for _,plugin in ipairs(dfhack.internal.listPlugins()) do
load_widgets('plugins.', plugin, require)
end
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'
load_widgets('', script_name, reqscript)
end
local env_name = 'plugins.' .. plugin
local ok, plugin_env = pcall(require, env_name)
if ok then
load_widgets(plugin, plugin_env)
end
::skip_path::
end
scriptmanager.foreach_module_script(load_widgets)
for name in pairs(widget_db) do
table.insert(widget_index, name)