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
121 core.setting_set("world_config_selected_mod", event.index)
123 if event.type == "DCL" then
130 if fields["key_enter"] ~= nil then
135 if fields["cb_mod_enable"] ~= nil then
136 local toset = core.is_yes(fields["cb_mod_enable"])
137 enable_mod(this,toset)
141 if fields["btn_mp_enable"] ~= nil or
142 fields["btn_mp_disable"] then
143 local toset = (fields["btn_mp_enable"] ~= nil)
144 enable_mod(this,toset)
148 if fields["cb_hide_gamemods"] ~= nil or
149 fields["cb_hide_mpcontent"] ~= nil then
150 local current = this.data.list:get_filtercriteria()
152 if current == nil then
156 if fields["cb_hide_gamemods"] ~= nil then
157 if core.is_yes(fields["cb_hide_gamemods"]) then
158 current.hide_game = true
159 this.data.hide_gamemods = true
160 core.setting_set("world_config_hide_gamemods", "true")
162 current.hide_game = false
163 this.data.hide_gamemods = false
164 core.setting_set("world_config_hide_gamemods", "false")
168 if fields["cb_hide_mpcontent"] ~= nil then
169 if core.is_yes(fields["cb_hide_mpcontent"]) then
170 current.hide_modpackcontents = true
171 this.data.hide_modpackcontents = true
172 core.setting_set("world_config_hide_modpackcontents", "true")
174 current.hide_modpackcontents = false
175 this.data.hide_modpackcontents = false
176 core.setting_set("world_config_hide_modpackcontents", "false")
180 this.data.list:set_filtercriteria(current)
184 if fields["btn_config_world_save"] then
186 local filename = this.data.worldspec.path ..
187 DIR_DELIM .. "world.mt"
189 local worldfile = Settings(filename)
190 local mods = worldfile:to_table()
192 local rawlist = this.data.list:get_raw_list()
195 for i,mod in ipairs(rawlist) do
196 if not mod.is_modpack and
197 mod.typ ~= "game_mod" then
199 worldfile:set("load_mod_"..mod.name, "true")
201 worldfile:set("load_mod_"..mod.name, "false")
203 mods["load_mod_"..mod.name] = nil
207 -- Remove mods that are not present anymore
208 for key,value in pairs(mods) do
209 if key:sub(1,9) == "load_mod_" then
210 worldfile:remove(key)
214 if not worldfile:write() then
215 core.log("error", "Failed to write world config file")
222 if fields["btn_config_world_cancel"] then
227 if fields["btn_all_mods"] then
228 local list = this.data.list:get_raw_list()
231 if list[i].typ ~= "game_mod" and
232 not list[i].is_modpack then
233 list[i].enabled = true
242 function create_configure_world_dlg(worldidx)
244 local dlg = dialog_create("sp_config_world",
249 dlg.data.hide_gamemods = core.setting_getbool("world_config_hide_gamemods")
250 dlg.data.hide_modpackcontents = core.setting_getbool("world_config_hide_modpackcontents")
251 dlg.data.selected_mod = tonumber(core.setting_get("world_config_selected_mod"))
252 if dlg.data.selected_mod == nil then
253 dlg.data.selected_mod = 0
256 dlg.data.worldspec = core.get_worlds()[worldidx]
257 if dlg.data.worldspec == nil then dlg:delete() return nil end
259 dlg.data.worldconfig = modmgr.get_worldconfig(dlg.data.worldspec.path)
261 if dlg.data.worldconfig == nil or dlg.data.worldconfig.id == nil or
262 dlg.data.worldconfig.id == "" then
268 dlg.data.list = filterlist.create(
269 modmgr.preparemodlist, --refresh
270 modmgr.comparemod, --compare
271 function(element,uid) --uid match
272 if element.name == uid then
276 function(element,criteria)
277 if criteria.hide_game and
278 element.typ == "game_mod" then
282 if criteria.hide_modpackcontents and
283 element.modpack ~= nil then
288 { worldpath= dlg.data.worldspec.path,
289 gameid = dlg.data.worldspec.gameid }
293 if dlg.data.selected_mod > dlg.data.list:size() then
294 dlg.data.selected_mod = 0
297 dlg.data.list:set_filtercriteria(
299 hide_game=dlg.data.hide_gamemods,
300 hide_modpackcontents= dlg.data.hide_modpackcontents
302 dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
303 dlg.data.list:set_sortmode("alphabetic")