diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt index e3fa25ac5..3e1a0e85e 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt @@ -123,8 +123,9 @@ object Library { val name = NAME.find(text)?.groupValues?.get(1)?.trim().orEmpty() val flags = STATE.find(text)?.groupValues?.get(1)?.toIntOrNull() ?: 0 if (name.isEmpty() || flags and 4 == 0) return@forEach - val art = steamCacheImage(cache, appId, STEAM_CAPSULES) - val hero = steamCacheImage(cache, appId, STEAM_HEROES) + val images = SteamCacheImages(File(cache, appId.toString())) + val art = images.find(STEAM_CAPSULES) + val hero = images.find(STEAM_HEROES) val lastPlayed = LAST_PLAYED.find(text)?.groupValues?.get(1)?.toLongOrNull() ?: 0L val installDir = INSTALL_DIR.find(text)?.groupValues?.get(1)?.trim() ?.takeIf { it.isNotEmpty() && it != "." && it != ".." && '/' !in it && '\\' !in it } @@ -133,9 +134,8 @@ object Library { appId, name, art, label, hero = hero, lastPlayed = lastPlayed, gameFiles = gameFiles, protonPrefix = protonPrefix(context, appId.toLong(), library, roots), - icon = steamCacheImage(cache, appId, listOf("icon.jpg", "icon.png")) - ?: File(cache, appId.toString()).listFiles()?.firstOrNull { it.isFile && STEAM_ICON.matches(it.name) }, - logo = steamCacheImage(cache, appId, STEAM_LOGOS), + icon = images.icon(), + logo = images.find(STEAM_LOGOS), )) } } @@ -212,13 +212,19 @@ object Library { ) } - internal fun steamCacheImage(cache: File, appId: Int, names: List): File? { - val appDir = File(cache, appId.toString()) - val dirs = listOf(appDir) + appDir.listFiles() - .orEmpty().filter { it.isDirectory }.sortedBy { it.name } - return names.asSequence() + internal fun steamCacheImage(cache: File, appId: Int, names: List): File? = + SteamCacheImages(File(cache, appId.toString())).find(names) + + internal class SteamCacheImages(appDir: File) { + private val entries = appDir.listFiles().orEmpty() + private val dirs = listOf(appDir) + entries.filter { it.isDirectory }.sortedBy { it.name } + + fun find(names: List): File? = names.asSequence() .flatMap { name -> dirs.asSequence().map { File(it, name) } } .firstOrNull { it.isFile && it.length() > 0L } + + fun icon(): File? = find(listOf("icon.jpg", "icon.png")) + ?: entries.firstOrNull { it.isFile && STEAM_ICON.matches(it.name) } } private class Spec(val id: String, val name: String, val system: String, val program: String, val folders: List, val exts: Set, val atPanel: Boolean = false) diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/GameLogoTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/GameLogoTest.kt index 5a577f5b3..505249979 100644 --- a/app/src/test/java/com/droiddeck/launcher/frontend/GameLogoTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/frontend/GameLogoTest.kt @@ -39,6 +39,28 @@ class GameLogoTest { assertNull(Library.steamCacheImage(cache, 730, Library.STEAM_LOGOS)) } + @Test fun aSteamScanKeepsEveryArtworkSlotAndSeesNewHashDirectories() { + val root = SessionPrefs.steamRoot(app, "arm64") + File(root, "steamapps/appmanifest_620.acf").apply { + parentFile!!.mkdirs() + writeText("\"AppState\"\n{\n\"name\" \"Portal 2\"\n\"StateFlags\" \"4\"\n}\n") + } + val cache = File(root, "appcache/librarycache/620") + fun cached(path: String) = File(cache, path).apply { + parentFile!!.mkdirs() + writeBytes(byteArrayOf(1)) + } + val cover = cached("library_capsule.jpg") + val hero = cached("hash/library_hero.jpg") + val icon = cached("${"a".repeat(40)}.jpg") + val game = Library.steamGames(app, arch = "arm64").single() + assertEquals(listOf(cover, hero, icon, null), listOf(game.art, game.hero, game.icon, game.logo)) + + val logo = cached("new-hash/logo.png") + val refreshed = Library.steamGames(app, arch = "arm64").single() + assertEquals(listOf(cover, hero, icon, logo), listOf(refreshed.art, refreshed.hero, refreshed.icon, refreshed.logo)) + } + @Test fun aChosenLogoWinsOverSteamsAndKeepsTheRest() { val stock = image("logo.png") val game = Library.SteamGame(620, "Portal 2", null, "Steam library", logo = stock) diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/SteamCacheImagesTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/SteamCacheImagesTest.kt new file mode 100644 index 000000000..c64a11fcc --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/frontend/SteamCacheImagesTest.kt @@ -0,0 +1,118 @@ +package com.droiddeck.launcher.frontend + +import java.io.File +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder + +class SteamCacheImagesTest { + @get:Rule val tmp = TemporaryFolder() + + private fun image(path: String, bytes: ByteArray = byteArrayOf(1)): File = + File(tmp.root, path).apply { parentFile!!.mkdirs(); writeBytes(bytes) } + + private class CountingDirectory(directory: File) : File(directory.path) { + var listings = 0 + private set + + override fun listFiles(): Array? { + listings++ + return super.listFiles()?.sortedByDescending { it.name }?.toTypedArray() + } + } + + @Test fun allArtworkAndTheHashIconShareOneDirectoryListing() { + val cover = image("620/library_capsule.jpg") + val hero = image("620/b/library_hero.jpg") + val logo = image("620/a/logo.png") + val icon = image("620/${"a".repeat(40)}.png") + val directory = CountingDirectory(File(tmp.root, "620")) + val images = Library.SteamCacheImages(directory) + + assertEquals(cover, images.find(listOf("library_capsule.jpg", "library_600x900.jpg"))) + assertEquals(hero, images.find(listOf("library_hero.jpg"))) + assertEquals(logo, images.find(Library.STEAM_LOGOS)) + assertEquals(icon, images.icon()) + assertEquals(1, directory.listings) + } + + @Test fun preferredFilenamesWinEvenWhenOnlyTheAlternativeIsInTheRoot() { + image("620/library_600x900.jpg") + val preferred = image("620/hash/library_capsule.jpg") + + assertEquals(preferred, Library.steamCacheImage( + tmp.root, 620, listOf("library_capsule.jpg", "library_600x900.jpg"), + )) + } + + @Test fun theRootWinsOverHashDirectoriesForTheSameFilename() { + image("620/hash/logo.png") + val root = image("620/logo.png") + + assertEquals(root, Library.steamCacheImage(tmp.root, 620, Library.STEAM_LOGOS)) + } + + @Test fun hashDirectoriesAreSearchedByNameNotListingOrder() { + image("620/z/logo.png") + val first = image("620/a/logo.png") + val directory = CountingDirectory(File(tmp.root, "620")) + + assertEquals(first, Library.SteamCacheImages(directory).find(Library.STEAM_LOGOS)) + assertEquals(1, directory.listings) + } + + @Test fun emptyFilesAndDirectoriesAreNotArtwork() { + image("620/logo.png", byteArrayOf()) + File(tmp.root, "620/a/logo.png").mkdirs() + val valid = image("620/b/logo.png") + + assertEquals(valid, Library.steamCacheImage(tmp.root, 620, Library.STEAM_LOGOS)) + } + + @Test fun namedIconsKeepTheirFilenamePriorityOverHashIcons() { + image("620/${"a".repeat(40)}.png") + image("620/icon.png") + val preferred = image("620/hash/icon.jpg") + + assertEquals(preferred, Library.SteamCacheImages(File(tmp.root, "620")).icon()) + } + + @Test fun hashIconsKeepTheOriginalRootListingOrderAndAllowEmptyFiles() { + image("620/${"0".repeat(40)}.jpg") + val first = image("620/${"F".repeat(40)}.png", byteArrayOf()) + File(tmp.root, "620/${"z".repeat(40)}.png").mkdirs() + image("620/${"g".repeat(40)}.png") + val directory = CountingDirectory(File(tmp.root, "620")) + val images = Library.SteamCacheImages(directory) + + assertEquals(first, images.icon()) + assertEquals(1, directory.listings) + } + + @Test fun hashIconsOutsideTheRootAndInvalidHashesAreIgnored() { + image("620/hash/${"a".repeat(40)}.jpg") + image("620/${"g".repeat(40)}.png") + image("620/${"a".repeat(39)}.png") + + assertNull(Library.SteamCacheImages(File(tmp.root, "620")).icon()) + } + + @Test fun missingDirectoriesAreListedOnlyOnce() { + val directory = CountingDirectory(File(tmp.root, "missing")) + val images = Library.SteamCacheImages(directory) + + assertNull(images.find(Library.STEAM_LOGOS)) + assertNull(images.icon()) + assertEquals(1, directory.listings) + } + + @Test fun aNewLookupSeesHashDirectoriesAddedAfterThePreviousScan() { + image("620/unrelated.txt") + assertNull(Library.steamCacheImage(tmp.root, 620, Library.STEAM_LOGOS)) + val added = image("620/new-hash/logo.png") + + assertEquals(added, Library.steamCacheImage(tmp.root, 620, Library.STEAM_LOGOS)) + } +} diff --git a/docs/development/steam-game-imports.md b/docs/development/steam-game-imports.md index 7967dd650..78ae2cc11 100644 --- a/docs/development/steam-game-imports.md +++ b/docs/development/steam-game-imports.md @@ -48,6 +48,15 @@ inherit those routes. Managed shortcuts are replaced; user-created shortcuts and unreadable shortcut files are preserved. Both routes use the existing ARM64 Proton registration. +## Launcher artwork + +The installed Steam game scan lists each game's `appcache/librarycache/` +directory once for its cover, background, icon and logo. Filename preference is +checked before directory preference: the app folder, then hash folders sorted by +name. Hash-named fallback icons are still taken only from the app folder in its +listing order. The directory snapshot is not kept between scans, so the next +scan sees new artwork folders. + ## Validation Run `python3 -m unittest discover -s tools/tests -p 'test_*.py'` and Android unit tests.