game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
lutris_api_service.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// lutris_api_service.h
4// ==============================================================================
5// Lutris API client for searching games and fetching installer scripts
6// ==============================================================================
7// Provides functionality to search the Lutris database for games and
8// retrieve installer scripts that automate game installation.
9//
10// API Endpoints:
11// - GET /api/games?search=<query> - Search for games
12// - GET /api/games/<slug> - Get game details
13// - GET /api/installers?game_slug=<slug> - Get installers for a game
14// ==============================================================================
15
16#pragma once
17
18#include <chrono>
19#include <cstdint>
20#include <functional>
21#include <map>
22#include <mutex>
23#include <string>
24#include <vector>
25
26// ==============================================================================
27// Installer File Definition
28// ==============================================================================
29
31{
32 std::string id; // Reference ID in script (e.g., "setup", "patch")
33 std::string url; // Download URL or "N/A: <message>" for manual files
34 std::string filename; // Target filename
35 std::string checksum; // Optional checksum (md5 or sha256)
36 uint64_t size = 0; // Expected file size (if known)
37 bool isManual = false; // True if user must provide this file
38};
39
40// ==============================================================================
41// Installation Step
42// ==============================================================================
43
45{
46 std::string task; // "extract", "copy", "execute", "task", "write_file", etc.
47 std::string description; // Human-readable description (generated)
48
49 // Parameters vary by task type - stored as key-value pairs
50 std::map<std::string, std::string> params;
51 // Examples:
52 // extract: {"file": "$setup", "dst": "$GAMEDIR"}
53 // copy: {"src": "data/*", "dst": "$GAMEDIR"}
54 // execute: {"file": "$setup", "args": "/silent"}
55 // task: {"name": "winetricks", "args": "vcrun2019"}
56};
57
58// ==============================================================================
59// Wine Configuration
60// ==============================================================================
61
63{
64 std::string version; // Wine version (e.g., "wine-ge-8-26")
65 std::string arch; // "win32" or "win64"
66 bool dxvk = false; // Enable DXVK
67 bool vkd3d = false; // Enable VKD3D
68 bool esync = false; // Enable Esync
69 bool fsync = false; // Enable Fsync
70 bool battleye = false; // Enable BattlEye anti-cheat support
71 bool eac = false; // Enable Easy Anti-Cheat support
72
73 std::map<std::string, std::string> dllOverrides; // DLL overrides (native/builtin)
74 std::map<std::string, std::string> environment; // Extra environment variables
75};
76
77// ==============================================================================
78// Complete Installer Script
79// ==============================================================================
80
82{
83 // Identification
84 std::string gameSlug;
85 std::string gameName;
86 std::string installerSlug;
87 std::string installerName;
88 std::string version;
89 std::string runner; // "wine", "linux", "steam", "dosbox", etc.
90
91 // Files to download
92 std::vector<LutrisInstallerFile> files;
93
94 // Installation steps
95 std::vector<LutrisInstallStep> steps;
96
97 // Wine/Proton configuration (if wine runner)
99
100 // Game configuration (how to run after install)
101 std::string executable; // Path to exe (relative to prefix or absolute)
102 std::string workingDir; // Working directory
103 std::string arguments; // Launch arguments
104 std::string prefix; // Wine prefix path (variable like $GAMEDIR/prefix)
105 std::map<std::string, std::string> environmentVariables;
106
107 // Metadata
108 std::string description;
109 std::string notes; // Installation notes (shown to user)
110 std::string createdAt;
111 std::string updatedAt;
112 int rating = 0; // User rating
113 int downloadCount = 0; // Number of installs
114 bool isPublished = false;
115 bool isDraft = false;
116
117 // Store IDs (if applicable)
118 int steamId = 0;
119 int gogId = 0;
120 std::string gogSlug;
121 std::string humbleId;
122
123 // Check if script has valid data
124 bool isValid() const
125 {
126 return !installerSlug.empty() && !runner.empty();
127 }
128
129 // Get total download size (for files with known sizes)
130 uint64_t getTotalDownloadSize() const;
131
132 // Check if has manual files that user must provide
133 bool hasManualFiles() const;
134};
135
136// ==============================================================================
137// Search Result
138// ==============================================================================
139
141{
142 std::string slug;
143 std::string name;
144 int year = 0;
145 int steamId = 0;
146 int gogId = 0;
147 std::string iconUrl;
148 std::string bannerUrl;
149 std::string coverUrl;
151 int userCount = 0; // Number of users who have this game
152
153 // Quick flags
154 bool hasWineInstaller = false;
155 bool hasSteamInstaller = false;
156 bool hasLinuxInstaller = false;
157};
158
159// ==============================================================================
160// Installer Summary (lightweight version for listing)
161// ==============================================================================
162
164{
165 std::string slug;
166 std::string name;
167 std::string version;
168 std::string runner;
169 std::string description;
170 int rating = 0;
172 bool isPublished = false;
173 std::string createdAt;
174};
175
176// ==============================================================================
177// Lutris API Service
178// ==============================================================================
179
180/**
181 * @class LutrisAPIService
182 * @brief Client for the Lutris.net API
183 *
184 * Provides methods to search for games in the Lutris database and
185 * retrieve installer scripts that can be used to automate game installation.
186 */
188{
189 public:
190 // =========================================================================
191 // Search
192 // =========================================================================
193
194 /**
195 * @brief Search for games by name
196 * @param query Search query
197 * @param maxResults Maximum number of results (default 20)
198 * @return List of matching games
199 */
200 static std::vector<LutrisSearchResult> searchGames(const std::string& query,
201 int maxResults = 20);
202
203 /**
204 * @brief Search for games asynchronously
205 * @param query Search query
206 * @param callback Called with results
207 * @param maxResults Maximum number of results
208 */
209 static void
210 searchGamesAsync(const std::string& query,
211 const std::function<void(const std::vector<LutrisSearchResult>&)>& callback,
212 int maxResults = 20);
213
214 /**
215 * @brief Find a game by Steam App ID
216 * @param steamAppId Steam application ID
217 * @return Game info if found
218 */
219 static LutrisSearchResult findBySteamId(int steamAppId);
220
221 /**
222 * @brief Find a game by GOG ID
223 * @param gogId GOG game ID
224 * @return Game info if found
225 */
226 static LutrisSearchResult findByGogId(int gogId);
227
228 // =========================================================================
229 // Installers
230 // =========================================================================
231
232 /**
233 * @brief Get available installers for a game
234 * @param gameSlug Game slug from search results
235 * @return List of installer summaries
236 */
237 static std::vector<LutrisInstallerSummary> getInstallers(const std::string& gameSlug);
238
239 /**
240 * @brief Get available installers asynchronously
241 * @param gameSlug Game slug
242 * @param callback Called with results
243 */
244 static void getInstallersAsync(
245 const std::string& gameSlug,
246 const std::function<void(const std::vector<LutrisInstallerSummary>&)>& callback);
247
248 /**
249 * @brief Get full installer script with all details
250 * @param gameSlug Game slug
251 * @param installerSlug Installer slug
252 * @return Complete installer script
253 */
254 static LutrisInstallerScript getInstallerScript(const std::string& gameSlug,
255 const std::string& installerSlug);
256
257 /**
258 * @brief Get installer script asynchronously
259 * @param gameSlug Game slug
260 * @param installerSlug Installer slug
261 * @param callback Called with result
262 */
263 static void
264 getInstallerScriptAsync(const std::string& gameSlug, const std::string& installerSlug,
265 const std::function<void(const LutrisInstallerScript&)>& callback);
266
267 // =========================================================================
268 // Game Details
269 // =========================================================================
270
271 /**
272 * @brief Get detailed information about a game
273 * @param gameSlug Game slug
274 * @return Search result with full details
275 */
276 static LutrisSearchResult getGameDetails(const std::string& gameSlug);
277
278 // =========================================================================
279 // Utilities
280 // =========================================================================
281
282 /**
283 * @brief Check if Lutris API is accessible
284 * @return True if API responds
285 */
286 static bool isApiAvailable();
287
288 /**
289 * @brief Clear cached data
290 */
291 static void clearCache();
292
293 private:
294 // `constexpr const char*` instead of `const std::string` so the
295 // initializer is a true compile-time constant — avoids the
296 // bugprone-throwing-static-initialization /
297 // CERT ERR58-CPP issue where a pre-main `std::string` allocation
298 // failure is uncatchable.
299 static constexpr const char* API_BASE = "https://lutris.net/api";
300
301 // HTTP request
302 static std::string httpGet(const std::string& url);
303
304 // JSON parsing helpers (following existing codebase pattern)
305 static std::string jsonGetString(const std::string& json, const std::string& key);
306 static int jsonGetInt(const std::string& json, const std::string& key);
307 static bool jsonGetBool(const std::string& json, const std::string& key);
308 static std::vector<std::string> jsonGetArray(const std::string& json, const std::string& key);
309 static std::vector<std::string> jsonSplitArray(const std::string& arrayJson);
310
311 // Parse responses
312 static LutrisSearchResult parseGameJson(const std::string& json);
313 static std::vector<LutrisSearchResult> parseSearchResults(const std::string& json);
314 static LutrisInstallerSummary parseInstallerSummary(const std::string& json);
315 static LutrisInstallerScript parseInstallerScript(const std::string& json);
316 static std::vector<LutrisInstallStep> parseSteps(const std::string& scriptJson);
317 static std::vector<LutrisInstallerFile> parseFiles(const std::string& filesJson);
318 static LutrisWineConfig parseWineConfig(const std::string& json);
319
320 // Generate step descriptions
321 static std::string describeStep(const LutrisInstallStep& step);
322
323 // Cache with TTL support
324 template <typename T> struct CacheEntry
325 {
326 T data;
327 std::chrono::steady_clock::time_point timestamp;
328
329 bool isExpired(std::chrono::minutes ttl) const
330 {
331 return std::chrono::steady_clock::now() - timestamp > ttl;
332 }
333 };
334
335 static std::map<std::string, CacheEntry<std::vector<LutrisSearchResult>>> searchCache_;
336 static std::map<std::string, CacheEntry<LutrisInstallerScript>> scriptCache_;
337 static std::mutex cacheMutex_;
338
339 // Cache TTL settings
340 static constexpr std::chrono::minutes SEARCH_CACHE_TTL{30}; // Search results valid for 30 min
341 static constexpr std::chrono::minutes SCRIPT_CACHE_TTL{60}; // Scripts valid for 1 hour
342};
Client for the Lutris.net API.
static LutrisSearchResult getGameDetails(const std::string &gameSlug)
Get detailed information about a game.
static void searchGamesAsync(const std::string &query, const std::function< void(const std::vector< LutrisSearchResult > &)> &callback, int maxResults=20)
Search for games asynchronously.
static LutrisSearchResult findBySteamId(int steamAppId)
Find a game by Steam App ID.
static void getInstallerScriptAsync(const std::string &gameSlug, const std::string &installerSlug, const std::function< void(const LutrisInstallerScript &)> &callback)
Get installer script asynchronously.
static void clearCache()
Clear cached data.
static void getInstallersAsync(const std::string &gameSlug, const std::function< void(const std::vector< LutrisInstallerSummary > &)> &callback)
Get available installers asynchronously.
static std::vector< LutrisInstallerSummary > getInstallers(const std::string &gameSlug)
Get available installers for a game.
static std::vector< LutrisSearchResult > searchGames(const std::string &query, int maxResults=20)
Search for games by name.
static LutrisInstallerScript getInstallerScript(const std::string &gameSlug, const std::string &installerSlug)
Get full installer script with all details.
static LutrisSearchResult findByGogId(int gogId)
Find a game by GOG ID.
static bool isApiAvailable()
Check if Lutris API is accessible.
nlohmann::json json
std::map< std::string, std::string > params
std::map< std::string, std::string > environmentVariables
LutrisWineConfig wineConfig
std::vector< LutrisInstallerFile > files
uint64_t getTotalDownloadSize() const
std::vector< LutrisInstallStep > steps
std::map< std::string, std::string > dllOverrides
std::map< std::string, std::string > environment