|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System.Windows.Media.Imaging; |
| 9 | + |
| 10 | +namespace StreamTweak |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Downloads and caches game cover art images. |
| 14 | + /// Currently supports Steam (library_600x900.jpg from Cloudflare CDN). |
| 15 | + /// Cover art is cached in %LOCALAPPDATA%\StreamTweak\covers\. |
| 16 | + /// </summary> |
| 17 | + public static class CoverArtFetcher |
| 18 | + { |
| 19 | + private static readonly HttpClient _http = new HttpClient |
| 20 | + { |
| 21 | + Timeout = TimeSpan.FromSeconds(10) |
| 22 | + }; |
| 23 | + |
| 24 | + // ── Public API ──────────────────────────────────────────────────────── |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Downloads missing cover art for all games in parallel (up to 5 concurrent). |
| 28 | + /// Already-cached images are skipped. Failures are silently ignored. |
| 29 | + /// </summary> |
| 30 | + public static async Task FetchAllAsync(IEnumerable<DiscoveredGame> games, string cacheDir) |
| 31 | + { |
| 32 | + Directory.CreateDirectory(cacheDir); |
| 33 | + |
| 34 | + var toFetch = games.Where(g => GetDownloadUrl(g) != null && GetCachedPath(g, cacheDir) == null).ToList(); |
| 35 | + if (toFetch.Count == 0) return; |
| 36 | + |
| 37 | + using var semaphore = new SemaphoreSlim(5); |
| 38 | + var tasks = toFetch.Select(g => FetchOneAsync(g, cacheDir, semaphore)); |
| 39 | + await Task.WhenAll(tasks); |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Returns the expected cache file path for a game regardless of whether it exists yet. |
| 44 | + /// Returns null for games with no deterministic filename (e.g., empty name). |
| 45 | + /// </summary> |
| 46 | + public static string? GetCacheFilePath(DiscoveredGame game, string cacheDir) |
| 47 | + { |
| 48 | + string? fileName = GetCacheFileName(game); |
| 49 | + return fileName == null ? null : Path.Combine(cacheDir, fileName); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Returns the full path to the cached cover image for a game, or null if not yet cached. |
| 54 | + /// </summary> |
| 55 | + public static string? GetCachedPath(DiscoveredGame game, string cacheDir) |
| 56 | + { |
| 57 | + string? path = GetCacheFilePath(game, cacheDir); |
| 58 | + return (path != null && File.Exists(path)) ? path : null; |
| 59 | + } |
| 60 | + |
| 61 | + // ── Internals ───────────────────────────────────────────────────────── |
| 62 | + |
| 63 | + private static async Task FetchOneAsync(DiscoveredGame game, string cacheDir, SemaphoreSlim semaphore) |
| 64 | + { |
| 65 | + await semaphore.WaitAsync(); |
| 66 | + try |
| 67 | + { |
| 68 | + string? url = GetDownloadUrl(game); |
| 69 | + if (url == null) return; |
| 70 | + |
| 71 | + string? fileName = GetCacheFileName(game); |
| 72 | + if (fileName == null) return; |
| 73 | + |
| 74 | + string cachePath = Path.Combine(cacheDir, fileName); |
| 75 | + if (File.Exists(cachePath)) return; // already cached |
| 76 | + |
| 77 | + byte[] bytes = await _http.GetByteArrayAsync(url); |
| 78 | + |
| 79 | + // Sunshine/Vibeshine requires PNG for image-path. |
| 80 | + // Steam CDN delivers JPEG → decode and re-encode as PNG. |
| 81 | + using var jpegStream = new MemoryStream(bytes); |
| 82 | + var decoder = BitmapDecoder.Create( |
| 83 | + jpegStream, |
| 84 | + BitmapCreateOptions.None, |
| 85 | + BitmapCacheOption.OnLoad); |
| 86 | + var frame = decoder.Frames[0]; |
| 87 | + |
| 88 | + using var pngStream = new FileStream(cachePath, FileMode.Create, FileAccess.Write); |
| 89 | + var encoder = new PngBitmapEncoder(); |
| 90 | + encoder.Frames.Add(BitmapFrame.Create(frame)); |
| 91 | + encoder.Save(pngStream); |
| 92 | + } |
| 93 | + catch { /* silently skip on network/IO errors */ } |
| 94 | + finally |
| 95 | + { |
| 96 | + semaphore.Release(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static string? GetDownloadUrl(DiscoveredGame game) |
| 101 | + { |
| 102 | + // Prefer API-provided URL from IStoreBrowseService (exact, always correct) |
| 103 | + if (!string.IsNullOrEmpty(game.CoverUrl)) |
| 104 | + return game.CoverUrl; |
| 105 | + |
| 106 | + // Legacy CDN fallback for Steam games without an API-provided URL |
| 107 | + return game.Store switch |
| 108 | + { |
| 109 | + "Steam" when game.SteamAppId != null => |
| 110 | + $"https://cdn.cloudflare.steamstatic.com/steam/apps/{game.SteamAppId}/library_600x900.jpg", |
| 111 | + _ => null |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + private static string? GetCacheFileName(DiscoveredGame game) |
| 116 | + { |
| 117 | + if (game.SteamAppId != null) |
| 118 | + return $"steam_{game.SteamAppId}.png"; |
| 119 | + |
| 120 | + string store = game.Store.Replace(" ", "").ToLowerInvariant(); |
| 121 | + |
| 122 | + // Non-Steam: prefer StoreId (stable, deterministic) over sanitized name |
| 123 | + if (game.StoreId != null) |
| 124 | + { |
| 125 | + string safeId = new string(game.StoreId |
| 126 | + .Where(c => char.IsLetterOrDigit(c) || c == '-' || c == '_' || c == '.') |
| 127 | + .ToArray()); |
| 128 | + if (!string.IsNullOrEmpty(safeId)) |
| 129 | + return $"{store}_{safeId}.png"; |
| 130 | + } |
| 131 | + |
| 132 | + // Fallback: sanitized name |
| 133 | + string safe = new string(game.Name |
| 134 | + .Where(c => char.IsLetterOrDigit(c) || c == '-') |
| 135 | + .ToArray()); |
| 136 | + return string.IsNullOrEmpty(safe) ? null : $"{store}_{safe}.png"; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments