From df60fdb194b0b24effbec68f27af9704c139909c Mon Sep 17 00:00:00 2001 From: K11MCH1 <81425424+K11MCH1@users.noreply.github.com> Date: Wed, 4 Nov 2026 10:34:32 +0900 Subject: [PATCH] Added games cache: skip bad records, expire names on read, retry failed saves A record whose fields could not be decoded threw while the cache loaded, and since every scan loads it first, one bad record blocked every scan; each record is now decoded on its own and skipped if it is malformed. name() returned names older than a week that were still in memory; it now applies the same age limit as rank(). save() cleared its dirty flag before writing and called startWrite outside its try, so a write that failed was never retried and could replace the scan's result with an exception; a failed write now leaves the cache dirty and is tried again on the next save. --- .../launcher/frontend/AddedGamesCache.kt | 22 ++++++----- .../launcher/frontend/AddedGamesCacheTest.kt | 37 +++++++++++++++++++ 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGamesCache.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGamesCache.kt index 0f5b08b117..c27764dfbd 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGamesCache.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGamesCache.kt @@ -17,18 +17,18 @@ internal class AddedGamesCache(val file: File) { init { val json = runCatching { JSONObject(AtomicFile(file).readFully().toString(Charsets.UTF_8)) }.getOrNull() json?.optJSONObject("ranks")?.let { all -> - for (path in all.keys()) { - val o = all.optJSONObject(path) ?: continue - val exes = o.optJSONArray("exes") ?: continue - val scores = o.optJSONArray("scores") ?: continue - if (exes.length() == 0 || exes.length() != scores.length()) continue - ranks[path] = Rank(o.optLong("modified"), o.optLong("at"), GameExePicker.Ranking( + for (path in all.keys()) runCatching { + val o = all.getJSONObject(path) + val exes = o.getJSONArray("exes") + val scores = o.getJSONArray("scores") + require(exes.length() > 0 && exes.length() == scores.length()) + ranks[path] = Rank(o.getLong("modified"), o.getLong("at"), GameExePicker.Ranking( List(exes.length()) { File(exes.getString(it)) }, List(scores.length()) { scores.getInt(it) }, o.optBoolean("uncertain"), )) } } json?.optJSONObject("names")?.let { all -> - for (key in all.keys()) all.optJSONObject(key)?.let { names[key] = Name(it.optString("name"), it.optLong("at")) } + for (key in all.keys()) runCatching { all.getJSONObject(key).let { names[key] = Name(it.getString("name"), it.getLong("at")) } } } } @@ -48,7 +48,7 @@ internal class AddedGamesCache(val file: File) { changed = true } - fun name(key: String): String? = names[key]?.name + fun name(key: String): String? = names[key]?.takeIf { System.currentTimeMillis() - it.at in 0 until KEEP_MS }?.name fun keepName(key: String, name: String) { names[key] = Name(name, System.currentTimeMillis()) @@ -69,12 +69,14 @@ internal class AddedGamesCache(val file: File) { val keptNames = JSONObject() names.forEach { (key, n) -> if (n.at >= oldest) keptNames.put(key, JSONObject().put("name", n.name).put("at", n.at)) } val atomic = AtomicFile(file) - val out = atomic.startWrite() + var out: java.io.FileOutputStream? = null try { + out = atomic.startWrite() out.write(JSONObject().put("ranks", keptRanks).put("names", keptNames).toString().toByteArray()) atomic.finishWrite(out) } catch (e: Exception) { - atomic.failWrite(out) + out?.let(atomic::failWrite) + changed = true } } diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesCacheTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesCacheTest.kt index 753deb24ea..804223911e 100644 --- a/app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesCacheTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesCacheTest.kt @@ -74,6 +74,43 @@ class AddedGamesCacheTest { assertNull(kept.name("unknown")) } + @Test fun aMalformedRecordIsSkippedAndTheOthersStillLoad() { + val (folder, exe) = game() + val (other, _) = game("Other") + val now = System.currentTimeMillis() + val good = JSONObject().put("modified", folder.lastModified()).put("at", now) + .put("exes", JSONArray(listOf(exe.path))).put("scores", JSONArray(listOf(72))) + val bad = JSONObject().put("modified", other.lastModified()).put("at", now) + .put("exes", JSONArray(listOf("bin/Other.exe"))).put("scores", JSONArray(listOf("bad"))) + store().writeText(JSONObject() + .put("ranks", JSONObject().put(folder.path, good).put(other.path, bad)) + .put("names", JSONObject().put("broken", "not a record").put("named", JSONObject().put("name", "Bastion").put("at", now))) + .toString()) + val kept = AddedGamesCache(store()) + assertEquals(listOf(exe), kept.rank(folder)?.exes) + assertNull(kept.rank(other)) + assertEquals("Bastion", kept.name("named")) + assertNull(kept.name("broken")) + } + + @Test fun aNameOlderThanAWeekIsIdentifiedAgain() { + store().writeText(JSONObject().put("names", JSONObject().put("named", JSONObject().put("name", "Bastion") + .put("at", System.currentTimeMillis() - AddedGamesCache.KEEP_MS - 1))).toString()) + assertNull(AddedGamesCache(store()).name("named")) + } + + @Test fun aSaveThatCannotBeWrittenIsTriedAgainNextTime() { + val blocked = File(tmp.root, "blocked").apply { writeText("a file where the cache folder should be") } + val file = File(blocked, "scan.json") + val cache = AddedGamesCache(file).apply { keepName("named", "Bastion") } + cache.save() + assertFalse(file.exists()) + blocked.delete() + blocked.mkdirs() + cache.save() + assertEquals("Bastion", AddedGamesCache(file).name("named")) + } + @Test fun aScanKeepsEachGamesRankingForTheNextLaunch() { val (folder, exe) = game("Card Game") AddedExes.put(app, AddedExes.Entry(folder.path, exe.path, picked = false))