fix: stop importing games as Spacewar (480) and follow a changed Steam app id (#1896)

This commit is contained in:
K11MCH1 authored and GitHub committed 2026-10-31 13:36:43 -04:00
1 parent 88719f25d2
commit 583f34fc55
9 files changed
+303 -26

No files matched your search

@@ -287,14 +287,17 @@ object AddedGameArt {
var sgdbDown = false
var changed = false
for (game in games) {
if (forgetOtherAppsArt(context, game)) changed = true
val cache = cacheDir(context, game)
val start = missing(context, game)
val again = refetch(cache) intersect start
if (again.isEmpty()) File(cache, REFETCH).delete()
val full = Slot.COVER in start
val allowed = if (full) Slot.values().toSet() else EXTRAS
val allowed = if (full) Slot.values().toSet() else EXTRAS + again
var need = start intersect allowed
if (need.isEmpty() || (need == setOf(Slot.ICON) && sgdbKey.isEmpty())) continue
val cache = cacheDir(context, game)
val tried = File(cache, EXTRAS_TRIED)
if (!full && tried.isFile && tried.lastModified() >= keyChangedAt && System.currentTimeMillis() - tried.lastModified() < RETRY_AFTER_MS) continue
if (!full && again.isEmpty() && tried.isFile && tried.lastModified() >= keyChangedAt && System.currentTimeMillis() - tried.lastModified() < RETRY_AFTER_MS) continue
cache.mkdirs()
if (Slot.LOGO in need && game.source != Library.ADDED && runCatching { sources.storeLogo(game) }.getOrDefault(false)) {
changed = true
@@ -324,11 +327,42 @@ object AddedGameArt {
asked = false
}
}
if ((missing(context, game) intersect EXTRAS).isEmpty()) tried.delete() else if (asked) tried.writeText("")
val left = missing(context, game)
if (again.isNotEmpty()) keepRefetch(cache, again intersect left)
if ((left intersect EXTRAS).isEmpty()) tried.delete() else if (asked) tried.writeText("")
}
return changed
}
internal const val REFETCH = "refetch"
private fun refetch(cache: File): Set<Slot> =
runCatching { File(cache, REFETCH).readText() }.getOrDefault("").split(',').mapNotNull { key -> Slot.values().firstOrNull { it.key == key } }.toSet()
private fun keepRefetch(cache: File, slots: Set<Slot>) {
if (slots.isEmpty()) File(cache, REFETCH).delete() else File(cache, REFETCH).writeText(slots.joinToString(",") { it.key })
}
internal fun forgetOtherAppsArt(context: Context, game: AddedGames.Game): Boolean {
val cache = cacheDir(context, game)
val recorded = SteamMatch.get(context, game.folder.path)?.takeIf { it.appId != null }
val legacy = runCatching { File(cache, "steam-appid").readText().trim().toInt() }.getOrNull()?.takeIf { it > 0 }
val before = recorded?.appId ?: legacy ?: return false
val identity = runCatching { GameIdentifier.identify(game.exe) }.getOrNull()
val now = identity?.appId
val fromFiles = recorded?.certainty == SteamMatch.Certainty.FILES || before == GameIdentifier.SPACEWAR
if (now == before || (now == null && !fromFiles)) return false
SteamMatch.put(context, game.folder.path,
if (identity != null && now != null) SteamMatch.Match(now, SteamMatch.Certainty.FILES, identity.source.name.lowercase(), System.currentTimeMillis())
else SteamMatch.Match(null, SteamMatch.Certainty.NONE, "", 0))
val dropped = Slot.values().filter { File(cache, SteamGridDb.cacheName(it)).delete() }.toSet()
Slot.values().forEach { File(cache, SteamGridDb.cacheName(it) + ".src").delete() }
listOf("sgdb-id", "steam-appid", EXTRAS_TRIED).forEach { File(cache, it).delete() }
keepRefetch(cache, refetch(cache) + dropped)
Log.i(TAG, "${game.name}: its files no longer name Steam app $before; its art is fetched again")
return dropped.isNotEmpty()
}
internal fun steamMatch(context: Context, game: AddedGames.Game, search: (String) -> Int?): SteamMatch.Match {
if (game.source == Store.BATTLENET.id) {
val id = game.storeId?.let(BattleNet::title)?.steam
@@ -5,6 +5,7 @@ import java.io.File
object GameIdentifier {
private const val MAX_PARENT_DEPTH = 6
internal const val SPACEWAR = 480
enum class Source { STEAM_APPID_TXT, STEAM_MANIFEST_ACF, STEAM_EMU_INI, GOG_INFO, PE_VERSION, FILENAME, NONE }
enum class Confidence { HIGH, MEDIUM, LOW }
@@ -92,7 +93,7 @@ object GameIdentifier {
for (candidate in arrayOf(File(dir, "steam_appid.txt"), File(dir, "steam_settings/steam_appid.txt"))) {
val id = readSmallTextFile(candidate)?.removePrefix("\uFEFF")?.trim()
?.takeWhile { it.isDigit() }?.toIntOrNull()
if (id != null && id > 0) return id
if (id != null && id > 0 && id != SPACEWAR) return id
}
return null
}
@@ -111,7 +112,7 @@ object GameIdentifier {
for (f in candidates) {
val text = readSmallTextFile(f) ?: continue
val id = re.find(text)?.groupValues?.getOrNull(1)?.toIntOrNull()
if (id != null && id > 0) return id
if (id != null && id > 0 && id != SPACEWAR) return id
}
return null
}
@@ -133,7 +134,8 @@ object GameIdentifier {
val match = manifests.firstOrNull { m ->
val text = readSmallTextFile(m) ?: return@firstOrNull false
val installdir = vdfValue(text, "installdir")
gameFolder != null && installdir != null && installdir.equals(gameFolder, ignoreCase = true)
gameFolder != null && installdir != null && installdir.equals(gameFolder, ignoreCase = true) &&
vdfValue(text, "appid")?.toIntOrNull() != SPACEWAR
} ?: return null
val text = readSmallTextFile(match) ?: return null
@@ -45,15 +45,15 @@ object SteamMatch {
identity?.appId?.let { id ->
return Match(id, Certainty.FILES, identity.source.name.lowercase(), now).also { put(context, key, it) }
}
val known = get(context, key)
val known = get(context, key)?.takeUnless { it.appId == GameIdentifier.SPACEWAR }
if (known != null && known.certainty == Certainty.NAME) return known
if (known != null && known.certainty == Certainty.NONE && now - known.checkedAt < RETRY_AFTER_MS) return known
legacy?.takeIf { it.isFile }?.let { runCatching { it.readText().trim().toInt() }.getOrNull() }?.takeIf { it > 0 }?.let { id ->
legacy?.takeIf { it.isFile }?.let { runCatching { it.readText().trim().toInt() }.getOrNull() }?.takeIf { it > 0 && it != GameIdentifier.SPACEWAR }?.let { id ->
return Match(id, Certainty.NAME, "store search", now).also { put(context, key, it) }
}
if (search == null) return known?.takeIf { it.certainty != Certainty.FILES } ?: Match(null, Certainty.NONE, "", 0)
val id = runCatching { search(name) }.getOrNull()
val match = if (id != null && id > 0) Match(id, Certainty.NAME, "store search", now) else Match(null, Certainty.NONE, "", now)
val match = if (id != null && id > 0 && id != GameIdentifier.SPACEWAR) Match(id, Certainty.NAME, "store search", now) else Match(null, Certainty.NONE, "", now)
put(context, key, match)
return match
}
@@ -154,6 +154,98 @@ class AddedGameArtFillTest {
assertFalse(File(AddedGameArt.cacheDir(app, second), AddedGameArt.EXTRAS_TRIED).exists())
}
private fun steamFrom(asked: MutableList<String>, bytes: Byte, search: Int? = null, slotsAsked: MutableList<Set<Slot>> = ArrayList()) = AddedGameArt.Sources(
storeLogo = { false },
steamSearch = { if (search == null) fail("searched although the game names its appid"); search },
steam = { id, cache, slots ->
asked += id
slotsAsked += slots
slots.forEach { File(cache, steamNames.getValue(it)).writeBytes(byteArrayOf(bytes)) }
slots.isNotEmpty()
},
sgdbGame = { _, _, _ -> null },
sgdb = { _, _, _, _ -> false },
)
@Test fun artOfTheAppTheFilesNamedBeforeIsFetchedAgain() {
val g = game("Peak", 0x8100000AL, cover = false)
File(g.folder, "steam_appid.txt").writeText("1029210")
val asked = ArrayList<String>()
assertTrue(AddedGameArt.fetchMissing(app, listOf(g), "", steamFrom(asked, 1)))
File(g.folder, "steam_appid.txt").writeText("3527290")
assertTrue(AddedGameArt.fetchMissing(app, listOf(g), "", steamFrom(asked, 2)))
assertEquals(listOf("1029210", "3527290"), asked)
assertArrayEquals(byteArrayOf(2), File(AddedGameArt.cacheDir(app, g), "p.jpg").readBytes())
assertFalse(AddedGameArt.fetchMissing(app, listOf(g), "", steamFrom(asked, 3)))
assertEquals(2, asked.size)
}
@Test fun aPictureTheUserChoseOutlivesTheChange() {
val g = game("Peak", 0x8100000BL, cover = false)
File(g.folder, "steam_appid.txt").writeText("1029210")
AddedGameArt.fetchMissing(app, listOf(g), "", steamFrom(ArrayList(), 1))
val picked = File(tmp.root, "mine.jpg").apply { writeBytes(byteArrayOf(7)) }
assertTrue(AddedGameArt.store(AddedGameArt.cacheDir(app, g), Slot.COVER, AddedGameArt.Option(AddedGameArt.FILE, picked.path, picked.path)))
File(g.folder, "steam_appid.txt").writeText("3527290")
val asked = ArrayList<String>()
val slots = ArrayList<Set<Slot>>()
AddedGameArt.fetchMissing(app, listOf(g), "", steamFrom(asked, 2, slotsAsked = slots))
assertArrayEquals(byteArrayOf(7), AddedGameArt.resolve(app, g).portrait!!.readBytes())
assertEquals(listOf("3527290"), asked)
assertEquals(listOf(setOf(Slot.WIDE, Slot.BACKGROUND, Slot.LOGO)), slots)
val cache = AddedGameArt.cacheDir(app, g)
assertArrayEquals(byteArrayOf(2), File(cache, "header.jpg").readBytes())
assertArrayEquals(byteArrayOf(2), File(cache, "hero.jpg").readBytes())
assertFalse(File(cache, AddedGameArt.REFETCH).exists())
}
private fun olderBuildsArt(g: AddedGames.Game, marker: String): File = AddedGameArt.cacheDir(app, g).apply {
mkdirs()
File(this, "steam-appid").writeText(marker)
steamNames.values.forEach { File(this, it).writeBytes(byteArrayOf(1)) }
}
@Test fun anOlderBuildsSpacewarMarkerGoesWithItsArt() {
val g = game("Peak", 0x8100000DL, cover = false)
File(g.folder, "steam_appid.txt").writeText("480")
val cache = olderBuildsArt(g, "480")
val asked = ArrayList<String>()
assertTrue(AddedGameArt.fetchMissing(app, listOf(g), "", steamFrom(asked, 2, search = 3527290)))
assertEquals(listOf("3527290"), asked)
assertArrayEquals(byteArrayOf(2), File(cache, "p.jpg").readBytes())
assertFalse(File(cache, "steam-appid").exists())
}
@Test fun anOlderBuildsMatchStaysWhileTheFilesNameNoApp() {
val g = game("Hades", 0x8100000EL, cover = false)
File(g.folder, "steam_appid.txt").delete()
val cache = olderBuildsArt(g, "1145360")
assertFalse(AddedGameArt.fetchMissing(app, listOf(g), "", steamFrom(ArrayList(), 2)))
assertEquals("1145360", File(cache, "steam-appid").readText())
assertArrayEquals(byteArrayOf(1), File(cache, "p.jpg").readBytes())
}
@Test fun anOlderBuildsMatchGivesWayToTheAppTheFilesName() {
val g = game("Peak", 0x8100000FL, cover = false)
File(g.folder, "steam_appid.txt").writeText("3527290")
val cache = olderBuildsArt(g, "1029210")
val asked = ArrayList<String>()
assertTrue(AddedGameArt.fetchMissing(app, listOf(g), "", steamFrom(asked, 2)))
assertEquals(listOf("3527290"), asked)
assertArrayEquals(byteArrayOf(2), File(cache, "p.jpg").readBytes())
assertFalse(File(cache, "steam-appid").exists())
}
@Test fun filesThatStopNamingAnAppFallBackToTheGamesName() {
val g = game("Peak", 0x8100000CL, cover = false)
File(g.folder, "steam_appid.txt").writeText("1029210")
val asked = ArrayList<String>()
AddedGameArt.fetchMissing(app, listOf(g), "", steamFrom(asked, 1))
File(g.folder, "steam_appid.txt").writeText("480")
assertTrue(AddedGameArt.fetchMissing(app, listOf(g), "", steamFrom(asked, 2, search = 3527290)))
assertEquals(listOf("1029210", "3527290"), asked)
}
@Test fun nothingIsFetchedWhenArtIsOff() {
SessionPrefs.setAddedGamesArt(app, false)
val calls = Calls()
@@ -66,6 +66,29 @@ class AddedGameArtLookupTest {
assertEquals(SteamMatch.Certainty.NAME, match.certainty)
}
@Test fun anEarlierBuildsSpacewarLookupIsNotAMatch() {
val dir = tmp.newFolder("Peak")
val exe = File(dir, "PEAK.exe").apply { writeText("x") }
val legacy = File(tmp.newFolder(), "steam-appid").apply { writeText("480") }
assertEquals(3527290, SteamMatch.resolve(app, dir.path, exe, "PEAK", legacy = legacy) { 3527290 }.appId)
}
@Test fun aSavedSpacewarMatchIsNotAMatch() {
val dir = tmp.newFolder("Peak")
val exe = File(dir, "PEAK.exe").apply { writeText("x") }
SteamMatch.put(app, dir.path, SteamMatch.Match(480, SteamMatch.Certainty.NAME, "store search", System.currentTimeMillis()))
assertNull(SteamMatch.resolve(app, dir.path, exe, "PEAK").appId)
assertEquals(3527290, SteamMatch.resolve(app, dir.path, exe, "PEAK") { 3527290 }.appId)
}
@Test fun aStoreSearchAnsweringSpacewarIsNoMatch() {
val dir = tmp.newFolder("Spacewar Fan Remake")
val exe = File(dir, "game.exe").apply { writeText("x") }
val match = SteamMatch.resolve(app, dir.path, exe, "Spacewar Fan Remake") { 480 }
assertNull(match.appId)
assertEquals(SteamMatch.Certainty.NONE, match.certainty)
}
@Test fun aStoreResultIsTakenOnlyWhenItsNameMatches() {
assertNull(AddedGameArt.pickStoreResult("My Obscure Game", listOf("10" to "Totally Different", "11" to "Another One")))
assertEquals("20", AddedGameArt.pickStoreResult("Hades", listOf("19" to "Hades II Soundtrack Bundle Extra", "20" to "Hades")))
@@ -31,8 +31,25 @@ class GameIdentifierTest {
fun steamAppIdTxt_inSteamSettingsSubdir_isFound() {
val dir = tmp.newFolder("Game")
File(dir, "steam_settings").mkdirs()
File(dir, "steam_settings/steam_appid.txt").writeText("480")
assertEquals(480, GameIdentifier.identify(exeIn(dir)).appId)
File(dir, "steam_settings/steam_appid.txt").writeText("3527290")
assertEquals(3527290, GameIdentifier.identify(exeIn(dir)).appId)
}
@Test
fun spacewarAppId_namesNoGame() {
val dir = tmp.newFolder("PEAK")
File(dir, "steam_appid.txt").writeText("480")
File(dir, "steam_emu.ini").writeText("[Settings]\nAppId=480\n")
assertNull(GameIdentifier.identify(exeIn(dir)).appId)
}
@Test
fun spacewarAppId_fallsThroughToTheGamesOwnId() {
val dir = tmp.newFolder("PEAK")
File(dir, "steam_appid.txt").writeText("480")
File(dir, "steam_settings").mkdirs()
File(dir, "steam_settings/steam_appid.txt").writeText("3527290")
assertEquals(3527290, GameIdentifier.identify(exeIn(dir)).appId)
}
@Test
@@ -44,6 +61,30 @@ class GameIdentifierTest {
assertEquals(GameIdentifier.Source.STEAM_EMU_INI, id.source)
}
@Test
fun spacewarManifest_namesNoGame() {
val steamapps = tmp.newFolder("steamapps")
File(steamapps, "appmanifest_480.acf").writeText(""""AppState" { "appid" "480" "name" "Spacewar" "installdir" "PEAK" }""")
val game = File(steamapps, "common/PEAK").apply { mkdirs() }
File(game, "steam_appid.txt").writeText("3527290")
val id = GameIdentifier.identify(exeIn(game, "PEAK.exe"))
assertEquals(3527290, id.appId)
assertEquals(GameIdentifier.Source.STEAM_APPID_TXT, id.source)
assertTrue(id.name != "Spacewar")
}
@Test
fun spacewarManifest_leavesTheGamesOwnManifestForTheSameFolder() {
val steamapps = tmp.newFolder("steamapps")
File(steamapps, "appmanifest_480.acf").writeText(""""AppState" { "appid" "480" "name" "Spacewar" "installdir" "PEAK" }""")
File(steamapps, "appmanifest_3527290.acf").writeText(""""AppState" { "appid" "3527290" "name" "PEAK" "installdir" "PEAK" }""")
val game = File(steamapps, "common/PEAK").apply { mkdirs() }
val id = GameIdentifier.identify(exeIn(game, "PEAK.exe"))
assertEquals(3527290, id.appId)
assertEquals("PEAK", id.name)
assertEquals(GameIdentifier.Source.STEAM_MANIFEST_ACF, id.source)
}
@Test
fun steamManifestAcf_matchedByInstalldir_givesAppIdAndName() {
val steamapps = tmp.newFolder("steamapps")