game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
steamgriddb_service.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// steamgriddb_service.h
4// ==============================================================================
5// SteamGridDB integration for fetching game images for non-Steam games
6// ==============================================================================
7// Provides image fetching from SteamGridDB (steamgriddb.com) for games that
8// aren't on Steam or don't have local cover art.
9//
10// ## Features
11// - Search for games by name
12// - Fetch cover art (vertical covers, grids)
13// - Fetch hero images (wide banners)
14// - Fetch logos with transparency
15// - Download and cache images locally
16//
17// ## API Key
18// SteamGridDB requires an API key for full access. Users can get a free key
19// from https://www.steamgriddb.com/profile/preferences/api
20// Without an API key, only limited functionality is available.
21//
22// ## Thread Safety
23// All methods are thread-safe and can be called from any thread.
24// ==============================================================================
25
26#pragma once
27
28#include <functional>
29#include <map>
30#include <mutex>
31#include <string>
32#include <vector>
33
34// ==============================================================================
35// SteamGridDB Data Structures
36// ==============================================================================
37
38/**
39 * @struct SteamGridDBGame
40 * @brief Search result from SteamGridDB
41 */
43{
44 int id = 0; ///< SteamGridDB game ID
45 std::string name; ///< Game name
46 std::string releaseDate; ///< Release date (YYYY-MM-DD)
47 bool verified = false; ///< If game is verified on SteamGridDB
48 int steamAppId = 0; ///< Steam App ID if linked (0 if not Steam)
49};
50
51/**
52 * @struct SteamGridDBImage
53 * @brief Image result from SteamGridDB
54 */
56{
57 int id = 0; ///< Image ID on SteamGridDB
58 std::string url; ///< URL to download image
59 std::string thumbUrl; ///< URL for thumbnail
60 int width = 0; ///< Image width in pixels
61 int height = 0; ///< Image height in pixels
62 std::string style; ///< Style: "alternate", "blurred", "white_logo", etc.
63 std::string mime; ///< MIME type (image/png, image/jpeg)
64 int score = 0; ///< Community score
65 bool nsfw = false; ///< NSFW flag
66};
67
68/**
69 * @struct SteamGridDBImageSet
70 * @brief Complete set of images for a game
71 */
73{
74 std::vector<SteamGridDBImage> grids; ///< Cover art / grid images (600x900)
75 std::vector<SteamGridDBImage> heroes; ///< Hero images (wide banners)
76 std::vector<SteamGridDBImage> logos; ///< Logo images with transparency
77 std::vector<SteamGridDBImage> icons; ///< Icon images
78};
79
80// ==============================================================================
81// SteamGridDB Service Class
82// ==============================================================================
83
84/**
85 * @class SteamGridDBService
86 * @brief Service for fetching game images from SteamGridDB
87 *
88 * Provides methods to search for games and fetch images from SteamGridDB.
89 * Requires an API key for full functionality.
90 *
91 * ## Usage Example
92 * @code
93 * // Set API key (from user settings)
94 * SteamGridDBService::setApiKey("your-api-key-here");
95 *
96 * // Search for a game
97 * auto games = SteamGridDBService::searchGames("World of Warcraft");
98 * if (!games.empty()) {
99 * // Fetch cover art
100 * auto images = SteamGridDBService::getGridImages(games[0].id);
101 * if (!images.empty()) {
102 * // Download the best cover
103 * std::string localPath = SteamGridDBService::downloadImage(
104 * images[0], "World of Warcraft");
105 * }
106 * }
107 * @endcode
108 */
110{
111 public:
112 // ==========================================================================
113 // Configuration
114 // ==========================================================================
115
116 /**
117 * @brief Set the API key for SteamGridDB
118 * @param key API key from steamgriddb.com
119 *
120 * Without an API key, only limited public endpoints are available.
121 */
122 static void setApiKey(const std::string& key);
123
124 /**
125 * @brief Check if API key is configured
126 * @return True if API key is set
127 */
128 static bool hasApiKey();
129
130 /**
131 * @brief Get the cache directory for downloaded images
132 * @return Path to cache directory
133 */
134 static std::string getCacheDirectory();
135
136 // ==========================================================================
137 // Game Search
138 // ==========================================================================
139
140 /**
141 * @brief Search for games by name
142 * @param query Search query (game name)
143 * @return Vector of matching games
144 */
145 static std::vector<SteamGridDBGame> searchGames(const std::string& query);
146
147 /**
148 * @brief Search for games by name asynchronously
149 * @param query Search query
150 * @param callback Function to call with results
151 */
152 static void
153 searchGamesAsync(const std::string& query,
154 const std::function<void(const std::vector<SteamGridDBGame>&)>& callback);
155
156 /**
157 * @brief Get game info by Steam App ID
158 * @param steamAppId Steam App ID
159 * @return Game info if found
160 */
161 static SteamGridDBGame getGameBySteamAppId(int steamAppId);
162
163 // ==========================================================================
164 // Image Fetching
165 // ==========================================================================
166
167 /**
168 * @brief Get grid/cover images for a game
169 * @param gameId SteamGridDB game ID
170 * @return Vector of available grid images
171 */
172 static std::vector<SteamGridDBImage> getGridImages(int gameId);
173
174 /**
175 * @brief Get hero images for a game
176 * @param gameId SteamGridDB game ID
177 * @return Vector of available hero images
178 */
179 static std::vector<SteamGridDBImage> getHeroImages(int gameId);
180
181 /**
182 * @brief Get logo images for a game
183 * @param gameId SteamGridDB game ID
184 * @return Vector of available logo images
185 */
186 static std::vector<SteamGridDBImage> getLogoImages(int gameId);
187
188 /**
189 * @brief Get icon images for a game
190 * @param gameId SteamGridDB game ID
191 * @return Vector of available icon images
192 */
193 static std::vector<SteamGridDBImage> getIconImages(int gameId);
194
195 /**
196 * @brief Get all images for a game
197 * @param gameId SteamGridDB game ID
198 * @return Complete image set
199 */
200 static SteamGridDBImageSet getAllImages(int gameId);
201
202 // ==========================================================================
203 // Image Download
204 // ==========================================================================
205
206 /**
207 * @brief Download an image to local cache
208 * @param image Image to download
209 * @param gameName Game name (used for filename)
210 * @return Local path to downloaded image, or empty on failure
211 */
212 static std::string downloadImage(const SteamGridDBImage& image, const std::string& gameName);
213
214 /**
215 * @brief Download best cover art for a game by name
216 * @param gameName Name of the game to search for
217 * @return Local path to downloaded cover, or empty if not found
218 *
219 * This is a convenience method that searches for the game,
220 * finds the best rated cover, and downloads it.
221 */
222 static std::string downloadBestCover(const std::string& gameName);
223
224 /**
225 * @brief Download best cover art asynchronously
226 * @param gameName Name of the game
227 * @param callback Function to call with local path (empty on failure)
228 */
229 static void downloadBestCoverAsync(const std::string& gameName,
230 const std::function<void(const std::string&)>& callback);
231
232 /**
233 * @brief Check if we have a cached cover for a game
234 * @param gameName Game name
235 * @return Path to cached cover, or empty if not cached
236 */
237 static std::string getCachedCover(const std::string& gameName);
238
239 // ==========================================================================
240 // Cache Management
241 // ==========================================================================
242
243 /**
244 * @brief Clear all cached images
245 */
246 static void clearCache();
247
248 /**
249 * @brief Get total size of cached images in bytes
250 */
251 static size_t getCacheSize();
252
253 private:
254 // ==========================================================================
255 // Internal Methods
256 // ==========================================================================
257
258 static std::string httpGet(const std::string& url,
259 const std::map<std::string, std::string>& headers = {});
260 static bool downloadFile(const std::string& url, const std::string& localPath);
261 static std::string urlEncode(const std::string& str);
262
263 // JSON parsing
264 static std::vector<SteamGridDBGame> parseGamesJson(const std::string& json);
265 static std::vector<SteamGridDBImage> parseImagesJson(const std::string& json);
266
267 // File utilities
268 static std::string sanitizeFilename(const std::string& name);
269 static std::string getImageExtension(const std::string& mime);
270
271 // ==========================================================================
272 // Static Members
273 // ==========================================================================
274
275 static std::string apiKey_;
276 static std::mutex apiKeyMutex_;
277};
Service for fetching game images from SteamGridDB.
static std::vector< SteamGridDBGame > searchGames(const std::string &query)
Search for games by name.
static std::string downloadImage(const SteamGridDBImage &image, const std::string &gameName)
Download an image to local cache.
static std::string getCachedCover(const std::string &gameName)
Check if we have a cached cover for a game.
static std::vector< SteamGridDBImage > getGridImages(int gameId)
Get grid/cover images for a game.
static SteamGridDBGame getGameBySteamAppId(int steamAppId)
Get game info by Steam App ID.
static std::string downloadBestCover(const std::string &gameName)
Download best cover art for a game by name.
static void searchGamesAsync(const std::string &query, const std::function< void(const std::vector< SteamGridDBGame > &)> &callback)
Search for games by name asynchronously.
static void setApiKey(const std::string &key)
Set the API key for SteamGridDB.
static std::vector< SteamGridDBImage > getLogoImages(int gameId)
Get logo images for a game.
static bool hasApiKey()
Check if API key is configured.
static SteamGridDBImageSet getAllImages(int gameId)
Get all images for a game.
static void downloadBestCoverAsync(const std::string &gameName, const std::function< void(const std::string &)> &callback)
Download best cover art asynchronously.
static void clearCache()
Clear all cached images.
static size_t getCacheSize()
Get total size of cached images in bytes.
static std::string getCacheDirectory()
Get the cache directory for downloaded images.
static std::vector< SteamGridDBImage > getHeroImages(int gameId)
Get hero images for a game.
static std::vector< SteamGridDBImage > getIconImages(int gameId)
Get icon images for a game.
nlohmann::json json
Search result from SteamGridDB.
int steamAppId
Steam App ID if linked (0 if not Steam).
std::string releaseDate
Release date (YYYY-MM-DD).
std::string name
Game name.
bool verified
If game is verified on SteamGridDB.
Complete set of images for a game.
std::vector< SteamGridDBImage > icons
Icon images.
std::vector< SteamGridDBImage > heroes
Hero images (wide banners).
std::vector< SteamGridDBImage > logos
Logo images with transparency.
std::vector< SteamGridDBImage > grids
Cover art / grid images (600x900).
Image result from SteamGridDB.
int width
Image width in pixels.
std::string thumbUrl
URL for thumbnail.
std::string mime
MIME type (image/png, image/jpeg).
std::string url
URL to download image.
std::string style
Style: "alternate", "blurred", "white_logo", etc.
int score
Community score.
int height
Image height in pixels.