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 "tablecolumns[color;tree;text]" ..
168 "table[5.5,0.75;5.75,6;world_config_modlist;" ..
169 pkgmgr.render_packagelist(data.list) .. ";" .. data.selected_mod .."]"
172 local function handle_buttons(this, fields)
173 if fields.world_config_modlist then
174 local event = core.explode_table_event(fields.world_config_modlist)
175 this.data.selected_mod = event.row
176 core.settings:set("world_config_selected_mod", event.row)
178 if event.type == "DCL" then
179 pkgmgr.enable_mod(this)
185 if fields.key_enter then
186 pkgmgr.enable_mod(this)
190 if fields.cb_mod_enable ~= nil then
191 pkgmgr.enable_mod(this, core.is_yes(fields.cb_mod_enable))
195 if fields.btn_mp_enable ~= nil or
196 fields.btn_mp_disable then
197 pkgmgr.enable_mod(this, fields.btn_mp_enable ~= nil)
201 if fields.btn_config_world_save then
202 local filename = this.data.worldspec.path .. DIR_DELIM .. "world.mt"
204 local worldfile = Settings(filename)
205 local mods = worldfile:to_table()
207 local rawlist = this.data.list:get_raw_list()
209 for i = 1, #rawlist do
210 local mod = rawlist[i]
211 if not mod.is_modpack and
212 not mod.is_game_content then
213 if modname_valid(mod.name) then
214 worldfile:set("load_mod_" .. mod.name,
215 mod.enabled and "true" or "false")
216 elseif mod.enabled then
217 gamedata.errormessage = fgettext_ne("Failed to enable mo" ..
218 "d \"$1\" as it contains disallowed characters. " ..
219 "Only characters [a-z0-9_] are allowed.",
222 mods["load_mod_" .. mod.name] = nil
226 -- Remove mods that are not present anymore
227 for key in pairs(mods) do
228 if key:sub(1, 9) == "load_mod_" then
229 worldfile:remove(key)
233 if not worldfile:write() then
234 core.log("error", "Failed to write world config file")
241 if fields.btn_config_world_cancel then
246 if fields.btn_config_world_cdb then
249 local dlg = create_store_dlg("mod")
256 if fields.btn_enable_all_mods then
257 local list = this.data.list:get_raw_list()
260 if not list[i].is_game_content
261 and not list[i].is_modpack then
262 list[i].enabled = true
269 if fields.btn_disable_all_mods then
270 local list = this.data.list:get_raw_list()
273 if not list[i].is_game_content
274 and not list[i].is_modpack then
275 list[i].enabled = false
285 function create_configure_world_dlg(worldidx)
286 local dlg = dialog_create("sp_config_world", get_formspec, handle_buttons)
288 dlg.data.selected_mod = tonumber(
289 core.settings:get("world_config_selected_mod")) or 0
291 dlg.data.worldspec = core.get_worlds()[worldidx]
292 if not dlg.data.worldspec then
297 dlg.data.worldconfig = pkgmgr.get_worldconfig(dlg.data.worldspec.path)
299 if not dlg.data.worldconfig or not dlg.data.worldconfig.id or
300 dlg.data.worldconfig.id == "" then