game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
file_verification_service.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// file_verification_service.h
4// ==============================================================================
5// Game file integrity verification service
6// ==============================================================================
7// Provides functionality for verifying and repairing game installations:
8// - Checksum verification (MD5, SHA1, SHA256)
9// - Missing file detection
10// - Size verification
11// - Steam manifest validation
12// - GOG verification file support
13//
14// ## Features
15// - Async verification with progress tracking
16// - Cached verification results
17// - Support for multiple verification methods
18// - Integration with Steam depot manifests
19//
20// ## Thread Safety
21// All public methods are thread-safe.
22// ==============================================================================
23
24#pragma once
25
26#include <atomic>
27#include <chrono>
28#include <filesystem>
29#include <functional>
30#include <future>
31#include <memory>
32#include <optional>
33#include <string>
34#include <vector>
35
36// ==============================================================================
37// Data Structures
38// ==============================================================================
39
40/**
41 * @enum VerificationStatus
42 * @brief Status of a file verification check
43 */
45{
46 Unknown, ///< Not yet verified
47 Valid, ///< File matches expected checksum/size
48 Invalid, ///< File doesn't match (corrupted or modified)
49 Missing, ///< File doesn't exist
50 SizeMismatch, ///< File exists but wrong size
51 ExtraFile, ///< File exists but not in manifest
52 Repairable, ///< Invalid but can be repaired
53 Skipped ///< Verification was skipped
54};
55
56/**
57 * @brief Get human-readable name for verification status
58 */
60
61/**
62 * @struct FileVerificationResult
63 * @brief Result of verifying a single file
64 */
66{
67 std::filesystem::path path; ///< File path (relative to game root)
69
70 // Expected values (from manifest)
71 std::string expectedChecksum;
72 std::string checksumType; ///< "md5", "sha1", "sha256"
73 int64_t expectedSize = -1;
74
75 // Actual values (from disk)
76 std::string actualChecksum;
77 int64_t actualSize = -1;
78
79 // Timing
81
82 bool isValid() const
83 {
85 }
91};
92
93/**
94 * @struct GameVerificationResult
95 * @brief Complete verification result for a game
96 */
98{
99 std::string gameId;
100 std::string gameName;
101 std::filesystem::path gamePath;
102
103 std::vector<FileVerificationResult> files;
104 std::chrono::system_clock::time_point verificationTime;
105 int totalTimeMs = 0;
106
107 // Summary statistics
108 int totalFiles = 0;
109 int validFiles = 0;
112 int extraFiles = 0;
113 int64_t totalBytes = 0;
114 int64_t verifiedBytes = 0;
115
116 bool isComplete() const
117 {
118 return invalidFiles == 0 && missingFiles == 0;
119 }
121 {
122 return totalFiles > 0
123 ? (static_cast<float>(validFiles) / static_cast<float>(totalFiles)) * 100.0f
124 : 0.0f;
125 }
126};
127
128/**
129 * @struct VerificationProgress
130 * @brief Progress information during verification
131 */
155
156/**
157 * @struct GameManifest
158 * @brief Manifest file containing expected file checksums
159 */
161{
162 std::string gameId;
163 std::string version;
164 std::string checksumType; ///< Default checksum type for files
165 std::vector<FileVerificationResult> files;
166
167 std::chrono::system_clock::time_point created;
168 bool isValid() const
169 {
170 return !files.empty();
171 }
172};
173
174// ==============================================================================
175// FileVerificationService Class
176// ==============================================================================
177
178/**
179 * @class FileVerificationService
180 * @brief Service for verifying game file integrity
181 *
182 * Provides methods for verifying game installations against manifests
183 * or generating new manifests for games.
184 *
185 * ## Usage Example
186 * @code
187 * FileVerificationService service;
188 *
189 * // Verify a game
190 * auto result = service.verifyGame(game, [](const VerificationProgress& p) {
191 * std::cout << p.currentFile << " (" << p.percentComplete << "%)" << std::endl;
192 * });
193 *
194 * if (!result.isComplete()) {
195 * std::cout << "Missing files: " << result.missingFiles << std::endl;
196 * }
197 * @endcode
198 */
200{
201 public:
202 using ProgressCallback = std::function<void(const VerificationProgress&)>;
203
206
207 // ==========================================================================
208 // Game Verification
209 // ==========================================================================
210
211 /**
212 * @brief Verify a game's file integrity
213 * @param gameId Game ID
214 * @param gamePath Path to game installation
215 * @param progressCallback Progress updates
216 * @return Verification result
217 */
218 GameVerificationResult verifyGame(const std::string& gameId,
219 const std::filesystem::path& gamePath,
220 const ProgressCallback& progressCallback = nullptr);
221
222 /**
223 * @brief Verify a game asynchronously
224 * @param gameId Game ID
225 * @param gamePath Path to game installation
226 * @param progressCallback Progress updates
227 * @return Future with verification result
228 */
229 std::future<GameVerificationResult>
230 verifyGameAsync(const std::string& gameId, const std::filesystem::path& gamePath,
231 const ProgressCallback& progressCallback = nullptr);
232
233 /**
234 * @brief Cancel an ongoing verification
235 * @param gameId Game ID
236 */
237 void cancelVerification(const std::string& gameId);
238
239 /**
240 * @brief Check if verification is in progress
241 * @param gameId Game ID
242 */
243 bool isVerifying(const std::string& gameId) const;
244
245 // ==========================================================================
246 // Steam Integration
247 // ==========================================================================
248
249 /**
250 * @brief Verify a Steam game using depot manifests
251 * @param appId Steam app ID
252 * @param gamePath Path to game installation
253 * @param progressCallback Progress updates
254 * @return Verification result
255 */
256 GameVerificationResult verifySteamGame(uint32_t appId, const std::filesystem::path& gamePath,
257 const ProgressCallback& progressCallback = nullptr);
258
259 /**
260 * @brief Load Steam depot manifest for a game
261 * @param appId Steam app ID
262 * @param depotId Depot ID (0 = auto-detect main depot)
263 * @return Manifest if found
264 */
265 std::optional<GameManifest> loadSteamManifest(uint32_t appId, uint32_t depotId = 0);
266
267 // ==========================================================================
268 // Manifest Management
269 // ==========================================================================
270
271 /**
272 * @brief Generate a manifest for a game installation
273 * @param gameId Game ID
274 * @param gamePath Path to game installation
275 * @param checksumType Checksum algorithm ("md5", "sha1", "sha256")
276 * @param progressCallback Progress updates
277 * @return Generated manifest
278 */
279 GameManifest generateManifest(const std::string& gameId, const std::filesystem::path& gamePath,
280 const std::string& checksumType = "sha256",
281 const ProgressCallback& progressCallback = nullptr);
282
283 /**
284 * @brief Save a manifest to file
285 * @param manifest Manifest to save
286 * @param path Output path
287 * @return True on success
288 */
289 bool saveManifest(const GameManifest& manifest, const std::filesystem::path& path);
290
291 /**
292 * @brief Load a manifest from file
293 * @param path Manifest file path
294 * @return Loaded manifest if valid
295 */
296 std::optional<GameManifest> loadManifest(const std::filesystem::path& path);
297
298 /**
299 * @brief Get default manifest path for a game
300 * @param gameId Game ID
301 * @return Path to manifest file
302 */
303 std::filesystem::path getManifestPath(const std::string& gameId) const;
304
305 // ==========================================================================
306 // File Operations
307 // ==========================================================================
308
309 /**
310 * @brief Calculate checksum of a file
311 * @param path File path
312 * @param type Checksum type ("md5", "sha1", "sha256")
313 * @return Checksum string, empty on error
314 */
315 static std::string calculateChecksum(const std::filesystem::path& path,
316 const std::string& type);
317
318 /**
319 * @brief Verify a single file.
320 * @param basePath Game-install root the manifest's relative `path`
321 * is anchored to. The path-traversal containment
322 * check rejects manifests whose canonical resolved
323 * path escapes this base via `..` segments,
324 * absolute paths, or symlinks.
325 * @param expected Expected verification record (relative path, size,
326 * sha256/sha1/md5 — whichever the manifest specifies).
327 * @return Actual verification result with mismatch details on failure.
328 */
329 FileVerificationResult verifyFile(const std::filesystem::path& basePath,
330 const FileVerificationResult& expected);
331
332 // ==========================================================================
333 // Cache Management
334 // ==========================================================================
335
336 /**
337 * @brief Get cached verification result
338 * @param gameId Game ID
339 * @return Cached result if available and not stale
340 */
341 std::optional<GameVerificationResult> getCachedResult(const std::string& gameId) const;
342
343 /**
344 * @brief Clear verification cache
345 * @param gameId Game ID (empty = clear all)
346 */
347 void clearCache(const std::string& gameId = "");
348
349 /**
350 * @brief Set cache validity duration
351 * @param seconds Cache validity in seconds (0 = no caching)
352 */
353 void setCacheValidity(int seconds);
354
355 private:
356 struct Impl;
357 std::unique_ptr<Impl> impl_;
358
359 // Internal helpers
360 GameVerificationResult verifyAgainstManifest(const GameManifest& manifest,
361 const std::filesystem::path& gamePath,
362 const ProgressCallback& callback,
363 std::atomic<bool>& cancelled);
364
365 void scanExtraFiles(GameVerificationResult& result, const GameManifest& manifest,
366 const std::filesystem::path& gamePath);
367};
GameVerificationResult verifySteamGame(uint32_t appId, const std::filesystem::path &gamePath, const ProgressCallback &progressCallback=nullptr)
Verify a Steam game using depot manifests.
GameVerificationResult verifyGame(const std::string &gameId, const std::filesystem::path &gamePath, const ProgressCallback &progressCallback=nullptr)
Verify a game's file integrity.
static std::string calculateChecksum(const std::filesystem::path &path, const std::string &type)
Calculate checksum of a file.
std::function< void(const VerificationProgress &)> ProgressCallback
bool isVerifying(const std::string &gameId) const
Check if verification is in progress.
void clearCache(const std::string &gameId="")
Clear verification cache.
std::optional< GameManifest > loadSteamManifest(uint32_t appId, uint32_t depotId=0)
Load Steam depot manifest for a game.
std::optional< GameVerificationResult > getCachedResult(const std::string &gameId) const
Get cached verification result.
std::optional< GameManifest > loadManifest(const std::filesystem::path &path)
Load a manifest from file.
void setCacheValidity(int seconds)
Set cache validity duration.
std::filesystem::path getManifestPath(const std::string &gameId) const
Get default manifest path for a game.
std::future< GameVerificationResult > verifyGameAsync(const std::string &gameId, const std::filesystem::path &gamePath, const ProgressCallback &progressCallback=nullptr)
Verify a game asynchronously.
void cancelVerification(const std::string &gameId)
Cancel an ongoing verification.
bool saveManifest(const GameManifest &manifest, const std::filesystem::path &path)
Save a manifest to file.
GameManifest generateManifest(const std::string &gameId, const std::filesystem::path &gamePath, const std::string &checksumType="sha256", const ProgressCallback &progressCallback=nullptr)
Generate a manifest for a game installation.
FileVerificationResult verifyFile(const std::filesystem::path &basePath, const FileVerificationResult &expected)
Verify a single file.
VerificationStatus
Status of a file verification check.
@ Missing
File doesn't exist.
@ Valid
File matches expected checksum/size.
@ Invalid
File doesn't match (corrupted or modified).
@ Repairable
Invalid but can be repaired.
@ ExtraFile
File exists but not in manifest.
@ SizeMismatch
File exists but wrong size.
@ Skipped
Verification was skipped.
std::string verificationStatusName(VerificationStatus status)
Get human-readable name for verification status.
Result of verifying a single file.
std::string checksumType
"md5", "sha1", "sha256"
std::filesystem::path path
File path (relative to game root).
Manifest file containing expected file checksums.
std::chrono::system_clock::time_point created
std::vector< FileVerificationResult > files
std::string checksumType
Default checksum type for files.
Complete verification result for a game.
std::vector< FileVerificationResult > files
std::chrono::system_clock::time_point verificationTime
Progress information during verification.