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