feat(appstore): cache catalogs on the SD card
Select opens the cached catalog when its sidecar URL still matches, so browsing costs no network or TLS memory; Refresh fetches. Caches are replaced through a backup so an interrupted download cannot leave an empty catalog behind. Switch to structured log.debug/info/error milestones and add BinaryKeyboard, a two-button recursive text entry experiment.
This commit is contained in:
+184
-33
@@ -3,7 +3,6 @@
|
||||
local ROOT = "/.apps/AppStore"
|
||||
local REPOSITORIES_PATH = ROOT .. "/repositories.txt"
|
||||
local DEFAULT_REPOSITORIES_PATH = ROOT .. "/repositories.default.txt"
|
||||
local CATALOG_PATH = ROOT .. "/.catalog.txt"
|
||||
local MAX_CATALOG_BYTES = 32768
|
||||
local MAX_MANIFEST_BYTES = 16384
|
||||
local MAX_APP_BYTES = 16 * 1024 * 1024
|
||||
@@ -16,6 +15,8 @@ local apps = {}
|
||||
local selected = 1
|
||||
local message = ""
|
||||
local pendingRepository = nil
|
||||
local pendingApp = nil
|
||||
local pendingRefresh = false
|
||||
local needsDraw = true
|
||||
|
||||
local function removeFile(path)
|
||||
@@ -79,7 +80,12 @@ local function loadRepositories()
|
||||
return nil, "Invalid repository entry"
|
||||
end
|
||||
if #result >= 8 then return nil, "Too many repositories" end
|
||||
result[#result + 1] = { name = name, url = url }
|
||||
result[#result + 1] = {
|
||||
name = name,
|
||||
url = url,
|
||||
cachePath = ROOT .. "/.catalog-" .. (#result + 1) .. ".txt",
|
||||
cacheUrlPath = ROOT .. "/.catalog-" .. (#result + 1) .. ".url"
|
||||
}
|
||||
return true
|
||||
end)
|
||||
if not ok then return nil, err end
|
||||
@@ -96,6 +102,8 @@ end
|
||||
|
||||
local function download(url, destination, maxBytes, expectedSize, hash)
|
||||
removeFile(destination)
|
||||
collectgarbage("collect")
|
||||
log.debug("APPSTORE DOWNLOAD " .. destination)
|
||||
return net.download(url, destination, {
|
||||
maxBytes = maxBytes,
|
||||
expectedSize = expectedSize,
|
||||
@@ -103,19 +111,12 @@ local function download(url, destination, maxBytes, expectedSize, hash)
|
||||
})
|
||||
end
|
||||
|
||||
local function loadCatalog(repository)
|
||||
apps = {}
|
||||
collectgarbage("collect")
|
||||
renderStatus("Downloading catalog...")
|
||||
local bytes, err = download(repository.url, CATALOG_PATH, MAX_CATALOG_BYTES)
|
||||
if not bytes then return nil, err end
|
||||
|
||||
local function parseCatalog(repository, path)
|
||||
local baseUrl = repository.url:match("^(.*)/[^/]+$")
|
||||
if not baseUrl then return nil, "Invalid catalog URL" end
|
||||
|
||||
local result = {}
|
||||
local ok
|
||||
ok, err = readLines(CATALOG_PATH, "XTEINK-CATALOG|1", function(line)
|
||||
local ok, err = readLines(path or repository.cachePath, "XTEINK-CATALOG|1", function(line)
|
||||
local id, description, manifestPath, sizeText, hash =
|
||||
line:match("^([%w_-]+)|([^|]+)|([^|]+)|(%d+)|([0-9a-fA-F]+)$")
|
||||
local size = tonumber(sizeText)
|
||||
@@ -134,10 +135,107 @@ local function loadCatalog(repository)
|
||||
}
|
||||
return true
|
||||
end)
|
||||
removeFile(CATALOG_PATH)
|
||||
if not ok then return nil, err end
|
||||
if #result == 0 then return nil, "Repository has no apps" end
|
||||
apps = result
|
||||
log.info("APPSTORE CATALOG_READY " .. #apps)
|
||||
return true
|
||||
end
|
||||
|
||||
local function hasCatalogCache(repository)
|
||||
return fs.exists(repository.cachePath) and fs.readFile(repository.cacheUrlPath) == repository.url
|
||||
end
|
||||
|
||||
local function clearCatalogCache(repository)
|
||||
removeFile(repository.cachePath)
|
||||
removeFile(repository.cacheUrlPath)
|
||||
end
|
||||
|
||||
local function recoverCatalogCache(repository)
|
||||
local cacheBackup = repository.cachePath .. ".backup"
|
||||
local urlBackup = repository.cacheUrlPath .. ".backup"
|
||||
if not fs.exists(repository.cachePath) and fs.exists(cacheBackup) then
|
||||
fs.rename(cacheBackup, repository.cachePath)
|
||||
end
|
||||
if not fs.exists(repository.cacheUrlPath) and fs.exists(urlBackup) then
|
||||
fs.rename(urlBackup, repository.cacheUrlPath)
|
||||
end
|
||||
if fs.exists(repository.cachePath) and fs.exists(repository.cacheUrlPath) then
|
||||
removeFile(cacheBackup)
|
||||
removeFile(urlBackup)
|
||||
end
|
||||
end
|
||||
|
||||
local function replaceCatalogCache(repository, temporary, urlTemporary)
|
||||
recoverCatalogCache(repository)
|
||||
local cacheBackup = repository.cachePath .. ".backup"
|
||||
local urlBackup = repository.cacheUrlPath .. ".backup"
|
||||
local hadCache = fs.exists(repository.cachePath)
|
||||
local hadUrl = fs.exists(repository.cacheUrlPath)
|
||||
if hadCache then removeFile(cacheBackup) end
|
||||
if hadUrl then removeFile(urlBackup) end
|
||||
local ok, err
|
||||
|
||||
if hadCache then
|
||||
ok, err = fs.rename(repository.cachePath, cacheBackup)
|
||||
if not ok then return nil, err or "Could not back up catalog cache" end
|
||||
end
|
||||
if hadUrl then
|
||||
ok, err = fs.rename(repository.cacheUrlPath, urlBackup)
|
||||
if not ok then
|
||||
if hadCache then fs.rename(cacheBackup, repository.cachePath) end
|
||||
return nil, err or "Could not back up catalog URL"
|
||||
end
|
||||
end
|
||||
|
||||
ok, err = fs.rename(temporary, repository.cachePath)
|
||||
if ok then ok, err = fs.rename(urlTemporary, repository.cacheUrlPath) end
|
||||
if not ok then
|
||||
removeFile(repository.cachePath)
|
||||
removeFile(repository.cacheUrlPath)
|
||||
if hadCache then fs.rename(cacheBackup, repository.cachePath) end
|
||||
if hadUrl then fs.rename(urlBackup, repository.cacheUrlPath) end
|
||||
return nil, err or "Could not update catalog cache"
|
||||
end
|
||||
|
||||
removeFile(cacheBackup)
|
||||
removeFile(urlBackup)
|
||||
return true
|
||||
end
|
||||
|
||||
local function loadCatalog(repository, refresh)
|
||||
apps = {}
|
||||
collectgarbage("collect")
|
||||
|
||||
if not refresh and hasCatalogCache(repository) then
|
||||
renderStatus("Loading cached catalog...")
|
||||
local ok = parseCatalog(repository)
|
||||
if ok then return true end
|
||||
clearCatalogCache(repository)
|
||||
end
|
||||
|
||||
renderStatus("Downloading catalog...")
|
||||
local temporary = repository.cachePath .. ".download"
|
||||
local bytes, err = download(repository.url, temporary, MAX_CATALOG_BYTES)
|
||||
if not bytes then return nil, err end
|
||||
|
||||
local ok
|
||||
ok, err = parseCatalog(repository, temporary)
|
||||
if not ok then removeFile(temporary) return nil, err end
|
||||
|
||||
local urlTemporary = repository.cacheUrlPath .. ".download"
|
||||
removeFile(urlTemporary)
|
||||
if not fs.writeFile(urlTemporary, repository.url) then
|
||||
removeFile(temporary)
|
||||
return true
|
||||
end
|
||||
|
||||
ok, err = replaceCatalogCache(repository, temporary, urlTemporary)
|
||||
if not ok then
|
||||
removeFile(temporary)
|
||||
removeFile(urlTemporary)
|
||||
return nil, err
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -176,12 +274,14 @@ local function parseManifest(path)
|
||||
end
|
||||
|
||||
local function failInstall(stage, installError)
|
||||
log.error("APPSTORE INSTALL_FAILED " .. tostring(installError))
|
||||
local ok, cleanupError = clearTree(stage)
|
||||
if not ok then return nil, tostring(installError) .. "; cleanup failed: " .. tostring(cleanupError) end
|
||||
return nil, installError
|
||||
end
|
||||
|
||||
local function install(app)
|
||||
log.info("APPSTORE INSTALL_START " .. app.id)
|
||||
local target = "/.apps/" .. app.id
|
||||
if fs.exists(target) then return nil, "App is already installed" end
|
||||
|
||||
@@ -202,6 +302,7 @@ local function install(app)
|
||||
local files
|
||||
files, err = parseManifest(manifestPath)
|
||||
if not files then return failInstall(stage, err) end
|
||||
log.info("APPSTORE MANIFEST_READY " .. app.id .. " " .. #files)
|
||||
|
||||
for index, file in ipairs(files) do
|
||||
local parent = file.path:match("^(.*)/[^/]+$")
|
||||
@@ -219,17 +320,19 @@ local function install(app)
|
||||
|
||||
ok, err = fs.rename(stage, target)
|
||||
if not ok then return failInstall(stage, err) end
|
||||
log.info("APPSTORE INSTALL_DONE " .. app.id)
|
||||
return true, app.id .. " installed"
|
||||
end
|
||||
|
||||
local function showMessage(text, returnScreen)
|
||||
log.info("APPSTORE MESSAGE " .. tostring(text))
|
||||
message = text
|
||||
screen = "message"
|
||||
pendingRepository = returnScreen
|
||||
needsDraw = true
|
||||
end
|
||||
|
||||
local function drawList(title, items, label, description)
|
||||
local function drawList(title, items, label, description, rightHint)
|
||||
gui.clear()
|
||||
gui.drawCenteredText(FONT_UI_12, 28, title, COLOR_BLACK, STYLE_BOLD)
|
||||
local visible = math.max(1, math.floor((gui.height() - 130) / 38))
|
||||
@@ -245,13 +348,13 @@ local function drawList(title, items, label, description)
|
||||
if items[selected] and description then
|
||||
gui.drawCenteredText(FONT_UI_10, gui.height() - 82, description(items[selected]), COLOR_BLACK, STYLE_NORMAL)
|
||||
end
|
||||
gui.drawButtonHints("Back", "", "Select", "")
|
||||
gui.drawButtonHints("Back", "Select", "", rightHint or "")
|
||||
gui.refresh(REFRESH_HALF)
|
||||
end
|
||||
|
||||
local function render()
|
||||
if screen == "repositories" then
|
||||
drawList("Repositories", repositories, function(item) return item.name end)
|
||||
drawList("Repositories", repositories, function(item) return item.name end, nil, "Refresh")
|
||||
elseif screen == "apps" then
|
||||
drawList("Apps", apps, function(item) return item.id end, function(item) return item.description end)
|
||||
elseif screen == "connecting" then
|
||||
@@ -259,7 +362,7 @@ local function render()
|
||||
else
|
||||
gui.clear()
|
||||
gui.drawCenteredText(FONT_UI_12, math.floor(gui.height() / 2), message:sub(1, 100), COLOR_BLACK, STYLE_BOLD)
|
||||
gui.drawButtonHints("Back", "", "OK", "")
|
||||
gui.drawButtonHints("Back", "OK", "", "")
|
||||
gui.refresh(REFRESH_HALF)
|
||||
end
|
||||
needsDraw = false
|
||||
@@ -271,53 +374,101 @@ local function moveSelection(delta, items)
|
||||
needsDraw = true
|
||||
end
|
||||
|
||||
local function openRepository(refresh)
|
||||
local repository = repositories[selected]
|
||||
recoverCatalogCache(repository)
|
||||
if not refresh and hasCatalogCache(repository) then
|
||||
net.wifiConnect()
|
||||
apps = {}
|
||||
collectgarbage("collect")
|
||||
renderStatus("Loading cached catalog...")
|
||||
local ok = parseCatalog(repository)
|
||||
if ok then
|
||||
selected = 1
|
||||
screen = "apps"
|
||||
needsDraw = true
|
||||
return
|
||||
end
|
||||
clearCatalogCache(repository)
|
||||
end
|
||||
|
||||
pendingRepository = repository
|
||||
pendingRefresh = refresh
|
||||
net.wifiConnect()
|
||||
screen = "connecting"
|
||||
needsDraw = true
|
||||
end
|
||||
|
||||
function init()
|
||||
local ok, err = loadRepositories()
|
||||
if not ok then
|
||||
showMessage(err, "exit")
|
||||
return
|
||||
end
|
||||
log.info("APPSTORE READY")
|
||||
end
|
||||
|
||||
function draw()
|
||||
if screen == "connecting" then
|
||||
if input.wasPressed("back") then
|
||||
net.wifiDisconnect()
|
||||
local returnScreen = pendingApp and "apps" or "repositories"
|
||||
pendingRepository = nil
|
||||
selected = 1
|
||||
screen = "repositories"
|
||||
pendingApp = nil
|
||||
pendingRefresh = false
|
||||
if returnScreen == "repositories" then selected = 1 end
|
||||
screen = returnScreen
|
||||
needsDraw = true
|
||||
else
|
||||
local status = net.wifiStatus()
|
||||
if status == "connected" then
|
||||
local repository = pendingRepository
|
||||
pendingRepository = nil
|
||||
local ok, err = loadCatalog(repository)
|
||||
if ok then
|
||||
selected = 1
|
||||
screen = "apps"
|
||||
needsDraw = true
|
||||
log.info("APPSTORE WIFI_CONNECTED")
|
||||
if pendingApp then
|
||||
local app = pendingApp
|
||||
pendingApp = nil
|
||||
local ok, result = install(app)
|
||||
showMessage(result, "apps")
|
||||
else
|
||||
showMessage(err, "repositories")
|
||||
local repository = pendingRepository
|
||||
local refresh = pendingRefresh
|
||||
pendingRepository = nil
|
||||
pendingRefresh = false
|
||||
local ok, err = loadCatalog(repository, refresh)
|
||||
if ok then
|
||||
selected = 1
|
||||
screen = "apps"
|
||||
needsDraw = true
|
||||
else
|
||||
showMessage(err, "repositories")
|
||||
end
|
||||
end
|
||||
elseif status == "failed" then
|
||||
log.error("APPSTORE WIFI_FAILED")
|
||||
net.wifiDisconnect()
|
||||
showMessage("Wi-Fi connection failed", "repositories")
|
||||
local returnScreen = pendingApp and "apps" or "repositories"
|
||||
pendingApp = nil
|
||||
showMessage("Wi-Fi connection failed", returnScreen)
|
||||
end
|
||||
end
|
||||
elseif input.wasPressed("up") then
|
||||
moveSelection(-1, screen == "apps" and apps or repositories)
|
||||
elseif input.wasPressed("down") then
|
||||
moveSelection(1, screen == "apps" and apps or repositories)
|
||||
elseif input.wasPressed("right") and screen == "repositories" then
|
||||
openRepository(true)
|
||||
elseif input.wasPressed("confirm") then
|
||||
if screen == "repositories" then
|
||||
pendingRepository = repositories[selected]
|
||||
net.wifiConnect()
|
||||
screen = "connecting"
|
||||
needsDraw = true
|
||||
openRepository(false)
|
||||
elseif screen == "apps" then
|
||||
local ok, result = install(apps[selected])
|
||||
showMessage(result, "apps")
|
||||
if net.wifiStatus() == "connected" then
|
||||
local ok, result = install(apps[selected])
|
||||
showMessage(result, "apps")
|
||||
else
|
||||
pendingApp = apps[selected]
|
||||
net.wifiConnect()
|
||||
screen = "connecting"
|
||||
needsDraw = true
|
||||
end
|
||||
elseif screen == "message" then
|
||||
if pendingRepository == "exit" then
|
||||
net.wifiDisconnect()
|
||||
|
||||
Reference in New Issue
Block a user