diff --git a/app/src/main/java/com/droiddeck/launcher/session/ProtonDefault.kt b/app/src/main/java/com/droiddeck/launcher/session/ProtonDefault.kt index 8e5f462ab6..7d26e80f2e 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/ProtonDefault.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/ProtonDefault.kt @@ -14,7 +14,8 @@ object ProtonDefault { data class Tool(val name: String, val display: String, val dir: String, val valve: Boolean) data class Request(val seq: Long, val valve: Boolean, val dir: String) - data class State(val default: String?, val applied: Long, val live: Boolean, val pending: String?, val tools: List) + data class State(val default: String?, val applied: Long, val live: Boolean, val pending: String?, val tools: List, + val source: String? = null) data class GameChoice(val valve: Boolean, val dir: String) enum class Outcome { LIVE, NEXT_START, NOT_RUNNABLE } @@ -34,6 +35,7 @@ object ProtonDefault { Tool(t.optString("name"), t.optString("display"), t.optString("dir"), t.optBoolean("valve")) }?.takeIf { it.name.isNotEmpty() } }, + source = o.optString("source").ifEmpty { null }, ) } @@ -133,6 +135,30 @@ object ProtonDefault { writeRequest(d, moved) } + // DroidDeck-Bionic keeps one build. A default naming another one moves to the installed build: an app from before + // updates carried the default over left it, or the guest fell back to its own tool when that build was gone (the + // request the app last wrote still names it). + internal fun bionicDefault(state: State, request: Request?, prefix: String, newest: String, now: Long): Request? { + val current = chosen(state, request) + val stale = when { + current != null && !current.first && current.second.startsWith(prefix) -> current.second + state.source == "fallback" && request != null && !request.valve && request.dir.startsWith(prefix) -> request.dir + else -> return null + } + return if (stale == newest) null else Request(nextSeq(now, state, request), false, newest) + } + + /** The default and the per-game picks that name a DroidDeck-Bionic build other than [newest] move to it. */ + @Synchronized + fun keepBionicCurrent(context: Context, prefix: String, newest: String) { + val d = dir(context) + bionicDefault(state(context), parseRequest(read(File(d, "request.json"))), prefix, newest, System.currentTimeMillis()) + ?.let { writeRequest(d, it) } + val games = parseGames(read(File(d, GAMES))) + val stale = games.values.filterNotNull().filter { !it.valve && it.dir.startsWith(prefix) && it.dir != newest }.map { it.dir }.toSet() + if (stale.isNotEmpty()) movedGames(games, stale, newest)?.let { writeGames(d, it) } + } + internal fun movedGames(choices: Map, from: Set, to: String): Map? { 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 } diff --git a/app/src/main/java/com/droiddeck/launcher/session/ProtonExtras.kt b/app/src/main/java/com/droiddeck/launcher/session/ProtonExtras.kt index 24dea1821f..0c744d3f8e 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/ProtonExtras.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/ProtonExtras.kt @@ -123,6 +123,7 @@ object ProtonExtras { private var lastAttempt = 0L fun checkLatest(context: Context, force: Boolean = false, now: Long = System.currentTimeMillis()): Boolean { + keepBionicCurrent(context) synchronized(checkLock) { val file = latestFile(context) val cache = readJson(file) @@ -193,6 +194,15 @@ object ProtonExtras { } } + // DroidDeck-Bionic: the default and the game picks name the installed build, also when they were left on one an + // older app deleted without moving them. + private fun keepBionicCurrent(context: Context) { + if (SessionState.running) return + val tool = tools.firstOrNull { it.id == BIONIC } ?: return + val newest = installedAll(context, tool).lastOrNull() ?: return + runCatching { ProtonDefault.keepBionicCurrent(context, tool.prefix, newest) }.onFailure { Log.w(TAG, "bionic default and picks", it) } + } + private fun replaceOlder(context: Context, tool: Tool) { val all = installedAll(context, tool) val newest = all.lastOrNull() ?: return diff --git a/app/src/test/java/com/droiddeck/launcher/session/ProtonDefaultTest.kt b/app/src/test/java/com/droiddeck/launcher/session/ProtonDefaultTest.kt index 1ab5dd9850..a3e31b4c66 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/ProtonDefaultTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/ProtonDefaultTest.kt @@ -151,6 +151,52 @@ class ProtonDefaultTest { ProtonDefault.movedDefault(parsed, ProtonDefault.Request(7, false, "DroidDeck-Proton 11-12.8"), from, "DroidDeck-Bionic-12.8-r31", 1)?.dir) } + @Test + fun aBionicDefaultOnAnotherBuildMovesToTheInstalledOne() { + val r37 = "DroidDeck-Bionic-12.8-r37" + val applied = ProtonDefault.parseState("""{"default": "DroidDeck-Bionic-12.8-r30-Proton", "applied": 7, + "tools": [{"name": "DroidDeck-Bionic-12.8-r30-Proton", "display": "r30", "dir": "DroidDeck-Bionic-12.8-r30", "valve": false}, + {"name": "droiddeck-proton-arm64", "display": "Proton ARM64", "dir": "Proton Experimental (ARM64)", "valve": true}]}""") + // the applied default is an older build + assertEquals(ProtonDefault.Request(100, false, r37), ProtonDefault.bionicDefault(applied, null, "DroidDeck-Bionic", r37, 100)) + // the guest fell back to its own tool because the requested build was gone: the request still names it + val fallback = ProtonDefault.parseState("""{"default": "droiddeck-proton-arm64", "applied": 50, "source": "fallback", + "tools": [{"name": "droiddeck-proton-arm64", "display": "Proton ARM64", "dir": "Proton Experimental (ARM64)", "valve": true}]}""") + assertEquals(ProtonDefault.Request(51, false, r37), + ProtonDefault.bionicDefault(fallback, ProtonDefault.Request(50, false, "DroidDeck-Bionic-12.8-r24"), "DroidDeck-Bionic", r37, 3)) + // already the installed build, another tool, or a fallback the request did not cause: left as it is + assertNull(ProtonDefault.bionicDefault(fallback, ProtonDefault.Request(50, false, r37), "DroidDeck-Bionic", r37, 3)) + assertNull(ProtonDefault.bionicDefault(fallback, ProtonDefault.Request(50, false, "DroidDeck-Proton 11-12.8"), "DroidDeck-Bionic", r37, 3)) + assertNull(ProtonDefault.bionicDefault(ProtonDefault.parseState(state), null, "DroidDeck-Bionic", r37, 100)) + val steam = ProtonDefault.parseState("""{"default": "droiddeck-proton-arm64", "applied": 50, "source": "steam", + "tools": [{"name": "droiddeck-proton-arm64", "display": "Proton ARM64", "dir": "Proton Experimental (ARM64)", "valve": true}]}""") + assertNull(ProtonDefault.bionicDefault(steam, ProtonDefault.Request(50, false, "DroidDeck-Bionic-12.8-r24"), "DroidDeck-Bionic", r37, 3)) + } + + @Test + fun keepingBionicCurrentRewritesTheDefaultAndThePicks() { + val context = RuntimeEnvironment.getApplication() + val d = File(LinuxRuntime.rootDir(context), "root/.local/share/droiddeck-compat").apply { deleteRecursively(); mkdirs() } + File(d, "state.json").writeText("""{"default": "droiddeck-proton-arm64", "applied": 9, "source": "fallback", + "tools": [{"name": "droiddeck-proton-arm64", "display": "Proton ARM64", "dir": "Proton Experimental (ARM64)", "valve": true}]}""") + File(d, "request.json").writeText("""{"seq": 9, "valve": false, "dir": "DroidDeck-Bionic-12.8-r24"}""") + ProtonDefault.requestGame(context, "42", proton("DroidDeck-Bionic-12.8-r29", false)) + ProtonDefault.requestGame(context, "43", proton("DroidDeck-Proton 11-12.8", false)) + ProtonDefault.requestGame(context, "44", null) + ProtonDefault.keepBionicCurrent(context, "DroidDeck-Bionic", "DroidDeck-Bionic-12.8-r37") + val request = ProtonDefault.parseRequest(File(d, "request.json").readText()) + assertEquals("DroidDeck-Bionic-12.8-r37", request?.dir) + assertTrue(request!!.seq > 9) + val games = ProtonDefault.parseGames(File(d, "games.json").readText()) + assertEquals(ProtonDefault.GameChoice(false, "DroidDeck-Bionic-12.8-r37"), games["42"]) + assertEquals(ProtonDefault.GameChoice(false, "DroidDeck-Proton 11-12.8"), games["43"]) + assertTrue(games.containsKey("44")) + assertNull(games["44"]) + val before = File(d, "request.json").readText() + File(d, "games.json").readText() + ProtonDefault.keepBionicCurrent(context, "DroidDeck-Bionic", "DroidDeck-Bionic-12.8-r37") + assertEquals(before, File(d, "request.json").readText() + File(d, "games.json").readText()) + } + @Test fun moveDefaultRewritesTheRequestFile() { val context = RuntimeEnvironment.getApplication()