2 --Copyright (C) 2018-20 rubenwardy
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.
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.
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.
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"))
26 local store = { packages = {}, packages_full = {} }
28 local http = minetest.get_http_api()
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 = {}
37 local search_string = ""
39 local num_per_page = 5
41 local filter_types_titles = {
42 fgettext("All packages"),
45 fgettext("Texture packs"),
48 local filter_types_type = {
56 local function download_package(param)
57 if core.download_file(param.package.url, param.filename) then
59 filename = param.filename,
63 core.log("error", "downloading " .. dump(param.package.url) .. " failed")
70 local function start_install(calling_dialog, package)
73 filename = os.tempfolder() .. "_MODNAME_" .. package.name .. ".zip",
76 local function callback(result)
77 if result.successful then
78 local path, msg = pkgmgr.install(package.type,
79 result.filename, package.name,
82 gamedata.errormessage = msg
84 core.log("action", "Installed package to " .. path)
87 local name_is_title = false
88 if package.type == "mod" then
89 local actual_type = pkgmgr.get_folder_type(path)
90 if actual_type.type == "modpack" then
91 conf_path = path .. DIR_DELIM .. "modpack.conf"
93 conf_path = path .. DIR_DELIM .. "mod.conf"
95 elseif package.type == "game" then
96 conf_path = path .. DIR_DELIM .. "game.conf"
98 elseif package.type == "txp" then
99 conf_path = path .. DIR_DELIM .. "texture_pack.conf"
103 local conf = Settings(conf_path)
104 if name_is_title then
105 conf:set("name", package.title)
107 conf:set("title", package.title)
108 conf:set("name", package.name)
110 if not conf:get("description") then
111 conf:set("description", package.short_description)
113 conf:set("author", package.author)
114 conf:set("release", package.release)
118 os.remove(result.filename)
120 gamedata.errormessage = fgettext("Failed to download $1", package.name)
123 package.downloading = false
127 package.downloading = true
129 if not core.handle_async(download_package, params, callback) then
130 core.log("error", "ERROR: async event failed")
131 gamedata.errormessage = fgettext("Failed to download $1", package.name)
136 local function get_screenshot(package)
137 if not package.thumbnail then
138 return defaulttexturedir .. "no_screenshot.png"
139 elseif screenshot_downloading[package.thumbnail] then
140 return defaulttexturedir .. "loading_screenshot.png"
143 -- Get tmp screenshot path
144 local filepath = screenshot_dir .. DIR_DELIM ..
145 package.type .. "-" .. package.author .. "-" .. package.name .. ".png"
147 -- Return if already downloaded
148 local file = io.open(filepath, "r")
154 -- Show error if we've failed to download before
155 if screenshot_downloaded[package.thumbnail] then
156 return defaulttexturedir .. "error_screenshot.png"
161 local function download_screenshot(params)
162 return core.download_file(params.url, params.dest)
164 local function callback(success)
165 screenshot_downloading[package.thumbnail] = nil
166 screenshot_downloaded[package.thumbnail] = true
168 core.log("warning", "Screenshot download failed for some reason")
172 if core.handle_async(download_screenshot,
173 { dest = filepath, url = package.thumbnail }, callback) then
174 screenshot_downloading[package.thumbnail] = true
176 core.log("error", "ERROR: async event failed")
177 return defaulttexturedir .. "error_screenshot.png"
180 return defaulttexturedir .. "loading_screenshot.png"
183 function store.load()
184 local version = core.get_version()
185 local base_url = core.settings:get("contentdb_url")
186 local url = base_url ..
187 "/api/packages/?type=mod&type=game&type=txp&protocol_version=" ..
188 core.get_max_supp_proto() .. "&engine_version=" .. version.string
190 for _, item in pairs(core.settings:get("contentdb_flag_blacklist"):split(",")) do
193 url = url .. "&hide=" .. item
197 local timeout = tonumber(minetest.settings:get("curl_file_download_timeout"))
198 local response = http.fetch_sync({ url = url, timeout = timeout })
199 if not response.succeeded then
203 store.packages_full = core.parse_json(response.data) or {}
205 for _, package in pairs(store.packages_full) do
206 package.url = base_url .. "/packages/" ..
207 package.author .. "/" .. package.name ..
208 "/releases/" .. package.release .. "/download/"
210 local name_len = #package.name
211 if package.type == "game" and name_len > 5 and package.name:sub(name_len - 4) == "_game" then
212 package.id = package.author:lower() .. "/" .. package.name:sub(1, name_len - 5)
214 package.id = package.author:lower() .. "/" .. package.name
218 store.packages = store.packages_full
222 function store.update_paths()
224 pkgmgr.refresh_globals()
225 for _, mod in pairs(pkgmgr.global_mods:get_list()) do
227 mod_hash[mod.author:lower() .. "/" .. mod.name] = mod
232 pkgmgr.update_gamelist()
233 for _, game in pairs(pkgmgr.games) do
234 if game.author ~= "" then
235 game_hash[game.author:lower() .. "/" .. game.id] = game
240 for _, txp in pairs(pkgmgr.get_texture_packs()) do
242 txp_hash[txp.author:lower() .. "/" .. txp.name] = txp
246 for _, package in pairs(store.packages_full) do
248 if package.type == "mod" then
249 content = mod_hash[package.id]
250 elseif package.type == "game" then
251 content = game_hash[package.id]
252 elseif package.type == "txp" then
253 content = txp_hash[package.id]
257 package.path = content.path
258 package.installed_release = content.release or 0
265 function store.filter_packages(query)
266 if query == "" and filter_type == 1 then
267 store.packages = store.packages_full
272 for word in query:lower():gmatch("%S+") do
273 table.insert(keywords, word)
276 local function matches_keywords(package, keywords)
277 for k = 1, #keywords do
278 local keyword = keywords[k]
280 if string.find(package.name:lower(), keyword, 1, true) or
281 string.find(package.title:lower(), keyword, 1, true) or
282 string.find(package.author:lower(), keyword, 1, true) or
283 string.find(package.short_description:lower(), keyword, 1, true) then
292 for _, package in pairs(store.packages_full) do
293 if (query == "" or matches_keywords(package, keywords)) and
294 (filter_type == 1 or package.type == filter_types_type[filter_type]) then
295 store.packages[#store.packages + 1] = package
301 function store.get_formspec(dlgdata)
304 dlgdata.pagemax = math.max(math.ceil(#store.packages / num_per_page), 1)
305 if cur_page > dlgdata.pagemax then
313 if #store.packages_full > 0 then
315 "formspec_version[3]",
317 "position[0.5,0.55]",
318 "container[0.375,0.375]",
319 "field[0,0;10.225,0.8;search_string;;", core.formspec_escape(search_string), "]",
320 "field_close_on_enter[search_string;false]",
321 "button[10.225,0;2,0.8;search;", fgettext("Search"), "]",
322 "dropdown[12.6,0;2.4,0.8;type;", table.concat(filter_types_titles, ","), ";", filter_type, "]",
326 "container[0,", H - 0.8 - 0.375, "]",
327 "button[0.375,0;4,0.8;back;", fgettext("Back to Main Menu"), "]",
329 "container[", W - 0.375 - 0.8*4 - 2, ",0]",
330 "image_button[0,0;0.8,0.8;", defaulttexturedir, "start_icon.png;pstart;]",
331 "image_button[0.8,0;0.8,0.8;", defaulttexturedir, "prev_icon.png;pback;]",
332 "style[pagenum;border=false]",
333 "button[1.6,0;2,0.8;pagenum;", tonumber(cur_page), " / ", tonumber(dlgdata.pagemax), "]",
334 "image_button[3.6,0;0.8,0.8;", defaulttexturedir, "next_icon.png;pnext;]",
335 "image_button[4.4,0;0.8,0.8;", defaulttexturedir, "end_icon.png;pend;]",
341 if #store.packages == 0 then
342 formspec[#formspec + 1] = "label[4,3;"
343 formspec[#formspec + 1] = fgettext("No results")
344 formspec[#formspec + 1] = "]"
349 "position[0.5,0.55]",
350 "label[4,3;", fgettext("No packages could be retrieved"), "]",
351 "container[0,", H - 0.8 - 0.375, "]",
352 "button[0,0;4,0.8;back;", fgettext("Back to Main Menu"), "]",
357 local start_idx = (cur_page - 1) * num_per_page + 1
358 for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
359 local package = store.packages[i]
360 formspec[#formspec + 1] = "container[0.375,"
361 formspec[#formspec + 1] = (i - start_idx) * 1.375 + (2*0.375 + 0.8)
362 formspec[#formspec + 1] = "]"
365 formspec[#formspec + 1] = "image[0,0;1.5,1;"
366 formspec[#formspec + 1] = core.formspec_escape(get_screenshot(package))
367 formspec[#formspec + 1] = "]"
370 formspec[#formspec + 1] = "label[1.875,0.1;"
371 formspec[#formspec + 1] = core.formspec_escape(
372 minetest.colorize(mt_color_green, package.title) ..
373 minetest.colorize("#BFBFBF", " by " .. package.author))
374 formspec[#formspec + 1] = "]"
377 local description_width = W - 0.375*5 - 1 - 2*1.5
378 formspec[#formspec + 1] = "container["
379 formspec[#formspec + 1] = W - 0.375*2
380 formspec[#formspec + 1] = ",0.1]"
382 if package.downloading then
383 formspec[#formspec + 1] = "style[download;border=false]"
385 formspec[#formspec + 1] = "button[-3.5,0;2,0.8;download;"
386 formspec[#formspec + 1] = fgettext("Downloading...")
387 formspec[#formspec + 1] = "]"
388 elseif not package.path then
389 formspec[#formspec + 1] = "button[-3,0;1.5,0.8;install_"
390 formspec[#formspec + 1] = tostring(i)
391 formspec[#formspec + 1] = ";"
392 formspec[#formspec + 1] = fgettext("Install")
393 formspec[#formspec + 1] = "]"
395 if package.installed_release < package.release then
396 description_width = description_width - 1.5
398 -- The install_ action also handles updating
399 formspec[#formspec + 1] = "button[-4.5,0;1.5,0.8;install_"
400 formspec[#formspec + 1] = tostring(i)
401 formspec[#formspec + 1] = ";"
402 formspec[#formspec + 1] = fgettext("Update")
403 formspec[#formspec + 1] = "]"
406 formspec[#formspec + 1] = "button[-3,0;1.5,0.8;uninstall_"
407 formspec[#formspec + 1] = tostring(i)
408 formspec[#formspec + 1] = ";"
409 formspec[#formspec + 1] = fgettext("Uninstall")
410 formspec[#formspec + 1] = "]"
413 formspec[#formspec + 1] = "button[-1.5,0;1.5,0.8;view_"
414 formspec[#formspec + 1] = tostring(i)
415 formspec[#formspec + 1] = ";"
416 formspec[#formspec + 1] = fgettext("View")
417 formspec[#formspec + 1] = "]"
418 formspec[#formspec + 1] = "container_end[]"
421 formspec[#formspec + 1] = "textarea[1.855,0.3;"
422 formspec[#formspec + 1] = tostring(description_width)
423 formspec[#formspec + 1] = ",0.8;;;"
424 formspec[#formspec + 1] = core.formspec_escape(package.short_description)
425 formspec[#formspec + 1] = "]"
427 formspec[#formspec + 1] = "container_end[]"
430 return table.concat(formspec, "")
433 function store.handle_submit(this, fields)
434 if fields.search or fields.key_enter_field == "search_string" then
435 search_string = fields.search_string:trim()
437 store.filter_packages(search_string)
446 if fields.pstart then
452 cur_page = this.data.pagemax
457 cur_page = cur_page + 1
458 if cur_page > this.data.pagemax then
465 if cur_page == 1 then
466 cur_page = this.data.pagemax
468 cur_page = cur_page - 1
474 local new_type = table.indexof(filter_types_titles, fields.type)
475 if new_type ~= filter_type then
476 filter_type = new_type
477 store.filter_packages(search_string)
482 local start_idx = (cur_page - 1) * num_per_page + 1
483 assert(start_idx ~= nil)
484 for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
485 local package = store.packages[i]
488 if fields["install_" .. i] then
489 start_install(this, package)
493 if fields["uninstall_" .. i] then
494 local dlg_delmod = create_delete_content_dlg(package)
495 dlg_delmod:set_parent(this)
501 if fields["view_" .. i] then
502 local url = ("%s/packages/%s?protocol_version=%d"):format(
503 core.settings:get("contentdb_url"), package.id, core.get_max_supp_proto())
512 function create_store_dlg(type)
513 if not store.loaded or #store.packages_full == 0 then
521 -- table.indexof does not work on tables that contain `nil`
522 for i, v in pairs(filter_types_type) do
530 store.filter_packages(search_string)
532 return dialog_create("store",