2 --Copyright (C) 2013 sapier
4 --This program is free software; you can redistribute it and/or modify
5 --it under the terms of the GNU Lesser General Public License as published by
6 --the Free Software Foundation; either version 2.1 of the License, or
7 --(at your option) any later version.
9 --This program is distributed in the hope that it will be useful,
10 --but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 --GNU Lesser General Public License for more details.
14 --You should have received a copy of the GNU Lesser General Public License along
15 --with this program; if not, write to the Free Software Foundation, Inc.,
16 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 --------------------------------------------------------------------------------
20 local function get_formspec(data)
22 local mod = data.list:get_list()[data.selected_mod]
25 "size[11,6.5,true]" ..
26 "label[0.5,-0.25;" .. fgettext("World:") .. "]" ..
27 "label[1.75,-0.25;" .. data.worldspec.name .. "]"
29 if data.hide_gamemods then
30 retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";true]"
32 retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";false]"
35 if data.hide_modpackcontents then
36 retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";true]"
38 retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";false]"
46 "label[0,0.45;" .. fgettext("Mod:") .. "]" ..
47 "label[0.75,0.45;" .. mod.name .. "]" ..
48 "label[0,1;" .. fgettext("Depends:") .. "]" ..
49 "textlist[0,1.5;5,4.25;world_config_depends;" ..
50 modmgr.get_dependencies(mod.path) .. ";0]" ..
51 "button[9.25,6.35;2,0.5;btn_config_world_save;" .. fgettext("Save") .. "]" ..
52 "button[7.4,6.35;2,0.5;btn_config_world_cancel;" .. fgettext("Cancel") .. "]"
54 if mod ~= nil and mod.name ~= "" and mod.typ ~= "game_mod" then
55 if mod.is_modpack then
56 local rawlist = data.list:get_raw_list()
58 local all_enabled = true
60 if rawlist[j].modpack == mod.name and
61 rawlist[j].enabled ~= true then
67 if all_enabled == false then
68 retval = retval .. "button[5.5,-0.125;2,0.5;btn_mp_enable;" .. fgettext("Enable MP") .. "]"
70 retval = retval .. "button[5.5,-0.125;2,0.5;btn_mp_disable;" .. fgettext("Disable MP") .. "]"
74 retval = retval .. "checkbox[5.5,-0.375;cb_mod_enable;" .. fgettext("enabled") .. ";true]"
76 retval = retval .. "checkbox[5.5,-0.375;cb_mod_enable;" .. fgettext("enabled") .. ";false]"
82 "button[8.5,-0.125;2.5,0.5;btn_all_mods;" .. fgettext("Enable all") .. "]" ..
83 "textlist[5.5,0.5;5.5,5.75;world_config_modlist;"
85 retval = retval .. modmgr.render_modlist(data.list)
86 retval = retval .. ";" .. data.selected_mod .."]"
91 local function enable_mod(this, toset)
92 local mod = this.data.list:get_list()[this.data.selected_mod]
94 if mod.typ == "game_mod" then
95 -- game mods can't be enabled or disabled
96 elseif not mod.is_modpack then
98 mod.enabled = not mod.enabled
103 local list = this.data.list:get_raw_list()
105 if list[i].modpack == mod.name then
107 toset = not list[i].enabled
109 list[i].enabled = toset
116 local function handle_buttons(this, fields)
118 if fields["world_config_modlist"] ~= nil then
119 local event = core.explode_textlist_event(fields["world_config_modlist"])
120 this.data.selected_mod = event.index
122 if event.type == "DCL" then
129 if fields["key_enter"] ~= nil then
134 if fields["cb_mod_enable"] ~= nil then
135 local toset = core.is_yes(fields["cb_mod_enable"])
136 enable_mod(this,toset)
140 if fields["btn_mp_enable"] ~= nil or
141 fields["btn_mp_disable"] then
142 local toset = (fields["btn_mp_enable"] ~= nil)
143 enable_mod(this,toset)
147 if fields["cb_hide_gamemods"] ~= nil or
148 fields["cb_hide_mpcontent"] ~= nil then
149 local current = this.data.list:get_filtercriteria()
151 if current == nil then
155 if core.is_yes(fields["cb_hide_gamemods"]) then
156 current.hide_game = true
157 this.data.hide_gamemods = true
159 current.hide_game = false
160 this.data.hide_gamemods = false
163 if core.is_yes(fields["cb_hide_mpcontent"]) then
164 current.hide_modpackcontents = true
165 this.data.hide_modpackcontents = true
167 current.hide_modpackcontents = false
168 this.data.hide_modpackcontents = false
171 this.data.list:set_filtercriteria(current)
175 if fields["btn_config_world_save"] then
177 local filename = this.data.worldspec.path ..
178 DIR_DELIM .. "world.mt"
180 local worldfile = Settings(filename)
181 local mods = worldfile:to_table()
183 local rawlist = this.data.list:get_raw_list()
186 for i,mod in ipairs(rawlist) do
187 if not mod.is_modpack and
188 mod.typ ~= "game_mod" then
190 worldfile:set("load_mod_"..mod.name, "true")
192 worldfile:set("load_mod_"..mod.name, "false")
194 mods["load_mod_"..mod.name] = nil
198 -- Remove mods that are not present anymore
199 for key,value in pairs(mods) do
200 if key:sub(1,9) == "load_mod_" then
201 worldfile:remove(key)
205 if not worldfile:write() then
206 core.log("error", "Failed to write world config file")
213 if fields["btn_config_world_cancel"] then
218 if fields["btn_all_mods"] then
219 local list = this.data.list:get_raw_list()
222 if list[i].typ ~= "game_mod" and
223 not list[i].is_modpack then
224 list[i].enabled = true
233 function create_configure_world_dlg(worldidx)
235 local dlg = dialog_create("sp_config_world",
240 --TODO read from settings
241 dlg.data.hide_gamemods = false
242 dlg.data.hide_modpackcontents = false
243 dlg.data.selected_mod = 0
245 dlg.data.worldspec = core.get_worlds()[worldidx]
246 if dlg.data.worldspec == nil then dlg:delete() return nil end
248 dlg.data.worldconfig = modmgr.get_worldconfig(dlg.data.worldspec.path)
250 if dlg.data.worldconfig == nil or dlg.data.worldconfig.id == nil or
251 dlg.data.worldconfig.id == "" then
257 dlg.data.list = filterlist.create(
258 modmgr.preparemodlist, --refresh
259 modmgr.comparemod, --compare
260 function(element,uid) --uid match
261 if element.name == uid then
265 function(element,criteria)
266 if criteria.hide_game and
267 element.typ == "game_mod" then
271 if criteria.hide_modpackcontents and
272 element.modpack ~= nil then
277 { worldpath= dlg.data.worldspec.path,
278 gameid = dlg.data.worldspec.gameid }
281 dlg.data.list:set_filtercriteria(
283 hide_game=dlg.data.hide_gamemods,
284 hide_modpackcontents= dlg.data.hide_modpackcontents
286 dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
287 dlg.data.list:set_sortmode("alphabetic")