Skip to content

Commit d6e1254

Browse files
committed
Add notifications for updated mods
1 parent f194ffd commit d6e1254

1 file changed

Lines changed: 75 additions & 24 deletions

File tree

gui/mod-manager.lua

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ local presets_file = json.open("dfhack-config/mod-manager.json")
1313
local GLOBAL_KEY = 'mod-manager'
1414

1515
-- Shamelessly taken from hack/library/lua/script-manager.lua
16-
function vanilla(dir)
16+
local function vanilla(dir)
1717
dir = dir.value
1818
dir = dir -- better safe than sorry i guess
1919
return dir:startswith('data/vanilla')
@@ -75,23 +75,29 @@ function get_modlist_fields(kind, viewscreen)
7575
end
7676
end
7777

78+
--- @return { success: boolean, version: string }
7879
local function move_mod_entry(viewscreen, to, from, mod_id, mod_version)
7980
local to_fields = get_modlist_fields(to, viewscreen)
8081
local from_fields = get_modlist_fields(from, viewscreen)
8182

8283
local mod_index = nil
84+
local loaded_version = nil
8385
for i, v in ipairs(from_fields.id) do
8486
local version = from_fields.numeric_version[i]
8587
local src_dir = from_fields.src_dir[i]
88+
local displayed_version = from_fields.displayed_version[i].value
8689
-- assumes that vanilla mods will not have multiple possible indices.
8790
if v.value == mod_id and (vanilla(src_dir) or version == mod_version) then
91+
if version ~= mod_version then
92+
loaded_version = displayed_version
93+
end
8894
mod_index = i
8995
break
9096
end
9197
end
9298

9399
if mod_index == nil then
94-
return false
100+
return { success= false, version= nil }
95101
end
96102

97103
for k, v in pairs(to_fields) do
@@ -106,13 +112,15 @@ local function move_mod_entry(viewscreen, to, from, mod_id, mod_version)
106112
v:erase(mod_index)
107113
end
108114

109-
return true
115+
return { success= true, version= loaded_version }
110116
end
111117

118+
--- @return { success: boolean, version: string }
112119
local function enable_mod(viewscreen, mod_id, mod_version)
113120
return move_mod_entry(viewscreen, "object_load_order", "available", mod_id, mod_version)
114121
end
115122

123+
--- @return { success: boolean, version: string }
116124
local function disable_mod(viewscreen, mod_id, mod_version)
117125
return move_mod_entry(viewscreen, "available", "object_load_order", mod_id, mod_version)
118126
end
@@ -127,19 +135,25 @@ local function get_active_modlist(viewscreen)
127135
return t
128136
end
129137

138+
--- @return { failures: [string], changed: [{ id: string, new: string }] }
130139
local function swap_modlist(viewscreen, modlist)
131140
local current = get_active_modlist(viewscreen)
132141
for _, v in ipairs(current) do
133142
disable_mod(viewscreen, v.id, v.version)
134143
end
135144

136145
local failures = {}
146+
local changed = {}
137147
for _, v in ipairs(modlist) do
138-
if not enable_mod(viewscreen, v.id, v.version) then
148+
res = enable_mod(viewscreen, v.id, v.version)
149+
if not res.success then
139150
table.insert(failures, v.id)
140151
end
152+
if res.version then
153+
table.insert(changed, { id= v.id, new= res.version })
154+
end
141155
end
142-
return failures
156+
return { failures= failures, changed= changed }
143157
end
144158

145159
--------------------
@@ -192,40 +206,77 @@ local function load_preset(idx, unset_default_on_failure)
192206

193207
local viewscreen = get_any_moddable_viewscreen()
194208
local modlist = presets_file.data[idx].modlist
195-
local failures = swap_modlist(viewscreen, modlist)
196-
197-
if #failures > 0 then
198-
local text = {}
199-
if unset_default_on_failure then
200-
presets_file.data[idx].default = false
201-
presets_file:write()
202-
203-
table.insert(text, {
204-
text='Failed to load some mods from your default preset.',
205-
pen=COLOR_LIGHTRED,
206-
})
209+
local results = swap_modlist(viewscreen, modlist)
210+
local failures = results.failures
211+
local changes = results.changed
212+
local text = {}
213+
214+
local failed = #failures > 0
215+
local changed = #changes > 0
216+
local should_warn = failed or changed
217+
218+
if should_warn then
219+
if failed then
220+
if unset_default_on_failure then
221+
presets_file.data[idx].default = false
222+
presets_file:write()
223+
224+
table.insert(text, {
225+
text='Failed to load some mods from your default preset.',
226+
pen=COLOR_LIGHTRED,
227+
})
228+
table.insert(text, NEWLINE)
229+
table.insert(text, {
230+
text='Preset is being unmarked as the default for safety.',
231+
pen=COLOR_LIGHTRED,
232+
})
233+
else
234+
table.insert(text, {
235+
text='Failed to load some mods from the preset.',
236+
pen=COLOR_LIGHTRED,
237+
})
238+
end
239+
end
240+
if failed and changed then
207241
table.insert(text, NEWLINE)
242+
end
243+
if changed then
208244
table.insert(text, {
209-
text='Preset is being unmarked as the default for safety.',
210-
pen=COLOR_LIGHTRED,
211-
})
212-
else
213-
table.insert(text, {
214-
text='Failed to load some mods from the preset.',
245+
text='Some vanilla mods have been updated.',
215246
pen=COLOR_LIGHTRED,
216247
})
217248
end
218249
table.insert(text, NEWLINE)
219-
table.insert(text, NEWLINE)
220250
table.insert(text, 'Please re-create your preset with mods you currently have installed.')
221251
table.insert(text, NEWLINE)
252+
table.insert(text, NEWLINE)
253+
end
254+
255+
if failed then
222256
table.insert(text, 'Here are the mods that failed to load:')
223257
table.insert(text, NEWLINE)
224258
table.insert(text, NEWLINE)
225259
for _, v in ipairs(failures) do
226260
table.insert(text, ('- %s'):format(v))
227261
table.insert(text, NEWLINE)
228262
end
263+
end
264+
265+
if failed and changed then
266+
table.insert(text, NEWLINE) -- just to separate the sections
267+
end
268+
269+
if changed then
270+
table.insert(text, 'Here are the vanilla mods that have been updated:')
271+
table.insert(text, NEWLINE)
272+
table.insert(text, NEWLINE)
273+
for _, v in ipairs(changes) do
274+
table.insert(text, ('- %s to %s'):format(v.id, v.new))
275+
table.insert(text, NEWLINE)
276+
end
277+
end
278+
279+
if should_warn then
229280
dialogs.showMessage("Warning", text)
230281
end
231282
end

0 commit comments

Comments
 (0)