]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/dlg_config_world.lua
Update my name
[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 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
167         local use_technical_names = core.settings:get_bool("show_technical_names")
168
169         return retval ..
170                 "tablecolumns[color;tree;text]" ..
171                 "table[5.5,0.75;5.75,6;world_config_modlist;" ..
172                 pkgmgr.render_packagelist(data.list, use_technical_names) .. ";" .. data.selected_mod .."]"
173 end
174
175 local function handle_buttons(this, fields)
176         if fields.world_config_modlist then
177                 local event = core.explode_table_event(fields.world_config_modlist)
178                 this.data.selected_mod = event.row
179                 core.settings:set("world_config_selected_mod", event.row)
180
181                 if event.type == "DCL" then
182                         pkgmgr.enable_mod(this)
183                 end
184
185                 return true
186         end
187
188         if fields.key_enter then
189                 pkgmgr.enable_mod(this)
190                 return true
191         end
192
193         if fields.cb_mod_enable ~= nil then
194                 pkgmgr.enable_mod(this, core.is_yes(fields.cb_mod_enable))
195                 return true
196         end
197
198         if fields.btn_mp_enable ~= nil or
199                         fields.btn_mp_disable then
200                 pkgmgr.enable_mod(this, fields.btn_mp_enable ~= nil)
201                 return true
202         end
203
204         if fields.btn_config_world_save then
205                 local filename = this.data.worldspec.path .. DIR_DELIM .. "world.mt"
206
207                 local worldfile = Settings(filename)
208                 local mods = worldfile:to_table()
209
210                 local rawlist = this.data.list:get_raw_list()
211                 local was_set = {}
212
213                 for i = 1, #rawlist do
214                         local mod = rawlist[i]
215                         if not mod.is_modpack and
216                                         not mod.is_game_content then
217                                 if modname_valid(mod.name) then
218                                         if mod.enabled then
219                                                 worldfile:set("load_mod_" .. mod.name, mod.virtual_path)
220                                                 was_set[mod.name] = true
221                                         elseif not was_set[mod.name] then
222                                                 worldfile:set("load_mod_" .. mod.name, "false")
223                                         end
224                                 elseif mod.enabled then
225                                         gamedata.errormessage = fgettext_ne("Failed to enable mo" ..
226                                                         "d \"$1\" as it contains disallowed characters. " ..
227                                                         "Only characters [a-z0-9_] are allowed.",
228                                                         mod.name)
229                                 end
230                                 mods["load_mod_" .. mod.name] = nil
231                         end
232                 end
233
234                 -- Remove mods that are not present anymore
235                 for key in pairs(mods) do
236                         if key:sub(1, 9) == "load_mod_" then
237                                 worldfile:remove(key)
238                         end
239                 end
240
241                 if not worldfile:write() then
242                         core.log("error", "Failed to write world config file")
243                 end
244
245                 this:delete()
246                 return true
247         end
248
249         if fields.btn_config_world_cancel then
250                 this:delete()
251                 return true
252         end
253
254         if fields.btn_config_world_cdb then
255                 this.data.list = nil
256
257                 local dlg = create_store_dlg("mod")
258                 dlg:set_parent(this)
259                 this:hide()
260                 dlg:show()
261                 return true
262         end
263
264         if fields.btn_enable_all_mods then
265                 local list = this.data.list:get_raw_list()
266
267                 -- When multiple copies of a mod are installed, we need to avoid enabling multiple of them
268                 -- at a time. So lets first collect all the enabled mods, and then use this to exclude
269                 -- multiple enables.
270
271                 local was_enabled = {}
272                 for i = 1, #list do
273                         if not list[i].is_game_content
274                                         and not list[i].is_modpack and list[i].enabled then
275                                 was_enabled[list[i].name] = true
276                         end
277                 end
278
279                 for i = 1, #list do
280                         if not list[i].is_game_content and not list[i].is_modpack and
281                                         not was_enabled[list[i].name] then
282                                 list[i].enabled = true
283                                 was_enabled[list[i].name] = true
284                         end
285                 end
286
287                 enabled_all = true
288                 return true
289         end
290
291         if fields.btn_disable_all_mods then
292                 local list = this.data.list:get_raw_list()
293
294                 for i = 1, #list do
295                         if not list[i].is_game_content
296                                         and not list[i].is_modpack then
297                                 list[i].enabled = false
298                         end
299                 end
300                 enabled_all = false
301                 return true
302         end
303
304         return false
305 end
306
307 function create_configure_world_dlg(worldidx)
308         local dlg = dialog_create("sp_config_world", get_formspec, handle_buttons)
309
310         dlg.data.selected_mod = tonumber(
311                         core.settings:get("world_config_selected_mod")) or 0
312
313         dlg.data.worldspec = core.get_worlds()[worldidx]
314         if not dlg.data.worldspec then
315                 dlg:delete()
316                 return
317         end
318
319         dlg.data.worldconfig = pkgmgr.get_worldconfig(dlg.data.worldspec.path)
320
321         if not dlg.data.worldconfig or not dlg.data.worldconfig.id or
322                         dlg.data.worldconfig.id == "" then
323                 dlg:delete()
324                 return
325         end
326
327         return dlg
328 end