feat: DroidDeck-Proton is downloaded, kept up to date and used by default (#1540)

This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-10-25 10:09:22 -04:00
1 parent 2a69505199
commit 3fdc74051c
12 files changed
+389 -16

No files matched your search

@@ -780,6 +780,43 @@ object ComponentsManager {
true
}
fun carryOver(context: Context, fromDir: String, toDir: String) {
synchronized(lock) {
val target = protons(context).firstOrNull { it.id == safeName(toDir) } ?: return
val state = loadState(context)
val wanted = carried(state, safeName(fromDir)) { File(packagesDir(context), it).isFile }
val active = state.sub("active").optJSONObject(target.id)
val queued = state.sub("queued").sub(target.id)
var changed = false
wanted.forEach { (comp, entry) ->
if (active?.has(comp) != true && !queued.has(comp)) {
queued.put(comp, entry)
changed = true
}
}
if (queued.length() == 0) state.sub("queued").remove(target.id)
if (changed) saveState(context, state)
}
}
internal fun carried(state: JSONObject, from: String, present: (String) -> Boolean): Map<String, JSONObject> = buildMap {
state.optJSONObject("active")?.optJSONObject(from)?.let { active ->
active.keys().forEach { comp ->
val entry = active.optJSONObject(comp) ?: return@forEach
val file = entry.optString("file")
if (comp in COMPONENTS && file.endsWith(".wcp") && present(file)) {
put(comp, JSONObject().put("kind", "package").put("file", file).put("label", entry.optString("label")))
}
}
}
state.optJSONObject("queued")?.optJSONObject(from)?.let { queued ->
queued.keys().forEach { comp ->
val entry = queued.optJSONObject(comp) ?: return@forEach
if (comp in COMPONENTS && entry.optString("kind") == "package" && present(entry.optString("file"))) put(comp, entry)
}
}
}
fun forgetProton(context: Context, dirName: String) {
synchronized(lock) {
val id = safeName(dirName)
@@ -114,12 +114,19 @@ object ProtonDefault {
return if (SessionState.running && state.live && (tool == null || state.pending != tool.name)) Outcome.LIVE else Outcome.NEXT_START
}
internal fun movedGames(choices: Map<String, GameChoice?>, from: Set<String>, to: String): Map<String, GameChoice?>? {
if (choices.values.none { it != null && !it.valve && it.dir in from }) return null
return choices.mapValues { (_, choice) -> if (choice != null && !choice.valve && choice.dir in from) GameChoice(false, to) else choice }
}
@Synchronized
fun requestGame(context: Context, scope: String, proton: ComponentsManager.Proton?): Outcome {
require(GameEnvironment.validScope(scope))
fun moveGames(context: Context, from: Set<String>, to: String) {
val d = dir(context)
val choices = parseGames(read(File(d, GAMES))).toMutableMap()
choices[scope] = proton?.let { GameChoice(it.valve, it.dir.name) }
val moved = movedGames(parseGames(read(File(d, GAMES))), from, to) ?: return
writeGames(d, moved)
}
private fun writeGames(d: File, choices: Map<String, GameChoice?>) {
val games = JSONObject()
choices.forEach { (id, choice) ->
games.put(id, choice?.let { JSONObject().put("valve", it.valve).put("dir", it.dir) } ?: JSONObject.NULL)
@@ -134,6 +141,15 @@ object ProtonDefault {
Log.w(TAG, "could not write the per-game Proton request", e)
throw e
}
}
@Synchronized
fun requestGame(context: Context, scope: String, proton: ComponentsManager.Proton?): Outcome {
require(GameEnvironment.validScope(scope))
val d = dir(context)
val choices = parseGames(read(File(d, GAMES))).toMutableMap()
choices[scope] = proton?.let { GameChoice(it.valve, it.dir.name) }
writeGames(d, choices)
val state = state(context)
val tool = proton?.let { selected -> state.tools.firstOrNull { it.valve == selected.valve && it.dir == selected.dir.name } }
return if (SessionState.running && state.live && (proton == null || tool != null && state.pending != tool.name)) {
@@ -16,16 +16,20 @@ 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"
const val CHECK_EVERY_MS = 6L * 60 * 60 * 1000
const val CHECK_EVERY_MS = 60L * 60 * 1000
@Volatile
var installInProgress = false
private set
@Volatile
private var replacing = false
class Tool(val id: String, val name: String, val prefix: String, val repo: String, val assetPattern: Regex)
internal data class Asset(val tag: String, val name: String, val url: String, val sha512: String?, val size: Long,
val sha256: String? = null)
const val OWN = "droiddeck"
val tools = listOf(
Tool("ge", "GE-Proton", "GE-Proton", "GloriousEggroll/proton-ge-custom", Regex("aarch64\\.tar\\.(gz|xz)$")),
Tool("cachyos", "proton-cachyos", "proton-cachyos", "CachyOS/proton-cachyos", Regex("arm64\\.tar\\.(gz|xz)$")),
@@ -78,11 +82,23 @@ object ProtonExtras {
fun update(context: Context, tool: Tool): String? = updateFor(installedTags(context, tool), cachedLatest(context, tool))
fun missing(context: Context, tool: Tool): String? =
if (tool.id == OWN && !declined(context) && installedAll(context, tool).isEmpty()) cachedLatest(context, tool) else null
private fun choices(context: Context) = context.getSharedPreferences("proton-auto-updates", Context.MODE_PRIVATE)
fun declined(context: Context): Boolean = choices(context).getBoolean("$OWN.declined", false)
fun setDeclined(context: Context, declined: Boolean) {
choices(context).edit().putBoolean("$OWN.declined", declined).apply()
}
fun cachedLatest(context: Context, tool: Tool): String? =
readJson(latestFile(context)).optJSONObject(tool.id)?.optString("tag")?.ifEmpty { null }
private val checkLock = Any()
private const val RETRY_MS = 15L * 60 * 1000
private const val USER_SETTINGS = "user_settings.py"
private var lastAttempt = 0L
fun checkLatest(context: Context, force: Boolean = false, now: Long = System.currentTimeMillis()): Boolean {
@@ -145,6 +161,44 @@ object ProtonExtras {
}
}
private fun replaceOlder(context: Context, tool: Tool) {
val all = installedAll(context, tool)
val newest = all.lastOrNull() ?: return
val older = all.dropLast(1)
if (older.isEmpty()) return
replacing = true
try {
for (old in older.asReversed()) {
if (SessionState.running) break
if (runCatching { ComponentsManager.carryOver(context, old, newest) }.onFailure { Log.w(TAG, "components of $old", it) }.isFailure) continue
if (runCatching { keepUserSettings(context, old, newest) }.onFailure { Log.w(TAG, "user settings of $old", it) }.isFailure) continue
if (runCatching { ProtonDefault.moveGames(context, setOf(old), newest) }.onFailure { Log.w(TAG, "per-game picks of $old", it) }.isFailure) continue
if (File(toolsDir(context), old).deleteRecursively()) ComponentsManager.forgetProton(context, old)
else Log.w(TAG, "could not remove $old")
}
} finally {
replacing = false
}
}
private fun keepUserSettings(context: Context, old: String, newest: String) {
val from = File(File(toolsDir(context), old), USER_SETTINGS)
val to = File(File(toolsDir(context), newest), USER_SETTINGS)
if (from.isFile && !to.exists()) from.copyTo(to)
}
fun tidy(context: Context) {
val tool = tools.firstOrNull { it.id == OWN } ?: return
if (installedAll(context, tool).size > 1) replaceOlder(context, tool)
}
fun awaitReplacement(timeoutMs: Long) {
val until = System.currentTimeMillis() + timeoutMs
while (replacing && System.currentTimeMillis() < until) {
try { Thread.sleep(50) } catch (_: InterruptedException) { Thread.currentThread().interrupt(); return }
}
}
@Synchronized
fun install(context: Context, tool: Tool, onProgress: (String, Int) -> Unit): String? {
if (installInProgress) return context.getString(R.string.pextra_busy)
@@ -246,6 +300,7 @@ object ProtonExtras {
archive.delete()
unqueue(context, tool)
record(context, tool, placed, asset.tag)
if (tool.id == OWN) replaceOlder(context, tool)
if (EsyncPacks.enabled(context)) {
onProgress(context.getString(R.string.esync_fetching), -1)
try {
@@ -372,6 +372,7 @@ class SessionService : Service() {
acquireLocks()
Thread({
OrphanReaper.reap("session starting")
ProtonExtras.awaitReplacement(120_000)
runSession(gen)
}, "session-start").start()
return START_NOT_STICKY