ContentDB: Add Update All and download queuing (#9995)

This commit is contained in:
rubenwardy 2020-08-17 19:09:33 +01:00 committed by GitHub
parent 291a6b70d6
commit 7242de1d4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 10 deletions

@ -45,6 +45,9 @@ local filter_types_titles = {
fgettext("Texture packs"), fgettext("Texture packs"),
} }
local number_downloading = 0
local download_queue = {}
local filter_types_type = { local filter_types_type = {
nil, nil,
"game", "game",
@ -67,12 +70,14 @@ local function download_package(param)
end end
end end
local function start_install(calling_dialog, package) local function start_install(package)
local params = { local params = {
package = package, package = package,
filename = os.tempfolder() .. "_MODNAME_" .. package.name .. ".zip", filename = os.tempfolder() .. "_MODNAME_" .. package.name .. ".zip",
} }
number_downloading = number_downloading + 1
local function callback(result) local function callback(result)
if result.successful then if result.successful then
local path, msg = pkgmgr.install(package.type, local path, msg = pkgmgr.install(package.type,
@ -121,9 +126,20 @@ local function start_install(calling_dialog, package)
end end
package.downloading = false package.downloading = false
number_downloading = number_downloading - 1
local next = download_queue[1]
if next then
table.remove(download_queue, 1)
start_install(next)
end
ui.update() ui.update()
end end
package.queued = false
package.downloading = true package.downloading = true
if not core.handle_async(download_package, params, callback) then if not core.handle_async(download_package, params, callback) then
@ -133,6 +149,16 @@ local function start_install(calling_dialog, package)
end end
end end
local function queue_download(package)
local max_concurrent_downloads = tonumber(minetest.settings:get("contentdb_max_concurrent_downloads"))
if number_downloading < max_concurrent_downloads then
start_install(package)
else
table.insert(download_queue, package)
package.queued = true
end
end
local function get_file_extension(path) local function get_file_extension(path)
local parts = path:split(".") local parts = path:split(".")
return parts[#parts] return parts[#parts]
@ -279,7 +305,7 @@ function store.filter_packages(query)
table.insert(keywords, word) table.insert(keywords, word)
end end
local function matches_keywords(package, keywords) local function matches_keywords(package)
for k = 1, #keywords do for k = 1, #keywords do
local keyword = keywords[k] local keyword = keywords[k]
@ -296,7 +322,7 @@ function store.filter_packages(query)
store.packages = {} store.packages = {}
for _, package in pairs(store.packages_full) do for _, package in pairs(store.packages_full) do
if (query == "" or matches_keywords(package, keywords)) and if (query == "" or matches_keywords(package)) and
(filter_type == 1 or package.type == filter_types_type[filter_type]) then (filter_type == 1 or package.type == filter_types_type[filter_type]) then
store.packages[#store.packages + 1] = package store.packages[#store.packages + 1] = package
end end
@ -321,11 +347,14 @@ function store.get_formspec(dlgdata)
"formspec_version[3]", "formspec_version[3]",
"size[15.75,9.5]", "size[15.75,9.5]",
"position[0.5,0.55]", "position[0.5,0.55]",
"style[status;border=false]",
"container[0.375,0.375]", "container[0.375,0.375]",
"field[0,0;10.225,0.8;search_string;;", core.formspec_escape(search_string), "]", "field[0,0;7.225,0.8;search_string;;", core.formspec_escape(search_string), "]",
"field_close_on_enter[search_string;false]", "field_close_on_enter[search_string;false]",
"button[10.225,0;2,0.8;search;", fgettext("Search"), "]", "button[7.225,0;2,0.8;search;", fgettext("Search"), "]",
"dropdown[12.6,0;2.4,0.8;type;", table.concat(filter_types_titles, ","), ";", filter_type, "]", "dropdown[9.6,0;2.4,0.8;type;", table.concat(filter_types_titles, ","), ";", filter_type, "]",
"container_end[]", "container_end[]",
-- Page nav buttons -- Page nav buttons
@ -344,6 +373,35 @@ function store.get_formspec(dlgdata)
"container_end[]", "container_end[]",
} }
if number_downloading > 0 then
formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;status;"
if #download_queue > 0 then
formspec[#formspec + 1] = fgettext("$1 downloading,\n$2 queued", number_downloading, #download_queue)
else
formspec[#formspec + 1] = fgettext("$1 downloading...", number_downloading)
end
formspec[#formspec + 1] = "]"
else
local num_avail_updates = 0
for i=1, #store.packages_full do
local package = store.packages_full[i]
if package.path and package.installed_release < package.release and
not (package.downloading or package.queued) then
num_avail_updates = num_avail_updates + 1
end
end
if num_avail_updates == 0 then
formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;status;"
formspec[#formspec + 1] = fgettext("No updates")
formspec[#formspec + 1] = "]"
else
formspec[#formspec + 1] = "button[12.75,0.375;2.625,0.8;update_all;"
formspec[#formspec + 1] = fgettext("Update All [$1]", num_avail_updates)
formspec[#formspec + 1] = "]"
end
end
if #store.packages == 0 then if #store.packages == 0 then
formspec[#formspec + 1] = "label[4,3;" formspec[#formspec + 1] = "label[4,3;"
formspec[#formspec + 1] = fgettext("No results") formspec[#formspec + 1] = fgettext("No results")
@ -386,11 +444,13 @@ function store.get_formspec(dlgdata)
formspec[#formspec + 1] = ",0.1]" formspec[#formspec + 1] = ",0.1]"
if package.downloading then if package.downloading then
formspec[#formspec + 1] = "style[download;border=false]" formspec[#formspec + 1] = "button[-3.5,0;2,0.8;status;"
formspec[#formspec + 1] = "button[-3.5,0;2,0.8;download;"
formspec[#formspec + 1] = fgettext("Downloading...") formspec[#formspec + 1] = fgettext("Downloading...")
formspec[#formspec + 1] = "]" formspec[#formspec + 1] = "]"
elseif package.queued then
formspec[#formspec + 1] = "button[-3.5,0;2,0.8;status;"
formspec[#formspec + 1] = fgettext("Queued")
formspec[#formspec + 1] = "]"
elseif not package.path then elseif not package.path then
formspec[#formspec + 1] = "button[-3,0;1.5,0.8;install_" formspec[#formspec + 1] = "button[-3,0;1.5,0.8;install_"
formspec[#formspec + 1] = tostring(i) formspec[#formspec + 1] = tostring(i)
@ -485,6 +545,17 @@ function store.handle_submit(this, fields)
end end
end end
if fields.update_all then
for i=1, #store.packages_full do
local package = store.packages_full[i]
if package.path and package.installed_release < package.release and
not (package.downloading or package.queued) then
queue_download(package)
end
end
return true
end
local start_idx = (cur_page - 1) * num_per_page + 1 local start_idx = (cur_page - 1) * num_per_page + 1
assert(start_idx ~= nil) assert(start_idx ~= nil)
for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
@ -492,7 +563,7 @@ function store.handle_submit(this, fields)
assert(package) assert(package)
if fields["install_" .. i] then if fields["install_" .. i] then
start_install(this, package) queue_download(package)
return true return true
end end

@ -2220,3 +2220,6 @@ contentdb_url (ContentDB URL) string https://content.minetest.net
# These flags are independent from Minetest versions, # These flags are independent from Minetest versions,
# so see a full list at https://content.minetest.net/help/content_flags/ # so see a full list at https://content.minetest.net/help/content_flags/
contentdb_flag_blacklist (ContentDB Flag Blacklist) string nonfree, desktop_default contentdb_flag_blacklist (ContentDB Flag Blacklist) string nonfree, desktop_default
# Maximum number of concurrent downloads. Downloads exceeding this limit will be queued.
contentdb_max_concurrent_downloads (ContentDB Max Concurrent Downloads) int 3

@ -330,6 +330,8 @@ void set_default_settings(Settings *settings)
// ContentDB // ContentDB
settings->setDefault("contentdb_url", "https://content.minetest.net"); settings->setDefault("contentdb_url", "https://content.minetest.net");
settings->setDefault("contentdb_max_concurrent_downloads", "3");
#ifdef __ANDROID__ #ifdef __ANDROID__
settings->setDefault("contentdb_flag_blacklist", "nonfree, android_default"); settings->setDefault("contentdb_flag_blacklist", "nonfree, android_default");
#else #else