diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index ec5ff76c43..d083baa564 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -1963,7 +1963,7 @@ class MainActivity : ComponentActivity() { steamGamesX64 = scannedX64 steamGames = all.map { g -> val newer = current[g.appId] - if (newer != null && g.library != Library.ADDED && steamArtRefreshes[g.appId] != artRefreshesAtStart[g.appId]) g.withArt(newer.art, newer.hero, newer.icon) else g + if (newer != null && g.library != Library.ADDED && steamArtRefreshes[g.appId] != artRefreshesAtStart[g.appId]) g.withArt(newer.art, newer.hero, newer.icon, newer.logo) else g } shortcutLibraryScanning = false handleGameLink(all) 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 1242d9add1..91691cb1e7 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt @@ -16,9 +16,10 @@ object Library { val icon: File? = null, val source: String = SOURCE_STEAM, val storeId: String? = null, + val logo: File? = null, ) { - fun withArt(art: File?, hero: File?, icon: File?) = - SteamGame(appId, name, art, library, gameId, hero, lastPlayed, gameFiles, protonPrefix, icon, source, storeId) + fun withArt(art: File?, hero: File?, icon: File?, logo: File? = this.logo) = + SteamGame(appId, name, art, library, gameId, hero, lastPlayed, gameFiles, protonPrefix, icon, source, storeId, logo) val gameIdString: String get() = java.lang.Long.toUnsignedString(gameId) val profileKey: String get() = if (library == ADDED) java.lang.Integer.toUnsignedString(appId) else appId.toString() @@ -68,6 +69,7 @@ object Library { ) private val STEAM_CAPSULES = listOf("library_capsule.jpg", "library_600x900.jpg") private val STEAM_HEROES = listOf("library_hero.jpg") + internal val STEAM_LOGOS = listOf("logo.png") private val STEAM_ICON = Regex("[a-fA-F0-9]{40}\\.(jpg|png)") private val NAME = Regex("^\\s*\"name\"\\s*\"([^\"]*)\"", RegexOption.MULTILINE) private val STATE = Regex("^\\s*\"StateFlags\"\\s*\"(\\d+)\"", RegexOption.MULTILINE) @@ -131,6 +133,7 @@ object Library { 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), )) } } @@ -203,6 +206,7 @@ object Library { protonPrefix = protonPrefix(context, g.steamAppId?.toLong() ?: g.appId, libraries = roots), icon = art.icon?.takeIf { it.extension.lowercase() != "ico" }, source = g.source, storeId = g.storeId, + logo = art.logo, ) } diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt b/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt index 3d03b23073..cd30e400cf 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt @@ -27,6 +27,7 @@ object LibraryCache { icon = o.file("icon"), source = o.optString("source").ifEmpty { if (o.getString("library") == Library.ADDED) Library.ADDED else Library.SOURCE_STEAM }, storeId = o.optString("storeId").ifEmpty { null }, + logo = o.file("logo"), ) } to x64 }.getOrElse { e -> Log.w(TAG, "unreadable, ignored: ${e.message}"); Pair(emptyList(), false) } @@ -44,7 +45,7 @@ object LibraryCache { for (g in games) array.put( JSONObject().put("appId", g.appId).put("name", g.name).put("library", g.library).put("gameId", g.gameId) .put("lastPlayed", g.lastPlayed).putFile("art", g.art).putFile("hero", g.hero) - .putFile("gameFiles", g.gameFiles).putFile("protonPrefix", g.protonPrefix).putFile("icon", g.icon) + .putFile("gameFiles", g.gameFiles).putFile("protonPrefix", g.protonPrefix).putFile("icon", g.icon).putFile("logo", g.logo) .put("source", g.source).apply { g.storeId?.let { put("storeId", it) } }, ) val f = file(context) diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/SteamGameArt.kt b/app/src/main/java/com/droiddeck/launcher/frontend/SteamGameArt.kt index cb4f49f0fd..25a3cb8fb5 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/SteamGameArt.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/SteamGameArt.kt @@ -22,10 +22,12 @@ object SteamGameArt { val cover = chosenFile(context, game.appId, AddedGameArt.Slot.COVER) val hero = chosenFile(context, game.appId, AddedGameArt.Slot.BACKGROUND) val icon = chosenFile(context, game.appId, AddedGameArt.Slot.ICON) - if (cover == null && hero == null && icon == null) return game + val logo = chosenFile(context, game.appId, AddedGameArt.Slot.LOGO) + if (cover == null && hero == null && icon == null && logo == null) return game return Library.SteamGame( game.appId, game.name, cover ?: game.art, game.library, game.gameId, hero ?: game.hero, game.lastPlayed, game.gameFiles, game.protonPrefix, icon ?: game.icon, game.source, game.storeId, + logo ?: game.logo, ) } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndGames.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndGames.kt index 6793488780..85dbc86949 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndGames.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndGames.kt @@ -26,6 +26,7 @@ import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width +import androidx.compose.foundation.layout.widthIn import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll @@ -324,11 +325,24 @@ private fun ColumnScope.GameHeroCopy(g: Library.SteamGame, titleSize: androidx.c maxLines = 1, overflow = TextOverflow.Ellipsis, ) } - Text( + GameTitle(g, titleSize) + Spacer(Modifier.height(8.dp)) +} + +@Composable +private fun GameTitle(g: Library.SteamGame, titleSize: androidx.compose.ui.unit.TextUnit) { + val logo = g.logo + var failed by remember(logo) { androidx.compose.runtime.mutableStateOf(false) } + if (logo != null && !failed) { + AsyncImage( + model = logo, contentDescription = g.name, contentScale = ContentScale.Fit, alignment = Alignment.CenterStart, + onError = { failed = true }, + modifier = Modifier.padding(vertical = 4.dp).height((titleSize.value * 2.2f).dp).widthIn(max = (titleSize.value * 9f).dp).testTag("game-logo"), + ) + } else Text( g.name, fontSize = titleSize, lineHeight = titleSize * 1.15f, fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.onBackground, maxLines = 2, overflow = TextOverflow.Ellipsis, ) - Spacer(Modifier.height(8.dp)) } @Composable diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/GameLogoTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/GameLogoTest.kt new file mode 100644 index 0000000000..5a577f5b37 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/frontend/GameLogoTest.kt @@ -0,0 +1,80 @@ +package com.droiddeck.launcher.frontend + +import com.droiddeck.launcher.session.SessionPrefs +import org.json.JSONArray +import org.json.JSONObject +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class GameLogoTest { + private val app get() = RuntimeEnvironment.getApplication() + @get:Rule val tmp = TemporaryFolder() + + @Before fun noGamesFolders() { + AddedGamesPublisher.now = true + SessionPrefs.setGameStorage(app, SessionPrefs.GAME_STORAGE_OFF, "") + SessionPrefs.setAddedGamesDirs(app, emptyList()) + } + + private fun image(name: String) = File(tmp.root, name).apply { parentFile?.mkdirs(); writeBytes(ByteArray(8) { 1 }) } + + @Test fun steamsCachedLogoIsFoundInTheAppFolderOrAHashedOne() { + val cache = tmp.newFolder("librarycache") + val logo = File(cache, "620/3f1c/logo.png").apply { parentFile?.mkdirs(); writeBytes(ByteArray(4) { 1 }) } + assertEquals(logo, Library.steamCacheImage(cache, 620, Library.STEAM_LOGOS)) + File(cache, "730").mkdirs() + File(cache, "730/logo.png").writeBytes(ByteArray(0)) + assertNull(Library.steamCacheImage(cache, 730, Library.STEAM_LOGOS)) + } + + @Test fun aChosenLogoWinsOverSteamsAndKeepsTheRest() { + val stock = image("logo.png") + val game = Library.SteamGame(620, "Portal 2", null, "Steam library", logo = stock) + assertTrue(SteamGameArt.withOverrides(app, game) === game) + val picked = image("mine.png") + assertTrue(SteamGameArt.choose(app, game, AddedGameArt.Slot.LOGO, AddedGameArt.Option(AddedGameArt.FILE, picked.path, picked.path))) + val shown = SteamGameArt.withOverrides(app, game) + assertTrue(shown.logo!!.name.startsWith("chosen-logo")) + assertNull(shown.art) + SteamGameArt.reset(app, game, AddedGameArt.Slot.LOGO) + assertEquals(stock, SteamGameArt.withOverrides(app, game).logo) + } + + @Test fun anAddedGameShowsItsFolderLogoUnlessOneWasChosen() { + val dir = tmp.newFolder("Celeste") + val own = File(dir, "logo.png").apply { writeBytes(ByteArray(4) { 1 }) } + val exe = File(dir, "Celeste.exe").apply { writeText("x") } + val g = AddedGames.Game(dir, "Celeste", exe, "/g/Celeste.exe", "/g", 0x82000001L, 0x82000001L, emptyList(), source = "gog") + assertEquals(own, Library.addedGame(app, g, emptyList()).logo) + val picked = image("pick.png") + assertTrue(AddedGameArt.choose(app, g, AddedGameArt.Slot.LOGO, AddedGameArt.Option(AddedGameArt.FILE, picked.path, picked.path))) + assertTrue(Library.addedGame(app, g, emptyList()).logo!!.name.startsWith("chosen-logo")) + val bare = AddedGames.Game(tmp.newFolder("Bare"), "Bare", exe, "/g/Bare.exe", "/g", 0x82000002L, 0x82000002L, emptyList()) + assertNull(Library.addedGame(app, bare, emptyList()).logo) + } + + @Test fun theLibraryCacheKeepsTheLogoAndReadsOlderEntries() { + val logo = image("logo.png") + val game = Library.SteamGame(620, "Portal 2", null, "Steam library", logo = logo) + LibraryCache.save(app, listOf(game, Library.SteamGame(730, "CS2", null, "Steam library"))) + val back = LibraryCache.load(app) + assertEquals(logo, back[0].logo) + assertNull(back[1].logo) + assertEquals(logo, game.withArt(null, null, null).logo) + val file = File(app.filesDir, "library-cache.json") + file.writeText(JSONArray().put(JSONObject().put("appId", 1).put("name", "Old").put("library", "x").put("gameId", 1L)).toString()) + assertNull(LibraryCache.load(app).single().logo) + } +}