Skip to content

Commit 7f7678e

Browse files
HybridDogSmallJoker
authored andcommittedMay 29, 2018
Tidy up dlg_config_world.lua (#5351)
Move code to pkgmgr
1 parent 75aa41c commit 7f7678e

File tree

2 files changed

+126
-127
lines changed

2 files changed

+126
-127
lines changed
 

‎builtin/mainmenu/dlg_config_world.lua

+84-127
Original file line numberDiff line numberDiff line change
@@ -24,159 +24,118 @@ local function modname_valid(name)
2424
end
2525

2626
local function get_formspec(data)
27-
local mod = data.list:get_list()[data.selected_mod]
27+
local mod = data.list:get_list()[data.selected_mod] or {name = ""}
2828

2929
local retval =
3030
"size[11.5,7.5,true]" ..
3131
"label[0.5,0;" .. fgettext("World:") .. "]" ..
3232
"label[1.75,0;" .. data.worldspec.name .. "]"
3333

34-
if mod == nil then
35-
mod = {name=""}
36-
end
37-
3834
local hard_deps, soft_deps = pkgmgr.get_dependencies(mod.path)
3935

4036
retval = retval ..
4137
"label[0,0.7;" .. fgettext("Mod:") .. "]" ..
4238
"label[0.75,0.7;" .. mod.name .. "]" ..
4339
"label[0,1.25;" .. fgettext("Dependencies:") .. "]" ..
44-
"textlist[0,1.75;5,2.125;world_config_depends;" ..
45-
hard_deps .. ";0]" ..
40+
"textlist[0,1.75;5,2.125;world_config_depends;" .. hard_deps .. ";0]" ..
4641
"label[0,3.875;" .. fgettext("Optional dependencies:") .. "]" ..
4742
"textlist[0,4.375;5,1.8;world_config_optdepends;" ..
4843
soft_deps .. ";0]" ..
49-
"button[3.25,7;2.5,0.5;btn_config_world_save;" .. fgettext("Save") .. "]" ..
50-
"button[5.75,7;2.5,0.5;btn_config_world_cancel;" .. fgettext("Cancel") .. "]"
44+
"button[3.25,7;2.5,0.5;btn_config_world_save;" ..
45+
fgettext("Save") .. "]" ..
46+
"button[5.75,7;2.5,0.5;btn_config_world_cancel;" ..
47+
fgettext("Cancel") .. "]"
5148

52-
if mod and mod.name ~= "" and not mod.is_game_content then
49+
if mod.name ~= "" and not mod.is_game_content then
5350
if mod.is_modpack then
54-
local rawlist = data.list:get_raw_list()
55-
56-
local all_enabled = true
57-
for j = 1, #rawlist, 1 do
58-
if rawlist[j].modpack == mod.name and not rawlist[j].enabled then
59-
all_enabled = false
60-
break
61-
end
62-
end
6351

64-
if all_enabled then
65-
retval = retval .. "button[5.5,0.125;2.5,0.5;btn_mp_disable;" ..
52+
if pkgmgr.is_modpack_entirely_enabled(data, mod.name) then
53+
retval = retval ..
54+
"button[5.5,0.125;2.5,0.5;btn_mp_disable;" ..
6655
fgettext("Disable MP") .. "]"
6756
else
68-
retval = retval .. "button[5.5,0.125;2.5,0.5;btn_mp_enable;" ..
57+
retval = retval ..
58+
"button[5.5,0.125;2.5,0.5;btn_mp_enable;" ..
6959
fgettext("Enable MP") .. "]"
7060
end
7161
else
72-
if mod.enabled then
73-
retval = retval .. "checkbox[5.5,-0.125;cb_mod_enable;" ..
74-
fgettext("enabled") .. ";true]"
75-
else
76-
retval = retval .. "checkbox[5.5,-0.125;cb_mod_enable;" ..
77-
fgettext("enabled") .. ";false]"
78-
end
62+
retval = retval ..
63+
"checkbox[5.5,-0.125;cb_mod_enable;" .. fgettext("enabled") ..
64+
";" .. tostring(mod.enabled) .. "]"
7965
end
8066
end
8167
if enabled_all then
8268
retval = retval ..
83-
"button[8.75,0.125;2.5,0.5;btn_disable_all_mods;" .. fgettext("Disable all") .. "]"
69+
"button[8.75,0.125;2.5,0.5;btn_disable_all_mods;" ..
70+
fgettext("Disable all") .. "]"
8471
else
8572
retval = retval ..
86-
"button[8.75,0.125;2.5,0.5;btn_enable_all_mods;" .. fgettext("Enable all") .. "]"
73+
"button[8.75,0.125;2.5,0.5;btn_enable_all_mods;" ..
74+
fgettext("Enable all") .. "]"
8775
end
88-
retval = retval ..
76+
return retval ..
8977
"tablecolumns[color;tree;text]" ..
90-
"table[5.5,0.75;5.75,6;world_config_modlist;"
91-
retval = retval .. pkgmgr.render_packagelist(data.list)
92-
retval = retval .. ";" .. data.selected_mod .."]"
93-
94-
return retval
95-
end
96-
97-
local function enable_mod(this, toset)
98-
local mod = this.data.list:get_list()[this.data.selected_mod]
99-
100-
if mod.is_game_content then
101-
-- game mods can't be enabled or disabled
102-
elseif not mod.is_modpack then
103-
if toset == nil then
104-
mod.enabled = not mod.enabled
105-
else
106-
mod.enabled = toset
107-
end
108-
else
109-
local list = this.data.list:get_raw_list()
110-
for i=1,#list,1 do
111-
if list[i].modpack == mod.name then
112-
if toset == nil then
113-
toset = not list[i].enabled
114-
end
115-
list[i].enabled = toset
116-
end
117-
end
118-
end
78+
"table[5.5,0.75;5.75,6;world_config_modlist;" ..
79+
pkgmgr.render_packagelist(data.list) .. ";" .. data.selected_mod .."]"
11980
end
12081

121-
12282
local function handle_buttons(this, fields)
123-
if fields["world_config_modlist"] ~= nil then
124-
local event = core.explode_table_event(fields["world_config_modlist"])
83+
if fields.world_config_modlist then
84+
local event = core.explode_table_event(fields.world_config_modlist)
12585
this.data.selected_mod = event.row
12686
core.settings:set("world_config_selected_mod", event.row)
12787

12888
if event.type == "DCL" then
129-
enable_mod(this)
89+
pkgmgr.enable_mod(this)
13090
end
13191

13292
return true
13393
end
13494

135-
if fields["key_enter"] ~= nil then
136-
enable_mod(this)
95+
if fields.key_enter then
96+
pkgmgr.enable_mod(this)
13797
return true
13898
end
13999

140-
if fields["cb_mod_enable"] ~= nil then
141-
local toset = core.is_yes(fields["cb_mod_enable"])
142-
enable_mod(this,toset)
100+
if fields.cb_mod_enable ~= nil then
101+
pkgmgr.enable_mod(this, core.is_yes(fields.cb_mod_enable))
143102
return true
144103
end
145104

146-
if fields["btn_mp_enable"] ~= nil or
147-
fields["btn_mp_disable"] then
148-
local toset = (fields["btn_mp_enable"] ~= nil)
149-
enable_mod(this,toset)
105+
if fields.btn_mp_enable ~= nil or
106+
fields.btn_mp_disable then
107+
pkgmgr.enable_mod(this, fields.btn_mp_enable ~= nil)
150108
return true
151109
end
152110

153-
if fields["btn_config_world_save"] then
154-
local filename = this.data.worldspec.path ..
155-
DIR_DELIM .. "world.mt"
111+
if fields.btn_config_world_save then
112+
local filename = this.data.worldspec.path .. DIR_DELIM .. "world.mt"
156113

157114
local worldfile = Settings(filename)
158115
local mods = worldfile:to_table()
159116

160117
local rawlist = this.data.list:get_raw_list()
161118

162-
local i,mod
163-
for i,mod in ipairs(rawlist) do
119+
for i = 1, #rawlist do
120+
local mod = rawlist[i]
164121
if not mod.is_modpack and
165122
not mod.is_game_content then
166123
if modname_valid(mod.name) then
167-
worldfile:set("load_mod_"..mod.name, tostring(mod.enabled))
168-
else
169-
if mod.enabled then
170-
gamedata.errormessage = fgettext_ne("Failed to enable mod \"$1\" as it contains disallowed characters. Only chararacters [a-z0-9_] are allowed.", mod.name)
171-
end
124+
worldfile:set("load_mod_" .. mod.name,
125+
tostring(mod.enabled))
126+
elseif mod.enabled then
127+
gamedata.errormessage = fgettext_ne("Failed to enable mo" ..
128+
"d \"$1\" as it contains disallowed characters. " ..
129+
"Only chararacters [a-z0-9_] are allowed.",
130+
mod.name)
172131
end
173-
mods["load_mod_"..mod.name] = nil
132+
mods["load_mod_" .. mod.name] = nil
174133
end
175134
end
176135

177136
-- Remove mods that are not present anymore
178-
for key,value in pairs(mods) do
179-
if key:sub(1,9) == "load_mod_" then
137+
for key in pairs(mods) do
138+
if key:sub(1, 9) == "load_mod_" then
180139
worldfile:remove(key)
181140
end
182141
end
@@ -189,7 +148,7 @@ local function handle_buttons(this, fields)
189148
return true
190149
end
191150

192-
if fields["btn_config_world_cancel"] then
151+
if fields.btn_config_world_cancel then
193152
this:delete()
194153
return true
195154
end
@@ -224,62 +183,60 @@ local function handle_buttons(this, fields)
224183
end
225184

226185
function create_configure_world_dlg(worldidx)
227-
local dlg = dialog_create("sp_config_world",
228-
get_formspec,
229-
handle_buttons,
230-
nil)
186+
local dlg = dialog_create("sp_config_world", get_formspec, handle_buttons)
231187

232-
dlg.data.selected_mod = tonumber(core.settings:get("world_config_selected_mod"))
233-
if dlg.data.selected_mod == nil then
234-
dlg.data.selected_mod = 0
235-
end
188+
dlg.data.selected_mod = tonumber(
189+
core.settings:get("world_config_selected_mod")) or 0
236190

237191
dlg.data.worldspec = core.get_worlds()[worldidx]
238-
if dlg.data.worldspec == nil then dlg:delete() return nil end
192+
if not dlg.data.worldspec then
193+
dlg:delete()
194+
return
195+
end
239196

240197
dlg.data.worldconfig = pkgmgr.get_worldconfig(dlg.data.worldspec.path)
241198

242-
if dlg.data.worldconfig == nil or dlg.data.worldconfig.id == nil or
199+
if not dlg.data.worldconfig or not dlg.data.worldconfig.id or
243200
dlg.data.worldconfig.id == "" then
244-
245201
dlg:delete()
246-
return nil
202+
return
247203
end
248204

249205
dlg.data.list = filterlist.create(
250-
pkgmgr.preparemodlist, --refresh
251-
pkgmgr.comparemod, --compare
252-
function(element,uid) --uid match
253-
if element.name == uid then
254-
return true
255-
end
256-
end,
257-
function(element, criteria)
258-
if criteria.hide_game and
259-
element.is_game_content then
260-
return false
261-
end
262-
263-
if criteria.hide_modpackcontents and
264-
element.modpack ~= nil then
265-
return false
266-
end
267-
return true
268-
end, --filter
269-
{ worldpath= dlg.data.worldspec.path,
270-
gameid = dlg.data.worldspec.gameid }
271-
)
206+
pkgmgr.preparemodlist,
207+
pkgmgr.comparemod,
208+
function(element, uid)
209+
if element.name == uid then
210+
return true
211+
end
212+
end,
213+
function(element, criteria)
214+
if criteria.hide_game and
215+
element.is_game_content then
216+
return false
217+
end
218+
219+
if criteria.hide_modpackcontents and
220+
element.modpack ~= nil then
221+
return false
222+
end
223+
return true
224+
end,
225+
{
226+
worldpath = dlg.data.worldspec.path,
227+
gameid = dlg.data.worldspec.gameid
228+
}
229+
)
272230

