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 init_data(data)
27 data.list = filterlist.create(
28 pkgmgr.preparemodlist,
30 function(element, uid)
31 if element.name == uid then
35 function(element, criteria)
36 if criteria.hide_game and
37 element.is_game_content then
41 if criteria.hide_modpackcontents and
42 element.modpack ~= nil then
48 worldpath = data.worldspec.path,
49 gameid = data.worldspec.gameid
52 if data.selected_mod > data.list:size() then
56 data.list:set_filtercriteria({
57 hide_game = data.hide_gamemods,
58 hide_modpackcontents = data.hide_modpackcontents
60 data.list:add_sort_mechanism("alphabetic", sort_mod_list)
61 data.list:set_sortmode("alphabetic")
64 local function get_formspec(data)
69 local mod = data.list:get_list()[data.selected_mod] or {name = ""}
72 "size[11.5,7.5,true]" ..
73 "label[0.5,0;" .. fgettext("World:") .. "]" ..
74 "label[1.75,0;" .. data.worldspec.name .. "]"
76 if mod.is_modpack or mod.type == "game" then
77 local info = core.formspec_escape(
78 core.get_content_info(mod.path).description)
80 if mod.is_modpack then
81 info = fgettext("No modpack description provided.")
83 info = fgettext("No game description provided.")
87 "textarea[0.25,0.7;5.75,7.2;;" .. info .. ";]"
89 local hard_deps, soft_deps = pkgmgr.get_dependencies(mod.path)
90 local hard_deps_str = table.concat(hard_deps, ",")
91 local soft_deps_str = table.concat(soft_deps, ",")
94 "label[0,0.7;" .. fgettext("Mod:") .. "]" ..
95 "label[0.75,0.7;" .. mod.name .. "]"
97 if hard_deps_str == "" then
98 if soft_deps_str == "" then
101 fgettext("No (optional) dependencies") .. "]"
104 "label[0,1.25;" .. fgettext("No hard dependencies") ..
106 "label[0,1.75;" .. fgettext("Optional dependencies:") ..
108 "textlist[0,2.25;5,4;world_config_optdepends;" ..
109 soft_deps_str .. ";0]"
112 if soft_deps_str == "" then
114 "label[0,1.25;" .. fgettext("Dependencies:") .. "]" ..
115 "textlist[0,1.75;5,4;world_config_depends;" ..
116 hard_deps_str .. ";0]" ..
117 "label[0,6;" .. fgettext("No optional dependencies") .. "]"
120 "label[0,1.25;" .. fgettext("Dependencies:") .. "]" ..
121 "textlist[0,1.75;5,2.125;world_config_depends;" ..
122 hard_deps_str .. ";0]" ..
123 "label[0,3.9;" .. fgettext("Optional dependencies:") ..
125 "textlist[0,4.375;5,1.8;world_config_optdepends;" ..
126 soft_deps_str .. ";0]"
132 "button[3.25,7;2.5,0.5;btn_config_world_save;" ..
133 fgettext("Save") .. "]" ..
134 "button[5.75,7;2.5,0.5;btn_config_world_cancel;" ..
135 fgettext("Cancel") .. "]" ..
136 "button[9,7;2.5,0.5;btn_config_world_cdb;" ..
137 fgettext("Find More Mods") .. "]"
139 if mod.name ~= "" and not mod.is_game_content then
140 if mod.is_modpack then
142 if pkgmgr.is_modpack_entirely_enabled(data, mod.name) then
144 "button[5.5,0.125;3,0.5;btn_mp_disable;" ..
145 fgettext("Disable modpack") .. "]"
148 "button[5.5,0.125;3,0.5;btn_mp_enable;" ..
149 fgettext("Enable modpack") .. "]"
153 "checkbox[5.5,-0.125;cb_mod_enable;" .. fgettext("enabled") ..
154 ";" .. tostring(mod.enabled) .. "]"
159 "button[8.95,0.125;2.5,0.5;btn_disable_all_mods;" ..
160 fgettext("Disable all") .. "]"
163 "button[8.95,0.125;2.5,0.5;btn_enable_all_mods;" ..
164 fgettext("Enable all") .. "]"
167 local use_technical_names = core.settings:get_bool("show_technical_names")
170 "tablecolumns[color;tree;text]" ..
171 "table[5.5,0.75;5.75,6;world_config_modlist;" ..
172 pkgmgr.render_packagelist(data.list, use_technical_names) .. ";" .. data.selected_mod .."]"
175 local function handle_buttons(this, fields)
176 if fields.world_config_modlist then
177 local event = core.explode_table_event(fields.world_config_modlist)
178 this.data.selected_mod = event.row
179 core.settings:set("world_config_selected_mod", event.row)
181 if event.type == "DCL" then
182 pkgmgr.enable_mod(this)
188 if fields.key_enter then
189 pkgmgr.enable_mod(this)
193 if fields.cb_mod_enable ~= nil then
194 pkgmgr.enable_mod(this, core.is_yes(fields.cb_mod_enable))
198 if fields.btn_mp_enable ~= nil or
199 fields.btn_mp_disable then
200 pkgmgr.enable_mod(this, fields.btn_mp_enable ~= nil)
204 if fields.btn_config_world_save then
205 local filename = this.data.worldspec.path .. DIR_DELIM .. "world.mt"
207 local worldfile = Settings(filename)
208 local mods = worldfile:to_table()
210 local rawlist = this.data.list:get_raw_list()
213 for i = 1, #rawlist do
214 local mod = rawlist[i]
215 if not mod.is_modpack and
216 not mod.is_game_content then
217 if modname_valid(mod.name) then
219 worldfile:set("load_mod_" .. mod.name, mod.virtual_path)
220 was_set[mod.name] = true
221 elseif not was_set[mod.name] then
222 worldfile:set("load_mod_" .. mod.name, "false")
224 elseif mod.enabled then
225 gamedata.errormessage = fgettext_ne("Failed to enable mo" ..
226 "d \"$1\" as it contains disallowed characters. " ..
227 "Only characters [a-z0-9_] are allowed.",
230 mods["load_mod_" .. mod.name] = nil
234 -- Remove mods that are not present anymore
235 for key in pairs(mods) do
236 if key:sub(1, 9) == "load_mod_" then
237 worldfile:remove(key)
241 if not worldfile:write() then
242 core.log("error", "Failed to write world config file")
249 if fields.btn_config_world_cancel then
254 if fields.btn_config_world_cdb then
257 local dlg = create_store_dlg("mod")
264 if fields.btn_enable_all_mods then
265 local list = this.data.list:get_raw_list()
267 -- When multiple copies of a mod are installed, we need to avoid enabling multiple of them
268 -- at a time. So lets first collect all the enabled mods, and then use this to exclude
271 local was_enabled = {}
273 if not list[i].is_game_content
274 and not list[i].is_modpack and list[i].enabled then
275 was_enabled[list[i].name] = true
280 if not list[i].is_game_content and not list[i].is_modpack and
281 not was_enabled[list[i].name] then
282 list[i].enabled = true
283 was_enabled[list[i].name] = true
291 if fields.btn_disable_all_mods then
292 local list = this.data.list:get_raw_list()
295 if not list[i].is_game_content
296 and not list[i].is_modpack then
297 list[i].enabled = false
307 function create_configure_world_dlg(worldidx)
308 local dlg = dialog_create("sp_config_world", get_formspec, handle_buttons)
310 dlg.data.selected_mod = tonumber(
311 core.settings:get("world_config_selected_mod")) or 0
313 dlg.data.worldspec = core.get_worlds()[worldidx]
314 if not dlg.data.worldspec then
319 dlg.data.worldconfig = pkgmgr.get_worldconfig(dlg.data.worldspec.path)
321 if not dlg.data.worldconfig or not dlg.data.worldconfig.id or
322 dlg.data.worldconfig.id == "" then