Skip to content

Commit

Permalink
ContentDB: Add Update All and download queuing (#9995)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwardy committed Aug 17, 2020
1 parent 291a6b7 commit 7242de1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
91 changes: 81 additions & 10 deletions builtin/mainmenu/dlg_contentstore.lua
Expand Up @@ -45,6 +45,9 @@ local filter_types_titles = {
fgettext("Texture packs"),
}

local number_downloading = 0
local download_queue = {}

local filter_types_type = {
nil,
"game",
Expand All @@ -67,12 +70,14 @@ local function download_package(param)
end
end

local function start_install(calling_dialog, package)
local function start_install(package)
local params = {
package = package,
filename = os.tempfolder() .. "_MODNAME_" .. package.name .. ".zip",
}

number_downloading = number_downloading + 1

local function callback(result)
if result.successful then
local path, msg = pkgmgr.install(package.type,
Expand Down Expand Up @@ -121,9 +126,20 @@ local function start_install(calling_dialog, package)
end

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()
end

package.queued = false
package.downloading = true

if not core.handle_async(download_package, params, callback) then
Expand All @@ -133,6 +149,16 @@ local function start_install(calling_dialog, package)
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 parts = path:split(".")
return parts[#parts]
Expand Down Expand Up @@ -279,7 +305,7 @@ function store.filter_packages(query)
table.insert(keywords, word)
end

local function matches_keywords(package, keywords)
local function matches_keywords(package)
for k = 1, #keywords do
local keyword = keywords[k]

Expand All @@ -296,7 +322,7 @@ function store.filter_packages(query)

store.packages = {}
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
store.packages[#store.packages + 1] = package
end
Expand All @@ -321,11 +347,14 @@ function store.get_formspec(dlgdata)
"formspec_version[3]",
"size[15.75,9.5]",
"position[0.5,0.55]",

"style[status;border=false]",

"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]",
"button[10.225,0;2,0.8;search;", fgettext("Search"), "]",
"dropdown[12.6,0;2.4,0.8;type;", table.concat(filter_types_titles, ","), ";", filter_type, "]",
"button[7.225,0;2,0.8;search;", fgettext("Search"), "]",
"dropdown[9.6,0;2.4,0.8;type;", table.concat(filter_types_titles, ","), ";", filter_type, "]",
"container_end[]",

-- Page nav buttons
Expand All @@ -344,6 +373,35 @@ function store.get_formspec(dlgdata)
"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
formspec[#formspec + 1] = "label[4,3;"
formspec[#formspec + 1] = fgettext("No results")
Expand Down Expand Up @@ -386,11 +444,13 @@ function store.get_formspec(dlgdata)
formspec[#formspec + 1] = ",0.1]"

if package.downloading then
formspec[#formspec + 1] = "style[download;border=false]"

formspec[#formspec + 1] = "button[-3.5,0;2,0.8;download;"
formspec[#formspec + 1] = "button[-3.5,0;2,0.8;status;"
formspec[#formspec + 1] = fgettext("Downloading...")
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
formspec[#formspec + 1] = "button[-3,0;1.5,0.8;install_"
formspec[#formspec + 1] = tostring(i)
Expand Down Expand Up @@ -485,14 +545,25 @@ function store.handle_submit(this, fields)
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
assert(start_idx ~= nil)
for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
local package = store.packages[i]
assert(package)

if fields["install_" .. i] then
start_install(this, package)
queue_download(package)
return true
end

Expand Down
3 changes: 3 additions & 0 deletions builtin/settingtypes.txt
Expand Up @@ -2220,3 +2220,6 @@ contentdb_url (ContentDB URL) string https://content.minetest.net
# These flags are independent from Minetest versions,
# so see a full list at https://content.minetest.net/help/content_flags/
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
2 changes: 2 additions & 0 deletions src/defaultsettings.cpp
Expand Up @@ -330,6 +330,8 @@ void set_default_settings(Settings *settings)

// ContentDB
settings->setDefault("contentdb_url", "https://content.minetest.net");
settings->setDefault("contentdb_max_concurrent_downloads", "3");

#ifdef __ANDROID__
settings->setDefault("contentdb_flag_blacklist", "nonfree, android_default");
#else
Expand Down

0 comments on commit 7242de1

Please sign in to comment.