]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/dlg_config_world.lua
Fix games not updating on deletion
[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 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 or mod.type == "game" then
37                 local info = minetest.formspec_escape(
38                         core.get_content_info(mod.path).description)
39                 if info == "" then
40                         if mod.is_modpack then
41                                 info = fgettext("No modpack description provided.")
42                         else
43                                 info = fgettext("No game description provided.")
44                         end
45                 end
46                 retval = retval ..
47                         "textarea[0.25,0.7;5.75,7.2;;" .. info .. ";]"
48         else
49                 retval = retval ..
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 ..
54                         ";0]" ..
55                         "label[0,3.875;" .. fgettext("Optional dependencies:") .. "]" ..
56                         "textlist[0,4.375;5,1.8;world_config_optdepends;" ..
57                         soft_deps .. ";0]"
58         end
59         retval = retval ..
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") .. "]"
64
65         if mod.name ~= "" and not mod.is_game_content then
66                 if mod.is_modpack then
67
68                         if pkgmgr.is_modpack_entirely_enabled(data, mod.name) then
69                                 retval = retval ..
70                                         "button[5.5,0.125;3,0.5;btn_mp_disable;" ..
71                                         fgettext("Disable modpack") .. "]"
72                         else
73                                 retval = retval ..
74                                         "button[5.5,0.125;3,0.5;btn_mp_enable;" ..
75                                         fgettext("Enable modpack") .. "]"
76                         end
77                 else
78                         retval = retval ..
79                                 "checkbox[5.5,-0.125;cb_mod_enable;" .. fgettext("enabled") ..
80                                 ";" .. tostring(mod.enabled) .. "]"
81                 end
82         end
83         if enabled_all then
84                 retval = retval ..
85                         "button[8.95,0.125;2.5,0.5;btn_disable_all_mods;" ..
86                         fgettext("Disable all") .. "]"
87         else
88                 retval = retval ..
89                         "button[8.95,0.125;2.5,0.5;btn_enable_all_mods;" ..
90                         fgettext("Enable all") .. "]"
91         end
92         return retval ..
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 .."]"
96 end
97
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)
103
104                 if event.type == "DCL" then
105                         pkgmgr.enable_mod(this)
106                 end
107
108                 return true
109         end
110
111         if fields.key_enter then
112                 pkgmgr.enable_mod(this)
113                 return true
114         end
115
116         if fields.cb_mod_enable ~= nil then
117                 pkgmgr.enable_mod(this, core.is_yes(fields.cb_mod_enable))
118                 return true
119         end
120
121         if fields.btn_mp_enable ~= nil or
122                         fields.btn_mp_disable then
123                 pkgmgr.enable_mod(this, fields.btn_mp_enable ~= nil)
124                 return true
125         end
126
127         if fields.btn_config_world_save then
128                 local filename = this.data.worldspec.path .. DIR_DELIM .. "world.mt"
129
130                 local worldfile = Settings(filename)
131                 local mods = worldfile:to_table()
132
133                 local rawlist = this.data.list:get_raw_list()
134
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.",
146                                                         mod.name)
147                                 end
148                                 mods["load_mod_" .. mod.name] = nil
149                         end
150                 end
151
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)
156                         end
157                 end
158
159                 if not worldfile:write() then
160                         core.log("error", "Failed to write world config file")
161                 end
162
163                 this:delete()
164                 return true
165         end
166
167         if fields.btn_config_world_cancel then
168                 this:delete()
169                 return true
170         end
171
172         if fields.btn_enable_all_mods then
173                 local list = this.data.list:get_raw_list()
174
175                 for i = 1, #list do
176                         if not list[i].is_game_content
177                                         and not list[i].is_modpack then
178                                 list[i].enabled = true
179                         end
180                 end
181                 enabled_all = true
182                 return true
183         end
184
185         if fields.btn_disable_all_mods then
186                 local list = this.data.list:get_raw_list()
187
188                 for i = 1, #list do
189                         if not list[i].is_game_content
190                                         and not list[i].is_modpack then
191                                 list[i].enabled = false
192                         end
193                 end
194                 enabled_all = false
195                 return true
196         end
197
198         return false
199 end
200
201 function create_configure_world_dlg(worldidx)
202         local dlg = dialog_create("sp_config_world", get_formspec, handle_buttons)
203
204         dlg.data.selected_mod = tonumber(
205                         core.settings:get("world_config_selected_mod")) or 0
206
207         dlg.data.worldspec = core.get_worlds()[worldidx]
208         if not dlg.data.worldspec then
209                 dlg:delete()
210                 return
211         end
212
213         dlg.data.worldconfig = pkgmgr.get_worldconfig(dlg.data.worldspec.path)
214
215         if not dlg.data.worldconfig or not dlg.data.worldconfig.id or
216                         dlg.data.worldconfig.id == "" then
217                 dlg:delete()
218                 return
219         end
220
221         dlg.data.list = filterlist.create(
222                 pkgmgr.preparemodlist,
223                 pkgmgr.comparemod,
224                 function(element, uid)
225                         if element.name == uid then
226                                 return true
227                         end
228                 end,
229                 function(element, criteria)
230                         if criteria.hide_game and
231                                         element.is_game_content then
232                                 return false
233                         end
234
235                         if criteria.hide_modpackcontents and
236                                         element.modpack ~= nil then
237                                 return false
238                         end
239                         return true
240                 end,
241                 {
242                         worldpath = dlg.data.worldspec.path,
243                         gameid = dlg.data.worldspec.gameid
244                 }
245         )
246
247
248         if dlg.data.selected_mod > dlg.data.list:size() then
249                 dlg.data.selected_mod = 0
250         end
251
252         dlg.data.list:set_filtercriteria({
253                 hide_game = dlg.data.hide_gamemods,
254                 hide_modpackcontents = dlg.data.hide_modpackcontents
255         })
256         dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
257         dlg.data.list:set_sortmode("alphabetic")
258
259         return dlg
260 end