2018-04-17 15:54:50 +02:00
|
|
|
--Minetest
|
2020-06-06 18:17:08 +02:00
|
|
|
--Copyright (C) 2018-20 rubenwardy
|
2018-04-17 15:54:50 +02:00
|
|
|
--
|
|
|
|
--This program is free software; you can redistribute it and/or modify
|
|
|
|
--it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
--the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
--(at your option) any later version.
|
|
|
|
--
|
|
|
|
--This program is distributed in the hope that it will be useful,
|
|
|
|
--but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
--GNU Lesser General Public License for more details.
|
|
|
|
--
|
|
|
|
--You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
--with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2021-02-10 12:02:34 +01:00
|
|
|
if not core.get_http_api then
|
2020-06-06 18:17:08 +02:00
|
|
|
function create_store_dlg()
|
|
|
|
return messagebox("store",
|
|
|
|
fgettext("ContentDB is not available when Minetest was compiled without cURL"))
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
local store = {
|
|
|
|
loading = false,
|
|
|
|
load_ok = false,
|
|
|
|
load_error = false,
|
|
|
|
|
|
|
|
-- Unordered preserves the original order of the ContentDB API,
|
|
|
|
-- before the package list is ordered based on installed state.
|
|
|
|
packages = {},
|
|
|
|
packages_full = {},
|
|
|
|
packages_full_unordered = {},
|
|
|
|
aliases = {},
|
|
|
|
}
|
2018-05-16 22:52:12 +02:00
|
|
|
|
2021-02-10 12:02:34 +01:00
|
|
|
local http = core.get_http_api()
|
2020-06-06 18:17:08 +02:00
|
|
|
|
2018-05-20 01:25:17 +02:00
|
|
|
-- Screenshot
|
2019-01-31 17:35:55 +01:00
|
|
|
local screenshot_dir = core.get_cache_path() .. DIR_DELIM .. "cdb"
|
2018-05-20 01:25:17 +02:00
|
|
|
assert(core.create_dir(screenshot_dir))
|
|
|
|
local screenshot_downloading = {}
|
|
|
|
local screenshot_downloaded = {}
|
|
|
|
|
|
|
|
-- Filter
|
2018-05-16 22:52:12 +02:00
|
|
|
local search_string = ""
|
|
|
|
local cur_page = 1
|
|
|
|
local num_per_page = 5
|
|
|
|
local filter_type = 1
|
|
|
|
local filter_types_titles = {
|
|
|
|
fgettext("All packages"),
|
|
|
|
fgettext("Games"),
|
|
|
|
fgettext("Mods"),
|
|
|
|
fgettext("Texture packs"),
|
|
|
|
}
|
|
|
|
|
2023-10-08 17:47:00 +02:00
|
|
|
-- Automatic package installation
|
|
|
|
local auto_install_spec = nil
|
|
|
|
|
2020-08-17 20:09:33 +02:00
|
|
|
local number_downloading = 0
|
|
|
|
local download_queue = {}
|
|
|
|
|
2018-05-16 22:52:12 +02:00
|
|
|
local filter_types_type = {
|
|
|
|
nil,
|
|
|
|
"game",
|
|
|
|
"mod",
|
|
|
|
"txp",
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:08:49 +02:00
|
|
|
local REASON_NEW = "new"
|
|
|
|
local REASON_UPDATE = "update"
|
|
|
|
local REASON_DEPENDENCY = "dependency"
|
|
|
|
|
|
|
|
|
|
|
|
local function get_download_url(package, reason)
|
|
|
|
local base_url = core.settings:get("contentdb_url")
|
2021-12-01 21:11:32 +01:00
|
|
|
local ret = base_url .. ("/packages/%s/releases/%d/download/"):format(
|
|
|
|
package.url_part, package.release)
|
2021-07-27 19:08:49 +02:00
|
|
|
if reason then
|
|
|
|
ret = ret .. "?reason=" .. reason
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2018-05-16 22:52:12 +02:00
|
|
|
|
2021-09-19 17:55:01 +02:00
|
|
|
local function download_and_extract(param)
|
|
|
|
local package = param.package
|
|
|
|
|
|
|
|
local filename = core.get_temp_path(true)
|
|
|
|
if filename == "" or not core.download_file(param.url, filename) then
|
|
|
|
core.log("error", "Downloading " .. dump(param.url) .. " failed")
|
2018-04-17 15:54:50 +02:00
|
|
|
return {
|
2023-06-01 20:35:40 +02:00
|
|
|
msg = fgettext_ne("Failed to download \"$1\"", package.title)
|
2018-04-17 15:54:50 +02:00
|
|
|
}
|
2021-09-19 17:55:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local tempfolder = core.get_temp_path()
|
|
|
|
if tempfolder ~= "" then
|
|
|
|
tempfolder = tempfolder .. DIR_DELIM .. "MT_" .. math.random(1, 1024000)
|
|
|
|
if not core.extract_zip(filename, tempfolder) then
|
|
|
|
tempfolder = nil
|
|
|
|
end
|
2018-04-17 15:54:50 +02:00
|
|
|
else
|
2021-09-19 17:55:01 +02:00
|
|
|
tempfolder = nil
|
|
|
|
end
|
|
|
|
os.remove(filename)
|
|
|
|
if not tempfolder then
|
2018-04-17 15:54:50 +02:00
|
|
|
return {
|
2023-06-01 20:35:40 +02:00
|
|
|
msg = fgettext_ne("Failed to extract \"$1\" (unsupported file type or broken archive)", package.title),
|
2018-04-17 15:54:50 +02:00
|
|
|
}
|
|
|
|
end
|
2021-09-19 17:55:01 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
path = tempfolder
|
|
|
|
}
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|
|
|
|
|
2021-07-27 19:08:49 +02:00
|
|
|
local function start_install(package, reason)
|
2018-04-17 15:54:50 +02:00
|
|
|
local params = {
|
|
|
|
package = package,
|
2021-07-27 19:08:49 +02:00
|
|
|
url = get_download_url(package, reason),
|
2018-04-17 15:54:50 +02:00
|
|
|
}
|
|
|
|
|
2020-08-17 20:09:33 +02:00
|
|
|
number_downloading = number_downloading + 1
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
local function callback(result)
|
2021-09-19 17:55:01 +02:00
|
|
|
if result.msg then
|
|
|
|
gamedata.errormessage = result.msg
|
|
|
|
else
|
|
|
|
local path, msg = pkgmgr.install_dir(package.type, result.path, package.name, package.path)
|
|
|
|
core.delete_dir(result.path)
|
2018-04-17 15:54:50 +02:00
|
|
|
if not path then
|
2023-06-01 20:35:40 +02:00
|
|
|
gamedata.errormessage = fgettext_ne("Error installing \"$1\": $2", package.title, msg)
|
2018-04-17 15:54:50 +02:00
|
|
|
else
|
2019-01-06 10:23:35 +01:00
|
|
|
core.log("action", "Installed package to " .. path)
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
local conf_path
|
|
|
|
local name_is_title = false
|
2020-05-27 18:02:09 +02:00
|
|
|
if package.type == "mod" then
|
2019-01-06 10:23:35 +01:00
|
|
|
local actual_type = pkgmgr.get_folder_type(path)
|
|
|
|
if actual_type.type == "modpack" then
|
|
|
|
conf_path = path .. DIR_DELIM .. "modpack.conf"
|
|
|
|
else
|
|
|
|
conf_path = path .. DIR_DELIM .. "mod.conf"
|
|
|
|
end
|
2020-05-27 18:02:09 +02:00
|
|
|
elseif package.type == "game" then
|
2018-04-17 15:54:50 +02:00
|
|
|
conf_path = path .. DIR_DELIM .. "game.conf"
|
|
|
|
name_is_title = true
|
2020-05-27 18:02:09 +02:00
|
|
|
elseif package.type == "txp" then
|
2018-04-17 15:54:50 +02:00
|
|
|
conf_path = path .. DIR_DELIM .. "texture_pack.conf"
|
|
|
|
end
|
|
|
|
|
|
|
|
if conf_path then
|
|
|
|
local conf = Settings(conf_path)
|
2022-05-21 17:23:30 +02:00
|
|
|
conf:set("title", package.title)
|
|
|
|
if not name_is_title then
|
|
|
|
conf:set("name", package.name)
|
2019-06-10 19:49:00 +02:00
|
|
|
end
|
|
|
|
if not conf:get("description") then
|
2020-05-27 18:02:09 +02:00
|
|
|
conf:set("description", package.short_description)
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|
2020-05-27 18:02:09 +02:00
|
|
|
conf:set("author", package.author)
|
|
|
|
conf:set("release", package.release)
|
2018-04-17 15:54:50 +02:00
|
|
|
conf:write()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-27 18:02:09 +02:00
|
|
|
package.downloading = false
|
2020-08-17 20:09:33 +02:00
|
|
|
|
|
|
|
number_downloading = number_downloading - 1
|
|
|
|
|
|
|
|
local next = download_queue[1]
|
|
|
|
if next then
|
|
|
|
table.remove(download_queue, 1)
|
|
|
|
|
2021-07-27 19:08:49 +02:00
|
|
|
start_install(next.package, next.reason)
|
2020-08-17 20:09:33 +02:00
|
|
|
end
|
|
|
|
|
2020-05-27 18:02:09 +02:00
|
|
|
ui.update()
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|
|
|
|
|
2020-08-17 20:09:33 +02:00
|
|
|
package.queued = false
|
2020-05-27 18:02:09 +02:00
|
|
|
package.downloading = true
|
|
|
|
|
2021-09-19 17:55:01 +02:00
|
|
|
if not core.handle_async(download_and_extract, params, callback) then
|
2018-05-16 22:52:12 +02:00
|
|
|
core.log("error", "ERROR: async event failed")
|
2023-06-01 20:35:40 +02:00
|
|
|
gamedata.errormessage = fgettext_ne("Failed to download $1", package.name)
|
2020-05-27 18:02:09 +02:00
|
|
|
return
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-27 19:08:49 +02:00
|
|
|
local function queue_download(package, reason)
|
2023-10-28 18:33:44 +02:00
|
|
|
if package.queued or package.downloading then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-02-10 12:02:34 +01:00
|
|
|
local max_concurrent_downloads = tonumber(core.settings:get("contentdb_max_concurrent_downloads"))
|
2022-07-09 22:32:08 +02:00
|
|
|
if number_downloading < math.max(max_concurrent_downloads, 1) then
|
2021-07-27 19:08:49 +02:00
|
|
|
start_install(package, reason)
|
2020-08-17 20:09:33 +02:00
|
|
|
else
|
2021-07-27 19:08:49 +02:00
|
|
|
table.insert(download_queue, { package = package, reason = reason })
|
2020-08-17 20:09:33 +02:00
|
|
|
package.queued = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-23 15:42:18 +01:00
|
|
|
local function get_raw_dependencies(package)
|
2023-04-14 21:23:25 +02:00
|
|
|
if package.type ~= "mod" then
|
|
|
|
return {}
|
|
|
|
end
|
2020-12-23 15:42:18 +01:00
|
|
|
if package.raw_deps then
|
|
|
|
return package.raw_deps
|
|
|
|
end
|
|
|
|
|
|
|
|
local url_fmt = "/api/packages/%s/dependencies/?only_hard=1&protocol_version=%s&engine_version=%s"
|
|
|
|
local version = core.get_version()
|
|
|
|
local base_url = core.settings:get("contentdb_url")
|
2023-10-28 18:33:44 +02:00
|
|
|
local url = base_url .. url_fmt:format(package.url_part, core.get_max_supp_proto(), core.urlencode(version.string))
|
2020-12-23 15:42:18 +01:00
|
|
|
|
|
|
|
local response = http.fetch_sync({ url = url })
|
|
|
|
if not response.succeeded then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local data = core.parse_json(response.data) or {}
|
|
|
|
|
|
|
|
local content_lookup = {}
|
|
|
|
for _, pkg in pairs(store.packages_full) do
|
|
|
|
content_lookup[pkg.id] = pkg
|
|
|
|
end
|
|
|
|
|
|
|
|
for id, raw_deps in pairs(data) do
|
|
|
|
local package2 = content_lookup[id:lower()]
|
|
|
|
if package2 and not package2.raw_deps then
|
|
|
|
package2.raw_deps = raw_deps
|
|
|
|
|
|
|
|
for _, dep in pairs(raw_deps) do
|
|
|
|
local packages = {}
|
|
|
|
for i=1, #dep.packages do
|
|
|
|
packages[#packages + 1] = content_lookup[dep.packages[i]:lower()]
|
|
|
|
end
|
|
|
|
dep.packages = packages
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return package.raw_deps
|
|
|
|
end
|
|
|
|
|
|
|
|
local function has_hard_deps(raw_deps)
|
|
|
|
for i=1, #raw_deps do
|
|
|
|
if not raw_deps[i].is_optional then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Recursively resolve dependencies, given the installed mods
|
|
|
|
local function resolve_dependencies_2(raw_deps, installed_mods, out)
|
|
|
|
local function resolve_dep(dep)
|
|
|
|
-- Check whether it's already installed
|
|
|
|
if installed_mods[dep.name] then
|
|
|
|
return {
|
|
|
|
is_optional = dep.is_optional,
|
|
|
|
name = dep.name,
|
|
|
|
installed = true,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Find exact name matches
|
|
|
|
local fallback
|
|
|
|
for _, package in pairs(dep.packages) do
|
|
|
|
if package.type ~= "game" then
|
|
|
|
if package.name == dep.name then
|
|
|
|
return {
|
|
|
|
is_optional = dep.is_optional,
|
|
|
|
name = dep.name,
|
|
|
|
installed = false,
|
|
|
|
package = package,
|
|
|
|
}
|
|
|
|
elseif not fallback then
|
|
|
|
fallback = package
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-09 17:57:19 +01:00
|
|
|
-- Otherwise, find the first mod that fulfills it
|
2020-12-23 15:42:18 +01:00
|
|
|
if fallback then
|
|
|
|
return {
|
|
|
|
is_optional = dep.is_optional,
|
|
|
|
name = dep.name,
|
|
|
|
installed = false,
|
|
|
|
package = fallback,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
is_optional = dep.is_optional,
|
|
|
|
name = dep.name,
|
|
|
|
installed = false,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, dep in pairs(raw_deps) do
|
|
|
|
if not dep.is_optional and not out[dep.name] then
|
|
|
|
local result = resolve_dep(dep)
|
|
|
|
out[dep.name] = result
|
|
|
|
if result and result.package and not result.installed then
|
|
|
|
local raw_deps2 = get_raw_dependencies(result.package)
|
|
|
|
if raw_deps2 then
|
|
|
|
resolve_dependencies_2(raw_deps2, installed_mods, out)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Resolve dependencies for a package, calls the recursive version.
|
|
|
|
local function resolve_dependencies(raw_deps, game)
|
|
|
|
assert(game)
|
|
|
|
|
|
|
|
local installed_mods = {}
|
|
|
|
|
|
|
|
local mods = {}
|
|
|
|
pkgmgr.get_game_mods(game, mods)
|
|
|
|
for _, mod in pairs(mods) do
|
|
|
|
installed_mods[mod.name] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, mod in pairs(pkgmgr.global_mods:get_list()) do
|
|
|
|
installed_mods[mod.name] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
local out = {}
|
|
|
|
if not resolve_dependencies_2(raw_deps, installed_mods, out) then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local retval = {}
|
|
|
|
for _, dep in pairs(out) do
|
|
|
|
retval[#retval + 1] = dep
|
|
|
|
end
|
|
|
|
|
|
|
|
table.sort(retval, function(a, b)
|
|
|
|
return a.name < b.name
|
|
|
|
end)
|
|
|
|
|
|
|
|
return retval
|
|
|
|
end
|
|
|
|
|
|
|
|
local install_dialog = {}
|
|
|
|
function install_dialog.get_formspec()
|
2022-09-12 19:24:54 +02:00
|
|
|
local selected_game, selected_game_idx = pkgmgr.find_by_gameid(core.settings:get("menu_last_game"))
|
|
|
|
if not selected_game_idx then
|
|
|
|
selected_game_idx = 1
|
|
|
|
selected_game = pkgmgr.games[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
local game_list = {}
|
|
|
|
for i, game in ipairs(pkgmgr.games) do
|
2022-09-16 12:58:46 +02:00
|
|
|
game_list[i] = core.formspec_escape(game.title)
|
2022-09-12 19:24:54 +02:00
|
|
|
end
|
|
|
|
|
2020-12-23 15:42:18 +01:00
|
|
|
local package = install_dialog.package
|
|
|
|
local raw_deps = install_dialog.raw_deps
|
|
|
|
local will_install_deps = install_dialog.will_install_deps
|
|
|
|
|
|
|
|
local deps_to_install = 0
|
|
|
|
local deps_not_found = 0
|
|
|
|
|
|
|
|
install_dialog.dependencies = resolve_dependencies(raw_deps, selected_game)
|
|
|
|
local formatted_deps = {}
|
|
|
|
for _, dep in pairs(install_dialog.dependencies) do
|
|
|
|
formatted_deps[#formatted_deps + 1] = "#fff"
|
2021-02-10 12:02:34 +01:00
|
|
|
formatted_deps[#formatted_deps + 1] = core.formspec_escape(dep.name)
|
2020-12-23 15:42:18 +01:00
|
|
|
if dep.installed then
|
|
|
|
formatted_deps[#formatted_deps + 1] = "#ccf"
|
|
|
|
formatted_deps[#formatted_deps + 1] = fgettext("Already installed")
|
|
|
|
elseif dep.package then
|
|
|
|
formatted_deps[#formatted_deps + 1] = "#cfc"
|
|
|
|
formatted_deps[#formatted_deps + 1] = fgettext("$1 by $2", dep.package.title, dep.package.author)
|
|
|
|
deps_to_install = deps_to_install + 1
|
|
|
|
else
|
|
|
|
formatted_deps[#formatted_deps + 1] = "#f00"
|
|
|
|
formatted_deps[#formatted_deps + 1] = fgettext("Not found")
|
|
|
|
deps_not_found = deps_not_found + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local message_bg = "#3333"
|
|
|
|
local message
|
|
|
|
if will_install_deps then
|
|
|
|
message = fgettext("$1 and $2 dependencies will be installed.", package.title, deps_to_install)
|
|
|
|
else
|
|
|
|
message = fgettext("$1 will be installed, and $2 dependencies will be skipped.", package.title, deps_to_install)
|
|
|
|
end
|
|
|
|
if deps_not_found > 0 then
|
|
|
|
message = fgettext("$1 required dependencies could not be found.", deps_not_found) ..
|
|
|
|
" " .. fgettext("Please check that the base game is correct.", deps_not_found) ..
|
|
|
|
"\n" .. message
|
|
|
|
message_bg = mt_color_orange
|
|
|
|
end
|
|
|
|
|
|
|
|
local formspec = {
|
|
|
|
"formspec_version[3]",
|
|
|
|
"size[7,7.85]",
|
|
|
|
"style[title;border=false]",
|
|
|
|
"box[0,0;7,0.5;#3333]",
|
|
|
|
"button[0,0;7,0.5;title;", fgettext("Install $1", package.title) , "]",
|
|
|
|
|
|
|
|
"container[0.375,0.70]",
|
|
|
|
|
|
|
|
"label[0,0.25;", fgettext("Base Game:"), "]",
|
2022-09-12 19:24:54 +02:00
|
|
|
"dropdown[2,0;4.25,0.5;selected_game;", table.concat(game_list, ","), ";", selected_game_idx, "]",
|
2020-12-23 15:42:18 +01:00
|
|
|
|
|
|
|
"label[0,0.8;", fgettext("Dependencies:"), "]",
|
|
|
|
|
|
|
|
"tablecolumns[color;text;color;text]",
|
|
|
|
"table[0,1.1;6.25,3;packages;", table.concat(formatted_deps, ","), "]",
|
|
|
|
|
|
|
|
"container_end[]",
|
|
|
|
|
|
|
|
"checkbox[0.375,5.1;will_install_deps;",
|
|
|
|
fgettext("Install missing dependencies"), ";",
|
|
|
|
will_install_deps and "true" or "false", "]",
|
|
|
|
|
|
|
|
"box[0,5.4;7,1.2;", message_bg, "]",
|
|
|
|
"textarea[0.375,5.5;6.25,1;;;", message, "]",
|
|
|
|
|
|
|
|
"container[1.375,6.85]",
|
|
|
|
"button[0,0;2,0.8;install_all;", fgettext("Install"), "]",
|
|
|
|
"button[2.25,0;2,0.8;cancel;", fgettext("Cancel"), "]",
|
|
|
|
"container_end[]",
|
|
|
|
}
|
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
return table.concat(formspec)
|
2020-12-23 15:42:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function install_dialog.handle_submit(this, fields)
|
|
|
|
if fields.cancel then
|
|
|
|
this:delete()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.will_install_deps ~= nil then
|
2021-02-10 12:02:34 +01:00
|
|
|
install_dialog.will_install_deps = core.is_yes(fields.will_install_deps)
|
2020-12-23 15:42:18 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.install_all then
|
2021-07-27 19:08:49 +02:00
|
|
|
queue_download(install_dialog.package, REASON_NEW)
|
2020-12-23 15:42:18 +01:00
|
|
|
|
|
|
|
if install_dialog.will_install_deps then
|
|
|
|
for _, dep in pairs(install_dialog.dependencies) do
|
|
|
|
if not dep.is_optional and not dep.installed and dep.package then
|
2021-07-27 19:08:49 +02:00
|
|
|
queue_download(dep.package, REASON_DEPENDENCY)
|
2020-12-23 15:42:18 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
this:delete()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-05-21 17:23:30 +02:00
|
|
|
if fields.selected_game then
|
2020-12-23 15:42:18 +01:00
|
|
|
for _, game in pairs(pkgmgr.games) do
|
2022-05-21 17:23:30 +02:00
|
|
|
if game.title == fields.selected_game then
|
2020-12-23 15:42:18 +01:00
|
|
|
core.settings:set("menu_last_game", game.id)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function install_dialog.create(package, raw_deps)
|
|
|
|
install_dialog.dependencies = nil
|
|
|
|
install_dialog.package = package
|
|
|
|
install_dialog.raw_deps = raw_deps
|
|
|
|
install_dialog.will_install_deps = true
|
2021-01-04 16:18:31 +01:00
|
|
|
return dialog_create("install_dialog",
|
2020-12-23 15:42:18 +01:00
|
|
|
install_dialog.get_formspec,
|
|
|
|
install_dialog.handle_submit,
|
|
|
|
nil)
|
|
|
|
end
|
|
|
|
|
2021-01-04 16:18:31 +01:00
|
|
|
|
|
|
|
local confirm_overwrite = {}
|
|
|
|
function confirm_overwrite.get_formspec()
|
|
|
|
local package = confirm_overwrite.package
|
|
|
|
|
2022-06-03 21:46:37 +02:00
|
|
|
return confirmation_formspec(
|
|
|
|
fgettext("\"$1\" already exists. Would you like to overwrite it?", package.name),
|
|
|
|
'install', fgettext("Overwrite"),
|
|
|
|
'cancel', fgettext("Cancel"))
|
2021-01-04 16:18:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function confirm_overwrite.handle_submit(this, fields)
|
|
|
|
if fields.cancel then
|
|
|
|
this:delete()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.install then
|
|
|
|
this:delete()
|
|
|
|
confirm_overwrite.callback()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function confirm_overwrite.create(package, callback)
|
|
|
|
assert(type(package) == "table")
|
|
|
|
assert(type(callback) == "function")
|
|
|
|
|
|
|
|
confirm_overwrite.package = package
|
|
|
|
confirm_overwrite.callback = callback
|
|
|
|
return dialog_create("confirm_overwrite",
|
|
|
|
confirm_overwrite.get_formspec,
|
|
|
|
confirm_overwrite.handle_submit,
|
|
|
|
nil)
|
|
|
|
end
|
|
|
|
|
2023-10-08 17:47:00 +02:00
|
|
|
local function install_or_update_package(this, package)
|
|
|
|
local install_parent
|
|
|
|
if package.type == "mod" then
|
|
|
|
install_parent = core.get_modpath()
|
|
|
|
elseif package.type == "game" then
|
|
|
|
install_parent = core.get_gamepath()
|
|
|
|
elseif package.type == "txp" then
|
|
|
|
install_parent = core.get_texturepath()
|
|
|
|
else
|
|
|
|
error("Unknown package type: " .. package.type)
|
|
|
|
end
|
|
|
|
|
2023-10-28 18:33:44 +02:00
|
|
|
if package.queued or package.downloading then
|
|
|
|
return
|
|
|
|
end
|
2023-10-08 17:47:00 +02:00
|
|
|
|
|
|
|
local function on_confirm()
|
|
|
|
local deps = get_raw_dependencies(package)
|
|
|
|
if deps and has_hard_deps(deps) then
|
|
|
|
local dlg = install_dialog.create(package, deps)
|
|
|
|
dlg:set_parent(this)
|
|
|
|
this:hide()
|
|
|
|
dlg:show()
|
|
|
|
else
|
|
|
|
queue_download(package, package.path and REASON_UPDATE or REASON_NEW)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if package.type == "mod" and #pkgmgr.games == 0 then
|
|
|
|
local dlg = messagebox("install_game",
|
|
|
|
fgettext("You need to install a game before you can install a mod"))
|
|
|
|
dlg:set_parent(this)
|
|
|
|
this:hide()
|
|
|
|
dlg:show()
|
|
|
|
elseif not package.path and core.is_dir(install_parent .. DIR_DELIM .. package.name) then
|
|
|
|
local dlg = confirm_overwrite.create(package, on_confirm)
|
|
|
|
dlg:set_parent(this)
|
|
|
|
this:hide()
|
|
|
|
dlg:show()
|
|
|
|
else
|
|
|
|
on_confirm()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-04 16:18:31 +01:00
|
|
|
|
2020-06-06 18:59:39 +02:00
|
|
|
local function get_file_extension(path)
|
|
|
|
local parts = path:split(".")
|
|
|
|
return parts[#parts]
|
|
|
|
end
|
|
|
|
|
2018-05-20 01:25:17 +02:00
|
|
|
local function get_screenshot(package)
|
2018-06-18 21:47:38 +02:00
|
|
|
if not package.thumbnail then
|
2018-05-20 01:25:17 +02:00
|
|
|
return defaulttexturedir .. "no_screenshot.png"
|
2018-06-18 21:47:38 +02:00
|
|
|
elseif screenshot_downloading[package.thumbnail] then
|
2018-05-20 01:25:17 +02:00
|
|
|
return defaulttexturedir .. "loading_screenshot.png"
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get tmp screenshot path
|
2020-06-06 18:59:39 +02:00
|
|
|
local ext = get_file_extension(package.thumbnail)
|
2018-05-20 01:25:17 +02:00
|
|
|
local filepath = screenshot_dir .. DIR_DELIM ..
|
2020-06-06 18:59:39 +02:00
|
|
|
("%s-%s-%s.%s"):format(package.type, package.author, package.name, ext)
|
2018-05-20 01:25:17 +02:00
|
|
|
|
|
|
|
-- Return if already downloaded
|
|
|
|
local file = io.open(filepath, "r")
|
|
|
|
if file then
|
|
|
|
file:close()
|
|
|
|
return filepath
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Show error if we've failed to download before
|
2018-06-18 21:47:38 +02:00
|
|
|
if screenshot_downloaded[package.thumbnail] then
|
2018-05-20 01:25:17 +02:00
|
|
|
return defaulttexturedir .. "error_screenshot.png"
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Download
|
|
|
|
|
|
|
|
local function download_screenshot(params)
|
|
|
|
return core.download_file(params.url, params.dest)
|
|
|
|
end
|
|
|
|
local function callback(success)
|
2018-06-18 21:47:38 +02:00
|
|
|
screenshot_downloading[package.thumbnail] = nil
|
|
|
|
screenshot_downloaded[package.thumbnail] = true
|
2018-05-20 01:25:17 +02:00
|
|
|
if not success then
|
|
|
|
core.log("warning", "Screenshot download failed for some reason")
|
|
|
|
end
|
2019-02-18 12:46:55 +01:00
|
|
|
ui.update()
|
2018-05-20 01:25:17 +02:00
|
|
|
end
|
|
|
|
if core.handle_async(download_screenshot,
|
2018-06-18 21:47:38 +02:00
|
|
|
{ dest = filepath, url = package.thumbnail }, callback) then
|
|
|
|
screenshot_downloading[package.thumbnail] = true
|
2018-05-20 01:25:17 +02:00
|
|
|
else
|
|
|
|
core.log("error", "ERROR: async event failed")
|
|
|
|
return defaulttexturedir .. "error_screenshot.png"
|
|
|
|
end
|
|
|
|
|
|
|
|
return defaulttexturedir .. "loading_screenshot.png"
|
|
|
|
end
|
|
|
|
|
2023-10-28 18:33:44 +02:00
|
|
|
local function fetch_pkgs()
|
2020-05-24 15:46:50 +02:00
|
|
|
local version = core.get_version()
|
|
|
|
local base_url = core.settings:get("contentdb_url")
|
2018-12-11 05:43:14 +01:00
|
|
|
local url = base_url ..
|
|
|
|
"/api/packages/?type=mod&type=game&type=txp&protocol_version=" ..
|
2023-10-28 18:33:44 +02:00
|
|
|
core.get_max_supp_proto() .. "&engine_version=" .. core.urlencode(version.string)
|
2019-02-03 18:54:56 +01:00
|
|
|
|
|
|
|
for _, item in pairs(core.settings:get("contentdb_flag_blacklist"):split(",")) do
|
|
|
|
item = item:trim()
|
|
|
|
if item ~= "" then
|
2023-10-28 18:33:44 +02:00
|
|
|
url = url .. "&hide=" .. core.urlencode(item)
|
2019-02-03 18:54:56 +01:00
|
|
|
end
|
|
|
|
end
|
2018-12-11 05:43:14 +01:00
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
local http = core.get_http_api()
|
2021-12-01 21:11:32 +01:00
|
|
|
local response = http.fetch_sync({ url = url })
|
2020-06-06 18:17:08 +02:00
|
|
|
if not response.succeeded then
|
|
|
|
return
|
|
|
|
end
|
2018-12-11 05:43:14 +01:00
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
local packages = core.parse_json(response.data)
|
|
|
|
if not packages or #packages == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local aliases = {}
|
2018-12-11 05:43:14 +01:00
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
for _, package in pairs(packages) do
|
2020-06-06 18:17:08 +02:00
|
|
|
local name_len = #package.name
|
2021-12-01 21:11:32 +01:00
|
|
|
-- This must match what store.update_paths() does!
|
|
|
|
package.id = package.author:lower() .. "/"
|
2020-06-06 18:17:08 +02:00
|
|
|
if package.type == "game" and name_len > 5 and package.name:sub(name_len - 4) == "_game" then
|
2021-12-01 21:11:32 +01:00
|
|
|
package.id = package.id .. package.name:sub(1, name_len - 5)
|
2020-06-06 18:17:08 +02:00
|
|
|
else
|
2021-12-01 21:11:32 +01:00
|
|
|
package.id = package.id .. package.name
|
2018-12-11 05:43:14 +01:00
|
|
|
end
|
2021-08-02 21:05:10 +02:00
|
|
|
|
2023-10-28 18:33:44 +02:00
|
|
|
package.url_part = core.urlencode(package.author) .. "/" .. core.urlencode(package.name)
|
2021-12-01 21:11:32 +01:00
|
|
|
|
2021-08-02 21:05:10 +02:00
|
|
|
if package.aliases then
|
|
|
|
for _, alias in ipairs(package.aliases) do
|
|
|
|
-- We currently don't support name changing
|
|
|
|
local suffix = "/" .. package.name
|
|
|
|
if alias:sub(-#suffix) == suffix then
|
2023-08-13 14:28:24 +02:00
|
|
|
aliases[alias:lower()] = package.id
|
2021-08-02 21:05:10 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-11 05:43:14 +01:00
|
|
|
end
|
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
return { packages = packages, aliases = aliases }
|
|
|
|
end
|
|
|
|
|
|
|
|
local function sort_and_filter_pkgs()
|
|
|
|
store.update_paths()
|
|
|
|
store.sort_packages()
|
|
|
|
store.filter_packages(search_string)
|
|
|
|
end
|
|
|
|
|
2023-10-08 17:47:00 +02:00
|
|
|
-- Resolves the package specification stored in auto_install_spec into an actual package.
|
|
|
|
-- May only be called after the package list has been loaded successfully.
|
|
|
|
local function resolve_auto_install_spec()
|
|
|
|
assert(store.load_ok)
|
|
|
|
|
|
|
|
if not auto_install_spec then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local resolved = nil
|
|
|
|
|
|
|
|
for _, pkg in ipairs(store.packages_full_unordered) do
|
2023-11-05 19:01:19 +01:00
|
|
|
if pkg.id == auto_install_spec then
|
2023-10-08 17:47:00 +02:00
|
|
|
resolved = pkg
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not resolved then
|
2023-11-09 19:54:47 +01:00
|
|
|
gamedata.errormessage = fgettext("The package $1 was not found.", auto_install_spec)
|
2023-10-08 17:47:00 +02:00
|
|
|
ui.update()
|
|
|
|
|
|
|
|
auto_install_spec = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return resolved
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Installs the package specified by auto_install_spec.
|
|
|
|
-- Only does something if:
|
|
|
|
-- a. The package list has been loaded successfully.
|
|
|
|
-- b. The store dialog is currently visible.
|
|
|
|
local function do_auto_install()
|
|
|
|
if not store.load_ok then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local pkg = resolve_auto_install_spec()
|
|
|
|
if not pkg then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local store_dlg = ui.find_by_name("store")
|
|
|
|
if not store_dlg or store_dlg.hidden then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
install_or_update_package(store_dlg, pkg)
|
|
|
|
auto_install_spec = nil
|
|
|
|
end
|
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
function store.load()
|
|
|
|
if store.load_ok then
|
|
|
|
sort_and_filter_pkgs()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if store.loading then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
store.loading = true
|
|
|
|
core.handle_async(
|
|
|
|
fetch_pkgs,
|
2023-10-28 18:33:44 +02:00
|
|
|
nil,
|
2023-08-13 14:28:24 +02:00
|
|
|
function(result)
|
|
|
|
if result then
|
2023-10-08 17:47:00 +02:00
|
|
|
store.load_ok = true
|
|
|
|
store.load_error = false
|
2023-08-13 14:28:24 +02:00
|
|
|
store.packages = result.packages
|
|
|
|
store.packages_full = result.packages
|
|
|
|
store.packages_full_unordered = result.packages
|
|
|
|
store.aliases = result.aliases
|
|
|
|
|
2023-10-08 17:47:00 +02:00
|
|
|
sort_and_filter_pkgs()
|
|
|
|
do_auto_install()
|
2023-08-13 14:28:24 +02:00
|
|
|
else
|
|
|
|
store.load_error = true
|
|
|
|
end
|
|
|
|
|
|
|
|
store.loading = false
|
2023-10-08 17:47:00 +02:00
|
|
|
ui.update()
|
2023-08-13 14:28:24 +02:00
|
|
|
end
|
|
|
|
)
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function store.update_paths()
|
|
|
|
local mod_hash = {}
|
|
|
|
pkgmgr.refresh_globals()
|
|
|
|
for _, mod in pairs(pkgmgr.global_mods:get_list()) do
|
2023-11-05 19:01:19 +01:00
|
|
|
local cdb_id = pkgmgr.get_contentdb_id(mod)
|
|
|
|
if cdb_id then
|
|
|
|
mod_hash[store.aliases[cdb_id] or cdb_id] = mod
|
2018-12-26 13:39:28 +01:00
|
|
|
end
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local game_hash = {}
|
|
|
|
pkgmgr.update_gamelist()
|
|
|
|
for _, game in pairs(pkgmgr.games) do
|
2023-11-05 19:01:19 +01:00
|
|
|
local cdb_id = pkgmgr.get_contentdb_id(game)
|
|
|
|
if cdb_id then
|
|
|
|
game_hash[store.aliases[cdb_id] or cdb_id] = game
|
2018-12-26 13:39:28 +01:00
|
|
|
end
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local txp_hash = {}
|
|
|
|
for _, txp in pairs(pkgmgr.get_texture_packs()) do
|
2023-11-05 19:01:19 +01:00
|
|
|
local cdb_id = pkgmgr.get_contentdb_id(txp)
|
|
|
|
if cdb_id then
|
|
|
|
txp_hash[store.aliases[cdb_id] or cdb_id] = txp
|
2018-12-26 13:39:28 +01:00
|
|
|
end
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
for _, package in pairs(store.packages_full) do
|
|
|
|
local content
|
|
|
|
if package.type == "mod" then
|
2019-01-30 17:43:28 +01:00
|
|
|
content = mod_hash[package.id]
|
2018-04-17 15:54:50 +02:00
|
|
|
elseif package.type == "game" then
|
2019-01-30 17:43:28 +01:00
|
|
|
content = game_hash[package.id]
|
2018-04-17 15:54:50 +02:00
|
|
|
elseif package.type == "txp" then
|
2019-01-30 17:43:28 +01:00
|
|
|
content = txp_hash[package.id]
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|
|
|
|
|
2019-01-30 17:43:28 +01:00
|
|
|
if content then
|
2018-04-17 15:54:50 +02:00
|
|
|
package.path = content.path
|
2019-01-30 17:43:28 +01:00
|
|
|
package.installed_release = content.release or 0
|
2018-04-17 15:54:50 +02:00
|
|
|
else
|
|
|
|
package.path = nil
|
2023-11-05 19:01:19 +01:00
|
|
|
package.installed_release = nil
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-28 12:24:36 +01:00
|
|
|
function store.sort_packages()
|
|
|
|
local ret = {}
|
|
|
|
|
2023-10-08 17:47:00 +02:00
|
|
|
local auto_install_pkg = resolve_auto_install_spec() -- can be nil
|
|
|
|
|
2021-01-28 12:24:36 +01:00
|
|
|
-- Add installed content
|
2023-10-08 17:47:00 +02:00
|
|
|
for _, pkg in ipairs(store.packages_full_unordered) do
|
|
|
|
if pkg.path and pkg ~= auto_install_pkg then
|
|
|
|
ret[#ret + 1] = pkg
|
2021-01-28 12:24:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-16 18:36:28 +02:00
|
|
|
-- Sort installed content first by "is there an update available?", then by title
|
2021-01-28 12:24:36 +01:00
|
|
|
table.sort(ret, function(a, b)
|
2023-09-16 18:36:28 +02:00
|
|
|
local a_updatable = a.installed_release < a.release
|
|
|
|
local b_updatable = b.installed_release < b.release
|
|
|
|
if a_updatable and not b_updatable then
|
|
|
|
return true
|
|
|
|
elseif b_updatable and not a_updatable then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2021-01-28 12:24:36 +01:00
|
|
|
return a.title < b.title
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- Add uninstalled content
|
2023-10-08 17:47:00 +02:00
|
|
|
for _, pkg in ipairs(store.packages_full_unordered) do
|
|
|
|
if not pkg.path and pkg ~= auto_install_pkg then
|
|
|
|
ret[#ret + 1] = pkg
|
2021-01-28 12:24:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-08 17:47:00 +02:00
|
|
|
-- Put the package that will be auto-installed at the very top
|
|
|
|
if auto_install_pkg then
|
|
|
|
table.insert(ret, 1, auto_install_pkg)
|
|
|
|
end
|
|
|
|
|
2021-01-28 12:24:36 +01:00
|
|
|
store.packages_full = ret
|
|
|
|
end
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
function store.filter_packages(query)
|
|
|
|
if query == "" and filter_type == 1 then
|
|
|
|
store.packages = store.packages_full
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local keywords = {}
|
|
|
|
for word in query:lower():gmatch("%S+") do
|
|
|
|
table.insert(keywords, word)
|
|
|
|
end
|
|
|
|
|
2020-08-17 20:09:33 +02:00
|
|
|
local function matches_keywords(package)
|
2018-04-17 15:54:50 +02:00
|
|
|
for k = 1, #keywords do
|
|
|
|
local keyword = keywords[k]
|
|
|
|
|
|
|
|
if string.find(package.name:lower(), keyword, 1, true) or
|
|
|
|
string.find(package.title:lower(), keyword, 1, true) or
|
|
|
|
string.find(package.author:lower(), keyword, 1, true) or
|
|
|
|
string.find(package.short_description:lower(), keyword, 1, true) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
store.packages = {}
|
|
|
|
for _, package in pairs(store.packages_full) do
|
2020-08-17 20:09:33 +02:00
|
|
|
if (query == "" or matches_keywords(package)) and
|
2018-04-17 15:54:50 +02:00
|
|
|
(filter_type == 1 or package.type == filter_types_type[filter_type]) then
|
|
|
|
store.packages[#store.packages + 1] = package
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
local function get_info_formspec(text)
|
|
|
|
local H = 9.5
|
|
|
|
return table.concat({
|
|
|
|
"formspec_version[6]",
|
|
|
|
"size[15.75,9.5]",
|
2023-09-16 18:36:28 +02:00
|
|
|
TOUCHSCREEN_GUI and "padding[0.01,0.01]" or "position[0.5,0.55]",
|
2023-08-13 14:28:24 +02:00
|
|
|
|
|
|
|
"label[4,4.35;", text, "]",
|
|
|
|
"container[0,", H - 0.8 - 0.375, "]",
|
|
|
|
"button[0.375,0;5,0.8;back;", fgettext("Back to Main Menu"), "]",
|
|
|
|
"container_end[]",
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-02-18 12:46:55 +01:00
|
|
|
function store.get_formspec(dlgdata)
|
2023-08-13 14:28:24 +02:00
|
|
|
if store.loading then
|
|
|
|
return get_info_formspec(fgettext("Loading..."))
|
|
|
|
end
|
|
|
|
if store.load_error then
|
|
|
|
return get_info_formspec(fgettext("No packages could be retrieved"))
|
|
|
|
end
|
|
|
|
assert(store.load_ok)
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
store.update_paths()
|
|
|
|
|
2019-02-18 12:46:55 +01:00
|
|
|
dlgdata.pagemax = math.max(math.ceil(#store.packages / num_per_page), 1)
|
|
|
|
if cur_page > dlgdata.pagemax then
|
2018-04-17 15:54:50 +02:00
|
|
|
cur_page = 1
|
|
|
|
end
|
|
|
|
|
2020-05-29 01:52:48 +02:00
|
|
|
local W = 15.75
|
|
|
|
local H = 9.5
|
2023-08-13 14:28:24 +02:00
|
|
|
local formspec = {
|
|
|
|
"formspec_version[6]",
|
|
|
|
"size[15.75,9.5]",
|
2023-09-16 18:36:28 +02:00
|
|
|
TOUCHSCREEN_GUI and "padding[0.01,0.01]" or "position[0.5,0.55]",
|
2023-08-13 14:28:24 +02:00
|
|
|
|
|
|
|
"style[status,downloading,queued;border=false]",
|
|
|
|
|
|
|
|
"container[0.375,0.375]",
|
|
|
|
"field[0,0;7.225,0.8;search_string;;", core.formspec_escape(search_string), "]",
|
2023-10-01 11:20:50 +02:00
|
|
|
"field_enter_after_edit[search_string;true]",
|
2023-08-13 14:28:24 +02:00
|
|
|
"image_button[7.3,0;0.8,0.8;", core.formspec_escape(defaulttexturedir .. "search.png"), ";search;]",
|
|
|
|
"image_button[8.125,0;0.8,0.8;", core.formspec_escape(defaulttexturedir .. "clear.png"), ";clear;]",
|
|
|
|
"dropdown[9.175,0;2.7875,0.8;type;", table.concat(filter_types_titles, ","), ";", filter_type, "]",
|
|
|
|
"container_end[]",
|
2019-02-03 02:14:15 +01:00
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
-- Page nav buttons
|
|
|
|
"container[0,", H - 0.8 - 0.375, "]",
|
|
|
|
"button[0.375,0;5,0.8;back;", fgettext("Back to Main Menu"), "]",
|
|
|
|
|
|
|
|
"container[", W - 0.375 - 0.8*4 - 2, ",0]",
|
|
|
|
"image_button[0,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "start_icon.png;pstart;]",
|
|
|
|
"image_button[0.8,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "prev_icon.png;pback;]",
|
|
|
|
"style[pagenum;border=false]",
|
|
|
|
"button[1.6,0;2,0.8;pagenum;", tonumber(cur_page), " / ", tonumber(dlgdata.pagemax), "]",
|
|
|
|
"image_button[3.6,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "next_icon.png;pnext;]",
|
|
|
|
"image_button[4.4,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "end_icon.png;pend;]",
|
|
|
|
"container_end[]",
|
2020-08-17 20:09:33 +02:00
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
"container_end[]",
|
|
|
|
}
|
|
|
|
|
|
|
|
if number_downloading > 0 then
|
|
|
|
formspec[#formspec + 1] = "button[12.5875,0.375;2.7875,0.8;downloading;"
|
|
|
|
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
|
2020-08-17 20:09:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
if num_avail_updates == 0 then
|
|
|
|
formspec[#formspec + 1] = "button[12.5875,0.375;2.7875,0.8;status;"
|
|
|
|
formspec[#formspec + 1] = fgettext("No updates")
|
|
|
|
formspec[#formspec + 1] = "]"
|
|
|
|
else
|
|
|
|
formspec[#formspec + 1] = "button[12.5875,0.375;2.7875,0.8;update_all;"
|
|
|
|
formspec[#formspec + 1] = fgettext("Update All [$1]", num_avail_updates)
|
2019-02-03 02:14:15 +01:00
|
|
|
formspec[#formspec + 1] = "]"
|
|
|
|
end
|
2023-08-13 14:28:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if #store.packages == 0 then
|
|
|
|
formspec[#formspec + 1] = "label[4,4.75;"
|
|
|
|
formspec[#formspec + 1] = fgettext("No results")
|
|
|
|
formspec[#formspec + 1] = "]"
|
2018-11-13 03:26:59 +01:00
|
|
|
end
|
2018-04-17 15:54:50 +02:00
|
|
|
|
2021-01-02 15:13:02 +01:00
|
|
|
-- download/queued tooltips always have the same message
|
|
|
|
local tooltip_colors = ";#dff6f5;#302c2e]"
|
|
|
|
formspec[#formspec + 1] = "tooltip[downloading;" .. fgettext("Downloading...") .. tooltip_colors
|
|
|
|
formspec[#formspec + 1] = "tooltip[queued;" .. fgettext("Queued") .. tooltip_colors
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
local start_idx = (cur_page - 1) * num_per_page + 1
|
|
|
|
for i=start_idx, math.min(#store.packages, start_idx+num_per_page-1) do
|
|
|
|
local package = store.packages[i]
|
2021-01-02 15:13:02 +01:00
|
|
|
local container_y = (i - start_idx) * 1.375 + (2*0.375 + 0.8)
|
2020-05-29 01:52:48 +02:00
|
|
|
formspec[#formspec + 1] = "container[0.375,"
|
2021-01-02 15:13:02 +01:00
|
|
|
formspec[#formspec + 1] = container_y
|
2018-04-17 15:54:50 +02:00
|
|
|
formspec[#formspec + 1] = "]"
|
|
|
|
|
|
|
|
-- image
|
2020-05-29 01:52:48 +02:00
|
|
|
formspec[#formspec + 1] = "image[0,0;1.5,1;"
|
2018-11-13 03:26:59 +01:00
|
|
|
formspec[#formspec + 1] = core.formspec_escape(get_screenshot(package))
|
2018-04-17 15:54:50 +02:00
|
|
|
formspec[#formspec + 1] = "]"
|
|
|
|
|
|
|
|
-- title
|
2020-05-29 01:52:48 +02:00
|
|
|
formspec[#formspec + 1] = "label[1.875,0.1;"
|
2018-11-08 22:55:38 +01:00
|
|
|
formspec[#formspec + 1] = core.formspec_escape(
|
2021-02-10 12:02:34 +01:00
|
|
|
core.colorize(mt_color_green, package.title) ..
|
|
|
|
core.colorize("#BFBFBF", " by " .. package.author))
|
2018-04-17 15:54:50 +02:00
|
|
|
formspec[#formspec + 1] = "]"
|
|
|
|
|
2020-05-29 01:52:48 +02:00
|
|
|
-- buttons
|
2023-09-16 18:36:28 +02:00
|
|
|
local description_width = W - 2.625 - 2 * 0.7 - 2 * 0.15
|
|
|
|
|
|
|
|
local second_base = "image_button[-1.55,0;0.7,0.7;" .. core.formspec_escape(defaulttexturedir)
|
|
|
|
local third_base = "image_button[-2.4,0;0.7,0.7;" .. core.formspec_escape(defaulttexturedir)
|
2020-05-29 01:52:48 +02:00
|
|
|
formspec[#formspec + 1] = "container["
|
|
|
|
formspec[#formspec + 1] = W - 0.375*2
|
|
|
|
formspec[#formspec + 1] = ",0.1]"
|
|
|
|
|
2020-05-27 18:02:09 +02:00
|
|
|
if package.downloading then
|
2021-01-02 15:13:02 +01:00
|
|
|
formspec[#formspec + 1] = "animated_image[-1.7,-0.15;1,1;downloading;"
|
|
|
|
formspec[#formspec + 1] = core.formspec_escape(defaulttexturedir)
|
|
|
|
formspec[#formspec + 1] = "cdb_downloading.png;3;400;]"
|
2020-08-17 20:09:33 +02:00
|
|
|
elseif package.queued then
|
2023-09-16 18:36:28 +02:00
|
|
|
formspec[#formspec + 1] = second_base
|
2022-04-24 23:49:07 +02:00
|
|
|
formspec[#formspec + 1] = "cdb_queued.png;queued;]"
|
2020-05-27 18:02:09 +02:00
|
|
|
elseif not package.path then
|
2021-01-02 15:13:02 +01:00
|
|
|
local elem_name = "install_" .. i .. ";"
|
|
|
|
formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#71aa34]"
|
2023-09-16 18:36:28 +02:00
|
|
|
formspec[#formspec + 1] = second_base .. "cdb_add.png;" .. elem_name .. "]"
|
2021-01-02 15:13:02 +01:00
|
|
|
formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Install") .. tooltip_colors
|
2018-05-16 22:52:12 +02:00
|
|
|
else
|
2019-01-06 16:54:22 +01:00
|
|
|
if package.installed_release < package.release then
|
|
|
|
-- The install_ action also handles updating
|
2021-01-02 15:13:02 +01:00
|
|
|
local elem_name = "install_" .. i .. ";"
|
|
|
|
formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#28ccdf]"
|
2023-09-16 18:36:28 +02:00
|
|
|
formspec[#formspec + 1] = third_base .. "cdb_update.png;" .. elem_name .. "]"
|
2021-01-02 15:13:02 +01:00
|
|
|
formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Update") .. tooltip_colors
|
2019-01-06 16:54:22 +01:00
|
|
|
|
2023-09-16 18:36:28 +02:00
|
|
|
description_width = description_width - 0.7 - 0.15
|
2021-01-02 15:13:02 +01:00
|
|
|
end
|
2023-09-16 18:36:28 +02:00
|
|
|
|
|
|
|
local elem_name = "uninstall_" .. i .. ";"
|
|
|
|
formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#a93b3b]"
|
|
|
|
formspec[#formspec + 1] = second_base .. "cdb_clear.png;" .. elem_name .. "]"
|
|
|
|
formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Uninstall") .. tooltip_colors
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|
2019-01-06 16:54:22 +01:00
|
|
|
|
2021-01-02 15:13:02 +01:00
|
|
|
local web_elem_name = "view_" .. i .. ";"
|
|
|
|
formspec[#formspec + 1] = "image_button[-0.7,0;0.7,0.7;" ..
|
|
|
|
core.formspec_escape(defaulttexturedir) .. "cdb_viewonline.png;" .. web_elem_name .. "]"
|
|
|
|
formspec[#formspec + 1] = "tooltip[" .. web_elem_name ..
|
|
|
|
fgettext("View more information in a web browser") .. tooltip_colors
|
2020-05-29 01:52:48 +02:00
|
|
|
formspec[#formspec + 1] = "container_end[]"
|
2020-05-17 20:09:10 +02:00
|
|
|
|
|
|
|
-- description
|
2020-05-29 01:52:48 +02:00
|
|
|
formspec[#formspec + 1] = "textarea[1.855,0.3;"
|
2020-05-17 20:09:10 +02:00
|
|
|
formspec[#formspec + 1] = tostring(description_width)
|
2020-05-29 01:52:48 +02:00
|
|
|
formspec[#formspec + 1] = ",0.8;;;"
|
2020-05-17 20:09:10 +02:00
|
|
|
formspec[#formspec + 1] = core.formspec_escape(package.short_description)
|
|
|
|
formspec[#formspec + 1] = "]"
|
2018-04-17 15:54:50 +02:00
|
|
|
|
|
|
|
formspec[#formspec + 1] = "container_end[]"
|
|
|
|
end
|
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
return table.concat(formspec)
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|
|
|
|
|
2019-02-18 12:46:55 +01:00
|
|
|
function store.handle_submit(this, fields)
|
2018-04-17 15:54:50 +02:00
|
|
|
if fields.search or fields.key_enter_field == "search_string" then
|
|
|
|
search_string = fields.search_string:trim()
|
|
|
|
cur_page = 1
|
|
|
|
store.filter_packages(search_string)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-12-19 22:00:20 +01:00
|
|
|
if fields.clear then
|
|
|
|
search_string = ""
|
|
|
|
cur_page = 1
|
|
|
|
store.filter_packages("")
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
if fields.back then
|
|
|
|
this:delete()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.pstart then
|
|
|
|
cur_page = 1
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.pend then
|
2019-02-18 12:46:55 +01:00
|
|
|
cur_page = this.data.pagemax
|
2018-04-17 15:54:50 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.pnext then
|
|
|
|
cur_page = cur_page + 1
|
2019-02-18 12:46:55 +01:00
|
|
|
if cur_page > this.data.pagemax then
|
2018-04-17 15:54:50 +02:00
|
|
|
cur_page = 1
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.pback then
|
|
|
|
if cur_page == 1 then
|
2019-02-18 12:46:55 +01:00
|
|
|
cur_page = this.data.pagemax
|
2018-04-17 15:54:50 +02:00
|
|
|
else
|
|
|
|
cur_page = cur_page - 1
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.type then
|
|
|
|
local new_type = table.indexof(filter_types_titles, fields.type)
|
|
|
|
if new_type ~= filter_type then
|
|
|
|
filter_type = new_type
|
2023-04-14 21:34:03 +02:00
|
|
|
cur_page = 1
|
2018-04-17 15:54:50 +02:00
|
|
|
store.filter_packages(search_string)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-17 20:09:33 +02:00
|
|
|
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
|
2021-07-27 19:08:49 +02:00
|
|
|
queue_download(package, REASON_UPDATE)
|
2020-08-17 20:09:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
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
|
2023-10-08 17:47:00 +02:00
|
|
|
install_or_update_package(this, package)
|
2018-04-17 15:54:50 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields["uninstall_" .. i] then
|
2020-12-23 15:42:18 +01:00
|
|
|
local dlg = create_delete_content_dlg(package)
|
|
|
|
dlg:set_parent(this)
|
2018-04-17 15:54:50 +02:00
|
|
|
this:hide()
|
2020-12-23 15:42:18 +01:00
|
|
|
dlg:show()
|
2018-04-17 15:54:50 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields["view_" .. i] then
|
2021-12-01 21:11:32 +01:00
|
|
|
local url = ("%s/packages/%s?protocol_version=%d"):format(
|
|
|
|
core.settings:get("contentdb_url"), package.url_part,
|
|
|
|
core.get_max_supp_proto())
|
2020-05-17 20:09:10 +02:00
|
|
|
core.open_url(url)
|
2018-04-17 15:54:50 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-10-08 17:47:00 +02:00
|
|
|
function store.handle_events(event)
|
|
|
|
if event == "DialogShow" then
|
2023-10-18 20:18:50 +02:00
|
|
|
-- On mobile, don't show the "MINETEST" header behind the dialog.
|
|
|
|
mm_game_theme.set_engine(TOUCHSCREEN_GUI)
|
|
|
|
|
2023-10-08 17:47:00 +02:00
|
|
|
-- If the store is already loaded, auto-install packages here.
|
|
|
|
do_auto_install()
|
2023-10-18 20:18:50 +02:00
|
|
|
|
2023-10-08 17:47:00 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Creates a ContentDB dialog.
|
|
|
|
---
|
|
|
|
--- @param type string | nil
|
|
|
|
--- Sets initial package filter. "game", "mod", "txp" or nil (no filter).
|
|
|
|
--- @param install_spec table | nil
|
2023-11-05 19:01:19 +01:00
|
|
|
--- ContentDB ID of package as returned by pkgmgr.get_contentdb_id().
|
2023-10-08 17:47:00 +02:00
|
|
|
--- Sets package to install or update automatically.
|
|
|
|
function create_store_dlg(type, install_spec)
|
2018-04-17 15:54:50 +02:00
|
|
|
search_string = ""
|
|
|
|
cur_page = 1
|
2020-06-04 22:53:26 +02:00
|
|
|
if type then
|
|
|
|
-- table.indexof does not work on tables that contain `nil`
|
|
|
|
for i, v in pairs(filter_types_type) do
|
|
|
|
if v == type then
|
|
|
|
filter_type = i
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2023-08-13 14:28:24 +02:00
|
|
|
else
|
|
|
|
filter_type = 1
|
2020-06-04 22:53:26 +02:00
|
|
|
end
|
|
|
|
|
2023-10-08 17:47:00 +02:00
|
|
|
-- Keep the old auto_install_spec if the caller doesn't specify one.
|
|
|
|
if install_spec then
|
|
|
|
auto_install_spec = install_spec
|
|
|
|
end
|
|
|
|
|
2023-08-13 14:28:24 +02:00
|
|
|
store.load()
|
2018-04-17 15:54:50 +02:00
|
|
|
|
|
|
|
return dialog_create("store",
|
|
|
|
store.get_formspec,
|
|
|
|
store.handle_submit,
|
2023-10-08 17:47:00 +02:00
|
|
|
store.handle_events)
|
2018-04-17 15:54:50 +02:00
|
|
|
end
|