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