mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
fix(recomps): list the catalog before checking game files, and give installed recomps their art
This commit is contained in:
1 parent
a56d4e38be
commit
fc9ae46bcd
4 files changed
+56
-8
No files matched your search
@@ -32,7 +32,7 @@ object RecompData {
|
||||
class Inputs(
|
||||
val installs: List<Install>, val owned: List<Owner>,
|
||||
val ids: Map<String, RecompStoreIds.Ids> = emptyMap(), val gogSlugs: Map<String, String> = 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<Pair<Int, String>>, entries: List<RecompCatalog.Entry>, offline: Boolean = false): Inputs {
|
||||
fun inputs(
|
||||
context: Context, steamOwned: Collection<Pair<Int, String>>, entries: List<RecompCatalog.Entry>, offline: Boolean = false,
|
||||
cachedOnly: Boolean = false,
|
||||
): Inputs {
|
||||
val root = LinuxRuntime.rootDir(context)
|
||||
val installs = mutableListOf<Install>()
|
||||
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 }
|
||||
|
||||
@@ -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<Pair<Int, String>>) {
|
||||
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<Pair<Int, String>>, release: RecompInstaller.Release? = null,
|
||||
|
||||
@@ -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<RecompCatalog.Platform?>(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}")) {
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user