]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/dlg_config_world.lua
World config: Add modpack descriptions and remove dependencies there (#7397)
[minetest.git] / builtin / mainmenu / dlg_config_world.lua
1 --Minetest
2 --Copyright (C) 2013 sapier
3 --
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.
8 --
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.
13 --
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.
17
18 --------------------------------------------------------------------------------
19
20 local enabled_all = false
21
22 local function modname_valid(name)
23         return not name:find("[^a-z0-9_]")
24 end
25
26 local function get_formspec(data)
27         local mod = data.list:get_list()[data.selected_mod] or {name = ""}
28
29         local retval =
30                 "size[11.5,7.5,true]" ..
31                 "label[0.5,0;" .. fgettext("World:") .. "]" ..
32                 "label[1.75,0;" .. data.worldspec.name .. "]"
33
34         local hard_deps, soft_deps = pkgmgr.get_dependencies(mod.path)
35
36         if mod.is_modpack then
37                 local info = minetest.formspec_escape(
38                         core.get_content_info(mod.path).description)
39                 if info == "" then
40                         info = fgettext("No modpack description provided.")
41                 end
42                 retval = retval ..
43                         "textarea[0.25,0.7;5.75,7.2;;" .. info .. ";]"
44         else
45                 retval = retval ..
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 ..
50                         ";0]" ..
51                         "label[0,3.875;" .. fgettext("Optional dependencies:") .. "]" ..
52                         "textlist[0,4.375;5,1.8;world_config_optdepends;" ..
53                         soft_deps .. ";0]"
54         end
55         retval = retval ..
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") .. "]"
60
61         if mod.name ~= "" and not mod.is_game_content then
62                 if mod.is_modpack then
63
64                         if pkgmgr.is_modpack_entirely_enabled(data, mod.name) then
65                                 retval = retval ..
66                                         "button[5.5,0.125;2.5,0.5;btn_mp_disable;" ..
67                                         fgettext("Disable MP") .. "]"
68                         else
69                                 retval = retval ..
70                                         "button[5.5,0.125;2.5,0.5;btn_mp_enable;" ..
71                                         fgettext("Enable MP") .. "]"
72                         end
73                 else
74                         retval = retval ..
75                                 "checkbox[5.5,-0.125;cb_mod_enable;" .. fgettext("enabled") ..
76                                 ";" .. tostring(mod.enabled) .. "]"
77                 end
78         end
79         if enabled_all then
80                 retval = retval ..
81                         "button[8.75,0.125;2.5,0.5;btn_disable_all_mods;" ..
82                         fgettext("Disable all") .. "]"
83         else
84                 retval = retval ..
85                         "button[8.75,0.125;2.5,0.5;btn_enable_all_mods;" ..
86                         fgettext("Enable all") .. "]"
87         end
88         return retval ..
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 .."]"
92 end
93
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)
99
100                 if event.type == "DCL" then
101                         pkgmgr.enable_mod(this)
102                 end
103
104                 return true
105         end
106
107         if fields.key_enter then
108                 pkgmgr.enable_mod(this)
109                 return true
110         end
111
112         if fields.cb_mod_enable ~= nil then
113                 pkgmgr.enable_mod(this, core.is_yes(fields.cb_mod_enable))
114                 return true
115         end
116
117         if fields.btn_mp_enable ~= nil or
118                         fields.btn_mp_disable then
119                 pkgmgr.enable_mod(this, fields.btn_mp_enable ~= nil)
120                 return true
121         end
122
123         if fields.btn_config_world_save then
124                 local filename = this.data.worldspec.path .. DIR_DELIM .. "world.mt"
125
126                 local worldfile = Settings(filename)
127                 local mods = worldfile:to_table()
128
129                 local rawlist = this.data.list:get_raw_list()
130
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.",
142                                                         mod.name)
143                                 end
144                                 mods["load_mod_" .. mod.name] = nil
145                         end
146                 end
147
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)
152                         end
153                 end
154
155                 if not worldfile:write() then
156                         core.log("error", "Failed to write world config file")
157                 end
158
159                 this:delete()
160                 return true
161         end
162
163         if fields.btn_config_world_cancel then
164                 this:delete()
165                 return true
166         end
167
168         if fields.btn_enable_all_mods then
169                 local list = this.data.list:get_raw_list()
170
171                 for i = 1, #list do
172                         if not list[i].is_game_content
173                                         and not list[i].is_modpack then
174                                 list[i].enabled = true
175                         end
176                 end
177                 enabled_all = true
178                 return true
179         end
180
181         if fields.btn_disable_all_mods then
182                 local list = this.data.list:get_raw_list()
183
184                 for i = 1, #list do
185                         if not list[i].is_game_content
186                                         and not list[i].is_modpack then
187                                 list[i].enabled = false
188                         end
189                 end
190                 enabled_all = false
191                 return true
192         end
193
194         return false
195 end
196
197 function create_configure_world_dlg(worldidx)
198         local dlg = dialog_create("sp_config_world", get_formspec, handle_buttons)
199
200         dlg.data.selected_mod = tonumber(
201                         core.settings:get("world_config_selected_mod")) or 0
202
203         dlg.data.worldspec = core.get_worlds()[worldidx]
204         if not dlg.data.worldspec then
205                 dlg:delete()
206                 return
207         end
208
209         dlg.data.worldconfig = pkgmgr.get_worldconfig(dlg.data.worldspec.path)
210
211         if not dlg.data.worldconfig or not dlg.data.worldconfig.id or
212                         dlg.data.worldconfig.id == "" then
213                 dlg:delete()
214                 return
215         end
216
217         dlg.data.list = filterlist.create(
218                 pkgmgr.preparemodlist,
219                 pkgmgr.comparemod,
220                 function(element, uid)
221                         if element.name == uid then
222                                 return true
223                         end
224                 end,
225                 function(element, criteria)
226                         if criteria.hide_game and
227                                         element.is_game_content then
228                                 return false
229                         end
230
231                         if criteria.hide_modpackcontents and
232                                         element.modpack ~= nil then
233                                 return false
234                         end
235                         return true
236                 end,
237                 {
238                         worldpath = dlg.data.worldspec.path,
239                         gameid = dlg.data.worldspec.gameid
240                 }
241         )
242
243
244         if dlg.data.selected_mod > dlg.data.list:size() then
245                 dlg.data.selected_mod = 0
246         end
247
248         dlg.data.list:set_filtercriteria({
249                 hide_game = dlg.data.hide_gamemods,
250                 hide_modpackcontents = dlg.data.hide_modpackcontents
251         })
252         dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
253         dlg.data.list:set_sortmode("alphabetic")
254
255         return dlg
256 end