]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/dlg_contentstore.lua
Replace 'minetest.' with 'core.' in builtin
[minetest.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 core.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 -- Unordered preserves the original order of the ContentDB API,
27 -- before the package list is ordered based on installed state.
28 local store = { packages = {}, packages_full = {}, packages_full_unordered = {} }
29
30 local http = core.get_http_api()
31
32 -- Screenshot
33 local screenshot_dir = core.get_cache_path() .. DIR_DELIM .. "cdb"
34 assert(core.create_dir(screenshot_dir))
35 local screenshot_downloading = {}
36 local screenshot_downloaded = {}
37
38 -- Filter
39 local search_string = ""
40 local cur_page = 1
41 local num_per_page = 5
42 local filter_type = 1
43 local filter_types_titles = {
44         fgettext("All packages"),
45         fgettext("Games"),
46         fgettext("Mods"),
47         fgettext("Texture packs"),
48 }
49
50 local number_downloading = 0
51 local download_queue = {}
52
53 local filter_types_type = {
54         nil,
55         "game",
56         "mod",
57         "txp",
58 }
59
60
61 local function download_package(param)
62         if core.download_file(param.package.url, param.filename) then
63                 return {
64                         filename = param.filename,
65                         successful = true,
66                 }
67         else
68                 core.log("error", "downloading " .. dump(param.package.url) .. " failed")
69                 return {
70                         successful = false,
71                 }
72         end
73 end
74
75 local function start_install(package)
76         local params = {
77                 package = package,
78                 filename = os.tempfolder() .. "_MODNAME_" .. package.name .. ".zip",
79         }
80
81         number_downloading = number_downloading + 1
82
83         local function callback(result)
84                 if result.successful then
85                         local path, msg = pkgmgr.install(package.type,
86                                         result.filename, package.name,
87                                         package.path)
88                         if not path then
89                                 gamedata.errormessage = msg
90                         else
91                                 core.log("action", "Installed package to " .. path)
92
93                                 local conf_path
94                                 local name_is_title = false
95                                 if package.type == "mod" then
96                                         local actual_type = pkgmgr.get_folder_type(path)
97                                         if actual_type.type == "modpack" then
98                                                 conf_path = path .. DIR_DELIM .. "modpack.conf"
99                                         else
100                                                 conf_path = path .. DIR_DELIM .. "mod.conf"
101                                         end
102                                 elseif package.type == "game" then
103                                         conf_path = path .. DIR_DELIM .. "game.conf"
104                                         name_is_title = true
105                                 elseif package.type == "txp" then
106                                         conf_path = path .. DIR_DELIM .. "texture_pack.conf"
107                                 end
108
109                                 if conf_path then
110                                         local conf = Settings(conf_path)
111                                         if name_is_title then
112                                                 conf:set("name",   package.title)
113                                         else
114                                                 conf:set("title",  package.title)
115                                                 conf:set("name",   package.name)
116                                         end
117                                         if not conf:get("description") then
118                                                 conf:set("description", package.short_description)
119                                         end
120                                         conf:set("author",     package.author)
121                                         conf:set("release",    package.release)
122                                         conf:write()
123                                 end
124                         end
125                         os.remove(result.filename)
126                 else
127                         gamedata.errormessage = fgettext("Failed to download $1", package.name)
128                 end
129
130                 package.downloading = false
131
132                 number_downloading = number_downloading - 1
133
134                 local next = download_queue[1]
135                 if next then
136                         table.remove(download_queue, 1)
137
138                         start_install(next)
139                 end
140
141                 ui.update()
142         end
143
144         package.queued = false
145         package.downloading = true
146
147         if not core.handle_async(download_package, params, callback) then
148                 core.log("error", "ERROR: async event failed")
149                 gamedata.errormessage = fgettext("Failed to download $1", package.name)
150                 return
151         end
152 end
153
154 local function queue_download(package)
155         local max_concurrent_downloads = tonumber(core.settings:get("contentdb_max_concurrent_downloads"))
156         if number_downloading < max_concurrent_downloads then
157                 start_install(package)
158         else
159                 table.insert(download_queue, package)
160                 package.queued = true
161         end
162 end
163
164 local function get_raw_dependencies(package)
165         if package.raw_deps then
166                 return package.raw_deps
167         end
168
169         local url_fmt = "/api/packages/%s/dependencies/?only_hard=1&protocol_version=%s&engine_version=%s"
170         local version = core.get_version()
171         local base_url = core.settings:get("contentdb_url")
172         local url = base_url .. url_fmt:format(package.id, core.get_max_supp_proto(), version.string)
173
174         local response = http.fetch_sync({ url = url })
175         if not response.succeeded then
176                 return
177         end
178
179         local data = core.parse_json(response.data) or {}
180
181         local content_lookup = {}
182         for _, pkg in pairs(store.packages_full) do
183                 content_lookup[pkg.id] = pkg
184         end
185
186         for id, raw_deps in pairs(data) do
187                 local package2 = content_lookup[id:lower()]
188                 if package2 and not package2.raw_deps then
189                         package2.raw_deps = raw_deps
190
191                         for _, dep in pairs(raw_deps) do
192                                 local packages = {}
193                                 for i=1, #dep.packages do
194                                         packages[#packages + 1] = content_lookup[dep.packages[i]:lower()]
195                                 end
196                                 dep.packages = packages
197                         end
198                 end
199         end
200
201         return package.raw_deps
202 end
203
204 local function has_hard_deps(raw_deps)
205         for i=1, #raw_deps do
206                 if not raw_deps[i].is_optional then
207                         return true
208                 end
209         end
210
211         return false
212 end
213
214 -- Recursively resolve dependencies, given the installed mods
215 local function resolve_dependencies_2(raw_deps, installed_mods, out)
216         local function resolve_dep(dep)
217                 -- Check whether it's already installed
218                 if installed_mods[dep.name] then
219                         return {
220                                 is_optional = dep.is_optional,
221                                 name = dep.name,
222                                 installed = true,
223                         }
224                 end
225
226                 -- Find exact name matches
227                 local fallback
228                 for _, package in pairs(dep.packages) do
229                         if package.type ~= "game" then
230                                 if package.name == dep.name then
231                                         return {
232                                                 is_optional = dep.is_optional,
233                                                 name = dep.name,
234                                                 installed = false,
235                                                 package = package,
236                                         }
237                                 elseif not fallback then
238                                         fallback = package
239                                 end
240                         end
241                 end
242
243                 -- Otherwise, find the first mod that fulfils it
244                 if fallback then
245                         return {
246                                 is_optional = dep.is_optional,
247                                 name = dep.name,
248                                 installed = false,
249                                 package = fallback,
250                         }
251                 end
252
253                 return {
254                         is_optional = dep.is_optional,
255                         name = dep.name,
256                         installed = false,
257                 }
258         end
259
260         for _, dep in pairs(raw_deps) do
261                 if not dep.is_optional and not out[dep.name] then
262                         local result  = resolve_dep(dep)
263                         out[dep.name] = result
264                         if result and result.package and not result.installed then
265                                 local raw_deps2 = get_raw_dependencies(result.package)
266                                 if raw_deps2 then
267                                         resolve_dependencies_2(raw_deps2, installed_mods, out)
268                                 end
269                         end
270                 end
271         end
272
273         return true
274 end
275
276 -- Resolve dependencies for a package, calls the recursive version.
277 local function resolve_dependencies(raw_deps, game)
278         assert(game)
279
280         local installed_mods = {}
281
282         local mods = {}
283         pkgmgr.get_game_mods(game, mods)
284         for _, mod in pairs(mods) do
285                 installed_mods[mod.name] = true
286         end
287
288         for _, mod in pairs(pkgmgr.global_mods:get_list()) do
289                 installed_mods[mod.name] = true
290         end
291
292         local out = {}
293         if not resolve_dependencies_2(raw_deps, installed_mods, out) then
294                 return nil
295         end
296
297         local retval = {}
298         for _, dep in pairs(out) do
299                 retval[#retval + 1] = dep
300         end
301
302         table.sort(retval, function(a, b)
303                 return a.name < b.name
304         end)
305
306         return retval
307 end
308
309 local install_dialog = {}
310 function install_dialog.get_formspec()
311         local package = install_dialog.package
312         local raw_deps = install_dialog.raw_deps
313         local will_install_deps = install_dialog.will_install_deps
314
315         local selected_game_idx = 1
316         local selected_gameid = core.settings:get("menu_last_game")
317         local games = table.copy(pkgmgr.games)
318         for i=1, #games do
319                 if selected_gameid and games[i].id == selected_gameid then
320                         selected_game_idx = i
321                 end
322
323                 games[i] = core.formspec_escape(games[i].name)
324         end
325
326         local selected_game = pkgmgr.games[selected_game_idx]
327         local deps_to_install = 0
328         local deps_not_found = 0
329
330         install_dialog.dependencies = resolve_dependencies(raw_deps, selected_game)
331         local formatted_deps = {}
332         for _, dep in pairs(install_dialog.dependencies) do
333                 formatted_deps[#formatted_deps + 1] = "#fff"
334                 formatted_deps[#formatted_deps + 1] = core.formspec_escape(dep.name)
335                 if dep.installed then
336                         formatted_deps[#formatted_deps + 1] = "#ccf"
337                         formatted_deps[#formatted_deps + 1] = fgettext("Already installed")
338                 elseif dep.package then
339                         formatted_deps[#formatted_deps + 1] = "#cfc"
340                         formatted_deps[#formatted_deps + 1] = fgettext("$1 by $2", dep.package.title, dep.package.author)
341                         deps_to_install = deps_to_install + 1
342                 else
343                         formatted_deps[#formatted_deps + 1] = "#f00"
344                         formatted_deps[#formatted_deps + 1] = fgettext("Not found")
345                         deps_not_found = deps_not_found + 1
346                 end
347         end
348
349         local message_bg = "#3333"
350         local message
351         if will_install_deps then
352                 message = fgettext("$1 and $2 dependencies will be installed.", package.title, deps_to_install)
353         else
354                 message = fgettext("$1 will be installed, and $2 dependencies will be skipped.", package.title, deps_to_install)
355         end
356         if deps_not_found > 0 then
357                 message = fgettext("$1 required dependencies could not be found.", deps_not_found) ..
358                                 " " .. fgettext("Please check that the base game is correct.", deps_not_found) ..
359                                 "\n" .. message
360                 message_bg = mt_color_orange
361         end
362
363         local formspec = {
364                 "formspec_version[3]",
365                 "size[7,7.85]",
366                 "style[title;border=false]",
367                 "box[0,0;7,0.5;#3333]",
368                 "button[0,0;7,0.5;title;", fgettext("Install $1", package.title) , "]",
369
370                 "container[0.375,0.70]",
371
372                 "label[0,0.25;", fgettext("Base Game:"), "]",
373                 "dropdown[2,0;4.25,0.5;gameid;", table.concat(games, ","), ";", selected_game_idx, "]",
374
375                 "label[0,0.8;", fgettext("Dependencies:"), "]",
376
377                 "tablecolumns[color;text;color;text]",
378                 "table[0,1.1;6.25,3;packages;", table.concat(formatted_deps, ","), "]",
379
380                 "container_end[]",
381
382                 "checkbox[0.375,5.1;will_install_deps;",
383                         fgettext("Install missing dependencies"), ";",
384                         will_install_deps and "true" or "false", "]",
385
386                 "box[0,5.4;7,1.2;", message_bg, "]",
387                 "textarea[0.375,5.5;6.25,1;;;", message, "]",
388
389                 "container[1.375,6.85]",
390                 "button[0,0;2,0.8;install_all;", fgettext("Install"), "]",
391                 "button[2.25,0;2,0.8;cancel;", fgettext("Cancel"), "]",
392                 "container_end[]",
393         }
394
395         return table.concat(formspec, "")
396 end
397
398 function install_dialog.handle_submit(this, fields)
399         if fields.cancel then
400                 this:delete()
401                 return true
402         end
403
404         if fields.will_install_deps ~= nil then
405                 install_dialog.will_install_deps = core.is_yes(fields.will_install_deps)
406                 return true
407         end
408
409         if fields.install_all then
410                 queue_download(install_dialog.package)
411
412                 if install_dialog.will_install_deps then
413                         for _, dep in pairs(install_dialog.dependencies) do
414                                 if not dep.is_optional and not dep.installed and dep.package then
415                                         queue_download(dep.package)
416                                 end
417                         end
418                 end
419
420                 this:delete()
421                 return true
422         end
423
424         if fields.gameid then
425                 for _, game in pairs(pkgmgr.games) do
426                         if game.name == fields.gameid then
427                                 core.settings:set("menu_last_game", game.id)
428                                 break
429                         end
430                 end
431                 return true
432         end
433
434         return false
435 end
436
437 function install_dialog.create(package, raw_deps)
438         install_dialog.dependencies = nil
439         install_dialog.package = package
440         install_dialog.raw_deps = raw_deps
441         install_dialog.will_install_deps = true
442         return dialog_create("install_dialog",
443                         install_dialog.get_formspec,
444                         install_dialog.handle_submit,
445                         nil)
446 end
447
448
449 local confirm_overwrite = {}
450 function confirm_overwrite.get_formspec()
451         local package = confirm_overwrite.package
452
453         return "size[11.5,4.5,true]" ..
454                         "label[2,2;" ..
455                         fgettext("\"$1\" already exists. Would you like to overwrite it?", package.name) .. "]"..
456                         "style[install;bgcolor=red]" ..
457                         "button[3.25,3.5;2.5,0.5;install;" .. fgettext("Overwrite") .. "]" ..
458                         "button[5.75,3.5;2.5,0.5;cancel;" .. fgettext("Cancel") .. "]"
459 end
460
461 function confirm_overwrite.handle_submit(this, fields)
462         if fields.cancel then
463                 this:delete()
464                 return true
465         end
466
467         if fields.install then
468                 this:delete()
469                 confirm_overwrite.callback()
470                 return true
471         end
472
473         return false
474 end
475
476 function confirm_overwrite.create(package, callback)
477         assert(type(package) == "table")
478         assert(type(callback) == "function")
479
480         confirm_overwrite.package = package
481         confirm_overwrite.callback = callback
482         return dialog_create("confirm_overwrite",
483                 confirm_overwrite.get_formspec,
484                 confirm_overwrite.handle_submit,
485                 nil)
486 end
487
488
489 local function get_file_extension(path)
490         local parts = path:split(".")
491         return parts[#parts]
492 end
493
494 local function get_screenshot(package)
495         if not package.thumbnail then
496                 return defaulttexturedir .. "no_screenshot.png"
497         elseif screenshot_downloading[package.thumbnail] then
498                 return defaulttexturedir .. "loading_screenshot.png"
499         end
500
501         -- Get tmp screenshot path
502         local ext = get_file_extension(package.thumbnail)
503         local filepath = screenshot_dir .. DIR_DELIM ..
504                 ("%s-%s-%s.%s"):format(package.type, package.author, package.name, ext)
505
506         -- Return if already downloaded
507         local file = io.open(filepath, "r")
508         if file then
509                 file:close()
510                 return filepath
511         end
512
513         -- Show error if we've failed to download before
514         if screenshot_downloaded[package.thumbnail] then
515                 return defaulttexturedir .. "error_screenshot.png"
516         end
517
518         -- Download
519
520         local function download_screenshot(params)
521                 return core.download_file(params.url, params.dest)
522         end
523         local function callback(success)
524                 screenshot_downloading[package.thumbnail] = nil
525                 screenshot_downloaded[package.thumbnail] = true
526                 if not success then
527                         core.log("warning", "Screenshot download failed for some reason")
528                 end
529                 ui.update()
530         end
531         if core.handle_async(download_screenshot,
532                         { dest = filepath, url = package.thumbnail }, callback) then
533                 screenshot_downloading[package.thumbnail] = true
534         else
535                 core.log("error", "ERROR: async event failed")
536                 return defaulttexturedir .. "error_screenshot.png"
537         end
538
539         return defaulttexturedir .. "loading_screenshot.png"
540 end
541
542 function store.load()
543         local version = core.get_version()
544         local base_url = core.settings:get("contentdb_url")
545         local url = base_url ..
546                 "/api/packages/?type=mod&type=game&type=txp&protocol_version=" ..
547                 core.get_max_supp_proto() .. "&engine_version=" .. version.string
548
549         for _, item in pairs(core.settings:get("contentdb_flag_blacklist"):split(",")) do
550                 item = item:trim()
551                 if item ~= "" then
552                         url = url .. "&hide=" .. item
553                 end
554         end
555
556         local timeout = tonumber(core.settings:get("curl_file_download_timeout"))
557         local response = http.fetch_sync({ url = url, timeout = timeout })
558         if not response.succeeded then
559                 return
560         end
561
562         store.packages_full = core.parse_json(response.data) or {}
563
564         for _, package in pairs(store.packages_full) do
565                 package.url = base_url .. "/packages/" ..
566                                 package.author .. "/" .. package.name ..
567                                 "/releases/" .. package.release .. "/download/"
568
569                 local name_len = #package.name
570                 if package.type == "game" and name_len > 5 and package.name:sub(name_len - 4) == "_game" then
571                         package.id = package.author:lower() .. "/" .. package.name:sub(1, name_len - 5)
572                 else
573                         package.id = package.author:lower() .. "/" .. package.name
574                 end
575         end
576
577         store.packages_full_unordered = store.packages_full
578         store.packages = store.packages_full
579         store.loaded = true
580 end
581
582 function store.update_paths()
583         local mod_hash = {}
584         pkgmgr.refresh_globals()
585         for _, mod in pairs(pkgmgr.global_mods:get_list()) do
586                 if mod.author and mod.release > 0 then
587                         mod_hash[mod.author:lower() .. "/" .. mod.name] = mod
588                 end
589         end
590
591         local game_hash = {}
592         pkgmgr.update_gamelist()
593         for _, game in pairs(pkgmgr.games) do
594                 if game.author ~= "" and game.release > 0 then
595                         game_hash[game.author:lower() .. "/" .. game.id] = game
596                 end
597         end
598
599         local txp_hash = {}
600         for _, txp in pairs(pkgmgr.get_texture_packs()) do
601                 if txp.author and txp.release > 0 then
602                         txp_hash[txp.author:lower() .. "/" .. txp.name] = txp
603                 end
604         end
605
606         for _, package in pairs(store.packages_full) do
607                 local content
608                 if package.type == "mod" then
609                         content = mod_hash[package.id]
610                 elseif package.type == "game" then
611                         content = game_hash[package.id]
612                 elseif package.type == "txp" then
613                         content = txp_hash[package.id]
614                 end
615
616                 if content then
617                         package.path = content.path
618                         package.installed_release = content.release or 0
619                 else
620                         package.path = nil
621                 end
622         end
623 end
624
625 function store.sort_packages()
626         local ret = {}
627
628         -- Add installed content
629         for i=1, #store.packages_full_unordered do
630                 local package = store.packages_full_unordered[i]
631                 if package.path then
632                         ret[#ret + 1] = package
633                 end
634         end
635
636         -- Sort installed content by title
637         table.sort(ret, function(a, b)
638                 return a.title < b.title
639         end)
640
641         -- Add uninstalled content
642         for i=1, #store.packages_full_unordered do
643                 local package = store.packages_full_unordered[i]
644                 if not package.path then
645                         ret[#ret + 1] = package
646                 end
647         end
648
649         store.packages_full = ret
650 end
651
652 function store.filter_packages(query)
653         if query == "" and filter_type == 1 then
654                 store.packages = store.packages_full
655                 return
656         end
657
658         local keywords = {}
659         for word in query:lower():gmatch("%S+") do
660                 table.insert(keywords, word)
661         end
662
663         local function matches_keywords(package)
664                 for k = 1, #keywords do
665                         local keyword = keywords[k]
666
667                         if string.find(package.name:lower(), keyword, 1, true) or
668                                         string.find(package.title:lower(), keyword, 1, true) or
669                                         string.find(package.author:lower(), keyword, 1, true) or
670                                         string.find(package.short_description:lower(), keyword, 1, true) then
671                                 return true
672                         end
673                 end
674
675                 return false
676         end
677
678         store.packages = {}
679         for _, package in pairs(store.packages_full) do
680                 if (query == "" or matches_keywords(package)) and
681                                 (filter_type == 1 or package.type == filter_types_type[filter_type]) then
682                         store.packages[#store.packages + 1] = package
683                 end
684         end
685 end
686
687 function store.get_formspec(dlgdata)
688         store.update_paths()
689
690         dlgdata.pagemax = math.max(math.ceil(#store.packages / num_per_page), 1)
691         if cur_page > dlgdata.pagemax then
692                 cur_page = 1
693         end
694
695         local W = 15.75
696         local H = 9.5
697         local formspec
698         if #store.packages_full > 0 then
699                 formspec = {
700                         "formspec_version[3]",
701                         "size[15.75,9.5]",
702                         "position[0.5,0.55]",
703
704                         "style[status,downloading,queued;border=false]",
705
706                         "container[0.375,0.375]",
707                         "field[0,0;7.225,0.8;search_string;;", core.formspec_escape(search_string), "]",
708                         "field_close_on_enter[search_string;false]",
709                         "image_button[7.3,0;0.8,0.8;", core.formspec_escape(defaulttexturedir .. "search.png"), ";search;]",
710                         "image_button[8.125,0;0.8,0.8;", core.formspec_escape(defaulttexturedir .. "clear.png"), ";clear;]",
711                         "dropdown[9.6,0;2.4,0.8;type;", table.concat(filter_types_titles, ","), ";", filter_type, "]",
712                         "container_end[]",
713
714                         -- Page nav buttons
715                         "container[0,", H - 0.8 - 0.375, "]",
716                         "button[0.375,0;4,0.8;back;", fgettext("Back to Main Menu"), "]",
717
718                         "container[", W - 0.375 - 0.8*4 - 2,  ",0]",
719                         "image_button[0,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "start_icon.png;pstart;]",
720                         "image_button[0.8,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "prev_icon.png;pback;]",
721                         "style[pagenum;border=false]",
722                         "button[1.6,0;2,0.8;pagenum;", tonumber(cur_page), " / ", tonumber(dlgdata.pagemax), "]",
723                         "image_button[3.6,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "next_icon.png;pnext;]",
724                         "image_button[4.4,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "end_icon.png;pend;]",
725                         "container_end[]",
726
727                         "container_end[]",
728                 }
729
730                 if number_downloading > 0 then
731                         formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;downloading;"
732                         if #download_queue > 0 then
733                                 formspec[#formspec + 1] = fgettext("$1 downloading,\n$2 queued", number_downloading, #download_queue)
734                         else
735                                 formspec[#formspec + 1] = fgettext("$1 downloading...", number_downloading)
736                         end
737                         formspec[#formspec + 1] = "]"
738                 else
739                         local num_avail_updates = 0
740                         for i=1, #store.packages_full do
741                                 local package = store.packages_full[i]
742                                 if package.path and package.installed_release < package.release and
743                                                 not (package.downloading or package.queued) then
744                                         num_avail_updates = num_avail_updates + 1
745                                 end
746                         end
747
748                         if num_avail_updates == 0 then
749                                 formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;status;"
750                                 formspec[#formspec + 1] = fgettext("No updates")
751                                 formspec[#formspec + 1] = "]"
752                         else
753                                 formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;update_all;"
754                                 formspec[#formspec + 1] = fgettext("Update All [$1]", num_avail_updates)
755                                 formspec[#formspec + 1] = "]"
756                         end
757                 end
758
759                 if #store.packages == 0 then
760                         formspec[#formspec + 1] = "label[4,3;"
761                         formspec[#formspec + 1] = fgettext("No results")
762                         formspec[#formspec + 1] = "]"
763                 end
764         else
765                 formspec = {
766                         "size[12,7]",
767                         "position[0.5,0.55]",
768                         "label[4,3;", fgettext("No packages could be retrieved"), "]",
769                         "container[0,", H - 0.8 - 0.375, "]",
770                         "button[0,0;4,0.8;back;", fgettext("Back to Main Menu"), "]",
771                         "container_end[]",
772                 }
773         end
774
775         -- download/queued tooltips always have the same message
776         local tooltip_colors = ";#dff6f5;#302c2e]"
777         formspec[#formspec + 1] = "tooltip[downloading;" .. fgettext("Downloading...") .. tooltip_colors
778         formspec[#formspec + 1] = "tooltip[queued;" .. fgettext("Queued") .. tooltip_colors
779
780         local start_idx = (cur_page - 1) * num_per_page + 1
781         for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
782                 local package = store.packages[i]
783                 local container_y = (i - start_idx) * 1.375 + (2*0.375 + 0.8)
784                 formspec[#formspec + 1] = "container[0.375,"
785                 formspec[#formspec + 1] = container_y
786                 formspec[#formspec + 1] = "]"
787
788                 -- image
789                 formspec[#formspec + 1] = "image[0,0;1.5,1;"
790                 formspec[#formspec + 1] = core.formspec_escape(get_screenshot(package))
791                 formspec[#formspec + 1] = "]"
792
793                 -- title
794                 formspec[#formspec + 1] = "label[1.875,0.1;"
795                 formspec[#formspec + 1] = core.formspec_escape(
796                                 core.colorize(mt_color_green, package.title) ..
797                                 core.colorize("#BFBFBF", " by " .. package.author))
798                 formspec[#formspec + 1] = "]"
799
800                 -- buttons
801                 local left_base = "image_button[-1.55,0;0.7,0.7;" .. core.formspec_escape(defaulttexturedir)
802                 formspec[#formspec + 1] = "container["
803                 formspec[#formspec + 1] = W - 0.375*2
804                 formspec[#formspec + 1] = ",0.1]"
805
806                 if package.downloading then
807                         formspec[#formspec + 1] = "animated_image[-1.7,-0.15;1,1;downloading;"
808                         formspec[#formspec + 1] = core.formspec_escape(defaulttexturedir)
809                         formspec[#formspec + 1] = "cdb_downloading.png;3;400;]"
810                 elseif package.queued then
811                         formspec[#formspec + 1] = left_base
812                         formspec[#formspec + 1] = core.formspec_escape(defaulttexturedir)
813                         formspec[#formspec + 1] = "cdb_queued.png;queued]"
814                 elseif not package.path then
815                         local elem_name = "install_" .. i .. ";"
816                         formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#71aa34]"
817                         formspec[#formspec + 1] = left_base .. "cdb_add.png;" .. elem_name .. "]"
818                         formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Install") .. tooltip_colors
819                 else
820                         if package.installed_release < package.release then
821
822                                 -- The install_ action also handles updating
823                                 local elem_name = "install_" .. i .. ";"
824                                 formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#28ccdf]"
825                                 formspec[#formspec + 1] = left_base .. "cdb_update.png;" .. elem_name .. "]"
826                                 formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Update") .. tooltip_colors
827                         else
828
829                                 local elem_name = "uninstall_" .. i .. ";"
830                                 formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#a93b3b]"
831                                 formspec[#formspec + 1] = left_base .. "cdb_clear.png;" .. elem_name .. "]"
832                                 formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Uninstall") .. tooltip_colors
833                         end
834                 end
835
836                 local web_elem_name = "view_" .. i .. ";"
837                 formspec[#formspec + 1] = "image_button[-0.7,0;0.7,0.7;" ..
838                         core.formspec_escape(defaulttexturedir) .. "cdb_viewonline.png;" .. web_elem_name .. "]"
839                 formspec[#formspec + 1] = "tooltip[" .. web_elem_name ..
840                         fgettext("View more information in a web browser") .. tooltip_colors
841                 formspec[#formspec + 1] = "container_end[]"
842
843                 -- description
844                 local description_width = W - 0.375*5 - 0.85 - 2*0.7
845                 formspec[#formspec + 1] = "textarea[1.855,0.3;"
846                 formspec[#formspec + 1] = tostring(description_width)
847                 formspec[#formspec + 1] = ",0.8;;;"
848                 formspec[#formspec + 1] = core.formspec_escape(package.short_description)
849                 formspec[#formspec + 1] = "]"
850
851                 formspec[#formspec + 1] = "container_end[]"
852         end
853
854         return table.concat(formspec, "")
855 end
856
857 function store.handle_submit(this, fields)
858         if fields.search or fields.key_enter_field == "search_string" then
859                 search_string = fields.search_string:trim()
860                 cur_page = 1
861                 store.filter_packages(search_string)
862                 return true
863         end
864
865         if fields.clear then
866                 search_string = ""
867                 cur_page = 1
868                 store.filter_packages("")
869                 return true
870         end
871
872         if fields.back then
873                 this:delete()
874                 return true
875         end
876
877         if fields.pstart then
878                 cur_page = 1
879                 return true
880         end
881
882         if fields.pend then
883                 cur_page = this.data.pagemax
884                 return true
885         end
886
887         if fields.pnext then
888                 cur_page = cur_page + 1
889                 if cur_page > this.data.pagemax then
890                         cur_page = 1
891                 end
892                 return true
893         end
894
895         if fields.pback then
896                 if cur_page == 1 then
897                         cur_page = this.data.pagemax
898                 else
899                         cur_page = cur_page - 1
900                 end
901                 return true
902         end
903
904         if fields.type then
905                 local new_type = table.indexof(filter_types_titles, fields.type)
906                 if new_type ~= filter_type then
907                         filter_type = new_type
908                         store.filter_packages(search_string)
909                         return true
910                 end
911         end
912
913         if fields.update_all then
914                 for i=1, #store.packages_full do
915                         local package = store.packages_full[i]
916                         if package.path and package.installed_release < package.release and
917                                         not (package.downloading or package.queued) then
918                                 queue_download(package)
919                         end
920                 end
921                 return true
922         end
923
924         local start_idx = (cur_page - 1) * num_per_page + 1
925         assert(start_idx ~= nil)
926         for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
927                 local package = store.packages[i]
928                 assert(package)
929
930                 if fields["install_" .. i] then
931                         local install_parent
932                         if package.type == "mod" then
933                                 install_parent = core.get_modpath()
934                         elseif package.type == "game" then
935                                 install_parent = core.get_gamepath()
936                         elseif package.type == "txp" then
937                                 install_parent = core.get_texturepath()
938                         else
939                                 error("Unknown package type: " .. package.type)
940                         end
941
942
943                         local function on_confirm()
944                                 local deps = get_raw_dependencies(package)
945                                 if deps and has_hard_deps(deps) then
946                                         local dlg = install_dialog.create(package, deps)
947                                         dlg:set_parent(this)
948                                         this:hide()
949                                         dlg:show()
950                                 else
951                                         queue_download(package)
952                                 end
953                         end
954
955                         if not package.path and core.is_dir(install_parent .. DIR_DELIM .. package.name) then
956                                 local dlg = confirm_overwrite.create(package, on_confirm)
957                                 dlg:set_parent(this)
958                                 this:hide()
959                                 dlg:show()
960                         else
961                                 on_confirm()
962                         end
963
964                         return true
965                 end
966
967                 if fields["uninstall_" .. i] then
968                         local dlg = create_delete_content_dlg(package)
969                         dlg:set_parent(this)
970                         this:hide()
971                         dlg:show()
972                         return true
973                 end
974
975                 if fields["view_" .. i] then
976                         local url = ("%s/packages/%s/%s?protocol_version=%d"):format(
977                                         core.settings:get("contentdb_url"),
978                                         package.author, package.name, core.get_max_supp_proto())
979                         core.open_url(url)
980                         return true
981                 end
982         end
983
984         return false
985 end
986
987 function create_store_dlg(type)
988         if not store.loaded or #store.packages_full == 0 then
989                 store.load()
990         end
991
992         store.update_paths()
993         store.sort_packages()
994
995         search_string = ""
996         cur_page = 1
997
998         if type then
999                 -- table.indexof does not work on tables that contain `nil`
1000                 for i, v in pairs(filter_types_type) do
1001                         if v == type then
1002                                 filter_type = i
1003                                 break
1004                         end
1005                 end
1006         end
1007
1008         store.filter_packages(search_string)
1009
1010         return dialog_create("store",
1011                         store.get_formspec,
1012                         store.handle_submit,
1013                         nil)
1014 end