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')
|
|
|
|
|
2023-04-03 16:59:42 -06:00
|
|
|
---------------------
|
|
|
|
-- enabled API
|
|
|
|
|
2022-12-05 18:08:08 -07:00
|
|
|
-- for each script that can be loaded as a module, calls cb(script_name, env)
|
2023-04-07 01:48:04 -06:00
|
|
|
function foreach_module_script(cb, preprocess_script_file_fn)
|
2022-12-05 18:08:08 -07:00
|
|
|
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
|
2023-04-07 01:48:04 -06:00
|
|
|
if f.isdir or not f.path:endswith('.lua') or
|
|
|
|
f.path:startswith('.git') or
|
|
|
|
f.path:startswith('test/') or
|
|
|
|
f.path:startswith('internal/') then
|
|
|
|
goto continue
|
2022-12-05 18:08:08 -07:00
|
|
|
end
|
2023-04-07 01:48:04 -06:00
|
|
|
if preprocess_script_file_fn then
|
|
|
|
preprocess_script_file_fn(script_path, f.path)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
::continue::
|
2022-12-05 18:08:08 -07:00
|
|
|
end
|
|
|
|
::skip_path::
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-29 01:55:49 -07:00
|
|
|
local enabled_map = nil
|
2022-12-05 18:08:08 -07:00
|
|
|
|
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
|
|
|
|
|
2023-04-07 01:48:04 -06:00
|
|
|
function reload(refresh_active_mod_scripts)
|
2022-12-05 18:08:08 -07:00
|
|
|
enabled_map = utils.OrderedTable()
|
2023-04-07 01:48:04 -06:00
|
|
|
local force_refresh_fn = refresh_active_mod_scripts and function(script_path, script_name)
|
|
|
|
if script_path:find('scripts_modactive') then
|
|
|
|
internal_script = dfhack.internal.scripts[script_path..'/'..script_name]
|
|
|
|
if internal_script then
|
|
|
|
internal_script.env = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end or nil
|
|
|
|
foreach_module_script(process_script, force_refresh_fn)
|
2022-12-05 18:08:08 -07:00
|
|
|
end
|
|
|
|
|
2023-01-29 01:55:49 -07:00
|
|
|
local function ensure_loaded()
|
|
|
|
if not enabled_map then
|
|
|
|
reload()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-05 18:08:08 -07:00
|
|
|
function list()
|
2023-01-29 01:55:49 -07:00
|
|
|
ensure_loaded()
|
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
|
|
|
|
|
2023-04-03 16:59:42 -06:00
|
|
|
---------------------
|
2023-07-20 18:43:57 -06:00
|
|
|
-- mod paths
|
2023-04-03 16:59:42 -06:00
|
|
|
|
|
|
|
-- this perhaps could/should be queried from the Steam API
|
|
|
|
-- are there any installation configurations where this will be wrong, though?
|
|
|
|
local WORKSHOP_MODS_PATH = '../../workshop/content/975370/'
|
|
|
|
local MODS_PATH = 'mods/'
|
|
|
|
local INSTALLED_MODS_PATH = 'data/installed_mods/'
|
|
|
|
|
|
|
|
-- last instance of the same version of the same mod wins, so read them in this
|
|
|
|
-- order (in increasing order of liklihood that players may have made custom
|
|
|
|
-- changes to the files)
|
|
|
|
local MOD_PATH_ROOTS = {WORKSHOP_MODS_PATH, MODS_PATH, INSTALLED_MODS_PATH}
|
|
|
|
|
|
|
|
local function get_mod_id_and_version(path)
|
|
|
|
local idfile = path .. '/info.txt'
|
|
|
|
local ok, lines = pcall(io.lines, idfile)
|
|
|
|
if not ok then return end
|
|
|
|
local id, version
|
|
|
|
for line in lines do
|
|
|
|
if not id then
|
|
|
|
_,_,id = line:find('^%[ID:([^%]]+)%]')
|
|
|
|
end
|
|
|
|
if not version then
|
|
|
|
-- note this doesn't include the closing brace since some people put
|
2023-07-20 18:43:57 -06:00
|
|
|
-- non-number characters in here, and DF only reads the leading digits
|
|
|
|
-- as the numeric version
|
2023-04-03 16:59:42 -06:00
|
|
|
_,_,version = line:find('^%[NUMERIC_VERSION:(%d+)')
|
|
|
|
end
|
|
|
|
-- note that we do *not* want to break out of this loop early since
|
|
|
|
-- lines has to hit EOF to close the file
|
|
|
|
end
|
|
|
|
return id, version
|
|
|
|
end
|
|
|
|
|
2023-07-20 18:43:57 -06:00
|
|
|
local function add_mod_paths(mod_paths, id, base_path, subdir)
|
|
|
|
local sep = base_path:endswith('/') and '' or '/'
|
|
|
|
local path = ('%s%s%s'):format(base_path, sep, subdir)
|
2023-04-03 16:59:42 -06:00
|
|
|
if dfhack.filesystem.isdir(path) then
|
2023-07-20 18:43:57 -06:00
|
|
|
print('indexing mod path: ' .. path)
|
|
|
|
table.insert(mod_paths, {id=id, path=path})
|
2023-04-03 16:59:42 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-20 18:43:57 -06:00
|
|
|
function get_mod_paths(installed_subdir, active_subdir)
|
2023-04-03 16:59:42 -06:00
|
|
|
-- ordered map of mod id -> {handled=bool, versions=map of version -> path}
|
|
|
|
local mods = utils.OrderedTable()
|
2023-07-20 18:43:57 -06:00
|
|
|
local mod_paths = {}
|
2023-04-03 16:59:42 -06:00
|
|
|
|
|
|
|
-- if a world is loaded, process active mods first, and lock to active version
|
2023-07-20 18:43:57 -06:00
|
|
|
if active_subdir and dfhack.isWorldLoaded() then
|
2023-04-03 16:59:42 -06:00
|
|
|
for _,path in ipairs(df.global.world.object_loader.object_load_order_src_dir) do
|
2023-04-07 01:48:04 -06:00
|
|
|
path = tostring(path.value)
|
2023-07-20 18:43:57 -06:00
|
|
|
-- skip vanilla "mods"
|
2023-04-03 16:59:42 -06:00
|
|
|
if not path:startswith(INSTALLED_MODS_PATH) then goto continue end
|
|
|
|
local id = get_mod_id_and_version(path)
|
|
|
|
if not id then goto continue end
|
|
|
|
mods[id] = {handled=true}
|
2023-07-20 18:43:57 -06:00
|
|
|
add_mod_paths(mod_paths, id, path, active_subdir)
|
|
|
|
add_mod_paths(mod_paths, id, path, installed_subdir)
|
2023-04-03 16:59:42 -06:00
|
|
|
::continue::
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- assemble version -> path maps for all (non-handled) mod source dirs
|
|
|
|
for _,mod_path_root in ipairs(MOD_PATH_ROOTS) do
|
|
|
|
local files = dfhack.filesystem.listdir_recursive(mod_path_root, 0)
|
|
|
|
if not files then goto skip_path_root end
|
|
|
|
for _,f in ipairs(files) do
|
|
|
|
if not f.isdir then goto continue end
|
|
|
|
local id, version = get_mod_id_and_version(f.path)
|
|
|
|
if not id or not version then goto continue end
|
|
|
|
local mod = ensure_key(mods, id)
|
|
|
|
if mod.handled then goto continue end
|
|
|
|
ensure_key(mod, 'versions')[version] = f.path
|
|
|
|
::continue::
|
|
|
|
end
|
|
|
|
::skip_path_root::
|
|
|
|
end
|
|
|
|
|
2023-07-20 18:43:57 -06:00
|
|
|
-- add paths from most recent version of all not-yet-handled mods
|
|
|
|
for id,v in pairs(mods) do
|
2023-04-03 16:59:42 -06:00
|
|
|
if v.handled then goto continue end
|
|
|
|
local max_version, path
|
|
|
|
for version,mod_path in pairs(v.versions) do
|
|
|
|
if not max_version or max_version < version then
|
|
|
|
path = mod_path
|
|
|
|
max_version = version
|
|
|
|
end
|
|
|
|
end
|
2023-07-20 18:43:57 -06:00
|
|
|
add_mod_paths(mod_paths, id, path, installed_subdir)
|
2023-04-03 16:59:42 -06:00
|
|
|
::continue::
|
|
|
|
end
|
|
|
|
|
2023-07-20 18:43:57 -06:00
|
|
|
return mod_paths
|
|
|
|
end
|
|
|
|
|
|
|
|
function get_mod_script_paths()
|
|
|
|
local paths = {}
|
|
|
|
for _,v in ipairs(get_mod_paths('scripts_modinstalled', 'scripts_modactive')) do
|
|
|
|
table.insert(paths, v.path)
|
|
|
|
end
|
|
|
|
return paths
|
2023-04-03 16:59:42 -06:00
|
|
|
end
|
|
|
|
|
2022-12-05 18:08:08 -07:00
|
|
|
return _ENV
|