generalize mod directory scanning

develop
Myk Taylor 2023-07-20 17:43:57 -07:00
parent 2151bb6a78
commit 462ee0cba7
No known key found for this signature in database
1 changed files with 26 additions and 24 deletions

@ -74,7 +74,7 @@ function list()
end end
--------------------- ---------------------
-- mod script paths -- mod paths
-- this perhaps could/should be queried from the Steam API -- this perhaps could/should be queried from the Steam API
-- are there any installation configurations where this will be wrong, though? -- are there any installation configurations where this will be wrong, though?
@ -98,8 +98,8 @@ local function get_mod_id_and_version(path)
end end
if not version then if not version then
-- note this doesn't include the closing brace since some people put -- note this doesn't include the closing brace since some people put
-- non-number characters in here, and DF only reads the digits as the -- non-number characters in here, and DF only reads the leading digits
-- numeric version -- as the numeric version
_,_,version = line:find('^%[NUMERIC_VERSION:(%d+)') _,_,version = line:find('^%[NUMERIC_VERSION:(%d+)')
end end
-- note that we do *not* want to break out of this loop early since -- note that we do *not* want to break out of this loop early since
@ -108,37 +108,31 @@ local function get_mod_id_and_version(path)
return id, version return id, version
end end
local function add_script_path(mod_script_paths, path) 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)
if dfhack.filesystem.isdir(path) then if dfhack.filesystem.isdir(path) then
print('indexing mod scripts: ' .. path) print('indexing mod path: ' .. path)
table.insert(mod_script_paths, path) table.insert(mod_paths, {id=id, path=path})
end end
end end
local function add_script_paths(mod_script_paths, base_path, include_modactive) function get_mod_paths(installed_subdir, active_subdir)
if not base_path:endswith('/') then
base_path = base_path .. '/'
end
if include_modactive then
add_script_path(mod_script_paths, base_path..'scripts_modactive')
end
add_script_path(mod_script_paths, base_path..'scripts_modinstalled')
end
function get_mod_script_paths()
-- ordered map of mod id -> {handled=bool, versions=map of version -> path} -- ordered map of mod id -> {handled=bool, versions=map of version -> path}
local mods = utils.OrderedTable() local mods = utils.OrderedTable()
local mod_script_paths = {} local mod_paths = {}
-- if a world is loaded, process active mods first, and lock to active version -- if a world is loaded, process active mods first, and lock to active version
if dfhack.isWorldLoaded() then if active_subdir and dfhack.isWorldLoaded() then
for _,path in ipairs(df.global.world.object_loader.object_load_order_src_dir) do for _,path in ipairs(df.global.world.object_loader.object_load_order_src_dir) do
path = tostring(path.value) path = tostring(path.value)
-- skip vanilla "mods"
if not path:startswith(INSTALLED_MODS_PATH) then goto continue end if not path:startswith(INSTALLED_MODS_PATH) then goto continue end
local id = get_mod_id_and_version(path) local id = get_mod_id_and_version(path)
if not id then goto continue end if not id then goto continue end
mods[id] = {handled=true} mods[id] = {handled=true}
add_script_paths(mod_script_paths, path, true) add_mod_paths(mod_paths, id, path, active_subdir)
add_mod_paths(mod_paths, id, path, installed_subdir)
::continue:: ::continue::
end end
end end
@ -159,8 +153,8 @@ function get_mod_script_paths()
::skip_path_root:: ::skip_path_root::
end end
-- add script paths from most recent version of all not-yet-handled mods -- add paths from most recent version of all not-yet-handled mods
for _,v in pairs(mods) do for id,v in pairs(mods) do
if v.handled then goto continue end if v.handled then goto continue end
local max_version, path local max_version, path
for version,mod_path in pairs(v.versions) do for version,mod_path in pairs(v.versions) do
@ -169,11 +163,19 @@ function get_mod_script_paths()
max_version = version max_version = version
end end
end end
add_script_paths(mod_script_paths, path) add_mod_paths(mod_paths, id, path, installed_subdir)
::continue:: ::continue::
end end
return mod_script_paths 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
end end
return _ENV return _ENV