mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 22:05:59 +00:00
feat(proton): offer DroidDeck-Proton test builds in Components (#2831)
Components > Proton offers a DroidDeck-Proton pre-release that is ahead of Stable through the existing channel toggle. Automatic installs and updates stay on Stable.
This commit is contained in:
1 parent
f180d98da7
commit
c35da09d1a
40 files changed
+297
-76
No files matched your search
@@ -9,13 +9,15 @@ import com.droiddeck.launcher.core.Downloader
|
||||
import com.droiddeck.launcher.core.FileUtils
|
||||
import com.droiddeck.launcher.runtime.LinuxRuntime
|
||||
import java.io.File
|
||||
import java.util.Locale
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
|
||||
object ProtonExtras {
|
||||
private const val TAG = "ProtonExtras"
|
||||
private const val NEED_BYTES = 4L * 1024 * 1024 * 1024
|
||||
private const val RELEASES = "https://api.github.com/repos/%s/releases?per_page=15"
|
||||
private const val RELEASES = "https://api.github.com/repos/%s/releases?per_page=15&page=%d"
|
||||
private const val RELEASE_PAGES = 5
|
||||
const val CHECK_EVERY_MS = 60L * 60 * 1000
|
||||
@Volatile
|
||||
var installInProgress = false
|
||||
@@ -104,6 +106,12 @@ object ProtonExtras {
|
||||
fun cachedLatest(context: Context, tool: Tool): String? =
|
||||
readJson(latestFile(context)).optJSONObject(tool.id)?.optString("tag")?.ifEmpty { null }
|
||||
|
||||
fun testBuild(context: Context, tool: Tool): String? =
|
||||
testFor(installedTags(context, tool), readJson(latestFile(context)).optJSONObject(tool.id)?.optString("test")?.ifEmpty { null })
|
||||
|
||||
internal fun testFor(installed: List<String>, test: String?): String? =
|
||||
test?.takeIf { it !in installed && (installed.isEmpty() || updateFor(installed, it) != null) }
|
||||
|
||||
private val checkLock = Any()
|
||||
private const val RETRY_MS = 15L * 60 * 1000
|
||||
private const val USER_SETTINGS = "user_settings.py"
|
||||
@@ -120,22 +128,27 @@ object ProtonExtras {
|
||||
if (due.isEmpty() || !force && (now - lastAttempt) in 0 until RETRY_MS) return false
|
||||
lastAttempt = now
|
||||
for (tool in due) {
|
||||
val tag = findLatestAsset(tool)?.tag ?: continue
|
||||
cache.put(tool.id, JSONObject().put("tag", tag).put("at", now))
|
||||
updateLatest(cache, tool, releases(tool), now)
|
||||
}
|
||||
runCatching { file.writeText(cache.toString()) }
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
private fun record(context: Context, tool: Tool, dir: String?, tag: String) {
|
||||
internal fun recordLatest(latest: JSONObject, tool: Tool, tag: String, test: Boolean, now: Long): Boolean {
|
||||
if (test) return false
|
||||
val entry = latest.optJSONObject(tool.id)
|
||||
val known = entry?.optString("tag")
|
||||
if (!known.isNullOrEmpty() && compareVersions(tag, known) < 0) return false
|
||||
val ahead = entry?.optString("test")?.takeIf { it.isNotEmpty() && compareVersions(it, tag) > 0 }
|
||||
latest.put(tool.id, JSONObject().put("tag", tag).put("at", now).putOpt("test", ahead))
|
||||
return true
|
||||
}
|
||||
|
||||
private fun record(context: Context, tool: Tool, dir: String?, tag: String, test: Boolean) {
|
||||
runCatching {
|
||||
val latest = readJson(latestFile(context))
|
||||
val known = latest.optJSONObject(tool.id)?.optString("tag")
|
||||
if (known.isNullOrEmpty() || compareVersions(tag, known) >= 0) {
|
||||
latest.put(tool.id, JSONObject().put("tag", tag).put("at", System.currentTimeMillis()))
|
||||
latestFile(context).writeText(latest.toString())
|
||||
}
|
||||
if (recordLatest(latest, tool, tag, test, System.currentTimeMillis())) latestFile(context).writeText(latest.toString())
|
||||
if (dir != null) {
|
||||
val releases = readJson(releasesFile(context))
|
||||
val present = toolsDir(context).list()?.toSet() ?: emptySet()
|
||||
@@ -208,17 +221,17 @@ object ProtonExtras {
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun install(context: Context, tool: Tool, onProgress: (String, Int) -> Unit): String? {
|
||||
fun install(context: Context, tool: Tool, test: Boolean = false, onProgress: (String, Int) -> Unit): String? {
|
||||
if (installInProgress) return context.getString(R.string.pextra_busy)
|
||||
installInProgress = true
|
||||
return try { installNow(context, tool, onProgress) } finally { installInProgress = false }
|
||||
return try { installNow(context, tool, test, onProgress) } finally { installInProgress = false }
|
||||
}
|
||||
|
||||
private fun installNow(context: Context, tool: Tool, onProgress: (String, Int) -> Unit): String? {
|
||||
private fun installNow(context: Context, tool: Tool, test: Boolean, onProgress: (String, Int) -> Unit): String? {
|
||||
if (SessionState.running) return context.getString(R.string.pextra_stop_session)
|
||||
if (!LinuxRuntime.isInstalled(context)) return context.getString(R.string.user_apps_runtime_required)
|
||||
|
||||
val asset = findLatestAsset(tool) ?: return context.getString(R.string.pextra_no_release, tool.name)
|
||||
val asset = releases(tool)?.let { pickAsset(tool, it, test && tool.id == OWN) } ?: return context.getString(R.string.pextra_no_release, tool.name)
|
||||
val downloads = File(context.filesDir, "proton-downloads").apply { mkdirs() }
|
||||
val archive = File(downloads, "${tool.id}-${asset.name}")
|
||||
if (asset.size > 0 && archive.length() > asset.size) archive.delete()
|
||||
@@ -313,7 +326,7 @@ object ProtonExtras {
|
||||
else {
|
||||
archive.delete()
|
||||
unqueue(context, tool)
|
||||
record(context, tool, placed, asset.tag)
|
||||
record(context, tool, placed, asset.tag, test)
|
||||
if (tool.id == OWN) replaceOlder(context, tool)
|
||||
if (EsyncPacks.enabled(context)) {
|
||||
onProgress(context.getString(R.string.esync_fetching), -1)
|
||||
@@ -331,36 +344,68 @@ object ProtonExtras {
|
||||
}
|
||||
}
|
||||
|
||||
private fun findLatestAsset(tool: Tool): Asset? {
|
||||
val releases = Downloader.downloadString(RELEASES.format(tool.repo)) ?: return null
|
||||
return try {
|
||||
pickAsset(tool, JSONArray(releases))
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "release metadata for ${tool.id}", e)
|
||||
null
|
||||
private fun releases(tool: Tool): JSONArray? = releases(tool) { page ->
|
||||
Downloader.downloadString(RELEASES.format(Locale.ROOT, tool.repo, page))?.let { body ->
|
||||
try {
|
||||
JSONArray(body)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "release metadata for ${tool.id}", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun pickAsset(tool: Tool, array: JSONArray): Asset? {
|
||||
for (i in 0 until array.length()) {
|
||||
val release = array.getJSONObject(i)
|
||||
if (release.optBoolean("draft", false) || release.optBoolean("prerelease", false)) continue
|
||||
val assets = release.optJSONArray("assets") ?: continue
|
||||
val entries = (0 until assets.length()).map { assets.getJSONObject(it) }
|
||||
val archive = entries.sortedBy { it.optString("name") }.firstOrNull { tool.assetPattern.containsMatchIn(it.optString("name")) } ?: continue
|
||||
internal fun releases(tool: Tool, fetch: (Int) -> JSONArray?): JSONArray? {
|
||||
val all = fetch(1) ?: return null
|
||||
var page = 1
|
||||
var last = all.length()
|
||||
while (last > 0 && page < RELEASE_PAGES && pickAsset(tool, all) == null) {
|
||||
val next = fetch(++page) ?: return null
|
||||
for (i in 0 until next.length()) all.put(next.opt(i))
|
||||
last = next.length()
|
||||
}
|
||||
return all
|
||||
}
|
||||
|
||||
internal fun pickAsset(tool: Tool, array: JSONArray, test: Boolean = false): Asset? {
|
||||
val candidates = (0 until array.length()).asSequence()
|
||||
.mapNotNull { array.optJSONObject(it) }
|
||||
.filter { !it.optBoolean("draft", false) && it.optBoolean("prerelease", false) == test }
|
||||
if (!test) return candidates.firstNotNullOfOrNull { assetOf(tool, it) }
|
||||
val stable = pickAsset(tool, array)?.tag ?: return null
|
||||
return candidates
|
||||
.filter { it.optString("tag_name").startsWith(tool.prefix) && updateFor(listOf(stable), it.optString("tag_name")) != null }
|
||||
.mapNotNull { assetOf(tool, it, requireChecksum = true) }
|
||||
.maxWithOrNull { a, b -> compareVersions(a.tag, b.tag) }
|
||||
}
|
||||
|
||||
internal fun updateLatest(cache: JSONObject, tool: Tool, array: JSONArray?, now: Long) {
|
||||
if (array == null) return
|
||||
val test = if (tool.id == OWN) pickAsset(tool, array, test = true)?.tag else null
|
||||
val tag = pickAsset(tool, array)?.tag
|
||||
if (tag == null) {
|
||||
cache.optJSONObject(tool.id)?.remove("test")
|
||||
return
|
||||
}
|
||||
cache.put(tool.id, JSONObject().put("tag", tag).put("at", now).putOpt("test", test))
|
||||
}
|
||||
|
||||
private fun assetOf(tool: Tool, release: JSONObject, requireChecksum: Boolean = false): Asset? = try {
|
||||
val assets = release.optJSONArray("assets")
|
||||
val entries = assets?.let { list -> (0 until list.length()).mapNotNull { list.optJSONObject(it) } }.orEmpty()
|
||||
entries.sortedBy { it.optString("name") }.filter { tool.assetPattern.containsMatchIn(it.optString("name")) }.mapNotNull { archive ->
|
||||
val name = archive.optString("name")
|
||||
val stem = name.substringBefore(".tar")
|
||||
val checksum = entries.firstOrNull {
|
||||
it.optString("name").startsWith(stem) && it.optString("name").contains("sha512", ignoreCase = true)
|
||||
}?.optString("browser_download_url")?.takeIf { it.startsWith("http") }
|
||||
return Asset(
|
||||
release.optString("tag_name"), name,
|
||||
archive.optString("browser_download_url").takeIf { it.startsWith("http") } ?: continue, checksum,
|
||||
archive.optLong("size", 0L),
|
||||
Hashes.githubSha256(archive.optString("digest")),
|
||||
)
|
||||
}
|
||||
return null
|
||||
archive.optString("browser_download_url").takeIf { it.startsWith("http") }?.let { url ->
|
||||
Asset(release.optString("tag_name"), name, url, checksum, archive.optLong("size", 0L), Hashes.githubSha256(archive.optString("digest")))
|
||||
}
|
||||
}.let { found -> if (requireChecksum) found.firstOrNull { it.sha256 != null || it.sha512 != null } else found.firstOrNull() }
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "release metadata for ${tool.id}", e)
|
||||
null
|
||||
}
|
||||
|
||||
private fun requestLines(context: Context): List<String> =
|
||||
|
||||
Reference in new issue
Block a user