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 or mod.type == "game" then
37 local info = minetest.formspec_escape(
38 core.get_content_info(mod.path).description)
40 if mod.is_modpack then
41 info = fgettext("No modpack description provided.")
43 info = fgettext("No game description provided.")
47 "textarea[0.25,0.7;5.75,7.2;;" .. info .. ";]"
50 "label[0,0.7;" .. fgettext("Mod:") .. "]" ..
51 "label[0.75,0.7;" .. mod.name .. "]" ..
52 "label[0,1.25;" .. fgettext("Dependencies:") .. "]" ..
53 "textlist[0,1.75;5,2.125;world_config_depends;" .. hard_deps ..
55 "label[0,3.875;" .. fgettext("Optional dependencies:") .. "]" ..
56 "textlist[0,4.375;5,1.8;world_config_optdepends;" ..
60 "button[3.25,7;2.5,0.5;btn_config_world_save;" ..
61 fgettext("Save") .. "]" ..
62 "button[5.75,7;2.5,0.5;btn_config_world_cancel;" ..
63 fgettext("Cancel") .. "]"
65 if mod.name ~= "" and not mod.is_game_content then
66 if mod.is_modpack then
68 if pkgmgr.is_modpack_entirely_enabled(data, mod.name) then
70 "button[5.5,0.125;3,0.5;btn_mp_disable;" ..
71 fgettext("Disable modpack") .. "]"
74 "button[5.5,0.125;3,0.5;btn_mp_enable;" ..
75 fgettext("Enable modpack") .. "]"
79 "checkbox[5.5,-0.125;cb_mod_enable;" .. fgettext("enabled") ..
80 ";" .. tostring(mod.enabled) .. "]"
85 "button[8.95,0.125;2.5,0.5;btn_disable_all_mods;" ..
86 fgettext("Disable all") .. "]"
89 "button[8.95,0.125;2.5,0.5;btn_enable_all_mods;" ..
90 fgettext("Enable all") .. "]"
93 "tablecolumns[color;tree;text]" ..
94 "table[5.5,0.75;5.75,6;world_config_modlist;" ..
95 pkgmgr.render_packagelist(data.list) .. ";" .. data.selected_mod .."]"
98 local function handle_buttons(this, fields)
99 if fields.world_config_modlist then
100 local event = core.explode_table_event(fields.world_config_modlist)
101 this.data.selected_mod = event.row
102 core.settings:set("world_config_selected_mod", event.row)
104 if event.type == "DCL" then
105 pkgmgr.enable_mod(this)
111 if fields.key_enter then
112 pkgmgr.enable_mod(this)
116 if fields.cb_mod_enable ~= nil then
117 pkgmgr.enable_mod(this, core.is_yes(fields.cb_mod_enable))
121 if fields.btn_mp_enable ~= nil or
122 fields.btn_mp_disable then
123 pkgmgr.enable_mod(this, fields.btn_mp_enable ~= nil)
127 if fields.btn_config_world_save then
128 local filename = this.data.worldspec.path .. DIR_DELIM .. "world.mt"
130 local worldfile = Settings(filename)
131 local mods = worldfile:to_table()
133 local rawlist = this.data.list:get_raw_list()
135 for i = 1, #rawlist do
136 local mod = rawlist[i]
137 if not mod.is_modpack and
138 not mod.is_game_content then
139 if modname_valid(mod.name) then
140 worldfile:set("load_mod_" .. mod.name,
141 mod.enabled and "true" or "false")
142 elseif mod.enabled then
143 gamedata.errormessage = fgettext_ne("Failed to enable mo" ..
144 "d \"$1\" as it contains disallowed characters. " ..
145 "Only characters [a-z0-9_] are allowed.",
148 mods["load_mod_" .. mod.name] = nil
152 -- Remove mods that are not present anymore
153 for key in pairs(mods) do
154 if key:sub(1, 9) == "load_mod_" then
155 worldfile:remove(key)
159 if not worldfile:write() then
160 core.log("error", "Failed to write world config file")
167 if fields.btn_config_world_cancel then
172 if fields.btn_enable_all_mods then
173 local list = this.data.list:get_raw_list()
176 if not list[i].is_game_content
177 and not list[i].is_modpack then
178 list[i].enabled = true
185 if fields.btn_disable_all_mods then
186 local list = this.data.list:get_raw_list()
189 if not list[i].is_game_content
190 and not list[i].is_modpack then
191 list[i].enabled = false
201 function create_configure_world_dlg(worldidx)
202 local dlg = dialog_create("sp_config_world", get_formspec, handle_buttons)
204 dlg.data.selected_mod = tonumber(
205 core.settings:get("world_config_selected_mod")) or 0
207 dlg.data.worldspec = core.get_worlds()[worldidx]
208 if not dlg.data.worldspec then
213 dlg.data.worldconfig = pkgmgr.get_worldconfig(dlg.data.worldspec.path)
215 if not dlg.data.worldconfig or not dlg.data.worldconfig.id or
216 dlg.data.worldconfig.id == "" then
221 dlg.data.list = filterlist.create(
222 pkgmgr.preparemodlist,
224 function(element, uid)
225 if element.name == uid then
229 function(element, criteria)
230 if criteria.hide_game and
231 element.is_game_content then
235 if criteria.hide_modpackcontents and
236 element.modpack ~= nil then
242 worldpath = dlg.data.worldspec.path,
243 gameid = dlg.data.worldspec.gameid
248 if dlg.data.selected_mod > dlg.data.list:size() then
249 dlg.data.selected_mod = 0
252 dlg.data.list:set_filtercriteria({
253 hide_game = dlg.data.hide_gamemods,
254 hide_modpackcontents = dlg.data.hide_modpackcontents
256 dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
257 dlg.data.list:set_sortmode("alphabetic")