]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/tab_content.lua
Add spider
[dragonfireclient.git] / builtin / mainmenu / tab_content.lua
1 --Minetest
2 --Copyright (C) 2014 sapier
3 --Copyright (C) 2018 rubenwardy <rw@rubenwardy.com>
4 --
5 --This program is free software; you can redistribute it and/or modify
6 --it under the terms of the GNU Lesser General Public License as published by
7 --the Free Software Foundation; either version 2.1 of the License, or
8 --(at your option) any later version.
9 --
10 --This program is distributed in the hope that it will be useful,
11 --but WITHOUT ANY WARRANTY; without even the implied warranty of
12 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 --GNU Lesser General Public License for more details.
14 --
15 --You should have received a copy of the GNU Lesser General Public License along
16 --with this program; if not, write to the Free Software Foundation, Inc.,
17 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 local packages_raw
20 local packages
21
22 local function modname_valid(name)
23         return not name:find("[^a-z0-9_]")
24 end
25
26 --------------------------------------------------------------------------------
27 local function get_formspec(tabview, name, tabdata)
28         if pkgmgr.global_mods == nil then
29                 pkgmgr.refresh_globals()
30         end
31         if pkgmgr.games == nil then
32                 pkgmgr.update_gamelist()
33         end
34
35         if packages == nil then
36                 packages_raw = {}
37                 table.insert_all(packages_raw, pkgmgr.games)
38                 table.insert_all(packages_raw, pkgmgr.get_texture_packs())
39                 table.insert_all(packages_raw, pkgmgr.global_mods:get_list())
40                 table.insert_all(packages_raw, pkgmgr.clientmods:get_list())
41
42                 local function get_data()
43                         return packages_raw
44                 end
45
46                 local function is_equal(element, uid) --uid match
47                         return (element.type == "game" and element.id == uid) or
48                                         element.name == uid
49                 end
50
51                 packages = filterlist.create(get_data, pkgmgr.compare_package,
52                                 is_equal, nil, {})
53
54                 local filename = core.get_clientmodpath() .. DIR_DELIM .. "mods.conf"
55
56                 local conffile = Settings(filename)
57                 local mods = conffile:to_table()
58
59                 for i = 1, #packages_raw do
60                         local mod = packages_raw[i]
61                         if mod.is_clientside and not mod.is_modpack then
62                                 if modname_valid(mod.name) then
63                                         conffile:set("load_mod_" .. mod.name,
64                                                 mod.enabled and "true" or "false")
65                                 elseif mod.enabled then
66                                         gamedata.errormessage = fgettext_ne("Failed to enable clientmo" ..
67                                                 "d \"$1\" as it contains disallowed characters. " ..
68                                                 "Only characters [a-z0-9_] are allowed.",
69                                                 mod.name)
70                                 end
71                                 mods["load_mod_" .. mod.name] = nil
72                         end
73                 end
74
75                 -- Remove mods that are not present anymore
76                 for key in pairs(mods) do
77                         if key:sub(1, 9) == "load_mod_" then
78                                 conffile:remove(key)
79                         end
80                 end
81
82                 if not conffile:write() then
83                         core.log("error", "Failed to write clientmod config file")
84                 end
85         end
86
87         if tabdata.selected_pkg == nil then
88                 tabdata.selected_pkg = 1
89         end
90
91
92         local retval =
93                 "label[0.05,-0.25;".. fgettext("Installed Packages:") .. "]" ..
94                 "tablecolumns[color;tree;text]" ..
95                 "table[0,0.25;5.1,4.3;pkglist;" ..
96                 pkgmgr.render_packagelist(packages) ..
97                 ";" .. tabdata.selected_pkg .. "]" ..
98                 "button[0,4.85;5.25,0.5;btn_contentdb;".. fgettext("Browse online content") .. "]"
99
100
101         local selected_pkg
102         if filterlist.size(packages) >= tabdata.selected_pkg then
103                 selected_pkg = packages:get_list()[tabdata.selected_pkg]
104         end
105
106         if selected_pkg ~= nil then
107                 --check for screenshot beeing available
108                 local screenshotfilename = selected_pkg.path .. DIR_DELIM .. "screenshot.png"
109                 local screenshotfile, error = io.open(screenshotfilename, "r")
110
111                 local modscreenshot
112                 if error == nil then
113                         screenshotfile:close()
114                         modscreenshot = screenshotfilename
115                 end
116
117                 if modscreenshot == nil then
118                                 modscreenshot = defaulttexturedir .. "no_screenshot.png"
119                 end
120
121                 local info = core.get_content_info(selected_pkg.path)
122                 local desc = fgettext("No package description available")
123                 if info.description and info.description:trim() ~= "" then
124                         desc = info.description
125                 end
126
127                 retval = retval ..
128                                 "image[5.5,0;3,2;" .. core.formspec_escape(modscreenshot) .. "]" ..
129                                 "label[8.25,0.6;" .. core.formspec_escape(selected_pkg.name) .. "]" ..
130                                 "box[5.5,2.2;6.15,2.35;#000]"
131
132                 if selected_pkg.type == "mod" then
133                         if selected_pkg.is_modpack then
134                                 if selected_pkg.is_clientside then
135                                         if pkgmgr.is_modpack_entirely_enabled({list = packages}, selected_pkg.name) then
136                                                 retval = retval ..
137                                                         "button[8.65,4.65;3.25,1;btn_mod_mgr_mp_disable;" ..
138                                                         fgettext("Disable modpack") .. "]"
139                                         else
140                                                 retval = retval ..
141                                                         "button[8.65,4.65;3.25,1;btn_mod_mgr_mp_enable;" ..
142                                                         fgettext("Enable modpack") .. "]"
143                                         end
144                                 else
145                                         retval = retval ..
146                                                 "button[8.65,4.65;3.25,1;btn_mod_mgr_rename_modpack;" ..
147                                                 fgettext("Rename") .. "]"
148                                 end
149                         else
150                                 --show dependencies
151                                 desc = desc .. "\n\n"
152                                 local toadd_hard = table.concat(info.depends or {}, "\n")
153                                 local toadd_soft = table.concat(info.optional_depends or {}, "\n")
154                                 if toadd_hard == "" and toadd_soft == "" then
155                                         desc = desc .. fgettext("No dependencies.")
156                                 else
157                                         if toadd_hard ~= "" then
158                                                 desc = desc ..fgettext("Dependencies:") ..
159                                                         "\n" .. toadd_hard
160                                         end
161                                         if toadd_soft ~= "" then
162                                                 if toadd_hard ~= "" then
163                                                         desc = desc .. "\n\n"
164                                                 end
165                                                 desc = desc .. fgettext("Optional dependencies:") ..
166                                                         "\n" .. toadd_soft
167                                         end
168                                 end
169                                 if selected_pkg.is_clientside then
170                                         if selected_pkg.enabled then
171                                                 retval = retval ..
172                                                         "button[8.65,4.65;3.25,1;btn_mod_mgr_disable_mod;" ..
173                                                         fgettext("Disable") .. "]"
174                                         else
175                                                 retval = retval ..
176                                                         "button[8.65,4.65;3.25,1;btn_mod_mgr_enable_mod;" ..
177                                                         fgettext("Enable") .. "]"
178                                         end
179                                 end
180                         end
181
182                 else
183                         if selected_pkg.type == "txp" then
184                                 if selected_pkg.enabled then
185                                         retval = retval ..
186                                                 "button[8.65,4.65;3.25,1;btn_mod_mgr_disable_txp;" ..
187                                                 fgettext("Disable Texture Pack") .. "]"
188                                 else
189                                         retval = retval ..
190                                                 "button[8.65,4.65;3.25,1;btn_mod_mgr_use_txp;" ..
191                                                 fgettext("Use Texture Pack") .. "]"
192                                 end
193                         end
194                 end
195
196                 retval = retval .. "textarea[5.85,2.2;6.35,2.9;;" ..
197                         fgettext("Information:") .. ";" .. desc .. "]"
198
199                 if core.may_modify_path(selected_pkg.path) then
200                         retval = retval ..
201                                 "button[5.5,4.65;3.25,1;btn_mod_mgr_delete_mod;" ..
202                                 fgettext("Uninstall Package") .. "]"
203                 end
204         end
205         return retval
206 end
207
208 --------------------------------------------------------------------------------
209 local function handle_doubleclick(pkg, pkg_name)
210         if pkg.type == "txp" then
211                 if core.settings:get("texture_path") == pkg.path then
212                         core.settings:set("texture_path", "")
213                 else
214                         core.settings:set("texture_path", pkg.path)
215                 end
216                 packages = nil
217         elseif pkg.is_clientside then
218                 pkgmgr.enable_mod({data = {list = packages, selected_mod = pkg_name}})
219                 packages = nil
220         end
221 end
222
223 --------------------------------------------------------------------------------
224 local function handle_buttons(tabview, fields, tabname, tabdata)
225         if fields["pkglist"] ~= nil then
226                 local event = core.explode_table_event(fields["pkglist"])
227                 tabdata.selected_pkg = event.row
228                 if event.type == "DCL" then
229                         handle_doubleclick(packages:get_list()[tabdata.selected_pkg], tabdata.selected_pkg)
230                 end
231                 return true
232         end
233
234         if fields.btn_mod_mgr_mp_enable ~= nil or
235                         fields.btn_mod_mgr_mp_disable ~= nil then
236                 pkgmgr.enable_mod({data = {list = packages, selected_mod = tabdata.selected_pkg}},
237                         fields.btn_mod_mgr_mp_enable ~= nil)
238                 packages = nil
239                 return true
240         end
241
242         if fields.btn_mod_mgr_enable_mod ~= nil or
243                         fields.btn_mod_mgr_disable_mod ~= nil then
244                 pkgmgr.enable_mod({data = {list = packages, selected_mod = tabdata.selected_pkg}},
245                         fields.btn_mod_mgr_enable_mod ~= nil)
246                 packages = nil
247                 return true
248         end
249
250         if fields["btn_contentdb"] ~= nil then
251                 local dlg = create_store_dlg()
252                 dlg:set_parent(tabview)
253                 tabview:hide()
254                 dlg:show()
255                 packages = nil
256                 return true
257         end
258
259         if fields["btn_mod_mgr_rename_modpack"] ~= nil then
260                 local mod = packages:get_list()[tabdata.selected_pkg]
261                 local dlg_renamemp = create_rename_modpack_dlg(mod)
262                 dlg_renamemp:set_parent(tabview)
263                 tabview:hide()
264                 dlg_renamemp:show()
265                 packages = nil
266                 return true
267         end
268
269         if fields["btn_mod_mgr_delete_mod"] ~= nil then
270                 local mod = packages:get_list()[tabdata.selected_pkg]
271                 local dlg_delmod = create_delete_content_dlg(mod)
272                 dlg_delmod:set_parent(tabview)
273                 tabview:hide()
274                 dlg_delmod:show()
275                 packages = nil
276                 return true
277         end
278
279         if fields.btn_mod_mgr_use_txp then
280                 local txp = packages:get_list()[tabdata.selected_pkg]
281                 core.settings:set("texture_path", txp.path)
282                 packages = nil
283                 return true
284         end
285
286
287         if fields.btn_mod_mgr_disable_txp then
288                 core.settings:set("texture_path", "")
289                 packages = nil
290                 return true
291         end
292
293         return false
294 end
295
296 --------------------------------------------------------------------------------
297 return {
298         name = "content",
299         caption = fgettext("Content"),
300         cbf_formspec = get_formspec,
301         cbf_button_handler = handle_buttons,
302         on_change = pkgmgr.update_gamelist
303 }