273231

274232
if dlg.data.selected_mod > dlg.data.list:size() then
275233
dlg.data.selected_mod = 0
276234
end
277235

278-
dlg.data.list:set_filtercriteria(
279-
{
280-
hide_game=dlg.data.hide_gamemods,
281-
hide_modpackcontents= dlg.data.hide_modpackcontents
282-
})
236+
dlg.data.list:set_filtercriteria({
237+
hide_game = dlg.data.hide_gamemods,
238+
hide_modpackcontents = dlg.data.hide_modpackcontents
239+
})
283240
dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
284241
dlg.data.list:set_sortmode("alphabetic")
285242

‎builtin/mainmenu/pkgmgr.lua

+42
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,48 @@ function pkgmgr.get_dependencies(path)
309309
return table.concat(info.depends or {}, ","), table.concat(info.optional_depends or {}, ",")
310310
end
311311

312+
----------- tests whether all of the mods in the modpack are enabled -----------
313+
function pkgmgr.is_modpack_entirely_enabled(data, name)
314+
local rawlist = data.list:get_raw_list()
315+
for j = 1, #rawlist do
316+
if rawlist[j].modpack == name and not rawlist[j].enabled then
317+
return false
318+
end
319+
end
320+
return true
321+
end
322+
323+
---------- toggles or en/disables a mod or modpack -----------------------------
324+
function pkgmgr.enable_mod(this, toset)
325+
local mod = this.data.list:get_list()[this.data.selected_mod]
326+
327+
-- game mods can't be enabled or disabled
328+
if mod.is_game_content then
329+
return
330+
end
331+
332+
-- toggle or en/disable the mod
333+
if not mod.is_modpack then
334+
if toset == nil then
335+
mod.enabled = not mod.enabled
336+
else
337+
mod.enabled = toset
338+
end
339+
return
340+
end
341+
342+
-- toggle or en/disable every mod in the modpack, interleaved unsupported
Has a conversation. Original line has a conversation.
343+
local list = this.data.list:get_raw_list()
344+
for i = 1, #list do
345+
if list[i].modpack == mod.name then
346+
if toset == nil then
347+
toset = not list[i].enabled
348+
end
349+
list[i].enabled = toset
350+
end
351+
end
352+
end
353+
312354
--------------------------------------------------------------------------------
313355
function pkgmgr.get_worldconfig(worldpath)
314356
local filename = worldpath ..

0 commit comments

Comments
 (0)
Please sign in to comment.