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 enabled_all = false
22 local function modname_valid(name)
23 return not name:find("[^a-z0-9_]")
26 local function get_formspec(data)
27 local mod = data.list:get_list()[data.selected_mod]
30 "size[11.5,7.5,true]" ..
31 "label[0.5,0;" .. fgettext("World:") .. "]" ..
32 "label[1.75,0;" .. data.worldspec.name .. "]"
38 local hard_deps, soft_deps = pkgmgr.get_dependencies(mod.path)
41 "label[0,0.7;" .. fgettext("Mod:") .. "]" ..
42 "label[0.75,0.7;" .. mod.name .. "]" ..
43 "label[0,1.25;" .. fgettext("Dependencies:") .. "]" ..
44 "textlist[0,1.75;5,2.125;world_config_depends;" ..
46 "label[0,3.875;" .. fgettext("Optional dependencies:") .. "]" ..
47 "textlist[0,4.375;5,1.8;world_config_optdepends;" ..
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") .. "]"
52 if mod and mod.name ~= "" and not mod.is_game_content then
53 if mod.is_modpack then
54 local rawlist = data.list:get_raw_list()
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
65 retval = retval .. "button[5.5,0.125;2.5,0.5;btn_mp_disable;" ..
66 fgettext("Disable MP") .. "]"
68 retval = retval .. "button[5.5,0.125;2.5,0.5;btn_mp_enable;" ..
69 fgettext("Enable MP") .. "]"
73 retval = retval .. "checkbox[5.5,-0.125;cb_mod_enable;" ..
74 fgettext("enabled") .. ";true]"
76 retval = retval .. "checkbox[5.5,-0.125;cb_mod_enable;" ..
77 fgettext("enabled") .. ";false]"
83 "button[8.75,0.125;2.5,0.5;btn_disable_all_mods;" .. fgettext("Disable all") .. "]"
86 "button[8.75,0.125;2.5,0.5;btn_enable_all_mods;" .. fgettext("Enable all") .. "]"
89 "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 .."]"
97 local function enable_mod(this, toset)
98 local mod = this.data.list:get_list()[this.data.selected_mod]
100 if mod.is_game_content then
101 -- game mods can't be enabled or disabled
102 elseif not mod.is_modpack then
104 mod.enabled = not mod.enabled
109 local list = this.data.list:get_raw_list()
111 if list[i].modpack == mod.name then
113 toset = not list[i].enabled
115 list[i].enabled = toset
122 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"])
125 this.data.selected_mod = event.row
126 core.settings:set("world_config_selected_mod", event.row)
128 if event.type == "DCL" then
135 if fields["key_enter"] ~= nil then
140 if fields["cb_mod_enable"] ~= nil then
141 local toset = core.is_yes(fields["cb_mod_enable"])
142 enable_mod(this,toset)
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)
153 if fields["btn_config_world_save"] then
154 local filename = this.data.worldspec.path ..
155 DIR_DELIM .. "world.mt"
157 local worldfile = Settings(filename)
158 local mods = worldfile:to_table()
160 local rawlist = this.data.list:get_raw_list()
163 for i,mod in ipairs(rawlist) do
164 if not mod.is_modpack and
165 not mod.is_game_content then
166 if modname_valid(mod.name) then
167 worldfile:set("load_mod_"..mod.name, tostring(mod.enabled))
170 gamedata.errormessage = fgettext_ne("Failed to enable mod \"$1\" as it contains disallowed characters. Only chararacters [a-z0-9_] are allowed.", mod.name)
173 mods["load_mod_"..mod.name] = nil
177 -- Remove mods that are not present anymore
178 for key,value in pairs(mods) do
179 if key:sub(1,9) == "load_mod_" then
180 worldfile:remove(key)
184 if not worldfile:write() then
185 core.log("error", "Failed to write world config file")
192 if fields["btn_config_world_cancel"] then
197 if fields.btn_enable_all_mods then
198 local list = this.data.list:get_raw_list()
201 if not list[i].is_game_content
202 and not list[i].is_modpack then
203 list[i].enabled = true
210 if fields.btn_disable_all_mods then
211 local list = this.data.list:get_raw_list()
214 if not list[i].is_game_content
215 and not list[i].is_modpack then
216 list[i].enabled = false
226 function create_configure_world_dlg(worldidx)
227 local dlg = dialog_create("sp_config_world",
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
237 dlg.data.worldspec = core.get_worlds()[worldidx]
238 if dlg.data.worldspec == nil then dlg:delete() return nil end
240 dlg.data.worldconfig = pkgmgr.get_worldconfig(dlg.data.worldspec.path)
242 if dlg.data.worldconfig == nil or dlg.data.worldconfig.id == nil or
243 dlg.data.worldconfig.id == "" then
249 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
257 function(element, criteria)
258 if criteria.hide_game and
259 element.is_game_content then
263 if criteria.hide_modpackcontents and
264 element.modpack ~= nil then
269 { worldpath= dlg.data.worldspec.path,
270 gameid = dlg.data.worldspec.gameid }
274 if dlg.data.selected_mod > dlg.data.list:size() then
275 dlg.data.selected_mod = 0
278 dlg.data.list:set_filtercriteria(
280 hide_game=dlg.data.hide_gamemods,
281 hide_modpackcontents= dlg.data.hide_modpackcontents
283 dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
284 dlg.data.list:set_sortmode("alphabetic")