game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
cloud_save_service.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// cloud_save_service.h
4// ==============================================================================
5// Cloud save synchronization service for game saves
6// ==============================================================================
7// Provides functionality for managing and synchronizing game saves:
8// - Local save backup and restore
9// - Cloud sync via multiple backends (Steam Cloud, GOG, custom)
10// - Save conflict detection and resolution
11// - Automatic sync on game launch/exit
12//
13// ## About Cloud Saves
14// Many games store saves in various locations. This service provides a unified
15// interface for backing up, syncing, and restoring game saves across devices.
16//
17// ## Thread Safety
18// All public methods are thread-safe.
19// ==============================================================================
20
21#pragma once
22
23#include <chrono>
24#include <filesystem>
25#include <functional>
26#include <future>
27#include <memory>
28#include <optional>
29#include <string>
30#include <vector>
31
32// ==============================================================================
33// Data Structures
34// ==============================================================================
35
36/**
37 * @enum CloudBackend
38 * @brief Supported cloud storage backends
39 */
40enum class CloudBackend
41{
42 None, ///< Local backup only
43 Steam, ///< Steam Cloud
44 GOG, ///< GOG Galaxy Cloud
45 Custom ///< Custom sync location (e.g., Syncthing, Nextcloud)
46};
47
48/**
49 * @enum SyncDirection
50 * @brief Direction of sync operation
51 */
52enum class SyncDirection
53{
54 Upload, ///< Local -> Cloud
55 Download, ///< Cloud -> Local
56 Bidirectional ///< Two-way sync
57};
58
59/**
60 * @enum ConflictResolution
61 * @brief How to resolve sync conflicts
62 */
64{
65 UseLocal, ///< Keep local version
66 UseRemote, ///< Keep remote version
67 UseNewest, ///< Use whichever is newer
68 AskUser, ///< Prompt user to choose
69 KeepBoth ///< Keep both versions (create backup)
70};
71
72/**
73 * @struct SaveLocation
74 * @brief Information about a game's save location
75 */
77{
78 std::string gameId; ///< Internal game ID
79 std::string gameName; ///< Display name
80 std::filesystem::path localPath; ///< Local save directory
81 std::string pattern; ///< File pattern (e.g., "*.sav", "**/*")
82 std::vector<std::string> excludePatterns; ///< Patterns to exclude
83
84 // Wine/Proton specific
85 bool isWinePrefix = false; ///< Save is in a Wine prefix
86 std::filesystem::path prefixPath; ///< Wine prefix path if applicable
87
88 // Metadata
89 std::string notes; ///< User notes about this save location
90 bool autoSync = false; ///< Auto-sync on launch/exit
91};
92
93/**
94 * @struct SaveFile
95 * @brief Information about a specific save file
96 */
98{
99 std::filesystem::path path; ///< Full path to file
100 std::string relativePath; ///< Path relative to save root
101 int64_t size = 0; ///< File size in bytes
102 std::chrono::system_clock::time_point modifiedTime;
103 std::string checksum; ///< SHA-256 hash for comparison
104
105 std::string getFormattedSize() const;
106 std::string getFormattedTime() const;
107};
108
109/**
110 * @struct SaveSnapshot
111 * @brief A complete snapshot of a game's saves at a point in time
112 */
114{
115 std::string id; ///< Unique snapshot ID
116 std::string gameId; ///< Game this belongs to
117 std::string gameName; ///< Game display name
118 std::chrono::system_clock::time_point timestamp;
119 std::vector<SaveFile> files; ///< Files in this snapshot
120 int64_t totalSize = 0; ///< Total size of all files
121 std::string description; ///< User-provided description
122 bool isAutomatic = false; ///< True if auto-created
123
124 std::string getFormattedTime() const;
125 std::string getFormattedSize() const;
126};
127
128/**
129 * @struct SyncStatus
130 * @brief Status of sync operation
131 */
133{
146
148 std::string currentFile;
150 int filesTotal = 0;
151 int64_t bytesTransferred = 0;
152 int64_t bytesTotal = 0;
154 std::string errorMessage;
155 std::vector<std::string> conflicts;
156
157 std::string getStateStr() const;
158};
159
160/**
161 * @struct SyncConflict
162 * @brief Details about a sync conflict
163 */
165{
166 std::string filePath;
169 std::string resolution; ///< How it was resolved (if auto)
170};
171
172// ==============================================================================
173// CloudSaveService Class
174// ==============================================================================
175
176/**
177 * @class CloudSaveService
178 * @brief Service for managing and syncing game saves
179 *
180 * Provides methods for backing up, restoring, and syncing game saves
181 * across local and cloud storage.
182 *
183 * ## Usage Example
184 * @code
185 * CloudSaveService service;
186 *
187 * // Register a game's save location
188 * SaveLocation loc;
189 * loc.gameId = "doom-eternal";
190 * loc.gameName = "DOOM Eternal";
191 * loc.localPath = "/home/user/.local/share/DOOM Eternal";
192 * loc.pattern = "all-files"; // use glob pattern for all files
193 * service.registerSaveLocation(loc);
194 *
195 * // Create a backup
196 * service.createSnapshot("doom-eternal", "Before mods");
197 *
198 * // Restore from snapshot
199 * auto snapshots = service.getSnapshots("doom-eternal");
200 * service.restoreSnapshot(snapshots[0].id);
201 * @endcode
202 */
204{
205 public:
206 using ProgressCallback = std::function<void(const SyncStatus&)>;
207 using ConflictCallback = std::function<ConflictResolution(const SyncConflict&)>;
208
211
212 // ==========================================================================
213 // Save Location Management
214 // ==========================================================================
215
216 /**
217 * @brief Register a game's save location
218 * @param location Save location info
219 * @return True on success
220 */
221 bool registerSaveLocation(const SaveLocation& location);
222
223 /**
224 * @brief Update an existing save location
225 * @param location Updated location info
226 * @return True on success
227 */
228 bool updateSaveLocation(const SaveLocation& location);
229
230 /**
231 * @brief Remove a save location registration
232 * @param gameId Game ID to remove
233 * @return True on success
234 */
235 bool removeSaveLocation(const std::string& gameId);
236
237 /**
238 * @brief Get all registered save locations
239 * @return Vector of save locations
240 */
241 std::vector<SaveLocation> getSaveLocations() const;
242
243 /**
244 * @brief Get save location for a specific game
245 * @param gameId Game ID
246 * @return Save location if found
247 */
248 std::optional<SaveLocation> getSaveLocation(const std::string& gameId) const;
249
250 /**
251 * @brief Detect save location for a game
252 * @param gameName Game name to search for
253 * @param prefixPath Optional Wine prefix path
254 * @return Detected save locations (may be multiple)
255 */
256 std::vector<SaveLocation>
257 detectSaveLocation(const std::string& gameName,
258 const std::filesystem::path& prefixPath = {}) const;
259
260 // ==========================================================================
261 // Local Backup (Snapshots)
262 // ==========================================================================
263
264 /**
265 * @brief Create a snapshot of current saves
266 * @param gameId Game ID
267 * @param description Optional description
268 * @return Snapshot ID on success
269 */
270 std::optional<std::string> createSnapshot(const std::string& gameId,
271 const std::string& description = "");
272
273 /**
274 * @brief Create snapshot asynchronously
275 */
276 std::future<std::optional<std::string>>
277 createSnapshotAsync(const std::string& gameId, const std::string& description = "");
278
279 /**
280 * @brief List all snapshots for a game
281 * @param gameId Game ID
282 * @return Vector of snapshots, newest first
283 */
284 std::vector<SaveSnapshot> getSnapshots(const std::string& gameId) const;
285
286 /**
287 * @brief Get a specific snapshot
288 * @param snapshotId Snapshot ID
289 * @return Snapshot info if found
290 */
291 std::optional<SaveSnapshot> getSnapshot(const std::string& snapshotId) const;
292
293 /**
294 * @brief Restore saves from a snapshot
295 * @param snapshotId Snapshot to restore
296 * @param backupCurrent Create backup of current saves first
297 * @return True on success
298 */
299 bool restoreSnapshot(const std::string& snapshotId, bool backupCurrent = true);
300
301 /**
302 * @brief Delete a snapshot
303 * @param snapshotId Snapshot to delete
304 * @return True on success
305 */
306 bool deleteSnapshot(const std::string& snapshotId);
307
308 /**
309 * @brief Delete old snapshots
310 * @param gameId Game ID
311 * @param keepCount Number of recent snapshots to keep
312 * @return Number of snapshots deleted
313 */
314 int pruneSnapshots(const std::string& gameId, int keepCount = 10);
315
316 // ==========================================================================
317 // Cloud Sync
318 // ==========================================================================
319
320 /**
321 * @brief Sync saves with cloud storage
322 * @param gameId Game ID
323 * @param direction Sync direction
324 * @param progressCallback Progress updates
325 * @param conflictCallback Conflict resolution callback
326 * @return True on success
327 */
328 bool syncSaves(const std::string& gameId,
330 const ProgressCallback& progressCallback = nullptr,
331 const ConflictCallback& conflictCallback = nullptr);
332
333 /**
334 * @brief Sync asynchronously
335 */
336 std::future<bool> syncSavesAsync(const std::string& gameId,
338 const ProgressCallback& progressCallback = nullptr,
339 const ConflictCallback& conflictCallback = nullptr);
340
341 /**
342 * @brief Check if sync is needed
343 * @param gameId Game ID
344 * @return True if local and remote are out of sync
345 */
346 bool needsSync(const std::string& gameId) const;
347
348 /**
349 * @brief Get pending conflicts
350 * @param gameId Game ID
351 * @return Vector of unresolved conflicts
352 */
353 std::vector<SyncConflict> getPendingConflicts(const std::string& gameId) const;
354
355 /**
356 * @brief Resolve a conflict
357 * @param gameId Game ID
358 * @param filePath Conflicted file path
359 * @param resolution How to resolve
360 * @return True on success
361 */
362 bool resolveConflict(const std::string& gameId, const std::string& filePath,
363 ConflictResolution resolution);
364
365 // ==========================================================================
366 // Auto-Sync
367 // ==========================================================================
368
369 /**
370 * @brief Enable auto-sync for a game
371 * @param gameId Game ID
372 * @param enabled Enable or disable
373 */
374 void setAutoSync(const std::string& gameId, bool enabled);
375
376 /**
377 * @brief Called when a game is about to launch
378 * @param gameId Game ID
379 *
380 * Downloads latest saves from cloud if auto-sync enabled.
381 */
382 void onGameLaunch(const std::string& gameId);
383
384 /**
385 * @brief Called when a game exits
386 * @param gameId Game ID
387 *
388 * Uploads saves to cloud if auto-sync enabled.
389 */
390 void onGameExit(const std::string& gameId);
391
392 // ==========================================================================
393 // Configuration
394 // ==========================================================================
395
396 /**
397 * @brief Set the local backup directory
398 * @param path Directory for storing snapshots
399 */
400 void setBackupDirectory(const std::filesystem::path& path);
401
402 /**
403 * @brief Get the backup directory
404 */
405 std::filesystem::path getBackupDirectory() const;
406
407 /**
408 * @brief Set custom sync directory
409 * @param path Custom cloud sync path (e.g., Syncthing folder)
410 */
411 void setCustomSyncDirectory(const std::filesystem::path& path);
412
413 /**
414 * @brief Set default conflict resolution
415 * @param resolution Default resolution for auto-sync
416 */
418
419 /**
420 * @brief Set maximum snapshots per game
421 * @param count Maximum to keep (0 = unlimited)
422 */
423 void setMaxSnapshotsPerGame(int count);
424
425 /**
426 * @brief Cancel ongoing operation
427 */
428 void cancelOperation();
429
430 // ==========================================================================
431 // Statistics
432 // ==========================================================================
433
434 /**
435 * @brief Get total backup size for a game
436 * @param gameId Game ID
437 * @return Total size in bytes
438 */
439 int64_t getTotalBackupSize(const std::string& gameId) const;
440
441 /**
442 * @brief Get total backup size for all games
443 * @return Total size in bytes
444 */
445 int64_t getTotalBackupSize() const;
446
447 /**
448 * @brief Get snapshot count for a game
449 * @param gameId Game ID
450 * @return Number of snapshots
451 */
452 int getSnapshotCount(const std::string& gameId) const;
453
454 private:
455 struct Impl;
456 std::unique_ptr<Impl> impl_;
457
458 // Internal helpers
459 std::vector<SaveFile> scanSaveFiles(const SaveLocation& location) const;
460 std::string calculateChecksum(const std::filesystem::path& path) const;
461 bool copyFiles(const std::vector<SaveFile>& files, const std::filesystem::path& destDir,
462 const ProgressCallback& callback);
463 std::string generateSnapshotId() const;
464 void loadSaveLocations();
465 void saveSaveLocations() const;
466 void loadSnapshots();
467};
468
469/**
470 * @brief Get human-readable backend name
471 */
472std::string cloudBackendName(CloudBackend backend);
473
474/**
475 * @brief Get human-readable sync direction name
476 */
477std::string syncDirectionName(SyncDirection direction);
void setCustomSyncDirectory(const std::filesystem::path &path)
Set custom sync directory.
bool needsSync(const std::string &gameId) const
Check if sync is needed.
void setBackupDirectory(const std::filesystem::path &path)
Set the local backup directory.
std::function< void(const SyncStatus &)> ProgressCallback
bool resolveConflict(const std::string &gameId, const std::string &filePath, ConflictResolution resolution)
Resolve a conflict.
std::optional< std::string > createSnapshot(const std::string &gameId, const std::string &description="")
Create a snapshot of current saves.
std::vector< SaveSnapshot > getSnapshots(const std::string &gameId) const
List all snapshots for a game.
void setAutoSync(const std::string &gameId, bool enabled)
Enable auto-sync for a game.
void setMaxSnapshotsPerGame(int count)
Set maximum snapshots per game.
bool restoreSnapshot(const std::string &snapshotId, bool backupCurrent=true)
Restore saves from a snapshot.
int getSnapshotCount(const std::string &gameId) const
Get snapshot count for a game.
void onGameLaunch(const std::string &gameId)
Called when a game is about to launch.
void onGameExit(const std::string &gameId)
Called when a game exits.
bool updateSaveLocation(const SaveLocation &location)
Update an existing save location.
int64_t getTotalBackupSize() const
Get total backup size for all games.
std::vector< SyncConflict > getPendingConflicts(const std::string &gameId) const
Get pending conflicts.
bool deleteSnapshot(const std::string &snapshotId)
Delete a snapshot.
bool registerSaveLocation(const SaveLocation &location)
Register a game's save location.
void setDefaultConflictResolution(ConflictResolution resolution)
Set default conflict resolution.
std::future< bool > syncSavesAsync(const std::string &gameId, SyncDirection direction=SyncDirection::Bidirectional, const ProgressCallback &progressCallback=nullptr, const ConflictCallback &conflictCallback=nullptr)
Sync asynchronously.
std::vector< SaveLocation > getSaveLocations() const
Get all registered save locations.
void cancelOperation()
Cancel ongoing operation.
std::optional< SaveLocation > getSaveLocation(const std::string &gameId) const
Get save location for a specific game.
std::function< ConflictResolution(const SyncConflict &)> ConflictCallback
std::future< std::optional< std::string > > createSnapshotAsync(const std::string &gameId, const std::string &description="")
Create snapshot asynchronously.
std::optional< SaveSnapshot > getSnapshot(const std::string &snapshotId) const
Get a specific snapshot.
std::vector< SaveLocation > detectSaveLocation(const std::string &gameName, const std::filesystem::path &prefixPath={}) const
Detect save location for a game.
bool removeSaveLocation(const std::string &gameId)
Remove a save location registration.
bool syncSaves(const std::string &gameId, SyncDirection direction=SyncDirection::Bidirectional, const ProgressCallback &progressCallback=nullptr, const ConflictCallback &conflictCallback=nullptr)
Sync saves with cloud storage.
int pruneSnapshots(const std::string &gameId, int keepCount=10)
Delete old snapshots.
std::filesystem::path getBackupDirectory() const
Get the backup directory.
std::string syncDirectionName(SyncDirection direction)
Get human-readable sync direction name.
SyncDirection
Direction of sync operation.
@ Download
Cloud -> Local.
@ Upload
Local -> Cloud.
@ Bidirectional
Two-way sync.
std::string cloudBackendName(CloudBackend backend)
Get human-readable backend name.
ConflictResolution
How to resolve sync conflicts.
@ AskUser
Prompt user to choose.
@ UseRemote
Keep remote version.
@ UseNewest
Use whichever is newer.
@ KeepBoth
Keep both versions (create backup).
@ UseLocal
Keep local version.
CloudBackend
Supported cloud storage backends.
@ Steam
Steam Cloud.
@ None
Local backup only.
@ Custom
Custom sync location (e.g., Syncthing, Nextcloud).
@ GOG
GOG Galaxy Cloud.
Information about a specific save file.
std::string getFormattedSize() const
std::string getFormattedTime() const
std::chrono::system_clock::time_point modifiedTime
std::string relativePath
Path relative to save root.
int64_t size
File size in bytes.
std::string checksum
SHA-256 hash for comparison.
std::filesystem::path path
Full path to file.
Information about a game's save location.
bool autoSync
Auto-sync on launch/exit.
std::string notes
User notes about this save location.
std::filesystem::path localPath
Local save directory.
std::filesystem::path prefixPath
Wine prefix path if applicable.
std::string gameName
Display name.
std::string pattern
File pattern (e.g., "*.sav", "**‍/*").
std::string gameId
Internal game ID.
std::vector< std::string > excludePatterns
Patterns to exclude.
bool isWinePrefix
Save is in a Wine prefix.
A complete snapshot of a game's saves at a point in time.
std::vector< SaveFile > files
Files in this snapshot.
std::string gameName
Game display name.
int64_t totalSize
Total size of all files.
std::string getFormattedTime() const
std::chrono::system_clock::time_point timestamp
std::string id
Unique snapshot ID.
std::string gameId
Game this belongs to.
std::string getFormattedSize() const
bool isAutomatic
True if auto-created.
std::string description
User-provided description.
Details about a sync conflict.
std::string filePath
std::string resolution
How it was resolved (if auto).
Status of sync operation.
std::string errorMessage
std::vector< std::string > conflicts
int64_t bytesTransferred
std::string currentFile
std::string getStateStr() const