]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/dlg_config_world.lua
Add more neighbors on mesh update (#6765)
[dragonfireclient.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 init_data(data)
27         data.list = filterlist.create(
28                 pkgmgr.preparemodlist,
29                 pkgmgr.comparemod,
30                 function(element, uid)
31                         if element.name == uid then
32                                 return true
33                         end
34                 end,
35                 function(element, criteria)
36                         if criteria.hide_game and
37                                         element.is_game_content then
38                                 return false
39                         end
40
41                         if criteria.hide_modpackcontents and
42                                         element.modpack ~= nil then
43                                 return false
44                         end
45                         return true
46                 end,
47                 {
48                         worldpath = data.worldspec.path,
49                         gameid = data.worldspec.gameid
50                 })
51
52         if data.selected_mod > data.list:size() then
53                 data.selected_mod = 0
54         end
55
56         data.list:set_filtercriteria({
57                 hide_game = data.hide_gamemods,
58                 hide_modpackcontents = data.hide_modpackcontents
59         })
60         data.list:add_sort_mechanism("alphabetic", sort_mod_list)
61         data.list:set_sortmode("alphabetic")
62 end
63
64 local function get_formspec(data)
65         if not data.list then
66                 init_data(data)
67         end
68
69         local mod = data.list:get_list()[data.selected_mod] or {name = ""}
70
71         local retval =
72                 "size[11.5,7.5,true]" ..
73                 "label[0.5,0;" .. fgettext("World:") .. "]" ..
74                 "label[1.75,0;" .. data.worldspec.name .. "]"
75
76         if mod.is_modpack or mod.type == "game" then
77                 local info = core.formspec_escape(
78                         core.get_content_info(mod.path).description)
79                 if info == "" then
80                         if mod.is_modpack then
81                                 info = fgettext("No modpack description provided.")
82                         else
83                                 info = fgettext("No game description provided.")
84                         end
85                 end
86                 retval = retval ..
87                         "textarea[0.25,0.7;5.75,7.2;;" .. info .. ";]"
88         else
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, ",")
92
93                 retval = retval ..
94                         "label[0,0.7;" .. fgettext("Mod:") .. "]" ..
95                         "label[0.75,0.7;" .. mod.name .. "]"
96
97                 if hard_deps_str == "" then
98                         if soft_deps_str == "" then
99                                 retval = retval ..
100                                         "label[0,1.25;" ..
101                                         fgettext("No (optional) dependencies") .. "]"
102                         else
103                                 retval = retval ..
104                                         "label[0,1.25;" .. fgettext("No hard dependencies") ..
105                                         "]" ..
106                                         "label[0,1.75;" .. fgettext("Optional dependencies:") ..
107                                         "]" ..
108                                         "textlist[0,2.25;5,4;world_config_optdepends;" ..
109                                         soft_deps_str .. ";0]"
110                         end
111                 else
112                         if soft_deps_str == "" then
113                                 retval = retval ..
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") .. "]"
118                         else
119                                 retval = retval ..
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:") ..
124                                         "]" ..
125                                         "textlist[0,4.375;5,1.8;world_config_optdepends;" ..
126                                         soft_deps_str .. ";0]"
127                         end
128                 end
129         end
130
131         retval = retval ..
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") .. "]"
138
139         if mod.name ~= "" and not mod.is_game_content then
140                 if mod.is_modpack then
141
142                         if pkgmgr.is_modpack_entirely_enabled(data, mod.name) then
143                                 retval = retval ..
144                                         "button[5.5,0.125;3,0.5;btn_mp_disable;" ..
145                                         fgettext("Disable modpack") .. "]"
146                         else
147                                 retval = retval ..
148                                         "button[5.5,0.125;3,0.5;btn_mp_enable;" ..
149                                         fgettext("Enable modpack") .. "]"
150                         end
151                 else
152                         retval = retval ..
153                                 "checkbox[5.5,-0.125;cb_mod_enable;" .. fgettext("enabled") ..
154                                 ";" .. tostring(mod.enabled) .. "]"
155                 end
156         end
157         if enabled_all then
158                 retval = retval ..
159                         "button[8.95,0.125;2.5,0.5;btn_disable_all_mods;" ..
160                         fgettext("Disable all") .. "]"
161         else
162                 retval = retval ..
163                         "button[8.95,0.125;2.5,0.5;btn_enable_all_mods;" ..
164                         fgettext("Enable all") .. "]"
165         end
166         return retval ..
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 .."]"
170 end
171
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)
177
178                 if event.type == "DCL" then
179                         pkgmgr.enable_mod(this)
180                 end
181
182                 return true
183         end
184
185         if fields.key_enter then
186                 pkgmgr.enable_mod(this)
187                 return true
188         end
189
190         if fields.cb_mod_enable ~= nil then
191                 pkgmgr.enable_mod(this, core.is_yes(fields.cb_mod_enable))
192                 return true
193         end
194
195         if fields.btn_mp_enable ~= nil or
196                         fields.btn_mp_disable then
197                 pkgmgr.enable_mod(this, fields.btn_mp_enable ~= nil)
198                 return true
199         end
200
201         if fields.btn_config_world_save then
202                 local filename = this.data.worldspec.path .. DIR_DELIM .. "world.mt"
203
204                 local worldfile = Settings(filename)
205                 local mods = worldfile:to_table()
206
207                 local rawlist = this.data.list:get_raw_list()
208
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.",
220                                                         mod.name)
221                                 end
222                                 mods["load_mod_" .. mod.name] = nil
223                         end
224                 end
225
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)
230                         end
231                 end
232
233                 if not worldfile:write() then
234                         core.log("error", "Failed to write world config file")
235                 end
236
237                 this:delete()
238                 return true
239         end
240
241         if fields.btn_config_world_cancel then
242                 this:delete()
243                 return true
244         end
245
246         if fields.btn_config_world_cdb then
247                 this.data.list = nil
248
249                 local dlg = create_store_dlg("mod")
250                 dlg:set_parent(this)
251                 this:hide()
252                 dlg:show()
253                 return true
254         end
255
256         if fields.btn_enable_all_mods then
257                 local list = this.data.list:get_raw_list()
258
259                 for i = 1, #list do
260                         if not list[i].is_game_content
261                                         and not list[i].is_modpack then
262                                 list[i].enabled = true
263                         end
264                 end
265                 enabled_all = true
266                 return true
267         end
268
269         if fields.btn_disable_all_mods then
270                 local list = this.data.list:get_raw_list()
271
272                 for i = 1, #list do
273                         if not list[i].is_game_content
274                                         and not list[i].is_modpack then
275                                 list[i].enabled = false
276                         end
277                 end
278                 enabled_all = false
279                 return true
280         end
281
282         return false
283 end
284
285 function create_configure_world_dlg(worldidx)
286         local dlg = dialog_create("sp_config_world", get_formspec, handle_buttons)
287
288         dlg.data.selected_mod = tonumber(
289                         core.settings:get("world_config_selected_mod")) or 0
290
291         dlg.data.worldspec = core.get_worlds()[worldidx]
292         if not dlg.data.worldspec then
293                 dlg:delete()
294                 return
295         end
296
297         dlg.data.worldconfig = pkgmgr.get_worldconfig(dlg.data.worldspec.path)
298
299         if not dlg.data.worldconfig or not dlg.data.worldconfig.id or
300                         dlg.data.worldconfig.id == "" then
301                 dlg:delete()
302                 return
303         end
304
305         return dlg
306 end