mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
feat: games that already have a cover still get a logo and icon
The automatic art fill no longer skips a game just because its folder has a cover. Store games and added games with their own cover now get a missing logo (the store's own logo first, then Steam, then SteamGridDB) and a missing icon from SteamGridDB. Existing, folder and chosen art is never replaced; a slot that stays empty is asked about again after a week, as before.
This commit is contained in:
1 parent
8ceff5641a
commit
be384ca2a2
3 files changed
+260
-36
No files matched your search
@@ -5,6 +5,8 @@ import android.graphics.Bitmap
|
||||
import android.util.Log
|
||||
import com.droiddeck.launcher.files.PeIconExtractor
|
||||
import com.droiddeck.launcher.session.SessionPrefs
|
||||
import com.droiddeck.launcher.stores.StoreArt
|
||||
import com.droiddeck.launcher.stores.StoreGameSidecar
|
||||
import org.json.JSONObject
|
||||
import java.io.File
|
||||
import java.net.HttpURLConnection
|
||||
@@ -228,41 +230,93 @@ object AddedGameArt {
|
||||
return if (tmp.renameTo(png)) png else null
|
||||
}
|
||||
|
||||
fun fetchMissing(context: Context, games: List<AddedGames.Game>): Boolean {
|
||||
internal class Sources(
|
||||
val storeLogo: (AddedGames.Game) -> Boolean,
|
||||
val steamSearch: (String) -> Int?,
|
||||
val steam: (String, File, Set<Slot>) -> Boolean,
|
||||
val sgdbGame: (String, Int?, String) -> Int?,
|
||||
val sgdb: (String, Int, File, Set<Slot>) -> Boolean,
|
||||
)
|
||||
|
||||
private val network = Sources(
|
||||
storeLogo = ::storeLogo,
|
||||
steamSearch = { name -> search(name)?.toIntOrNull() },
|
||||
steam = ::download,
|
||||
sgdbGame = SteamGridDb::gameId,
|
||||
sgdb = SteamGridDb::download,
|
||||
)
|
||||
|
||||
private val EXTRAS = setOf(Slot.LOGO, Slot.ICON)
|
||||
private val STEAM_SLOTS = setOf(Slot.COVER, Slot.WIDE, Slot.BACKGROUND, Slot.LOGO)
|
||||
internal const val EXTRAS_TRIED = "extras-tried"
|
||||
|
||||
internal fun missing(context: Context, game: AddedGames.Game): Set<Slot> {
|
||||
val own = local(game)
|
||||
val cache = cacheDir(context, game)
|
||||
fun has(slot: Slot, mine: File?, cached: String) = chosen(cache, slot) != null || mine != null || File(cache, cached).isFile
|
||||
return Slot.values().filterNot { slot ->
|
||||
when (slot) {
|
||||
Slot.COVER -> has(slot, own.portrait, "p.jpg")
|
||||
Slot.WIDE -> has(slot, own.header, "header.jpg")
|
||||
Slot.BACKGROUND -> has(slot, own.hero, "hero.jpg")
|
||||
Slot.LOGO -> has(slot, own.logo, "logo.png")
|
||||
Slot.ICON -> has(slot, own.icon, "icon-sgdb.png")
|
||||
}
|
||||
}.toSet()
|
||||
}
|
||||
|
||||
private fun storeLogo(game: AddedGames.Game): Boolean {
|
||||
val url = StoreGameSidecar.read(game.folder)?.logo ?: return false
|
||||
return StoreArt.fetchLogo(game.folder, url)
|
||||
}
|
||||
|
||||
fun fetchMissing(context: Context, games: List<AddedGames.Game>): Boolean =
|
||||
fetchMissing(context, games, SteamGridDb.key(context), network)
|
||||
|
||||
internal fun fetchMissing(context: Context, games: List<AddedGames.Game>, sgdbKey: String, sources: Sources): Boolean {
|
||||
if (!SessionPrefs.addedGamesArt(context)) return false
|
||||
val sgdbKey = SteamGridDb.key(context)
|
||||
val keyChangedAt = SteamGridDb.keyChangedAt(context)
|
||||
var sgdbDown = false
|
||||
var changed = false
|
||||
for (game in games) {
|
||||
if (local(game).portrait != null) continue
|
||||
val start = missing(context, game)
|
||||
val full = Slot.COVER in start
|
||||
val allowed = if (full) Slot.values().toSet() else EXTRAS
|
||||
var need = start intersect allowed
|
||||
if (need.isEmpty() || (need == setOf(Slot.ICON) && sgdbKey.isEmpty())) continue
|
||||
val cache = cacheDir(context, game)
|
||||
if (File(cache, "p.jpg").isFile) continue
|
||||
val tried = File(cache, EXTRAS_TRIED)
|
||||
if (!full && tried.isFile && tried.lastModified() >= keyChangedAt && System.currentTimeMillis() - tried.lastModified() < RETRY_AFTER_MS) continue
|
||||
cache.mkdirs()
|
||||
val match = steamMatch(context, game) { name ->
|
||||
runCatching { search(name) }.onFailure { Log.w(TAG, "${game.name}: store search failed (${it.message})") }.getOrNull()?.toIntOrNull()
|
||||
if (Slot.LOGO in need && game.source != Library.ADDED && runCatching { sources.storeLogo(game) }.getOrDefault(false)) {
|
||||
changed = true
|
||||
need = need - Slot.LOGO
|
||||
}
|
||||
val appId = match.appId?.takeIf { match.certainty == SteamMatch.Certainty.FILES }
|
||||
val id = match.appId?.toString()
|
||||
if (id != null && download(id, cache)) changed = true
|
||||
if (File(cache, "p.jpg").isFile || sgdbKey.isEmpty() || sgdbDown) continue
|
||||
val stored: String? = try {
|
||||
remembered(File(cache, "sgdb-id"), keyChangedAt) {
|
||||
SteamGridDb.gameId(sgdbKey, appId ?: id?.toIntOrNull(), game.name)?.toString()
|
||||
val match by lazy {
|
||||
steamMatch(context, game) { name ->
|
||||
runCatching { sources.steamSearch(name) }.onFailure { Log.w(TAG, "${game.name}: store search failed (${it.message})") }.getOrNull()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "${game.name}: SteamGridDB lookup failed (${e.javaClass.simpleName}); not asking again this pass")
|
||||
sgdbDown = true
|
||||
null
|
||||
}
|
||||
val gridId = stored?.toIntOrNull() ?: continue
|
||||
val grabbed = try {
|
||||
SteamGridDb.download(sgdbKey, gridId, cache)
|
||||
} catch (e: Exception) {
|
||||
sgdbDown = true
|
||||
false
|
||||
val steamWant = need intersect STEAM_SLOTS
|
||||
if (steamWant.isNotEmpty()) {
|
||||
val id = match.appId?.toString()
|
||||
if (id != null && sources.steam(id, cache, steamWant)) changed = true
|
||||
need = missing(context, game) intersect allowed
|
||||
}
|
||||
if (grabbed) changed = true
|
||||
var asked = true
|
||||
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()
|
||||
}?.toIntOrNull()
|
||||
if (gridId != null && sources.sgdb(sgdbKey, gridId, cache, need)) changed = true
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "${game.name}: SteamGridDB failed (${e.javaClass.simpleName}); not asking again this pass")
|
||||
sgdbDown = true
|
||||
asked = false
|
||||
}
|
||||
}
|
||||
if ((missing(context, game) intersect EXTRAS).isEmpty()) tried.delete() else if (asked) tried.writeText("")
|
||||
}
|
||||
return changed
|
||||
}
|
||||
@@ -314,11 +368,13 @@ object AddedGameArt {
|
||||
return pickStoreResult(name, (0 until items.length()).map { items.getJSONObject(it) }.map { it.optString("id") to it.optString("name") })
|
||||
}
|
||||
|
||||
private fun download(id: String, cache: File): Boolean {
|
||||
private fun download(id: String, cache: File, slots: Set<Slot>): Boolean {
|
||||
var got = false
|
||||
for ((remote, localName) in listOf(
|
||||
"library_600x900.jpg" to "p.jpg", "header.jpg" to "header.jpg", "library_hero.jpg" to "hero.jpg", "logo.png" to "logo.png",
|
||||
for ((slot, remote, localName) in listOf(
|
||||
Triple(Slot.COVER, "library_600x900.jpg", "p.jpg"), Triple(Slot.WIDE, "header.jpg", "header.jpg"),
|
||||
Triple(Slot.BACKGROUND, "library_hero.jpg", "hero.jpg"), Triple(Slot.LOGO, "logo.png", "logo.png"),
|
||||
)) {
|
||||
if (slot !in slots) continue
|
||||
val dst = File(cache, localName)
|
||||
if (dst.isFile) continue
|
||||
val bytes = get("$CDN$id/$remote") ?: steamAssetUrl(id, remote)?.let { get(it) } ?: continue
|
||||
|
||||
@@ -84,18 +84,23 @@ object SteamGridDb {
|
||||
.filter { it.second > 0 && it.first > 0 }
|
||||
.maxByOrNull { it.second }?.first
|
||||
|
||||
fun download(key: String, id: Int, cache: File): Boolean {
|
||||
internal fun cacheName(slot: AddedGameArt.Slot): String =
|
||||
when (slot) {
|
||||
AddedGameArt.Slot.COVER -> "p.jpg"
|
||||
AddedGameArt.Slot.WIDE -> "header.jpg"
|
||||
AddedGameArt.Slot.BACKGROUND -> "hero.jpg"
|
||||
AddedGameArt.Slot.LOGO -> "logo.png"
|
||||
AddedGameArt.Slot.ICON -> "icon-sgdb.png"
|
||||
}
|
||||
|
||||
fun download(key: String, id: Int, cache: File, slots: Set<AddedGameArt.Slot> = AddedGameArt.Slot.values().toSet()): Boolean {
|
||||
var got = false
|
||||
for ((path, local) in listOf(
|
||||
"grids/game/$id?dimensions=600x900&$QUERY" to "p.jpg",
|
||||
"grids/game/$id?dimensions=460x215,920x430&$QUERY" to "header.jpg",
|
||||
"heroes/game/$id?$QUERY" to "hero.jpg",
|
||||
"logos/game/$id?$QUERY" to "logo.png",
|
||||
"icons/game/$id?$QUERY" to "icon-sgdb.png",
|
||||
)) {
|
||||
for (slot in AddedGameArt.Slot.values()) {
|
||||
if (slot !in slots) continue
|
||||
val local = cacheName(slot)
|
||||
val dst = File(cache, local)
|
||||
if (dst.isFile) continue
|
||||
val items = json(key, "$API/$path")?.optJSONArray("data") ?: continue
|
||||
val items = json(key, "$API/${path(id, slot)}")?.optJSONArray("data") ?: continue
|
||||
val url = best(items, local.endsWith(".png")) ?: continue
|
||||
val bytes = AddedGameArt.get(url) ?: continue
|
||||
val tmp = File(cache, "$local.tmp")
|
||||
|
||||
Reference in new issue
Block a user