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 --------------------------------------------------------------------------------
19 local function modname_valid(name)
20 return not name:find("[^a-z0-9_]")
23 local function get_formspec(data)
25 local mod = data.list:get_list()[data.selected_mod]
28 "size[11,6.5,true]" ..
29 "label[0.5,-0.25;" .. fgettext("World:") .. "]" ..
30 "label[1.75,-0.25;" .. data.worldspec.name .. "]"
32 if data.hide_gamemods then
33 retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";true]"
35 retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";false]"
38 if data.hide_modpackcontents then
39 retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";true]"
41 retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";false]"
49 "label[0,0.45;" .. fgettext("Mod:") .. "]" ..
50 "label[0.75,0.45;" .. mod.name .. "]" ..
51 "label[0,1;" .. fgettext("Depends:") .. "]" ..
52 "textlist[0,1.5;5,4.25;world_config_depends;" ..
53 modmgr.get_dependencies(mod.path) .. ";0]" ..
54 "button[9.25,6.35;2,0.5;btn_config_world_save;" .. fgettext("Save") .. "]" ..
55 "button[7.4,6.35;2,0.5;btn_config_world_cancel;" .. fgettext("Cancel") .. "]"
57 if mod ~= nil and mod.name ~= "" and mod.typ ~= "game_mod" then
58 if mod.is_modpack then
59 local rawlist = data.list:get_raw_list()
61 local all_enabled = true
63 if rawlist[j].modpack == mod.name and
64 rawlist[j].enabled ~= true then
70 if all_enabled == false then
71 retval = retval .. "button[5.5,-0.125;2,0.5;btn_mp_enable;" .. fgettext("Enable MP") .. "]"
73 retval = retval .. "button[5.5,-0.125;2,0.5;btn_mp_disable;" .. fgettext("Disable MP") .. "]"
77 retval = retval .. "checkbox[5.5,-0.375;cb_mod_enable;" .. fgettext("enabled") .. ";true]"
79 retval = retval .. "checkbox[5.5,-0.375;cb_mod_enable;" .. fgettext("enabled") .. ";false]"
85 "button[8.5,-0.125;2.5,0.5;btn_all_mods;" .. fgettext("Enable all") .. "]" ..
86 "textlist[5.5,0.5;5.5,5.75;world_config_modlist;"
88 retval = retval .. modmgr.render_modlist(data.list)
89 retval = retval .. ";" .. data.selected_mod .."]"
94 local function enable_mod(this, toset)
95 local mod = this.data.list:get_list()[this.data.selected_mod]
97 if mod.typ == "game_mod" then
98 -- game mods can't be enabled or disabled
99 elseif not mod.is_modpack then
101 mod.enabled = not mod.enabled
106 local list = this.data.list:get_raw_list()
108 if list[i].modpack == mod.name then
110 toset = not list[i].enabled
112 list[i].enabled = toset
119 local function handle_buttons(this, fields)
121 if fields["world_config_modlist"] ~= nil then
122 local event = core.explode_textlist_event(fields["world_config_modlist"])
123 this.data.selected_mod = event.index
124 core.setting_set("world_config_selected_mod", event.index)
126 if event.type == "DCL" then
133 if fields["key_enter"] ~= nil then
138 if fields["cb_mod_enable"] ~= nil then
139 local toset = core.is_yes(fields["cb_mod_enable"])
140 enable_mod(this,toset)
144 if fields["btn_mp_enable"] ~= nil or
145 fields["btn_mp_disable"] then
146 local toset = (fields["btn_mp_enable"] ~= nil)
147 enable_mod(this,toset)
151 if fields["cb_hide_gamemods"] ~= nil or
152 fields["cb_hide_mpcontent"] ~= nil then
153 local current = this.data.list:get_filtercriteria()
155 if current == nil then
159 if fields["cb_hide_gamemods"] ~= nil then
160 if core.is_yes(fields["cb_hide_gamemods"]) then
161 current.hide_game = true
162 this.data.hide_gamemods = true
163 core.setting_set("world_config_hide_gamemods", "true")
165 current.hide_game = false
166 this.data.hide_gamemods = false
167 core.setting_set("world_config_hide_gamemods", "false")
171 if fields["cb_hide_mpcontent"] ~= nil then
172 if core.is_yes(fields["cb_hide_mpcontent"]) then
173 current.hide_modpackcontents = true
174 this.data.hide_modpackcontents = true
175 core.setting_set("world_config_hide_modpackcontents", "true")
177 current.hide_modpackcontents = false
178 this.data.hide_modpackcontents = false
179 core.setting_set("world_config_hide_modpackcontents", "false")
183 this.data.list:set_filtercriteria(current)
187 if fields["btn_config_world_save"] then
189 local filename = this.data.worldspec.path ..
190 DIR_DELIM .. "world.mt"
192 local worldfile = Settings(filename)
193 local mods = worldfile:to_table()
195 local rawlist = this.data.list:get_raw_list()
198 for i,mod in ipairs(rawlist) do
199 if not mod.is_modpack and
200 mod.typ ~= "game_mod" then
201 if modname_valid(mod.name) then
202 worldfile:set("load_mod_"..mod.name, tostring(mod.enabled))
205 gamedata.errormessage = fgettext_ne("Failed to enable mod \"$1\" as it contains disallowed characters. Only chararacters [a-z0-9_] are allowed.", mod.name)
208 mods["load_mod_"..mod.name] = nil
212 -- Remove mods that are not present anymore
213 for key,value in pairs(mods) do
214 if key:sub(1,9) == "load_mod_" then
215 worldfile:remove(key)
219 if not worldfile:write() then
220 core.log("error", "Failed to write world config file")
227 if fields["btn_config_world_cancel"] then
232 if fields["btn_all_mods"] then
233 local list = this.data.list:get_raw_list()
236 if list[i].typ ~= "game_mod" and
237 not list[i].is_modpack then
238 list[i].enabled = true
247 function create_configure_world_dlg(worldidx)
249 local dlg = dialog_create("sp_config_world",
254 dlg.data.hide_gamemods = core.setting_getbool("world_config_hide_gamemods")
255 dlg.data.hide_modpackcontents = core.setting_getbool("world_config_hide_modpackcontents")
256 dlg.data.selected_mod = tonumber(core.setting_get("world_config_selected_mod"))
257 if dlg.data.selected_mod == nil then
258 dlg.data.selected_mod = 0
261 dlg.data.worldspec = core.get_worlds()[worldidx]
262 if dlg.data.worldspec == nil then dlg:delete() return nil end
264 dlg.data.worldconfig = modmgr.get_worldconfig(dlg.data.worldspec.path)
266 if dlg.data.worldconfig == nil or dlg.data.worldconfig.id == nil or
267 dlg.data.worldconfig.id == "" then
273 dlg.data.list = filterlist.create(
274 modmgr.preparemodlist, --refresh
275 modmgr.comparemod, --compare
276 function(element,uid) --uid match
277 if element.name == uid then
281 function(element,criteria)
282 if criteria.hide_game and
283 element.typ == "game_mod" then
287 if criteria.hide_modpackcontents and
288 element.modpack ~= nil then
293 { worldpath= dlg.data.worldspec.path,
294 gameid = dlg.data.worldspec.gameid }
298 if dlg.data.selected_mod > dlg.data.list:size() then
299 dlg.data.selected_mod = 0
302 dlg.data.list:set_filtercriteria(
304 hide_game=dlg.data.hide_gamemods,
305 hide_modpackcontents= dlg.data.hide_modpackcontents
307 dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
308 dlg.data.list:set_sortmode("alphabetic")