]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/dlg_config_world.lua
6e6224c926f816f43a5f1d67f2825b135275951b
[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
28         local mod = data.list:get_list()[data.selected_mod]
29
30         local retval =
31                 "size[11.5,7.5,true]" ..
32                 "label[0.5,0;" .. fgettext("World:") .. "]" ..
33                 "label[1.75,0;" .. data.worldspec.name .. "]"
34
35         if data.hide_gamemods then
36                 retval = retval .. "checkbox[1,6;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";true]"
37         else
38                 retval = retval .. "checkbox[1,6;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";false]"
39         end
40
41         if data.hide_modpackcontents then
42                 retval = retval .. "checkbox[6,6;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";true]"
43         else
44                 retval = retval .. "checkbox[6,6;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";false]"
45         end
46
47         if mod == nil then
48                 mod = {name=""}
49         end
50         
51         retval = retval ..
52                 "label[0,0.7;" .. fgettext("Mod:") .. "]" ..
53                 "label[0.75,0.7;" .. mod.name .. "]" ..
54                 "label[0,1.25;" .. fgettext("Depends:") .. "]" ..
55                 "textlist[0,1.75;5,4.25;world_config_depends;" ..
56                 modmgr.get_dependencies(mod.path) .. ";0]" ..
57                 "button[3.25,7;2.5,0.5;btn_config_world_save;" .. fgettext("Save") .. "]" ..
58                 "button[5.75,7;2.5,0.5;btn_config_world_cancel;" .. fgettext("Cancel") .. "]"
59
60         if mod ~= nil and mod.name ~= "" and mod.typ ~= "game_mod" then
61                 if mod.is_modpack then
62                         local rawlist = data.list:get_raw_list()
63
64                         local all_enabled = true
65                         for j=1,#rawlist,1 do
66                                 if rawlist[j].modpack == mod.name and
67                                         rawlist[j].enabled ~= true then
68                                                 all_enabled = false
69                                                 break
70                                 end
71                         end
72
73                         if all_enabled == false then
74                                 retval = retval .. "button[5.5,0.125;2.5,0.5;btn_mp_enable;" .. fgettext("Enable MP") .. "]"
75                         else
76                                 retval = retval .. "button[5.5,0.125;2.5,0.5;btn_mp_disable;" .. fgettext("Disable MP") .. "]"
77                         end
78                 else
79                         if mod.enabled then
80                                 retval = retval .. "checkbox[5.5,-0.125;cb_mod_enable;" .. fgettext("enabled") .. ";true]"
81                         else
82                                 retval = retval .. "checkbox[5.5,-0.125;cb_mod_enable;" .. fgettext("enabled") .. ";false]"
83                         end
84                 end
85         end
86         if enabled_all then 
87                 retval = retval ..
88                         "button[8.75,0.125;2.5,0.5;btn_disable_all_mods;" .. fgettext("Disable all") .. "]" ..
89                         "textlist[5.5,0.75;5.75,5.25;world_config_modlist;"
90         else
91                 retval = retval ..
92                         "button[8.75,0.125;2.5,0.5;btn_enable_all_mods;" .. fgettext("Enable all") .. "]" ..
93                         "textlist[5.5,0.75;5.75,5.25;world_config_modlist;"
94         end
95         retval = retval .. modmgr.render_modlist(data.list)
96         retval = retval .. ";" .. data.selected_mod .."]"
97
98         return retval
99 end
100
101 local function enable_mod(this, toset)
102         local mod = this.data.list:get_list()[this.data.selected_mod]
103
104         if mod.typ == "game_mod" then
105                 -- game mods can't be enabled or disabled
106         elseif not mod.is_modpack then
107                 if toset == nil then
108                         mod.enabled = not mod.enabled
109                 else
110                         mod.enabled = toset
111                 end
112         else
113                 local list = this.data.list:get_raw_list()
114                 for i=1,#list,1 do
115                         if list[i].modpack == mod.name then
116                                 if toset == nil then
117                                         toset = not list[i].enabled
118                                 end
119                                 list[i].enabled = toset
120                         end
121                 end
122         end
123 end
124
125
126 local function handle_buttons(this, fields)
127
128         if fields["world_config_modlist"] ~= nil then
129                 local event = core.explode_textlist_event(fields["world_config_modlist"])
130                 this.data.selected_mod = event.index
131                 core.setting_set("world_config_selected_mod", event.index)
132
133                 if event.type == "DCL" then
134                         enable_mod(this)
135                 end
136                 
137                 return true
138         end
139
140         if fields["key_enter"] ~= nil then
141                 enable_mod(this)
142                 return true
143         end
144
145         if fields["cb_mod_enable"] ~= nil then
146                 local toset = core.is_yes(fields["cb_mod_enable"])
147                 enable_mod(this,toset)
148                 return true
149         end
150
151         if fields["btn_mp_enable"] ~= nil or
152                 fields["btn_mp_disable"] then
153                 local toset = (fields["btn_mp_enable"] ~= nil)
154                 enable_mod(this,toset)
155                 return true
156         end
157
158         if fields["cb_hide_gamemods"] ~= nil or
159                 fields["cb_hide_mpcontent"] ~= nil then
160                 local current = this.data.list:get_filtercriteria()
161
162                 if current == nil then
163                         current = {}
164                 end
165                 
166                 if fields["cb_hide_gamemods"] ~= nil then
167                         if core.is_yes(fields["cb_hide_gamemods"]) then
168                                 current.hide_game = true
169                                 this.data.hide_gamemods = true
170                                 core.setting_set("world_config_hide_gamemods", "true")
171                         else
172                                 current.hide_game = false
173                                 this.data.hide_gamemods = false
174                                 core.setting_set("world_config_hide_gamemods", "false")
175                         end
176                 end
177
178                 if fields["cb_hide_mpcontent"] ~= nil then
179                         if core.is_yes(fields["cb_hide_mpcontent"]) then
180                                 current.hide_modpackcontents = true
181                                 this.data.hide_modpackcontents = true
182                                 core.setting_set("world_config_hide_modpackcontents", "true")
183                         else
184                                 current.hide_modpackcontents = false
185                                 this.data.hide_modpackcontents = false
186                                 core.setting_set("world_config_hide_modpackcontents", "false")
187                         end
188                 end
189
190                 this.data.list:set_filtercriteria(current)
191                 return true
192         end
193
194         if fields["btn_config_world_save"] then
195
196                 local filename = this.data.worldspec.path ..
197                                 DIR_DELIM .. "world.mt"
198
199                 local worldfile = Settings(filename)
200                 local mods = worldfile:to_table()
201
202                 local rawlist = this.data.list:get_raw_list()
203
204                 local i,mod
205                 for i,mod in ipairs(rawlist) do
206                         if not mod.is_modpack and
207                                         mod.typ ~= "game_mod" then
208                                 if modname_valid(mod.name) then
209                                         worldfile:set("load_mod_"..mod.name, tostring(mod.enabled))
210                                 else
211                                         if mod.enabled then
212                                                 gamedata.errormessage = fgettext_ne("Failed to enable mod \"$1\" as it contains disallowed characters. Only chararacters [a-z0-9_] are allowed.", mod.name)
213                                         end
214                                 end
215                                 mods["load_mod_"..mod.name] = nil
216                         end
217                 end
218
219                 -- Remove mods that are not present anymore
220                 for key,value in pairs(mods) do
221                         if key:sub(1,9) == "load_mod_" then
222                                 worldfile:remove(key)
223                         end
224                 end
225
226                 if not worldfile:write() then
227                         core.log("error", "Failed to write world config file")
228                 end
229         
230                 this:delete()
231                 return true
232         end
233
234         if fields["btn_config_world_cancel"] then
235                 this:delete()
236                 return true
237         end
238
239         if fields.btn_enable_all_mods then
240                 local list = this.data.list:get_raw_list()
241
242                 for i = 1, #list do
243                         if list[i].typ ~= "game_mod" and not list[i].is_modpack then
244                                 list[i].enabled = true
245                         end
246                 end
247                 enabled_all = true
248                 return true
249         end
250         
251         if fields.btn_disable_all_mods then
252                 local list = this.data.list:get_raw_list()
253
254                 for i = 1, #list do
255                         if list[i].typ ~= "game_mod" and not list[i].is_modpack then
256                                 list[i].enabled = false
257                         end
258                 end
259                 enabled_all = false
260                 return true
261         end
262
263         return false
264 end
265
266 function create_configure_world_dlg(worldidx)
267
268         local dlg = dialog_create("sp_config_world",
269                                         get_formspec,
270                                         handle_buttons,
271                                         nil)
272
273         dlg.data.hide_gamemods = core.setting_getbool("world_config_hide_gamemods")
274         dlg.data.hide_modpackcontents = core.setting_getbool("world_config_hide_modpackcontents")
275         dlg.data.selected_mod = tonumber(core.setting_get("world_config_selected_mod"))
276         if dlg.data.selected_mod == nil then
277                 dlg.data.selected_mod = 0
278         end
279
280         dlg.data.worldspec = core.get_worlds()[worldidx]
281         if dlg.data.worldspec == nil then dlg:delete() return nil end
282
283         dlg.data.worldconfig = modmgr.get_worldconfig(dlg.data.worldspec.path)
284         
285         if dlg.data.worldconfig == nil or dlg.data.worldconfig.id == nil or
286                         dlg.data.worldconfig.id == "" then
287
288                 dlg:delete()
289                 return nil
290         end
291         
292         dlg.data.list = filterlist.create(
293                         modmgr.preparemodlist, --refresh
294                         modmgr.comparemod, --compare
295                         function(element,uid) --uid match
296                                         if element.name == uid then
297                                                 return true
298                                         end
299                                 end,
300                                 function(element,criteria)
301                                         if criteria.hide_game and
302                                                 element.typ == "game_mod" then
303                                                         return false
304                                         end
305
306                                         if criteria.hide_modpackcontents and
307                                                 element.modpack ~= nil then
308                                                         return false
309                                                 end
310                                         return true
311                                 end, --filter
312                                 { worldpath= dlg.data.worldspec.path,
313                                   gameid = dlg.data.worldspec.gameid }
314                         )
315
316
317         if dlg.data.selected_mod > dlg.data.list:size() then
318                 dlg.data.selected_mod = 0
319         end
320
321         dlg.data.list:set_filtercriteria(
322                 {
323                         hide_game=dlg.data.hide_gamemods,
324                         hide_modpackcontents= dlg.data.hide_modpackcontents
325                 })
326         dlg.data.list:add_sort_mechanism("alphabetic", sort_mod_list)
327         dlg.data.list:set_sortmode("alphabetic")
328
329         return dlg
330 end