diff --git a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt index fe384b2511..62b0300691 100644 --- a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt @@ -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 diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AutoComponents.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AutoComponents.kt index acbb86b0cd..69ccb08c9c 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/AutoComponents.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AutoComponents.kt @@ -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 = + read(context).optJSONArray(key)?.let { a -> (0 until a.length()).map { a.optString(it) } }.orEmpty() + + @Synchronized fun add(context: Context, key: String, ids: List) { + 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 { 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, catalog: Map): List { + val out = LinkedHashSet() + 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) { diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/WinCompSourcesTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/WinCompSourcesTest.kt index b6c98b00a8..4d1d23ee13 100644 --- a/app/src/test/java/com/droiddeck/launcher/frontend/WinCompSourcesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/frontend/WinCompSourcesTest.kt @@ -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>() + 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) + } } diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents index f86cf91981..d98e6d66ad 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents @@ -563,16 +563,30 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING held = set() saved_overrides = state.get("overrides") if isinstance(state.get("overrides"), dict) else {} saved_requires = state.get("requires") if isinstance(state.get("requires"), dict) else {} + recorded = [c for c in state.get("components", []) if isinstance(c, str) and valid_id(c)] + owners = set(state["files"].values()) + registry_owners = {r.get("cid") for r in (state.get("registry") or {}).values() if isinstance(r, dict)} + inferred = set() + guessed = set(state.get("guessed") or []) if "requires" in state else set(ids) + loose = [c for c in recorded if c not in ids and (c in owners or c in registry_owners)] + for cid in ids: + if cid in guessed and component(store, cid) is None and cid in recorded: + saved_requires = dict(saved_requires, **{cid: loose}) + inferred.add(cid) kept_overrides, kept_requires = {}, {} for cid in expand(store, ids, saved_requires): found = component(store, cid) if found is None: - if cid in saved_requires or any(owner == cid for owner in state["files"].values()): + if cid in saved_requires or cid in recorded or cid in owners or cid in registry_owners: held.add(cid) - names = [n for n in saved_overrides.get(cid, []) if isinstance(n, str)] + if cid in saved_overrides: + names = [n for n in saved_overrides.get(cid, []) if isinstance(n, str)] + else: + names = sorted({Path(rel).stem.lower() for rel, owner in state["files"].items() + if owner == cid and rel.lower().endswith(".dll")}) overrides += names kept_overrides[cid] = names - if isinstance(saved_requires.get(cid), list): + if isinstance(saved_requires.get(cid), list) and cid not in inferred: kept_requires[cid] = saved_requires[cid] notes.append("%s is not in the store; kept the files it placed until it is installed again" % cid) else: @@ -613,6 +627,8 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING compat.mkdir(parents=True, exist_ok=True) new_state = {"files": keep, "components": installed + sorted(held), "overrides": kept_overrides, "requires": kept_requires, "registry": state.get("registry", {})} + if inferred: + new_state["guessed"] = sorted(inferred) written = 0 if values or new_state["registry"]: written, registry_notes = apply_registry(pfx, values, new_state, held) diff --git a/tools/tests/test_wincomponents.py b/tools/tests/test_wincomponents.py index 12a9027fe0..d7686ebf16 100644 --- a/tools/tests/test_wincomponents.py +++ b/tools/tests/test_wincomponents.py @@ -150,6 +150,76 @@ class WinComponentsTest(unittest.TestCase): self.assertFalse((self.pfx / "syswow64/dmband.dll").exists()) self.assertFalse((self.pfx / "syswow64/openal32.dll").exists()) + def test_state_from_before_the_update_keeps_a_missing_bundles_parts(self): + self.component("dmband", ["dmband"], {"syswow64/dmband.dll": b"band"}) + meta = self.store / "directmusic" + meta.mkdir(parents=True) + (meta / "component.json").write_text(json.dumps({"id": "directmusic", "overrides": [], "requires": ["dmband"]})) + self.pick("directmusic") + self.apply() + state_file = self.compat / ".droiddeck-wincomponents.json" + state = json.loads(state_file.read_text()) + rel = next(iter(state["files"])) + state_file.write_text(json.dumps({"files": state["files"], "components": state["components"], "registry": {}})) + for cid in ("dmband", "directmusic"): + for path in sorted((self.store / cid).rglob("*"), reverse=True): + path.rmdir() if path.is_dir() else path.unlink() + (self.store / cid).rmdir() + dll = self.compat / "pfx" / rel + for _ in range(2): + names, notes = self.apply() + self.assertEqual(MODULE["overrides_value"](names), "dmband=n,b") + self.assertEqual(dll.read_bytes(), b"band") + self.assertEqual(len(notes), 2) + saved = json.loads(state_file.read_text()) + self.assertEqual(saved["guessed"], ["directmusic"]) + self.assertNotIn("directmusic", saved["requires"]) + self.component("dmband", ["dmband"], {"syswow64/dmband.dll": b"band2"}) + names, _ = self.apply() + self.assertEqual(MODULE["overrides_value"](names), "dmband=n,b") + self.assertEqual(dll.read_bytes(), b"band2") + self.assertEqual(json.loads(state_file.read_text())["guessed"], ["directmusic"]) + self.pick() + self.apply() + self.assertFalse(dll.exists()) + + def test_a_missing_legacy_bundle_with_its_own_files_keeps_its_parts_and_their_registry(self): + self.pfx.parent.mkdir(parents=True) + user = self.compat / "pfx/user.reg" + user.write_text('WINE REGISTRY Version 2\n\n[Software\\\\Test] 1700000000\n"Mode"="ours"\n') + ident = "HKCU\tsoftware\\test\tmode" + own = self.compat / "pfx/drive_c/windows/syswow64/bundle.dll" + part = self.compat / "pfx/drive_c/windows/syswow64/part.dll" + for path in (own, part): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(b"x") + self.compat.joinpath(".droiddeck-wincomponents.json").write_text(json.dumps({ + "files": {"drive_c/windows/syswow64/bundle.dll": "bundle", "drive_c/windows/syswow64/part.dll": "part"}, + "components": ["bundle", "part", "tweak"], + "registry": {ident: {"prev": None, "cid": "tweak", "hive": "HKCU", "key": "Software\\Test", "name": "Mode"}}})) + self.pick("bundle") + names, _ = self.apply() + self.assertEqual(set(MODULE["overrides_value"](names).split(";")), {"bundle=n,b", "part=n,b"}) + self.assertTrue(own.exists() and part.exists()) + self.assertIn('"Mode"="ours"', user.read_text()) + saved = json.loads((self.compat / ".droiddeck-wincomponents.json").read_text()) + self.assertEqual(saved["guessed"], ["bundle"]) + self.assertIn(ident, saved["registry"]) + + def test_a_missing_component_that_only_set_registry_values_keeps_them(self): + self.pfx.parent.mkdir(parents=True) + user = self.compat / "pfx/user.reg" + user.write_text('WINE REGISTRY Version 2\n\n[Software\\\\Test] 1700000000\n"Mode"="ours"\n') + ident = "HKCU\tsoftware\\test\tmode" + self.compat.joinpath(".droiddeck-wincomponents.json").write_text(json.dumps({ + "files": {}, "components": ["tweak"], "requires": {}, "overrides": {"tweak": []}, + "registry": {ident: {"prev": None, "cid": "tweak", "hive": "HKCU", "key": "Software\\Test", "name": "Mode"}}})) + self.pick("tweak") + _, notes = self.apply() + self.assertIn('"Mode"="ours"', user.read_text()) + self.assertIn("tweak is not in the store; kept the files it placed until it is installed again", notes) + self.assertIn(ident, json.loads((self.compat / ".droiddeck-wincomponents.json").read_text())["registry"]) + def test_no_selection_touches_nothing(self): self.assertEqual(self.apply(), ([], [])) self.assertFalse(self.compat.exists())