From fc9ae46bcdc0bfce02b11d0f34752348402dbb9f Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Tue, 1 Dec 2026 17:50:20 -0500 Subject: [PATCH] fix(recomps): list the catalog before checking game files, and give installed recomps their art --- .../droiddeck/launcher/runtime/RecompData.kt | 26 ++++++++++++++----- .../droiddeck/launcher/store/RecompsState.kt | 24 ++++++++++++++++- .../com/droiddeck/launcher/ui/RecompsPage.kt | 1 + .../droiddeck/launcher/runtime/RecompsTest.kt | 13 ++++++++++ 4 files changed, 56 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/RecompData.kt b/app/src/main/java/com/droiddeck/launcher/runtime/RecompData.kt index 80980f23ab..b5c2600f9f 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/RecompData.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/RecompData.kt @@ -32,7 +32,7 @@ object RecompData { class Inputs( val installs: List, val owned: List, val ids: Map = emptyMap(), val gogSlugs: Map = emptyMap(), - val hasher: (File, String) -> String? = ::hashOf, val done: () -> Unit = {}, + val hasher: (File, String) -> String? = ::hashOf, val done: () -> Unit = {}, val cachedOnly: Boolean = false, ) // Decomp Buddy's rules (src/stores.js): "Fallout 2: A Post Nuclear Role Playing Game" is Fallout 2, "Fallout 2" is @@ -91,10 +91,16 @@ object RecompData { return dir } - private fun rightVersion(data: RecompCatalog.Data, file: File, hasher: (File, String) -> String?): Boolean { + // null: a hash is needed but not known yet (the bulk pass reads only hashes already cached). + private fun rightVersion(data: RecompCatalog.Data, file: File, inputs: Inputs): Boolean? { if (data.size > 0 && file.length() != data.size) return false if (data.hashes.isEmpty()) return true - return data.hashes.any { h -> hasher(file, h.algo)?.equals(h.value, ignoreCase = true) == true } + var unknown = false + for (h in data.hashes) { + val got = inputs.hasher(file, h.algo) + if (got == null) unknown = true else if (got.equals(h.value, ignoreCase = true)) return true + } + return if (unknown && inputs.cachedOnly) null else false } fun resolve(entry: RecompCatalog.Entry, inputs: Inputs): State { @@ -110,14 +116,15 @@ object RecompData { incomplete = incomplete ?: State.Incomplete(install.store, install.host, file, wrongVersion = false) continue } - if (!rightVersion(entry.data, found, inputs.hasher)) { + val right = rightVersion(entry.data, found, inputs) + if (right == false) { incomplete = State.Incomplete(install.store, install.host, file, wrongVersion = true) continue } val dir = folderOf(found, file) val rel = dir.relativeTo(install.host).invariantSeparatorsPath.trim('/') return State.Found(install.store, dir, if (rel.isEmpty() || rel == ".") install.guest else "${install.guest.trimEnd('/')}/$rel", - how == Match.EXACT, checked = true) + how == Match.EXACT, checked = right == true) } incomplete?.let { return it } inputs.owned.mapNotNull { o -> match(entry, inputs, o.store, o.id, o.title)?.let { o to it } }.minByOrNull { it.second.ordinal } @@ -126,7 +133,10 @@ object RecompData { return if (entry.data.kind == RecompCatalog.DataKind.NONE) State.NotNeeded else State.BringYourOwn(entry.data) } - fun inputs(context: Context, steamOwned: Collection>, entries: List, offline: Boolean = false): Inputs { + fun inputs( + context: Context, steamOwned: Collection>, entries: List, offline: Boolean = false, + cachedOnly: Boolean = false, + ): Inputs { val root = LinuxRuntime.rootDir(context) val installs = mutableListOf() StoresState.installed.forEach { game -> @@ -145,7 +155,7 @@ object RecompData { val gogIds = installs.filter { it.store == Store.GOG }.mapNotNull { it.id } + owned.filter { it.store == Store.GOG }.mapNotNull { it.id } val gogSlugs = if (ids.values.any { it.gog.isNotEmpty() }) runCatching { RecompStoreIds.gogSlugs(context, gogIds, offline) }.getOrDefault(emptyMap()) else emptyMap() val hashes = HashCache(File(File(context.filesDir, "recomps"), "hashes.json")) - return Inputs(installs, owned, ids, gogSlugs, { f, algo -> hashes.get(f, algo) }, hashes::save) + return Inputs(installs, owned, ids, gogSlugs, { f, algo -> if (cachedOnly) hashes.cached(f, algo) else hashes.get(f, algo) }, hashes::save, cachedOnly) } private fun guestPath(context: Context, root: File, host: File): String { @@ -171,6 +181,8 @@ object RecompData { private val json = runCatching { JSONObject(file.readText()) }.getOrDefault(JSONObject()) private var dirty = false + fun cached(f: File, algo: String): String? = json.optString("${f.absolutePath}|${f.length()}|${f.lastModified()}|$algo").takeIf { it.isNotEmpty() } + fun get(f: File, algo: String): String? { val key = "${f.absolutePath}|${f.length()}|${f.lastModified()}|$algo" json.optString(key).takeIf { it.isNotEmpty() }?.let { return it } diff --git a/app/src/main/java/com/droiddeck/launcher/store/RecompsState.kt b/app/src/main/java/com/droiddeck/launcher/store/RecompsState.kt index 550890d786..bdaf4dc5d1 100644 --- a/app/src/main/java/com/droiddeck/launcher/store/RecompsState.kt +++ b/app/src/main/java/com/droiddeck/launcher/store/RecompsState.kt @@ -55,17 +55,24 @@ object RecompsState { val n = ++seq loading = entries.isEmpty() Thread({ + synchronized(refreshLock) { + if (n != seq) return@Thread val list = runCatching { RecompCatalog.load(app, reloadCatalog) }.getOrDefault(emptyList()) val apps = runCatching { RecompAndroid.installed(app, list) }.getOrDefault(emptyMap()) val files = runCatching { RecompInstaller.installed(app) }.getOrDefault(emptyMap()) // Catalogs can rename an entry; an install also belongs to the entry with its repo. val byRepo = files.values.filter { it.repo != null }.associateBy { it.repo!!.lowercase() } val chosen = choices(app) + // Installs from before the shortcut work, or whose art download failed, get their cover now. + list.forEach { e -> (files[e.id] ?: byRepo[e.repo.lowercase()])?.let { inst -> runCatching { com.droiddeck.launcher.runtime.RecompShortcuts.prepare(e, inst) } } } val have = list.mapNotNull { e -> val both = listOfNotNull(files[e.id] ?: byRepo[e.repo.lowercase()], apps[e.id]) (both.firstOrNull { it.platform == chosen[e.id] } ?: both.firstOrNull())?.let { e.id to it } }.toMap() - val inputs = runCatching { RecompData.inputs(app, steamOwned, list) }.getOrNull() + // The list shows as soon as the catalog is read; game-file checks follow without hashing anything new. + main.post { if (n == seq && entries.isEmpty()) { entries = list; installed = have; loading = false } } + if (n != seq) return@Thread + val inputs = runCatching { RecompData.inputs(app, steamOwned, list, cachedOnly = true) }.getOrNull() val states = list.associate { e -> e.id to (inputs?.let { RecompData.resolve(e, it) } ?: RecompData.State.BringYourOwn(e.data)) } inputs?.done?.invoke() val apks = runCatching { RecompAndroid.incompatible(app) }.getOrDefault(emptySet()) @@ -84,9 +91,24 @@ object RecompsState { }.toMap() main.post { updates = found } } + } }, "recomps-refresh").start() } + private val refreshLock = Any() + + // Checks one recomp's game files against the catalog's hashes, which can take a while for large files. + fun verify(context: Context, entry: RecompCatalog.Entry, steamOwned: Collection>) { + if (fixed || !entry.pc || data[entry.id].let { it is RecompData.State.Found && it.checked }) return + val app = context.applicationContext + Thread({ + val inputs = runCatching { RecompData.inputs(app, steamOwned, listOf(entry)) }.getOrNull() ?: return@Thread + val state = RecompData.resolve(entry, inputs) + inputs.done() + main.post { data = data + (entry.id to state) } + }, "recomps-verify").start() + } + // [chosen] is a build the player picked; it is installed as it is, without swapping in an arm64 twin. fun install( context: Context, entry: RecompCatalog.Entry, steamOwned: Collection>, release: RecompInstaller.Release? = null, diff --git a/app/src/main/java/com/droiddeck/launcher/ui/RecompsPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/RecompsPage.kt index df5b076942..b77fac5423 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/RecompsPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/RecompsPage.kt @@ -433,6 +433,7 @@ private fun RecompDetail( val e = row.entry val open: (String) -> Unit = { url -> runCatching { ctx.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } } var picked by remember(e.id, row.installed?.platform) { mutableStateOf(null) } + LaunchedEffect(e.id) { RecompsState.verify(ctx, e, steamOwned) } BoxWithConstraints(modifier) { val stacked = maxWidth < 640.dp Column(Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(bottom = 24.dp).testTag("recomp-detail-${e.id}")) { diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/RecompsTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/RecompsTest.kt index 3a354bff6b..78b38a5ef8 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/RecompsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/RecompsTest.kt @@ -187,6 +187,19 @@ class RecompsTest { assertEquals(listOf("diablo-pc", "zelda64recomp", "ironwail", "otherm"), all.map { it.id }) } + @Test fun theBulkPassNeverHashesAndLeavesTheCheckOpen() { + val plans = DecompBuddyPlans.read(java.io.StringReader(decompBuddyPlans)) + val d = RecompCatalog.fromDecompBuddy(decompBuddyFeed, plans).single() + val gog = temporary.newFolder("Diablo bulk") + File(gog, "diabdat.mpq").writeText("hello") + val install = RecompData.Install(Store.GOG, "1412601690", "Diablo", gog, "/g") + var hashed = 0 + val bulk = RecompData.Inputs(listOf(install), emptyList(), hasher = { _, _ -> hashed++; null }, cachedOnly = true) + val found = RecompData.resolve(d, bulk) as RecompData.State.Found + assertTrue(!found.checked) + assertTrue(hashed > 0) + } + @Test fun theNeededFileFindsTheFolderInsideAStoreInstall() { val plans = DecompBuddyPlans.read(java.io.StringReader(decompBuddyPlans)) val d = RecompCatalog.fromDecompBuddy(decompBuddyFeed, plans).single()