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] or {name = ""}
30 "size[11.5,7.5,true]" ..
31 "label[0.5,0;" .. fgettext("World:") .. "]" ..
32 "label[1.75,0;" .. data.worldspec.name .. "]"
34 local hard_deps, soft_deps = pkgmgr.get_dependencies(mod.path)
36 if mod.is_modpack then
37 local info = minetest.formspec_escape(
38 core.get_content_info(mod.path).description)
40 info = fgettext("No modpack description provided.")
43 "textarea[0.25,0.7;5.75,7.2;;" .. info .. ";]"
46 "label[0,0.7;" .. fgettext("Mod:") .. "]" ..
47 "label[0.75,0.7;" .. mod.name .. "]" ..
48 "label[0,1.25;" .. fgettext("Dependencies:") .. "]" ..
49 "textlist[0,1.75;5,2.125;world_config_depends;" .. hard_deps ..
51 "label[0,3.875;" .. fgettext("Optional dependencies:") .. "]" ..
52 "textlist[0,4.375;5,1.8;world_config_optdepends;" ..
56 "button[3.25,7;2.5,0.5;btn_config_world_save;" ..
57 fgettext("Save") .. "]" ..
58 "button[5.75,7;2.5,0.5;btn_config_world_cancel;" ..
59 fgettext("Cancel") .. "]"
61 if mod.name ~= "" and not mod.is_game_content then
62 if mod.is_modpack then
64 if pkgmgr.is_modpack_entirely_enabled(data, mod.name) then
66 "button[5.5,0.125;2.5,0.5;btn_mp_disable;" ..
67 fgettext("Disable MP") .. "]"
70 "button[5.5,0.125;2.5,0.5;btn_mp_enable;" ..
71 fgettext("Enable MP") .. "]"
75 "checkbox[5.5,-0.125;cb_mod_enable;" .. fgettext("enabled") ..
76 ";" .. tostring(mod.enabled) .. "]"
81 "button[8.75,0.125;2.5,0.5;btn_disable_all_mods;" ..
82 fgettext("Disable all") .. "]"
85 "button[8.75,0.125;2.5,0.5;btn_enable_all_mods;" ..
86 fgettext("Enable all") .. "]"
89 "tablecolumns[color;tree;text]" ..
90 "table[5.5,0.75;5.75,6;world_config_modlist;" ..
91 pkgmgr.render_packagelist(data.list) .. ";" .. data.selected_mod .."]"
94 local function handle_buttons(this, fields)
95 if fields.world_config_modlist then
96 local event = core.explode_table_event(fields.world_config_modlist)
97 this.data.selected_mod = event.row
98 core.settings:set("world_config_selected_mod", event.row)
100 if event.type == "DCL" then
101 pkgmgr.enable_mod(this)
107 if fields.key_enter then
108 pkgmgr.enable_mod(this)
112 if fields.cb_mod_enable ~= nil then
113 pkgmgr.enable_mod(this, core.is_yes(fields.cb_mod_enable))
117 if fields.btn_mp_enable ~= nil or
118 fields.btn_mp_disable then
119 pkgmgr.enable_mod(this, fields.btn_mp_enable ~= nil)
123 if fields.btn_config_world_save then
124 local filename = this.data.worldspec.path .. DIR_DELIM .. "world.mt"
126 local worldfile = Settings(filename)
127 local mods = worldfile:to_table()
129 local rawlist = this.data.list:get_raw_list()
131 for i = 1, #rawlist do
132 local mod = rawlist[i]
133 if not mod.is_modpack and
134 not mod.is_game_content then
135 if modname_valid(mod.name) then
136 worldfile:set("load_mod_" .. mod.name,
137 tostring(mod.enabled))
138 elseif mod.enabled then
139 gamedata.errormessage = fgettext_ne("Failed to enable mo" ..
140 "d \"$1\" as it contains disallowed characters. " ..
141 "Only chararacters [a-z0-9_] are allowed.",
144 mods["load_mod_" .. mod.name] = nil
148 -- Remove mods that are not present anymore
149 for key in pairs(mods) do
150 if key:sub(1, 9) == "load_mod_" then
151 worldfile:remove(key)
155 if not worldfile:write() then
156 core.log("error", "Failed to write world config file")
163 if fields.btn_config_world_cancel then
168 if fields.btn_enable_all_mods then
169 local list = this.data.list:get_raw_list()
172 if not list[i].is_game_content
173 and not list[i].is_modpack then
174 list[i].enabled = true
181 if fields.btn_disable_all_mods then
182 local list = this.data.list:get_raw_list()
185 if not list[i].is_game_content
186 and not list[i].is_modpack then
187 list[i].enabled = false
197 function create_configure_world_dlg(worldidx)
198 local dlg = dialog_create("sp_config_world", get_formspec, handle_buttons)
200 dlg.data.selected_mod = tonumber(
201 core.settings:get("world_config_selected_mod")) or 0
203 dlg.data.worldspec = core.get_worlds()[worldidx]
204 if not dlg.data.worldspec then
209 dlg.data.worldconfig = pkgmgr.get_worldconfig(dlg.data.worldspec.path)
211 if not dlg.data.worldconfig or not dlg.data.worldconfig.id or
212 dlg.data.worldconfig.id == "" then
217 dlg.data.list = filterlist.create(
218 pkgmgr.preparemodlist,
220 function(element, uid)
221 if element.name == uid then
225 function(element, criteria)
226 if criteria.hide_game and
227 element.is_game_content then
231 if criteria.hide_modpackcontents and
232 element.modpack ~= nil then
238 worldpath = dlg.data.worldspec.path,
239 gameid = dlg.data.worldspec.gameid
244 if dlg.data.selected_mod > dlg.data.list:size() then
245 dlg.data.selected_mod = 0
248 dlg.data.list:set_filtercriteria({
249 hide_game = dlg.data.hide_gamemods,
250 hide_modpackcontents = dlg.data.hide_modpackcontents
252 dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
253 dlg.data.list:set_sortmode("alphabetic")