Skip to content

Commit

Permalink
Add package update detection on Content tab (#13807)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwardy committed Oct 28, 2023
1 parent ddce858 commit 4ee32c5
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 54 deletions.
1 change: 1 addition & 0 deletions LICENSE.txt
Expand Up @@ -61,6 +61,7 @@ Zughy:
textures/base/pack/cdb_downloading.png
textures/base/pack/cdb_queued.png
textures/base/pack/cdb_update.png
textures/base/pack/cdb_update_cropped.png
textures/base/pack/cdb_viewonline.png
textures/base/pack/settings_btn.png
textures/base/pack/settings_info.png
Expand Down
10 changes: 7 additions & 3 deletions builtin/fstk/tabview.lua
Expand Up @@ -154,13 +154,17 @@ end
local function tab_header(self, size)
local toadd = ""

for i=1,#self.tablist,1 do

for i = 1, #self.tablist do
if toadd ~= "" then
toadd = toadd .. ","
end

toadd = toadd .. self.tablist[i].caption
local caption = self.tablist[i].caption
if type(caption) == "function" then
caption = caption(self)
end

toadd = toadd .. caption
end
return string.format("tabheader[%f,%f;%f,%f;%s;%s;%i;true;false]",
self.header_x, self.header_y, size.width, size.height, self.name, toadd, self.last_tab_index)
Expand Down
Expand Up @@ -74,15 +74,6 @@ local REASON_UPDATE = "update"
local REASON_DEPENDENCY = "dependency"


-- encodes for use as URL parameter or path component
local function urlencode(str)
return str:gsub("[^%a%d()._~-]", function(char)
return ("%%%02X"):format(char:byte())
end)
end
assert(urlencode("sample text?") == "sample%20text%3F")


local function get_download_url(package, reason)
local base_url = core.settings:get("contentdb_url")
local ret = base_url .. ("/packages/%s/releases/%d/download/"):format(
Expand Down Expand Up @@ -202,6 +193,10 @@ local function start_install(package, reason)
end

local function queue_download(package, reason)
if package.queued or package.downloading then
return
end

local max_concurrent_downloads = tonumber(core.settings:get("contentdb_max_concurrent_downloads"))
if number_downloading < math.max(max_concurrent_downloads, 1) then
start_install(package, reason)
Expand All @@ -222,7 +217,7 @@ local function get_raw_dependencies(package)
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")
local url = base_url .. url_fmt:format(package.url_part, core.get_max_supp_proto(), urlencode(version.string))
local url = base_url .. url_fmt:format(package.url_part, core.get_max_supp_proto(), core.urlencode(version.string))

local response = http.fetch_sync({ url = url })
if not response.succeeded then
Expand Down Expand Up @@ -547,6 +542,9 @@ local function install_or_update_package(this, package)
error("Unknown package type: " .. package.type)
end

if package.queued or package.downloading then
return
end

local function on_confirm()
local deps = get_raw_dependencies(package)
Expand Down Expand Up @@ -630,17 +628,17 @@ local function get_screenshot(package)
return defaulttexturedir .. "loading_screenshot.png"
end

local function fetch_pkgs(param)
local function fetch_pkgs()
local version = core.get_version()
local base_url = core.settings:get("contentdb_url")
local url = base_url ..
"/api/packages/?type=mod&type=game&type=txp&protocol_version=" ..
core.get_max_supp_proto() .. "&engine_version=" .. param.urlencode(version.string)
core.get_max_supp_proto() .. "&engine_version=" .. core.urlencode(version.string)

for _, item in pairs(core.settings:get("contentdb_flag_blacklist"):split(",")) do
item = item:trim()
if item ~= "" then
url = url .. "&hide=" .. param.urlencode(item)
url = url .. "&hide=" .. core.urlencode(item)
end
end

Expand All @@ -666,7 +664,7 @@ local function fetch_pkgs(param)
package.id = package.id .. package.name
end

package.url_part = param.urlencode(package.author) .. "/" .. param.urlencode(package.name)
package.url_part = core.urlencode(package.author) .. "/" .. core.urlencode(package.name)

if package.aliases then
for _, alias in ipairs(package.aliases) do
Expand Down Expand Up @@ -701,7 +699,8 @@ local function resolve_auto_install_spec()

for _, pkg in ipairs(store.packages_full_unordered) do
if pkg.author == auto_install_spec.author and
pkg.name == auto_install_spec.name then
(pkg.name == auto_install_spec.name or
(pkg.type == "game" and pkg.name == auto_install_spec.name .. "_game")) then
resolved = pkg
break
end
Expand Down Expand Up @@ -752,7 +751,7 @@ function store.load()
store.loading = true
core.handle_async(
fetch_pkgs,
{ urlencode = urlencode },
nil,
function(result)
if result then
store.load_ok = true
Expand Down
22 changes: 22 additions & 0 deletions builtin/mainmenu/content/init.lua
@@ -0,0 +1,22 @@
--Minetest
--Copyright (C) 2023 rubenwardy
--
--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.

local path = core.get_mainmenu_path() .. DIR_DELIM .. "content"

dofile(path .. DIR_DELIM .. "pkgmgr.lua")
dofile(path .. DIR_DELIM .. "update_detector.lua")
dofile(path .. DIR_DELIM .. "dlg_contentstore.lua")
51 changes: 38 additions & 13 deletions builtin/mainmenu/pkgmgr.lua → builtin/mainmenu/content/pkgmgr.lua
Expand Up @@ -177,6 +177,7 @@ function pkgmgr.get_mods(path, virtual_path, listing, modpack)
end
end

--------------------------------------------------------------------------------
function pkgmgr.get_texture_packs()
local txtpath = core.get_texturepath()
local txtpath_system = core.get_texturepath_share()
Expand All @@ -195,6 +196,23 @@ function pkgmgr.get_texture_packs()
return retval
end

--------------------------------------------------------------------------------
function pkgmgr.get_all()
local result = {}

for _, mod in pairs(pkgmgr.global_mods:get_list()) do
result[#result + 1] = mod
end
for _, game in pairs(pkgmgr.games) do
result[#result + 1] = game
end
for _, txp in pairs(pkgmgr.get_texture_packs()) do
result[#result + 1] = txp
end

return result
end

--------------------------------------------------------------------------------
function pkgmgr.get_folder_type(path)
local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
Expand Down Expand Up @@ -260,7 +278,10 @@ function pkgmgr.is_valid_modname(modpath)
end

--------------------------------------------------------------------------------
function pkgmgr.render_packagelist(render_list, use_technical_names, with_error)
--- @param render_list filterlist
--- @param use_technical_names boolean to show technical names instead of human-readable titles
--- @param with_icon table or nil, from virtual path to icon object
function pkgmgr.render_packagelist(render_list, use_technical_names, with_icon)
if not render_list then
if not pkgmgr.global_mods then
pkgmgr.refresh_globals()
Expand All @@ -273,10 +294,10 @@ function pkgmgr.render_packagelist(render_list, use_technical_names, with_error)
for i, v in ipairs(list) do
local color = ""
local icon = 0
local error = with_error and with_error[v.virtual_path]
local function update_error(val)
if val and (not error or (error.type == "warning" and val.type == "error")) then
error = val
local icon_info = with_icon and with_icon[v.virtual_path or v.path]
local function update_icon_info(val)
if val and (not icon_info or (icon_info.type == "warning" and val.type == "error")) then
icon_info = val
end
end

Expand All @@ -286,8 +307,8 @@ function pkgmgr.render_packagelist(render_list, use_technical_names, with_error)

for j = 1, #rawlist do
if rawlist[j].modpack == list[i].name then
if with_error then
update_error(with_error[rawlist[j].virtual_path])
if with_icon then
update_icon_info(with_icon[rawlist[j].virtual_path or rawlist[j].path])
end

if rawlist[j].enabled then
Expand All @@ -303,10 +324,10 @@ function pkgmgr.render_packagelist(render_list, use_technical_names, with_error)
color = mt_color_blue

local rawlist = render_list:get_raw_list()
if v.type == "game" and with_error then
if v.type == "game" and with_icon then
for j = 1, #rawlist do
if rawlist[j].is_game_content then
update_error(with_error[rawlist[j].virtual_path])
update_icon_info(with_icon[rawlist[j].virtual_path or rawlist[j].path])
end
end
end
Expand All @@ -315,13 +336,17 @@ function pkgmgr.render_packagelist(render_list, use_technical_names, with_error)
color = mt_color_green
end

if error then
if error.type == "warning" then
if icon_info then
if icon_info.type == "warning" then
color = mt_color_orange
icon = 2
else
elseif icon_info.type == "error" then
color = mt_color_red
icon = 3
elseif icon_info.type == "update" then
icon = 4
else
error("Unknown icon type " .. icon_info.type)
end
end

Expand All @@ -332,7 +357,7 @@ function pkgmgr.render_packagelist(render_list, use_technical_names, with_error)
retval[#retval + 1] = "0"
end

if with_error then
if with_icon then
retval[#retval + 1] = icon
end

Expand Down
Expand Up @@ -57,7 +57,7 @@ local function reset()
end

setfenv(loadfile("builtin/common/misc_helpers.lua"), env)()
setfenv(loadfile("builtin/mainmenu/pkgmgr.lua"), env)()
setfenv(loadfile("builtin/mainmenu/content/pkgmgr.lua"), env)()

function env.pkgmgr.update_gamelist()
table.insert(calls, { "update_gamelist" })
Expand Down

0 comments on commit 4ee32c5

Please sign in to comment.