Windows components: restore bundle parts, wait for restores and keep older picks (#1788)

This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-10-29 20:31:18 -04:00
1 parent 5d6f9c7d74
commit ee3876b965
5 files changed
+209 -15

No files matched your search

@@ -668,6 +668,20 @@ class FrontEndActivity : ComponentActivity() {
private var shortcutPicker by mutableStateOf(false)
private var shortcutLibraryScanning by mutableStateOf(false)
@Volatile private var libraryScanGeneration = 0
@Volatile private var lastComponentRestore = 0L
@Volatile private var componentRestoreRetry = false
private val componentRestoreLock = Any()
private fun retryComponentRestore() {
val run = synchronized(componentRestoreLock) {
componentRestoreRetry = false
val now = android.os.SystemClock.elapsedRealtime()
(!isDestroyed && now - lastComponentRestore >= COMPONENT_RESTORE_MS).also { if (it) lastComponentRestore = now }
}
if (run) Thread({
runCatching { com.droiddeck.launcher.frontend.AutoComponents.restoreMissing(this) }.onFailure { Log.w(TAG, "components: restore failed", it) }
}, "components-restore").start()
}
private var pendingGameLink: String? = null
private var gameLinkReading = false
private var gameLinkTried = false
@@ -2497,14 +2511,28 @@ class FrontEndActivity : ComponentActivity() {
val all = games.distinctBy { it.gameId }
if (ready && scanGeneration == libraryScanGeneration) com.droiddeck.launcher.frontend.LibraryCache.save(this, all, scannedX64)
val endedSession = SessionState.sessionId.takeIf { !SessionState.running && it != null && it != componentSweepSession }
if (ready && (!componentSweepDone || endedSession != null)) {
val sweepDue = ready && (!componentSweepDone || endedSession != null)
if (sweepDue) {
componentSweepDone = true
endedSession?.let { componentSweepSession = it }
ui.postDelayed({ Thread({
com.droiddeck.launcher.frontend.AutoComponents.restoreMissing(this)
com.droiddeck.launcher.frontend.AutoComponents.sweep(this, all)
}, "components-sweep").start() }, 10_000)
}
val restoreDue = synchronized(componentRestoreLock) {
val now = android.os.SystemClock.elapsedRealtime()
val due = ready && (lastComponentRestore == 0L || now - lastComponentRestore >= COMPONENT_RESTORE_MS)
if (due) {
lastComponentRestore = now
} else if (ready && !componentRestoreRetry) {
componentRestoreRetry = true
ui.postDelayed({ retryComponentRestore() }, (COMPONENT_RESTORE_MS - (now - lastComponentRestore)).coerceAtLeast(0L))
}
due
}
if (restoreDue || sweepDue) ui.postDelayed({
Thread({
if (restoreDue) runCatching { com.droiddeck.launcher.frontend.AutoComponents.restoreMissing(this) }.onFailure { Log.w(TAG, "components: restore failed", it) }
if (sweepDue) com.droiddeck.launcher.frontend.AutoComponents.sweep(this, all)
}, "components-restore").start()
}, 10_000)
ui.post {
if (scanGeneration == libraryScanGeneration) {
val current = steamGames.filter { it.library != Library.ADDED }.associateBy { it.appId }
@@ -2682,6 +2710,7 @@ class FrontEndActivity : ComponentActivity() {
companion object {
private const val TAG = "MainActivity"
private const val MEDIA_SETTLE_MS = 1500L
private const val COMPONENT_RESTORE_MS = 60_000L
private const val LINK_PANEL_MS = 1500L
private const val SESSION_SWITCH_WAIT_MS = 30_000L
private const val SESSION_SWITCH_POLL_MS = 250L
@@ -89,7 +89,13 @@ object AutoComponents {
private fun enqueue(context: Context, job: Job) {
if (!queuedKeys.add(job.key)) {
if (job.asked && jobs.removeIf { it.key == job.key && !it.asked }) jobs.add(job)
synchronized(jobs) {
val old = jobs.firstOrNull { it.key == job.key }
if (old != null && ((job.asked && !old.asked) || (job.inSession && !old.inSession)) && jobs.remove(old)) {
jobs.add(Job(old.game ?: job.game, old.component, old.asked || job.asked,
if (job.asked && !old.asked) job.seq else old.seq, old.pics ?: job.pics, old.inSession || job.inSession))
}
}
if (runNow && !draining) drain(context)
return
}
@@ -197,7 +203,7 @@ object AutoComponents {
queueDownload(context, id, asked, inSession)
missing.add(id)
}
if (inSession) Pending.set(context, key, missing)
if (inSession) Pending.set(context, key, (Pending.ids(context, key).filter { it !in installed && it !in wanted } + missing).distinct())
if (before.keys != kept.keys) {
WinComponents.setAuto(context, key, kept)
Log.i(TAG, "${game.name}: on by itself ${kept.keys.joinToString().ifEmpty { "(none)" }}")
@@ -236,6 +242,16 @@ object AutoComponents {
write(context, games)
}
@Synchronized fun ids(context: Context, key: String): List<String> =
read(context).optJSONArray(key)?.let { a -> (0 until a.length()).map { a.optString(it) } }.orEmpty()
@Synchronized fun add(context: Context, key: String, ids: List<String>) {
val games = read(context)
val have = games.optJSONArray(key)?.let { a -> (0 until a.length()).map { a.optString(it) } }.orEmpty()
games.put(key, JSONArray((have + ids).distinct()))
write(context, games)
}
@Synchronized fun drop(context: Context, id: String) {
val games = read(context)
for (k in games.keys().asSequence().toList()) {
@@ -321,17 +337,29 @@ object AutoComponents {
fun restoreMissing(context: Context): List<String> {
val selections = WinComponents.selections(context)
if (selections.isEmpty()) return emptyList()
val installed = installedOf(context)
val picked = selections.values.flatMap { it.effective }.filter { WinComponents.validId(it) && it !in installed }.distinct()
if (picked.isEmpty()) return emptyList()
val catalog = (catalogOf(context, false) ?: catalogOf(context, true))?.associateBy { it.name } ?: return emptyList()
val lost = picked.filter { it in catalog }
if (lost.isEmpty()) return emptyList()
val installed = installedOf(context)
val lostFor = selections.mapValues { (_, selection) ->
withParts(selection.effective.filter { WinComponents.validId(it) }, catalog).filter { it !in installed && it in catalog }
}.filterValues { it.isNotEmpty() }
if (lostFor.isEmpty()) return emptyList()
val lost = lostFor.values.flatten().distinct()
Log.i(TAG, "wincomp: reinstalling ${lost.joinToString()}, picked for games but missing from the runtime")
lostFor.forEach { (key, ids) -> Pending.add(context, key, ids) }
lost.forEach { queueDownload(context, it, asked = true, inSession = true) }
return lost
}
private fun withParts(ids: List<String>, catalog: Map<String, WinComponents.Component>): List<String> {
val out = LinkedHashSet<String>()
fun visit(id: String, depth: Int) {
if (depth > 8 || !out.add(id)) return
catalog[id]?.dependencies?.forEach { visit(it, depth + 1) }
}
ids.forEach { visit(it, 0) }
return out.toList()
}
fun refreshLater(context: Context, game: Library.SteamGame) = queueGame(context, game, asked = true)
fun sweep(context: Context, games: List<Library.SteamGame>) {
@@ -446,4 +446,55 @@ class WinCompSourcesTest {
AutoComponents.drain(app)
assertTrue(AutoComponents.queued().isEmpty())
}
@Test fun aLostPartOfAnInstalledBundleIsReinstalledAndTheGameWaitsForIt() = withFakeWorker {
val bundle = WinComponents.Component("directmusic", "", "", "ready", listOf("dmband"), emptyList())
AutoComponents.catalogOf = { _, _ -> catalog3 + ready("dmband") + bundle }
installedNow.addAll(listOf("directmusic", "d3dx9"))
WinComponents.setUser(app, "4242", "directmusic", true)
WinComponents.setUser(app, "4242", "d3dx9", true)
session = true
gameUp = true
assertEquals(listOf("dmband"), AutoComponents.restoreMissing(app))
assertTrue(downloaded.isEmpty())
assertEquals("[\"dmband\"]", AutoComponents.Pending.read(app).optJSONArray("4242").toString())
gameUp = false
AutoComponents.drain(app)
assertEquals(listOf("dmband"), downloaded)
assertEquals(0, AutoComponents.Pending.read(app).length())
assertTrue(AutoComponents.restoreMissing(app).isEmpty())
}
@Test fun aRestoreRunsADownloadQueuedBeforeTheSessionWithoutWaitingForItToEnd() = withFakeWorker {
WinComponents.setUser(app, "4243", "vcredist2019_dll", true)
session = true
AutoComponents.queueGame(app, game(tmp.newFolder("Wait"), Library.ADDED), asked = true)
AutoComponents.restoreMissing(app)
AutoComponents.restoreMissing(app)
assertEquals(listOf("vcredist2019_dll"), downloaded)
session = false
AutoComponents.drain(app)
}
@Test fun aGameJobThatRunsFirstKeepsARestoredComponentPending() = withFakeWorker {
AutoComponents.catalogOf = { _, _ -> catalog3 + ready("dmband") }
val seen = ArrayList<List<String>>()
AutoComponents.installer = { _, c, _, _ ->
if (c.name == "dmband") seen.add(AutoComponents.Pending.ids(app, "1262580"))
downloaded.add(c.name); installedNow.add(c.name); null
}
val game = payback()
WinComponents.setUser(app, "1262580", "dmband", true)
session = true
gameUp = true
AutoComponents.queueInSession(app, game)
AutoComponents.restoreMissing(app)
gameUp = false
AutoComponents.drain(app)
assertEquals(1, seen.size)
assertTrue("dmband" in seen.single())
assertEquals(0, AutoComponents.Pending.read(app).length())
session = false
AutoComponents.drain(app)
}
}