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)
28 local mod = data.list:get_list()[data.selected_mod]
31 "size[11.5,7.5,true]" ..
32 "label[0.5,0;" .. fgettext("World:") .. "]" ..
33 "label[1.75,0;" .. data.worldspec.name .. "]"
35 if data.hide_gamemods then
36 retval = retval .. "checkbox[1,6;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";true]"
38 retval = retval .. "checkbox[1,6;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";false]"
41 if data.hide_modpackcontents then
42 retval = retval .. "checkbox[6,6;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";true]"
44 retval = retval .. "checkbox[6,6;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";false]"
52 "label[0,0.7;" .. fgettext("Mod:") .. "]" ..
53 "label[0.75,0.7;" .. mod.name .. "]" ..
54 "label[0,1.25;" .. fgettext("Depends:") .. "]" ..
55 "textlist[0,1.75;5,4.25;world_config_depends;" ..
56 modmgr.get_dependencies(mod.path) .. ";0]" ..
57 "button[3.25,7;2.5,0.5;btn_config_world_save;" .. fgettext("Save") .. "]" ..
58 "button[5.75,7;2.5,0.5;btn_config_world_cancel;" .. fgettext("Cancel") .. "]"
60 if mod ~= nil and mod.name ~= "" and mod.typ ~= "game_mod" then
61 if mod.is_modpack then
62 local rawlist = data.list:get_raw_list()
64 local all_enabled = true
66 if rawlist[j].modpack == mod.name and
67 rawlist[j].enabled ~= true then
73 if all_enabled == false then
74 retval = retval .. "button[5.5,0.125;2.5,0.5;btn_mp_enable;" .. fgettext("Enable MP") .. "]"
76 retval = retval .. "button[5.5,0.125;2.5,0.5;btn_mp_disable;" .. fgettext("Disable MP") .. "]"
80 retval = retval .. "checkbox[5.5,-0.125;cb_mod_enable;" .. fgettext("enabled") .. ";true]"
82 retval = retval .. "checkbox[5.5,-0.125;cb_mod_enable;" .. fgettext("enabled") .. ";false]"
88 "button[8.75,0.125;2.5,0.5;btn_disable_all_mods;" .. fgettext("Disable all") .. "]" ..
89 "textlist[5.5,0.75;5.75,5.25;world_config_modlist;"
92 "button[8.75,0.125;2.5,0.5;btn_enable_all_mods;" .. fgettext("Enable all") .. "]" ..
93 "textlist[5.5,0.75;5.75,5.25;world_config_modlist;"
95 retval = retval .. modmgr.render_modlist(data.list)
96 retval = retval .. ";" .. data.selected_mod .."]"
101 local function enable_mod(this, toset)
102 local mod = this.data.list:get_list()[this.data.selected_mod]
104 if mod.typ == "game_mod" then
105 -- game mods can't be enabled or disabled
106 elseif not mod.is_modpack then
108 mod.enabled = not mod.enabled
113 local list = this.data.list:get_raw_list()
115 if list[i].modpack == mod.name then
117 toset = not list[i].enabled
119 list[i].enabled = toset
126 local function handle_buttons(this, fields)
128 if fields["world_config_modlist"] ~= nil then
129 local event = core.explode_textlist_event(fields["world_config_modlist"])
130 this.data.selected_mod = event.index
131 core.setting_set("world_config_selected_mod", event.index)
133 if event.type == "DCL" then
140 if fields["key_enter"] ~= nil then
145 if fields["cb_mod_enable"] ~= nil then
146 local toset = core.is_yes(fields["cb_mod_enable"])
147 enable_mod(this,toset)
151 if fields["btn_mp_enable"] ~= nil or
152 fields["btn_mp_disable"] then
153 local toset = (fields["btn_mp_enable"] ~= nil)
154 enable_mod(this,toset)
158 if fields["cb_hide_gamemods"] ~= nil or
159 fields["cb_hide_mpcontent"] ~= nil then
160 local current = this.data.list:get_filtercriteria()
162 if current == nil then
166 if fields["cb_hide_gamemods"] ~= nil then
167 if core.is_yes(fields["cb_hide_gamemods"]) then
168 current.hide_game = true
169 this.data.hide_gamemods = true
170 core.setting_set("world_config_hide_gamemods", "true")
172 current.hide_game = false
173 this.data.hide_gamemods = false
174 core.setting_set("world_config_hide_gamemods", "false")
178 if fields["cb_hide_mpcontent"] ~= nil then
179 if core.is_yes(fields["cb_hide_mpcontent"]) then
180 current.hide_modpackcontents = true
181 this.data.hide_modpackcontents = true
182 core.setting_set("world_config_hide_modpackcontents", "true")
184 current.hide_modpackcontents = false
185 this.data.hide_modpackcontents = false
186 core.setting_set("world_config_hide_modpackcontents", "false")
190 this.data.list:set_filtercriteria(current)
194 if fields["btn_config_world_save"] then
196 local filename = this.data.worldspec.path ..
197 DIR_DELIM .. "world.mt"
199 local worldfile = Settings(filename)
200 local mods = worldfile:to_table()
202 local rawlist = this.data.list:get_raw_list()
205 for i,mod in ipairs(rawlist) do
206 if not mod.is_modpack and
207 mod.typ ~= "game_mod" then
208 if modname_valid(mod.name) then
209 worldfile:set("load_mod_"..mod.name, tostring(mod.enabled))
212 gamedata.errormessage = fgettext_ne("Failed to enable mod \"$1\" as it contains disallowed characters. Only chararacters [a-z0-9_] are allowed.", mod.name)
215 mods["load_mod_"..mod.name] = nil
219 -- Remove mods that are not present anymore
220 for key,value in pairs(mods) do
221 if key:sub(1,9) == "load_mod_" then
222 worldfile:remove(key)
226 if not worldfile:write() then
227 core.log("error", "Failed to write world config file")
234 if fields["btn_config_world_cancel"] then
239 if fields.btn_enable_all_mods then
240 local list = this.data.list:get_raw_list()
243 if list[i].typ ~= "game_mod" and not list[i].is_modpack then
244 list[i].enabled = true
251 if fields.btn_disable_all_mods then
252 local list = this.data.list:get_raw_list()
255 if list[i].typ ~= "game_mod" and not list[i].is_modpack then
256 list[i].enabled = false
266 function create_configure_world_dlg(worldidx)
268 local dlg = dialog_create("sp_config_world",
273 dlg.data.hide_gamemods = core.setting_getbool("world_config_hide_gamemods")
274 dlg.data.hide_modpackcontents = core.setting_getbool("world_config_hide_modpackcontents")
275 dlg.data.selected_mod = tonumber(core.setting_get("world_config_selected_mod"))
276 if dlg.data.selected_mod == nil then
277 dlg.data.selected_mod = 0
280 dlg.data.worldspec = core.get_worlds()[worldidx]
281 if dlg.data.worldspec == nil then dlg:delete() return nil end
283 dlg.data.worldconfig = modmgr.get_worldconfig(dlg.data.worldspec.path)
285 if dlg.data.worldconfig == nil or dlg.data.worldconfig.id == nil or
286 dlg.data.worldconfig.id == "" then
292 dlg.data.list = filterlist.create(
293 modmgr.preparemodlist, --refresh
294 modmgr.comparemod, --compare
295 function(element,uid) --uid match
296 if element.name == uid then
300 function(element,criteria)
301 if criteria.hide_game and
302 element.typ == "game_mod" then
306 if criteria.hide_modpackcontents and
307 element.modpack ~= nil then
312 { worldpath= dlg.data.worldspec.path,
313 gameid = dlg.data.worldspec.gameid }
317 if dlg.data.selected_mod > dlg.data.list:size() then
318 dlg.data.selected_mod = 0
321 dlg.data.list:set_filtercriteria(
323 hide_game=dlg.data.hide_gamemods,
324 hide_modpackcontents= dlg.data.hide_modpackcontents
326 dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
327 dlg.data.list:set_sortmode("alphabetic")