feat: Change artwork for Steam games; remove “Show more” from the SteamGridDB search. (#939)

This commit is contained in:
FX1922 authored and GitHub committed 2026-10-16 02:08:09 -04:00
1 parent 8726e43658
commit bab09b798a
12 files changed
+292 -78

No files matched your search

@@ -42,7 +42,7 @@ object AddedGameArt {
private val CHOSEN = Regex("""chosen-(\w+)\.(\w+)""")
private fun chosen(cache: File, slot: Slot): File? =
internal fun chosen(cache: File, slot: Slot): File? =
cache.listFiles { f -> CHOSEN.matchEntire(f.name)?.groupValues?.get(1) == slot.key }?.firstOrNull()
private val IMAGE = listOf("png", "jpg", "jpeg")
@@ -117,36 +117,37 @@ object AddedGameArt {
folderImages(game.folder).forEach { out.add(Option(FOLDER, it.path, it.path)) }
val cache = cacheDir(context, game)
val steamId = SteamMatch.resolve(context, game.folder.path, game.exe, game.name, legacy = File(cache, "steam-appid")).appId?.toString()
val remote = when (slot) { Slot.COVER -> "library_600x900.jpg"; Slot.BACKGROUND -> "library_hero.jpg"; Slot.LOGO -> "logo.png"; Slot.ICON -> null }
if (steamId != null && remote != null) {
val url = "$CDN$steamId/$remote".takeIf { exists(it) } ?: steamAssetUrl(steamId, remote)
if (url != null) out.add(Option(STEAM, url, url))
}
var status = SteamGridDb.Status.OK
val key = SteamGridDb.key(context)
if (key.isEmpty()) {
status = SteamGridDb.Status.NO_KEY
} else {
cache.mkdirs()
try {
val gridId = remembered(File(cache, "sgdb-id"), SteamGridDb.keyChangedAt(context), retryMiss = true) {
SteamGridDb.gameId(key, steamId?.toIntOrNull(), game.name)?.toString()
}?.toIntOrNull()
if (gridId == null) status = SteamGridDb.Status.NO_GAME
else SteamGridDb.images(key, gridId, slot, if (slot == Slot.ICON) 3 else 5)
.forEach { (thumb, full) -> out.add(Option(SGDB, thumb, full)) }
} catch (e: Exception) {
Log.w(TAG, "${game.name}: SteamGridDB failed (${e.javaClass.simpleName})")
status = (e as? SteamGridDb.Failed)?.status ?: SteamGridDb.Status.SERVER
}
}
if (steamId != null) steamOption(steamId, slot)?.let { out.add(it) }
val (found, status) = sgdbChoices(context, File(cache, "sgdb-id"), steamId?.toIntOrNull(), game.name, slot)
out.addAll(found)
return Choices(out, status)
}
internal fun steamOption(steamId: String, slot: Slot): Option? {
val remote = when (slot) { Slot.COVER -> "library_600x900.jpg"; Slot.BACKGROUND -> "library_hero.jpg"; Slot.LOGO -> "logo.png"; Slot.ICON -> null } ?: return null
val url = "$CDN$steamId/$remote".takeIf { exists(it) } ?: steamAssetUrl(steamId, remote) ?: return null
return Option(STEAM, url, url)
}
internal fun sgdbChoices(context: Context, marker: File, steamId: Int?, name: String, slot: Slot): Pair<List<Option>, SteamGridDb.Status> {
val key = SteamGridDb.key(context)
if (key.isEmpty()) return emptyList<Option>() to SteamGridDb.Status.NO_KEY
marker.parentFile?.mkdirs()
return try {
val gridId = remembered(marker, SteamGridDb.keyChangedAt(context), retryMiss = true) {
SteamGridDb.gameId(key, steamId, name)?.toString()
}?.toIntOrNull() ?: return emptyList<Option>() to SteamGridDb.Status.NO_GAME
SteamGridDb.images(key, gridId, slot, if (slot == Slot.ICON) 3 else 5).map { (thumb, full) -> Option(SGDB, thumb, full) } to SteamGridDb.Status.OK
} catch (e: Exception) {
Log.w(TAG, "$name: SteamGridDB failed (${e.javaClass.simpleName})")
emptyList<Option>() to ((e as? SteamGridDb.Failed)?.status ?: SteamGridDb.Status.SERVER)
}
}
fun searchGames(context: Context, term: String): Found<SteamGridDb.Game> = sgdb(context) { SteamGridDb.search(it, term) }
fun gameImages(context: Context, id: Int, slot: Slot, page: Int): Found<Option> = sgdb(context) { key ->
SteamGridDb.page(key, id, slot, page).map { (thumb, full) -> Option(SGDB, thumb, full) }
fun gameImages(context: Context, id: Int, slot: Slot): Found<Option> = sgdb(context) { key ->
SteamGridDb.page(key, id, slot).map { (thumb, full) -> Option(SGDB, thumb, full) }
}
private fun <T> sgdb(context: Context, call: (String) -> List<T>): Found<T> {
@@ -161,17 +162,21 @@ object AddedGameArt {
}
fun choose(context: Context, game: AddedGames.Game, slot: Slot, option: Option): Boolean {
if (!store(cacheDir(context, game), slot, option)) return false
SessionPrefs.setAddedGameArtSource(context, game.folder.path, slot.key, option.group + "|" + option.full)
return true
}
internal fun store(cache: File, slot: Slot, option: Option): Boolean {
val bytes = if (option.full.startsWith("https://")) get(option.full) else runCatching { File(option.full).readBytes() }.getOrNull()
bytes ?: return false
val ext = option.full.substringAfterLast('.', "png").substringBefore('?').lowercase().takeIf { it in CHOOSABLE + "ico" } ?: "png"
val cache = cacheDir(context, game).apply { mkdirs() }
cache.mkdirs()
chosen(cache, slot)?.delete()
val dst = File(cache, "chosen-${slot.key}.${if (ext == "jpeg") "jpg" else ext}")
val tmp = File(cache, dst.name + ".tmp")
tmp.writeBytes(bytes)
if (!tmp.renameTo(dst)) return false
SessionPrefs.setAddedGameArtSource(context, game.folder.path, slot.key, option.group + "|" + option.full)
return true
return tmp.renameTo(dst)
}
fun chosenSource(context: Context, game: AddedGames.Game, slot: Slot): Pair<String, String> {
@@ -116,13 +116,13 @@ object Library {
val installDir = INSTALL_DIR.find(text)?.groupValues?.get(1)?.trim()
?.takeIf { it.isNotEmpty() && it != "." && it != ".." && '/' !in it && '\\' !in it }
val gameFiles = installDir?.let { File(steamapps, "common/$it").takeIf(File::isDirectory) }
out[appId] = SteamGame(
out[appId] = SteamGameArt.withOverrides(context, SteamGame(
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) },
)
))
}
}
return out.values.toList()
@@ -152,7 +152,7 @@ object Library {
)
}
private fun steamCacheImage(cache: File, appId: Int, names: List<String>): File? {
internal fun steamCacheImage(cache: File, appId: Int, names: List<String>): File? {
val appDir = File(cache, appId.toString())
val dirs = listOf(appDir) + appDir.listFiles()
.orEmpty().filter { it.isDirectory }.sortedBy { it.name }
@@ -0,0 +1,76 @@
package com.droiddeck.launcher.frontend
import android.content.Context
import com.droiddeck.launcher.runtime.LinuxRuntime
import com.droiddeck.launcher.stores.SteamLiveShortcuts
import java.io.File
object SteamGameArt {
internal fun dir(context: Context, appId: Int) = File(context.filesDir, "steam-art/$appId")
private fun sourceFile(context: Context, appId: Int, slot: AddedGameArt.Slot) = File(dir(context, appId), "source-${slot.key}")
fun chosenFile(context: Context, appId: Int, slot: AddedGameArt.Slot): File? = AddedGameArt.chosen(dir(context, appId), slot)
fun withOverrides(context: Context, game: Library.SteamGame): Library.SteamGame {
if (game.library == Library.ADDED || !dir(context, game.appId).isDirectory) return game
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
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,
)
}
fun chosenSource(context: Context, appId: Int, slot: AddedGameArt.Slot): Pair<String, String> {
val value = sourceFile(context, appId, slot).takeIf { it.isFile }?.readText()?.trim().orEmpty()
return value.substringBefore('|') to value.substringAfter('|', "")
}
fun describe(context: Context, game: Library.SteamGame): List<AddedGameArt.SlotState> {
val cache = File(LinuxRuntime.rootDir(context), "root/.local/share/Steam/appcache/librarycache")
val own = withOverrides(context, game)
return AddedGameArt.Slot.values().map { slot ->
chosenFile(context, game.appId, slot)?.let {
return@map AddedGameArt.SlotState(slot, it, chosenSource(context, game.appId, slot).first.ifEmpty { AddedGameArt.FILE }, true)
}
val auto = when (slot) {
AddedGameArt.Slot.COVER -> own.art
AddedGameArt.Slot.BACKGROUND -> own.hero
AddedGameArt.Slot.ICON -> own.icon
AddedGameArt.Slot.LOGO -> Library.steamCacheImage(cache, game.appId, listOf("logo.png"))
}
AddedGameArt.SlotState(slot, auto, if (auto != null) AddedGameArt.STEAM else AddedGameArt.AUTO, false)
}
}
fun choices(context: Context, game: Library.SteamGame, slot: AddedGameArt.Slot): AddedGameArt.Choices {
val out = ArrayList<AddedGameArt.Option>()
AddedGameArt.steamOption(game.appId.toString(), slot)?.let { out.add(it) }
val (found, status) = AddedGameArt.sgdbChoices(context, File(dir(context, game.appId), "sgdb-id"), game.appId, game.name, slot)
out.addAll(found)
return AddedGameArt.Choices(out, status)
}
fun choose(context: Context, game: Library.SteamGame, slot: AddedGameArt.Slot, option: AddedGameArt.Option): Boolean {
val folder = dir(context, game.appId)
if (!AddedGameArt.store(folder, slot, option)) return false
sourceFile(context, game.appId, slot).writeText(option.group + "|" + option.full)
push(context, game, slot)
return true
}
fun reset(context: Context, game: Library.SteamGame, slot: AddedGameArt.Slot) {
AddedGameArt.chosen(dir(context, game.appId), slot)?.delete()
sourceFile(context, game.appId, slot).delete()
push(context, game, slot)
}
private fun push(context: Context, game: Library.SteamGame, slot: AddedGameArt.Slot) {
val type = slot.assetType ?: return
val file = chosenFile(context, game.appId, slot)
AddedGamesPublisher.later(context) { SteamLiveShortcuts.setArt(context, game.appId.toLong(), type, file) }
}
}
@@ -118,23 +118,21 @@ object SteamGridDb {
.filter { it.optInt("id") > 0 && it.optString("name").isNotEmpty() }
.map { Game(it.optInt("id"), it.optString("name"), it.optBoolean("verified")) }
internal fun path(id: Int, slot: AddedGameArt.Slot, page: Int): String {
val base = when (slot) {
internal fun path(id: Int, slot: AddedGameArt.Slot): String =
when (slot) {
AddedGameArt.Slot.COVER -> "grids/game/$id?dimensions=600x900&$QUERY"
AddedGameArt.Slot.BACKGROUND -> "heroes/game/$id?$QUERY"
AddedGameArt.Slot.LOGO -> "logos/game/$id?$QUERY"
AddedGameArt.Slot.ICON -> "icons/game/$id?$QUERY"
}
return if (page > 0) "$base&page=$page" else base
}
fun page(key: String, id: Int, slot: AddedGameArt.Slot, page: Int): List<Pair<String, String>> {
val items = json(key, "$API/${path(id, slot, page)}")?.optJSONArray("data") ?: return emptyList()
fun page(key: String, id: Int, slot: AddedGameArt.Slot): List<Pair<String, String>> {
val items = json(key, "$API/${path(id, slot)}")?.optJSONArray("data") ?: return emptyList()
return ranked(items, pngOnly = slot == AddedGameArt.Slot.LOGO || slot == AddedGameArt.Slot.ICON)
}
fun images(key: String, id: Int, slot: AddedGameArt.Slot, limit: Int): List<Pair<String, String>> =
page(key, id, slot, 0).take(limit)
page(key, id, slot).take(limit)
internal fun ranked(items: JSONArray, pngOnly: Boolean): List<Pair<String, String>> =
(0 until items.length()).map { items.getJSONObject(it) }