]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/dlg_contentstore.lua
CheatDB Support & Enable/Disable CSMs in Main Menu
[dragonfireclient.git] / builtin / mainmenu / dlg_contentstore.lua
1 --Minetest
2 --Copyright (C) 2018-20 rubenwardy
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 if not minetest.get_http_api then
19         function create_store_dlg()
20                 return messagebox("store",
21                                 fgettext("ContentDB is not available when Minetest was compiled without cURL"))
22         end
23         return
24 end
25
26 local store = { packages = {}, packages_full = {} }
27
28 local http = minetest.get_http_api()
29
30 -- Screenshot
31 local screenshot_dir = core.get_cache_path() .. DIR_DELIM .. "cdb"
32 assert(core.create_dir(screenshot_dir))
33 local screenshot_downloading = {}
34 local screenshot_downloaded = {}
35
36 -- Filter
37 local search_string = ""
38 local cur_page = 1
39 local num_per_page = 5
40 local filter_type = 1
41 local filter_types_titles = {
42         fgettext("All packages"),
43 --      fgettext("Games"),
44         fgettext("Clientmods"),
45         fgettext("Texture packs"),
46 }
47
48 local number_downloading = 0
49 local download_queue = {}
50
51 local filter_types_type = {
52         nil,
53 --      "game",
54         "mod",
55         "txp",
56 }
57
58
59 local function download_package(param)
60         if core.download_file(param.package.url, param.filename) then
61                 return {
62                         filename = param.filename,
63                         successful = true,
64                 }
65         else
66                 core.log("error", "downloading " .. dump(param.package.url) .. " failed")
67                 return {
68                         successful = false,
69                 }
70         end
71 end
72
73 local function start_install(package)
74         local params = {
75                 package = package,
76                 filename = os.tempfolder() .. "_MODNAME_" .. package.name .. ".zip",
77         }
78
79         number_downloading = number_downloading + 1
80
81         local function callback(result)
82                 if result.successful then
83                         local path, msg = pkgmgr.install(package.type,
84                                         result.filename, package.name,
85                                         package.path)
86                         if not path then
87                                 gamedata.errormessage = msg
88                         else
89                                 core.log("action", "Installed package to " .. path)
90
91                                 local conf_path
92                                 local name_is_title = false
93                                 if package.type == "mod" then
94                                         local actual_type = pkgmgr.get_folder_type(path)
95                                         if actual_type.type == "modpack" then
96                                                 conf_path = path .. DIR_DELIM .. "modpack.conf"
97                                         else
98                                                 conf_path = path .. DIR_DELIM .. "mod.conf"
99                                         end
100                                 elseif package.type == "game" then
101                                         conf_path = path .. DIR_DELIM .. "game.conf"
102                                         name_is_title = true
103                                 elseif package.type == "txp" then
104                                         conf_path = path .. DIR_DELIM .. "texture_pack.conf"
105                                 end
106
107                                 if conf_path then
108                                         local conf = Settings(conf_path)
109                                         if name_is_title then
110                                                 conf:set("name",   package.title)
111                                         else
112                                                 conf:set("title",  package.title)
113                                                 conf:set("name",   package.name)
114                                         end
115                                         if not conf:get("description") then
116                                                 conf:set("description", package.short_description)
117                                         end
118                                         conf:set("author",     package.author)
119                                         conf:set("release",    package.release)
120                                         conf:write()
121                                 end
122                         end
123                         os.remove(result.filename)
124                 else
125                         gamedata.errormessage = fgettext("Failed to download $1", package.name)
126                 end
127
128                 package.downloading = false
129
130                 number_downloading = number_downloading - 1
131
132                 local next = download_queue[1]
133                 if next then
134                         table.remove(download_queue, 1)
135
136                         start_install(next)
137                 end
138
139                 ui.update()
140         end
141
142         package.queued = false
143         package.downloading = true
144
145         if not core.handle_async(download_package, params, callback) then
146                 core.log("error", "ERROR: async event failed")
147                 gamedata.errormessage = fgettext("Failed to download $1", package.name)
148                 return
149         end
150 end
151
152 local function queue_download(package)
153         local max_concurrent_downloads = tonumber(minetest.settings:get("contentdb_max_concurrent_downloads"))
154         if number_downloading < max_concurrent_downloads then
155                 start_install(package)
156         else
157                 table.insert(download_queue, package)
158                 package.queued = true
159         end
160 end
161
162 local function get_file_extension(path)
163         local parts = path:split(".")
164         return parts[#parts]
165 end
166
167 local function get_screenshot(package)
168         if not package.thumbnail then
169                 return defaulttexturedir .. "no_screenshot.png"
170         elseif screenshot_downloading[package.thumbnail] then
171                 return defaulttexturedir .. "loading_screenshot.png"
172         end
173
174         -- Get tmp screenshot path
175         local ext = get_file_extension(package.thumbnail)
176         local filepath = screenshot_dir .. DIR_DELIM ..
177                 ("%s-%s-%s.%s"):format(package.type, package.author, package.name, ext)
178
179         -- Return if already downloaded
180         local file = io.open(filepath, "r")
181         if file then
182                 file:close()
183                 return filepath
184         end
185
186         -- Show error if we've failed to download before
187         if screenshot_downloaded[package.thumbnail] then
188                 return defaulttexturedir .. "error_screenshot.png"
189         end
190
191         -- Download
192
193         local function download_screenshot(params)
194                 return core.download_file(params.url, params.dest)
195         end
196         local function callback(success)
197                 screenshot_downloading[package.thumbnail] = nil
198                 screenshot_downloaded[package.thumbnail] = true
199                 if not success then
200                         core.log("warning", "Screenshot download failed for some reason")
201                 end
202                 ui.update()
203         end
204         if core.handle_async(download_screenshot,
205                         { dest = filepath, url = package.thumbnail }, callback) then
206                 screenshot_downloading[package.thumbnail] = true
207         else
208                 core.log("error", "ERROR: async event failed")
209                 return defaulttexturedir .. "error_screenshot.png"
210         end
211
212         return defaulttexturedir .. "loading_screenshot.png"
213 end
214
215 function store.load()
216         local version = core.get_version()
217         local base_url = core.settings:get("contentdb_url")
218         local url = base_url ..
219                 "/api/packages/?type=mod&type=game&type=txp&protocol_version=" ..
220                 core.get_max_supp_proto() .. "&engine_version=" .. version.string
221
222         for _, item in pairs(core.settings:get("contentdb_flag_blacklist"):split(",")) do
223                 item = item:trim()
224                 if item ~= "" then
225                         url = url .. "&hide=" .. item
226                 end
227         end
228
229         local timeout = tonumber(minetest.settings:get("curl_file_download_timeout"))
230         local response = http.fetch_sync({ url = url, timeout = timeout })
231         if not response.succeeded then
232                 return
233         end
234
235         store.packages_full = core.parse_json(response.data) or {}
236
237         for _, package in pairs(store.packages_full) do
238                 package.url = base_url .. "/packages/" ..
239                                 package.author .. "/" .. package.name ..
240                                 "/releases/" .. package.release .. "/download/"
241
242                 local name_len = #package.name
243                 if package.type == "game" and name_len > 5 and package.name:sub(name_len - 4) == "_game" then
244                         package.id = package.author:lower() .. "/" .. package.name:sub(1, name_len - 5)
245                 else
246                         package.id = package.author:lower() .. "/" .. package.name
247                 end
248         end
249
250         store.packages = store.packages_full
251         store.loaded = true
252 end
253
254 function store.update_paths()
255         local mod_hash = {}
256         pkgmgr.refresh_globals()
257         for _, mod in pairs(pkgmgr.clientmods:get_list()) do
258                 if mod.author then
259                         mod_hash[mod.author:lower() .. "/" .. mod.name] = mod
260                 end
261         end
262
263         local game_hash = {}
264         pkgmgr.update_gamelist()
265         for _, game in pairs(pkgmgr.games) do
266                 if game.author ~= "" then
267                         game_hash[game.author:lower() .. "/" .. game.id] = game
268                 end
269         end
270
271         local txp_hash = {}
272         for _, txp in pairs(pkgmgr.get_texture_packs()) do
273                 if txp.author then
274                         txp_hash[txp.author:lower() .. "/" .. txp.name] = txp
275                 end
276         end
277
278         for _, package in pairs(store.packages_full) do
279                 local content
280                 if package.type == "mod" then
281                         content = mod_hash[package.id]
282                 elseif package.type == "game" then
283                         content = game_hash[package.id]
284                 elseif package.type == "txp" then
285                         content = txp_hash[package.id]
286                 end
287
288                 if content then
289                         package.path = content.path
290                         package.installed_release = content.release or 0
291                 else
292                         package.path = nil
293                 end
294         end
295 end
296
297 function store.filter_packages(query)
298         if query == "" and filter_type == 1 then
299                 store.packages = store.packages_full
300                 return
301         end
302
303         local keywords = {}
304         for word in query:lower():gmatch("%S+") do
305                 table.insert(keywords, word)
306         end
307
308         local function matches_keywords(package)
309                 for k = 1, #keywords do
310                         local keyword = keywords[k]
311
312                         if string.find(package.name:lower(), keyword, 1, true) or
313                                         string.find(package.title:lower(), keyword, 1, true) or
314                                         string.find(package.author:lower(), keyword, 1, true) or
315                                         string.find(package.short_description:lower(), keyword, 1, true) then
316                                 return true
317                         end
318                 end
319
320                 return false
321         end
322
323         store.packages = {}
324         for _, package in pairs(store.packages_full) do
325                 if (query == "" or matches_keywords(package)) and
326                                 (filter_type == 1 or package.type == filter_types_type[filter_type]) then
327                         store.packages[#store.packages + 1] = package
328                 end
329         end
330
331 end
332
333 function store.get_formspec(dlgdata)
334         store.update_paths()
335
336         dlgdata.pagemax = math.max(math.ceil(#store.packages / num_per_page), 1)
337         if cur_page > dlgdata.pagemax then
338                 cur_page = 1
339         end
340
341         local W = 15.75
342         local H = 9.5
343
344         local formspec
345         if #store.packages_full > 0 then
346                 formspec = {
347                         "formspec_version[3]",
348                         "size[15.75,9.5]",
349                         "position[0.5,0.55]",
350
351                         "style[status;border=false]",
352
353                         "container[0.375,0.375]",
354                         "field[0,0;7.225,0.8;search_string;;", core.formspec_escape(search_string), "]",
355                         "field_close_on_enter[search_string;false]",
356                         "button[7.225,0;2,0.8;search;", fgettext("Search"), "]",
357                         "dropdown[9.6,0;2.4,0.8;type;", table.concat(filter_types_titles, ","), ";", filter_type, "]",
358                         "container_end[]",
359
360                         -- Page nav buttons
361                         "container[0,", H - 0.8 - 0.375, "]",
362                         "button[0.375,0;4,0.8;back;", fgettext("Back to Main Menu"), "]",
363
364                         "container[", W - 0.375 - 0.8*4 - 2,  ",0]",
365                         "image_button[0,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "start_icon.png;pstart;]",
366                         "image_button[0.8,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "prev_icon.png;pback;]",
367                         "style[pagenum;border=false]",
368                         "button[1.6,0;2,0.8;pagenum;", tonumber(cur_page), " / ", tonumber(dlgdata.pagemax), "]",
369                         "image_button[3.6,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "next_icon.png;pnext;]",
370                         "image_button[4.4,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "end_icon.png;pend;]",
371                         "container_end[]",
372
373                         "container_end[]",
374                 }
375
376                 if number_downloading > 0 then
377                         formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;status;"
378                         if #download_queue > 0 then
379                                 formspec[#formspec + 1] = fgettext("$1 downloading,\n$2 queued", number_downloading, #download_queue)
380                         else
381                                 formspec[#formspec + 1] = fgettext("$1 downloading...", number_downloading)
382                         end
383                         formspec[#formspec + 1] = "]"
384                 else
385                         local num_avail_updates = 0
386                         for i=1, #store.packages_full do
387                                 local package = store.packages_full[i]
388                                 if package.path and package.installed_release < package.release and
389                                                 not (package.downloading or package.queued) then
390                                         num_avail_updates = num_avail_updates + 1
391                                 end
392                         end
393
394                         if num_avail_updates == 0 then
395                                 formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;status;"
396                                 formspec[#formspec + 1] = fgettext("No updates")
397                                 formspec[#formspec + 1] = "]"
398                         else
399                                 formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;update_all;"
400                                 formspec[#formspec + 1] = fgettext("Update All [$1]", num_avail_updates)
401                                 formspec[#formspec + 1] = "]"
402                         end
403                 end
404
405                 if #store.packages == 0 then
406                         formspec[#formspec + 1] = "label[4,3;"
407                         formspec[#formspec + 1] = fgettext("No results")
408                         formspec[#formspec + 1] = "]"
409                 end
410         else
411                 formspec = {
412                         "size[12,7]",
413                         "position[0.5,0.55]",
414                         "label[4,3;", fgettext("No packages could be retrieved"), "]",
415                         "container[0,", H - 0.8 - 0.375, "]",
416                         "button[0,0;4,0.8;back;", fgettext("Back to Main Menu"), "]",
417                         "container_end[]",
418                 }
419         end
420
421         local start_idx = (cur_page - 1) * num_per_page + 1
422         for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
423                 local package = store.packages[i]
424                 formspec[#formspec + 1] = "container[0.375,"
425                 formspec[#formspec + 1] = (i - start_idx) * 1.375 + (2*0.375 + 0.8)
426                 formspec[#formspec + 1] = "]"
427
428                 -- image
429                 formspec[#formspec + 1] = "image[0,0;1.5,1;"
430                 formspec[#formspec + 1] = core.formspec_escape(get_screenshot(package))
431                 formspec[#formspec + 1] = "]"
432
433                 -- title
434                 formspec[#formspec + 1] = "label[1.875,0.1;"
435                 formspec[#formspec + 1] = core.formspec_escape(
436                                 minetest.colorize(mt_color_green, package.title) ..
437                                 minetest.colorize("#BFBFBF", " by " .. package.author))
438                 formspec[#formspec + 1] = "]"
439
440                 -- buttons
441                 local description_width = W - 0.375*5 - 1 - 2*1.5
442                 formspec[#formspec + 1] = "container["
443                 formspec[#formspec + 1] = W - 0.375*2
444                 formspec[#formspec + 1] = ",0.1]"
445
446                 if package.downloading then
447                         formspec[#formspec + 1] = "button[-3.5,0;2,0.8;status;"
448                         formspec[#formspec + 1] = fgettext("Downloading...")
449                         formspec[#formspec + 1] = "]"
450                 elseif package.queued then
451                         formspec[#formspec + 1] = "button[-3.5,0;2,0.8;status;"
452                         formspec[#formspec + 1] = fgettext("Queued")
453                         formspec[#formspec + 1] = "]"
454                 elseif not package.path then
455                         formspec[#formspec + 1] = "button[-3,0;1.5,0.8;install_"
456                         formspec[#formspec + 1] = tostring(i)
457                         formspec[#formspec + 1] = ";"
458                         formspec[#formspec + 1] = fgettext("Install")
459                         formspec[#formspec + 1] = "]"
460                 else
461                         if package.installed_release < package.release then
462                                 description_width = description_width - 1.5
463
464                                 -- The install_ action also handles updating
465                                 formspec[#formspec + 1] = "button[-4.5,0;1.5,0.8;install_"
466                                 formspec[#formspec + 1] = tostring(i)
467                                 formspec[#formspec + 1] = ";"
468                                 formspec[#formspec + 1] = fgettext("Update")
469                                 formspec[#formspec + 1] = "]"
470                         end
471
472                         formspec[#formspec + 1] = "button[-3,0;1.5,0.8;uninstall_"
473                         formspec[#formspec + 1] = tostring(i)
474                         formspec[#formspec + 1] = ";"
475                         formspec[#formspec + 1] = fgettext("Uninstall")
476                         formspec[#formspec + 1] = "]"
477                 end
478
479                 formspec[#formspec + 1] = "button[-1.5,0;1.5,0.8;view_"
480                 formspec[#formspec + 1] = tostring(i)
481                 formspec[#formspec + 1] = ";"
482                 formspec[#formspec + 1] = fgettext("View")
483                 formspec[#formspec + 1] = "]"
484                 formspec[#formspec + 1] = "container_end[]"
485
486                 -- description
487                 formspec[#formspec + 1] = "textarea[1.855,0.3;"
488                 formspec[#formspec + 1] = tostring(description_width)
489                 formspec[#formspec + 1] = ",0.8;;;"
490                 formspec[#formspec + 1] = core.formspec_escape(package.short_description)
491                 formspec[#formspec + 1] = "]"
492
493                 formspec[#formspec + 1] = "container_end[]"
494         end
495
496         return table.concat(formspec, "")
497 end
498
499 function store.handle_submit(this, fields)
500         if fields.search or fields.key_enter_field == "search_string" then
501                 search_string = fields.search_string:trim()
502                 cur_page = 1
503                 store.filter_packages(search_string)
504                 return true
505         end
506
507         if fields.back then
508                 this:delete()
509                 return true
510         end
511
512         if fields.pstart then
513                 cur_page = 1
514                 return true
515         end
516
517         if fields.pend then
518                 cur_page = this.data.pagemax
519                 return true
520         end
521
522         if fields.pnext then
523                 cur_page = cur_page + 1
524                 if cur_page > this.data.pagemax then
525                         cur_page = 1
526                 end
527                 return true
528         end
529
530         if fields.pback then
531                 if cur_page == 1 then
532                         cur_page = this.data.pagemax
533                 else
534                         cur_page = cur_page - 1
535                 end
536                 return true
537         end
538
539         if fields.type then
540                 local new_type = table.indexof(filter_types_titles, fields.type)
541                 if new_type ~= filter_type then
542                         filter_type = new_type
543                         store.filter_packages(search_string)
544                         return true
545                 end
546         end
547
548         if fields.update_all then
549                 for i=1, #store.packages_full do
550                         local package = store.packages_full[i]
551                         if package.path and package.installed_release < package.release and
552                                         not (package.downloading or package.queued) then
553                                 queue_download(package)
554                         end
555                 end
556                 return true
557         end
558
559         local start_idx = (cur_page - 1) * num_per_page + 1
560         assert(start_idx ~= nil)
561         for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
562                 local package = store.packages[i]
563                 assert(package)
564
565                 if fields["install_" .. i] then
566                         queue_download(package)
567                         return true
568                 end
569
570                 if fields["uninstall_" .. i] then
571                         local dlg_delmod = create_delete_content_dlg(package)
572                         dlg_delmod:set_parent(this)
573                         this:hide()
574                         dlg_delmod:show()
575                         return true
576                 end
577
578                 if fields["view_" .. i] then
579                         local url = ("%s/packages/%s/%s?protocol_version=%d"):format(
580                                         core.settings:get("contentdb_url"),
581                                         package.author, package.name, core.get_max_supp_proto())
582                         core.open_url(url)
583                         return true
584                 end
585         end
586
587         return false
588 end
589
590 function create_store_dlg(type)
591         if not store.loaded or #store.packages_full == 0 then
592                 store.load()
593         end
594
595         search_string = ""
596         cur_page = 1
597
598         if type then
599                 -- table.indexof does not work on tables that contain `nil`
600                 for i, v in pairs(filter_types_type) do
601                         if v == type then
602                                 filter_type = i
603                                 break
604                         end
605                 end
606         end
607
608         store.filter_packages(search_string)
609
610         return dialog_create("store",
611                         store.get_formspec,
612                         store.handle_submit,
613                         nil)
614 end