Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 22 additions & 39 deletions gui/mod-manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,15 @@ local widgets = require('gui.widgets')
local presets_file = json.open("dfhack-config/mod-manager.json")
local GLOBAL_KEY = 'mod-manager'

-- hardly an elegant solution, but mysteriously,
-- using from_fields.src_dir[i].startswith('data/vanilla') in move_mod_entry()
-- leads to lua complaining that it 'cannot read field string.startswith: not found'
local vanilla_modules = {
['vanilla_text'] = true,
['vanilla_languages'] = true,
['vanilla_descriptors'] = true,
['vanilla_materials'] = true,
['vanilla_environment'] = true,
['vanilla_plants'] = true,
['vanilla_items'] = true,
['vanilla_buildings'] = true,
['vanilla_bodies'] = true,
['vanilla_creatures'] = true,
['vanilla_entities'] = true,
['vanilla_reactions'] = true,
['vanilla_interactions'] = true,
['vanilla_descriptors_graphics'] = true,
['vanilla_plants_graphics'] = true,
['vanilla_items_graphics'] = true,
['vanilla_buildings_graphics'] = true,
['vanilla_creatures_graphics'] = true,
['vanilla_interactions_graphics'] = true,
['vanilla_world_map'] = true,
['vanilla_interface'] = true,
['vanilla_music'] = true,
}
-- Shamelessly taken from hack/library/lua/script-manager.lua
function vanilla(dir)
dir = dir.value
dir = dir -- better safe than sorry i guess
return dir:startswith('data/vanilla')
end

-- get_moddable_viewscreen(), get_any_moddable_viewscreen() and get_modlist_fields are declared
-- as global functions so external tools can call them to get the DF mod list
function get_moddable_viewscreen(type)
local vs = nil
if type == 'region' then
Expand All @@ -50,10 +31,12 @@ function get_moddable_viewscreen(type)
return vs
end

-- get_newregion_viewscreen and get_modlist_fields are declared as global functions
-- so external tools can call them to get the DF mod list
function get_newregion_viewscreen()
return get_moddable_viewscreen('region')
function get_any_moddable_viewscreen()
local vs = dfhack.gui.getViewscreenByType(df.viewscreen_new_regionst, 0)
if not vs then
vs = dfhack.gui.getViewscreenByType(df.viewscreen_new_arenast, 0)
end
return vs
end

function get_modlist_fields(kind, viewscreen)
Expand Down Expand Up @@ -99,9 +82,9 @@ local function move_mod_entry(viewscreen, to, from, mod_id, mod_version)
local mod_index = nil
for i, v in ipairs(from_fields.id) do
local version = from_fields.numeric_version[i]
local vanilla = vanilla_modules[mod_id]
local src_dir = from_fields.src_dir[i]
-- assumes that vanilla mods will not have multiple possible indices.
if v.value == mod_id and (vanilla or version == mod_version) then
if v.value == mod_id and (vanilla(src_dir) or version == mod_version) then
mod_index = i
break
end
Expand Down Expand Up @@ -176,7 +159,7 @@ ModmanageMenu.ATTRS {
}

local function save_new_preset(preset_name)
local viewscreen = get_newregion_viewscreen()
local viewscreen = get_any_moddable_viewscreen()
local modlist = get_active_modlist(viewscreen)
table.insert(presets_file.data, { name = preset_name, modlist = modlist })
presets_file:write()
Expand All @@ -196,7 +179,7 @@ local function overwrite_preset(idx)
return
end

local viewscreen = get_newregion_viewscreen()
local viewscreen = get_any_moddable_viewscreen()
local modlist = get_active_modlist(viewscreen)
presets_file.data[idx].modlist = modlist
presets_file:write()
Expand All @@ -207,7 +190,7 @@ local function load_preset(idx, unset_default_on_failure)
return
end

local viewscreen = get_newregion_viewscreen()
local viewscreen = get_any_moddable_viewscreen()
local modlist = presets_file.data[idx].modlist
local failures = swap_modlist(viewscreen, modlist)

Expand Down Expand Up @@ -244,7 +227,7 @@ local function load_preset(idx, unset_default_on_failure)
table.insert(text, NEWLINE)
end
dialogs.showMessage("Warning", text)
end
end
end

local function find_preset_by_name(name)
Expand Down Expand Up @@ -612,7 +595,7 @@ ModmanageOverlay.ATTRS {
desc = "Adds a link to the mod selection screen for accessing the mod manager.",
default_pos = { x=5, y=-6 },
version = 2,
viewscreens = { "new_region/Mods" },
viewscreens = { "new_region/Mods", "new_arena/Mods" },
default_enabled=true,
}

Expand Down Expand Up @@ -675,7 +658,7 @@ notification_timer_fn()
local default_applied = false
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
if sc == SC_VIEWSCREEN_CHANGED then
local vs = get_newregion_viewscreen()
local vs = get_any_moddable_viewscreen()
if vs and not default_applied then
default_applied = true
for i, v in ipairs(presets_file.data) do
Expand Down