From f1c1ca59fe1c4a194b3e9922a0613a401a99ddee Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Wed, 4 Nov 2026 14:12:16 -0500 Subject: [PATCH] fix(battlenet): use the right artwork for each game (#2092) * fix(battlenet): use the right artwork for each game World of Warcraft: Forever took retail art and its logo from a loose name lookup, and only fetched its own art when the Store tab was opened. Shop pages also supplied trailer stills and seasonal promos. Each game now has chosen Blizzard key art with the shop page as a fallback, installed games fetch their art without opening the store, and lookups by name must match exactly. Art found by the old lookup is fetched again once. * fix(battlenet): keep cached art if the new art cannot be fetched Classic's variants fetch through the shared title, and a failing chosen image falls back to the shop page. --- .../launcher/frontend/AddedGameArt.kt | 24 ++++++++- .../launcher/frontend/SteamGridDb.kt | 11 +++- .../droiddeck/launcher/runtime/BattleNet.kt | 24 ++++++--- .../launcher/stores/battlenet/BlizzardArt.kt | 54 ++++++++++++++++--- .../launcher/frontend/AddedGameArtFillTest.kt | 27 +++++++++- .../frontend/AddedGameArtLookupTest.kt | 5 ++ .../stores/battlenet/BlizzardArtTest.kt | 20 +++++++ 7 files changed, 146 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGameArt.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGameArt.kt index bad61e6023..a4385fbb2d 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGameArt.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGameArt.kt @@ -243,6 +243,8 @@ object AddedGameArt { val steam: (String, File, Set) -> Boolean, val sgdbGame: (String, Int?, String) -> Int?, val sgdb: (String, Int, File, Set) -> Boolean, + val sgdbExactGame: (String, Int?, String) -> Int? = { _, _, _ -> null }, + val battleNetArt: (Context, BattleNet.Title) -> Boolean = { _, _ -> false }, ) private val network = Sources( @@ -251,6 +253,8 @@ object AddedGameArt { steam = ::download, sgdbGame = SteamGridDb::gameId, sgdb = SteamGridDb::download, + sgdbExactGame = SteamGridDb::exactGameId, + battleNetArt = { context, title -> BattleNet.fetchCovers(context, listOf(BattleNet.artTitle(title))) }, ) private val EXTRAS = setOf(Slot.LOGO, Slot.ICON) @@ -288,6 +292,9 @@ object AddedGameArt { var changed = false for (game in games) { if (forgetOtherAppsArt(context, game)) changed = true + if (forgetNameMatchedBattleNetArt(context, game)) changed = true + val battleNetTitle = game.storeId?.takeIf { game.source == Store.BATTLENET.id }?.let(BattleNet::title) + if (battleNetTitle != null && runCatching { sources.battleNetArt(context, battleNetTitle) }.getOrDefault(false)) changed = true val cache = cacheDir(context, game) val start = missing(context, game) val again = refetch(cache) intersect start @@ -318,7 +325,7 @@ object AddedGameArt { if (need.isNotEmpty() && sgdbKey.isNotEmpty()) { if (sgdbDown) asked = false else try { val gridId = remembered(File(cache, "sgdb-id"), keyChangedAt) { - sources.sgdbGame(sgdbKey, match.appId, game.name)?.toString() + (if (battleNetTitle != null) sources.sgdbExactGame else sources.sgdbGame)(sgdbKey, match.appId, game.name)?.toString() }?.toIntOrNull() if (gridId != null && sources.sgdb(sgdbKey, gridId, cache, need)) changed = true } catch (e: Exception) { @@ -343,6 +350,21 @@ object AddedGameArt { if (slots.isEmpty()) File(cache, REFETCH).delete() else File(cache, REFETCH).writeText(slots.joinToString(",") { it.key }) } + internal const val BATTLENET_ART_CHECKED = "battlenet-art-2" + + internal fun forgetNameMatchedBattleNetArt(context: Context, game: AddedGames.Game): Boolean { + val title = game.storeId?.takeIf { game.source == Store.BATTLENET.id }?.let(BattleNet::title) ?: return false + val cache = cacheDir(context, game) + val checked = File(cache, BATTLENET_ART_CHECKED) + if (title.steam != null || checked.isFile) return false + val dropped = EXTRAS.filter { File(cache, SteamGridDb.cacheName(it)).delete() } + EXTRAS.forEach { File(cache, SteamGridDb.cacheName(it) + ".src").delete() } + listOf("sgdb-id", EXTRAS_TRIED).forEach { File(cache, it).delete() } + cache.mkdirs() + checked.writeText("") + return dropped.isNotEmpty() + } + 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 } diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/SteamGridDb.kt b/app/src/main/java/com/droiddeck/launcher/frontend/SteamGridDb.kt index 2eaa89410d..0afe9837a0 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/SteamGridDb.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/SteamGridDb.kt @@ -70,15 +70,22 @@ object SteamGridDb { if (!tmp.renameTo(f)) { f.delete(); tmp.renameTo(f) } } - fun gameId(key: String, steamAppId: Int?, name: String): Int? { + fun gameId(key: String, steamAppId: Int?, name: String): Int? = gameId(key, steamAppId, name, ::match) + + fun exactGameId(key: String, steamAppId: Int?, name: String): Int? = gameId(key, steamAppId, name, ::exactMatch) + + private fun gameId(key: String, steamAppId: Int?, name: String, pick: (String, List>) -> Int?): Int? { if (steamAppId != null) { json(key, "$API/games/steam/$steamAppId")?.optJSONObject("data")?.optInt("id")?.takeIf { it > 0 }?.let { return it } } val term = URLEncoder.encode(name, "UTF-8").replace("+", "%20") val results = json(key, "$API/search/autocomplete/$term")?.optJSONArray("data") ?: return null - return match(name, (0 until results.length()).map { results.getJSONObject(it) }.map { it.optInt("id") to it.optString("name") }) + return pick(name, (0 until results.length()).map { results.getJSONObject(it) }.map { it.optInt("id") to it.optString("name") }) } + internal fun exactMatch(name: String, results: List>): Int? = + results.firstOrNull { (id, got) -> id > 0 && AddedGameArt.nameMatch(name, got) == 3 }?.first + internal fun match(name: String, results: List>): Int? = results.map { (id, got) -> id to AddedGameArt.nameMatch(name, got) } .filter { it.second > 0 && it.first > 0 } diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt b/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt index 72e77145c9..fa58c26ea5 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt @@ -66,8 +66,10 @@ object BattleNet { private const val COVER_RETRY_MS = 24 * 3600_000L + internal fun artTitle(title: Title) = TITLES.firstOrNull { it.code == title.code } ?: title + private fun coverDir(context: Context, title: Title) = - File(context.filesDir, "battlenet-art/${(TITLES.firstOrNull { it.code == title.code } ?: title).uid}") + File(context.filesDir, "battlenet-art/${artTitle(title).uid}") fun cover(context: Context, title: Title): File? = art(context, title).cover @@ -79,7 +81,7 @@ object BattleNet { var got = false for (title in titles) { val dir = coverDir(context, title) - val tried = File(dir, ".tried") + val tried = File(dir, ".tried-2") val wide = File(dir, BLIZZARD) val done = File(dir, "p.jpg").isFile && (title.shop == null || wide.isFile) if (done || System.currentTimeMillis() - tried.lastModified() < COVER_RETRY_MS) continue @@ -87,11 +89,15 @@ object BattleNet { if (title.steam != null && runCatching { art.download(title.steam.toString(), dir) }.getOrDefault(false)) got = true if (title.shop != null && !wide.isFile) { val cover = File(dir, BLIZZARD_COVER).takeUnless { File(dir, "p.jpg").isFile } - if (runCatching { com.droiddeck.launcher.stores.battlenet.BlizzardArt.fetch(title.shop, wide, cover) } - .onFailure { Log.w(TAG, "Blizzard art for ${title.uid}: ${it.javaClass.simpleName}") }.getOrDefault(false)) got = true + if (runCatching { com.droiddeck.launcher.stores.battlenet.BlizzardArt.fetch(title.code, title.shop, wide, cover) } + .onFailure { Log.w(TAG, "Blizzard art for ${title.uid}: ${it.javaClass.simpleName}") }.getOrDefault(false)) { + got = true + File(dir, OLD_BLIZZARD).delete() + File(dir, OLD_BLIZZARD_COVER).delete() + } } if (!File(dir, "p.jpg").isFile && !File(dir, BLIZZARD_COVER).isFile && key.isNotEmpty()) { - val id = runCatching { com.droiddeck.launcher.frontend.SteamGridDb.gameId(key, title.steam, title.name) }.getOrNull() + val id = runCatching { com.droiddeck.launcher.frontend.SteamGridDb.exactGameId(key, title.steam, title.name) }.getOrNull() if (id != null && runCatching { com.droiddeck.launcher.frontend.SteamGridDb.download(key, id, dir) }.getOrDefault(false)) got = true } runCatching { tried.writeText("") ; tried.setLastModified(System.currentTimeMillis()) } @@ -111,13 +117,15 @@ object BattleNet { class Art(val cover: File?, val header: File?) - private const val BLIZZARD = "blizzard.jpg" - private const val BLIZZARD_COVER = "p-blizzard.jpg" + private const val BLIZZARD = "blizzard-2.jpg" + private const val BLIZZARD_COVER = "p-blizzard-2.jpg" + private const val OLD_BLIZZARD = "blizzard.jpg" + private const val OLD_BLIZZARD_COVER = "p-blizzard.jpg" fun art(context: Context, title: Title): Art { val dir = coverDir(context, title) fun first(vararg names: String) = names.map { File(dir, it) }.firstOrNull { it.isFile } - return Art(first("p.jpg", BLIZZARD_COVER), first(BLIZZARD, "header.jpg")) + return Art(first("p.jpg", BLIZZARD_COVER, OLD_BLIZZARD_COVER), first(BLIZZARD, OLD_BLIZZARD, "header.jpg")) } fun prefix(context: Context) = File(LinuxRuntime.rootDir(context), "root/.local/share/Steam/steamapps/compatdata/$APP_ID") diff --git a/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BlizzardArt.kt b/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BlizzardArt.kt index 3fc972ab20..fadbe31f54 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BlizzardArt.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BlizzardArt.kt @@ -17,6 +17,41 @@ internal object BlizzardArt { private val OG = Regex(""" host == h.trimStart('.') || host.endsWith(h) } } - fun fetch(slug: String, wide: File, cover: File?): Boolean { - val page = get(URL(SHOP + slug), MAX_PAGE, true)?.toString(Charsets.UTF_8) ?: return false - val url = imageUrl(page) ?: return false - val bytes = get(URL(url.substringBefore('?') + "?imwidth=$WIDE"), MAX_IMAGE, false) ?: return false - val art = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) ?: return false + private fun image(url: String): Bitmap? { + val bytes = get(URL(url.substringBefore('?') + "?imwidth=$WIDE"), MAX_IMAGE, false) ?: return null + return BitmapFactory.decodeByteArray(bytes, 0, bytes.size) + } + + private fun shopImage(slug: String): Bitmap? { + val page = get(URL(SHOP + slug), MAX_PAGE, true)?.toString(Charsets.UTF_8) ?: return null + return imageUrl(page)?.let(::image) + } + + fun fetch(code: String, slug: String, wide: File, cover: File?): Boolean { + val art = runCatching { KEY_ART[code]?.let(::image) }.getOrNull() ?: shopImage(slug) ?: return false try { val scaled = if (art.width > WIDE) Bitmap.createScaledBitmap(art, WIDE, art.height * WIDE / art.width, true) else art save(scaled, wide) diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/AddedGameArtFillTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/AddedGameArtFillTest.kt index b9b223d657..cc8237af3d 100644 --- a/app/src/test/java/com/droiddeck/launcher/frontend/AddedGameArtFillTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/frontend/AddedGameArtFillTest.kt @@ -24,12 +24,12 @@ class AddedGameArtFillTest { private val coverBytes = byteArrayOf(9, 9, 9) - private fun game(name: String, appId: Long, cover: Boolean = true, source: String = Library.ADDED): AddedGames.Game { + private fun game(name: String, appId: Long, cover: Boolean = true, source: String = Library.ADDED, storeId: String? = null): AddedGames.Game { val dir = tmp.newFolder(name) File(dir, "steam_appid.txt").writeText("504230") if (cover) File(dir, "cover.jpg").writeBytes(coverBytes) val exe = File(dir, "$name.exe").apply { writeText("x") } - return AddedGames.Game(dir, name, exe, "/g/$name.exe", "/g", appId, appId, emptyList(), source = source) + return AddedGames.Game(dir, name, exe, "/g/$name.exe", "/g", appId, appId, emptyList(), source = source, storeId = storeId) } private class Calls { @@ -167,6 +167,29 @@ class AddedGameArtFillTest { sgdb = { _, _, _, _ -> false }, ) + @Test fun nameMatchedBattleNetArtIsForgottenOnce() { + val game = game("World of Warcraft: Forever", 9001, source = com.droiddeck.launcher.stores.Store.BATTLENET.id, storeId = "wow_forever") + val cache = AddedGameArt.cacheDir(app, game).apply { mkdirs() } + File(cache, "logo.png").writeBytes(byteArrayOf(1)) + File(cache, "p.jpg").writeBytes(byteArrayOf(3)) + File(cache, "sgdb-id").writeText("5") + assertTrue(AddedGameArt.forgetNameMatchedBattleNetArt(app, game)) + assertFalse(File(cache, "logo.png").isFile) + assertTrue(File(cache, "p.jpg").isFile) + assertFalse(File(cache, "sgdb-id").isFile) + File(cache, "logo.png").writeBytes(byteArrayOf(2)) + assertFalse(AddedGameArt.forgetNameMatchedBattleNetArt(app, game)) + assertTrue(File(cache, "logo.png").isFile) + } + + @Test fun battleNetArtFoundByItsSteamIdIsKept() { + val game = game("Call of Duty: Modern Warfare III", 9002, source = com.droiddeck.launcher.stores.Store.BATTLENET.id, storeId = "pinta") + val cache = AddedGameArt.cacheDir(app, game).apply { mkdirs() } + File(cache, "logo.png").writeBytes(byteArrayOf(1)) + assertFalse(AddedGameArt.forgetNameMatchedBattleNetArt(app, game)) + assertTrue(File(cache, "logo.png").isFile) + } + @Test fun artOfTheAppTheFilesNamedBeforeIsFetchedAgain() { val g = game("Peak", 0x8100000AL, cover = false) File(g.folder, "steam_appid.txt").writeText("1029210") diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/AddedGameArtLookupTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/AddedGameArtLookupTest.kt index 9a949bad2a..124940b918 100644 --- a/app/src/test/java/com/droiddeck/launcher/frontend/AddedGameArtLookupTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/frontend/AddedGameArtLookupTest.kt @@ -195,4 +195,9 @@ class AddedGameArtLookupTest { assertEquals("grids/game/5?dimensions=600x900&nsfw=false&humor=false", SteamGridDb.path(5, AddedGameArt.Slot.COVER)) assertEquals("grids/game/5?dimensions=460x215,920x430&nsfw=false&humor=false", SteamGridDb.path(5, AddedGameArt.Slot.WIDE)) } + + @Test fun anExactLookupDoesNotTakeTheBaseGameForASubtitledOne() { + assertNull(SteamGridDb.exactMatch("World of Warcraft: Forever", listOf(1 to "World of Warcraft"))) + assertEquals(2, SteamGridDb.exactMatch("World of Warcraft: Forever", listOf(1 to "World of Warcraft", 2 to "World of Warcraft: Forever"))) + } } diff --git a/app/src/test/java/com/droiddeck/launcher/stores/battlenet/BlizzardArtTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/battlenet/BlizzardArtTest.kt index 415c6c7cfe..b94b93c909 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/battlenet/BlizzardArtTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/battlenet/BlizzardArtTest.kt @@ -2,6 +2,7 @@ package com.droiddeck.launcher.stores.battlenet import org.junit.Assert.assertEquals import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue import org.junit.Test class BlizzardArtTest { @@ -27,4 +28,23 @@ class BlizzardArtTest { assertNull(BlizzardArt.imageUrl("""""")) assertNull(BlizzardArt.imageUrl("""\"imageUrl\":\"//catalog.blzstatic.com//ff9/a-prod-thumb_bundle.jpg\"""")) } + + @Test fun everyChosenImageComesFromBlizzardsHosts() { + BlizzardArt.KEY_ART.values.forEach { assertTrue(it, BlizzardArt.allowed(it)) } + } + + @Test fun everyChosenImageBelongsToAKnownTitle() { + val codes = com.droiddeck.launcher.runtime.BattleNet.TITLES.map { it.code }.toSet() + BlizzardArt.KEY_ART.keys.forEach { assertTrue(it, it in codes) } + } + + @Test fun classicVariantsShareTheClassicArtTitle() { + val battleNet = com.droiddeck.launcher.runtime.BattleNet + val era = battleNet.TITLES.first { it.uid == "wow_classic_era" } + assertEquals("wow_classic", battleNet.artTitle(era).uid) + } + + @Test fun worldOfWarcraftForeverUsesItsOwnArt() { + assertTrue("WoW_Forever" in BlizzardArt.KEY_ART.getValue("WoWF")) + } }