game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
game_state_sync.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3
4#include "game.h"
5#include "game_library.h"
6#include "steam_integration.h"
7#include <atomic>
8#include <functional>
9#include <map>
10#include <memory>
11#include <mutex>
12#include <set>
13#include <thread>
14
15/**
16 * @brief Automatic game state synchronization service
17 *
18 * Runs in a background thread to ensure game metadata stays synchronized
19 * with reality by:
20 * - Verifying installation status matches file system
21 * - Detecting and fixing stuck transitional states (Launching/Installing/etc)
22 * - Syncing Steam game states with Steam's data
23 * - Auto-recovering from errors
24 * - Updating playtime from Steam
25 *
26 * This prevents games from getting stuck in "Installing", "Launching",
27 * "Updating" states and keeps metadata fresh.
28 */
29class GameStateSync
30{
31 public:
32 // Callback for when state changes are detected
34 std::function<void(const std::string& gameId, GameStatus oldStatus, GameStatus newStatus)>;
35
36 // ==========================================================================
37 // Singleton Access
38 // ==========================================================================
39
40 static GameStateSync& instance();
41
42 // ==========================================================================
43 // Control Methods
44 // ==========================================================================
45
46 /**
47 * @brief Start the background sync thread
48 * @param library Pointer to game library to sync
49 * @param steamScanner Pointer to Steam scanner for verification
50 */
51 void start(GameLibrary* library, SteamLibraryScanner* steamScanner);
52
53 /**
54 * @brief Stop the background sync thread
55 */
56 void stop();
57
58 /**
59 * @brief Request immediate sync of a specific game
60 * @param gameId Game ID to sync now
61 */
62 void requestImmediateSync(const std::string& gameId);
63
64 /**
65 * @brief Request immediate sync of all games
66 */
67 void requestFullSync();
68
69 // ==========================================================================
70 // Configuration
71 // ==========================================================================
72
73 /**
74 * @brief Set sync interval (default: 10 seconds)
75 * @param intervalSeconds How often to check game states
76 */
77 void setSyncInterval(int intervalSeconds);
78
79 /**
80 * @brief Set timeout for transitional states (default: 60 seconds)
81 * @param timeoutSeconds Max time a game can stay in Launching/Installing/etc
82 */
83 void setTransitionalStateTimeout(int timeoutSeconds);
84
85 /**
86 * @brief Enable/disable automatic status recovery
87 * @param enabled If true, automatically fix stuck states
88 */
89 void setAutoRecoveryEnabled(bool enabled);
90
91 // ==========================================================================
92 // Callbacks
93 // ==========================================================================
94
96
97 // Callback for safe cross-thread game mutation (execute-under-lock pattern).
98 // Signature matches GameLibraryController::executeWithGame.
99 // MUST be set before starting the sync thread when running alongside the UI.
101 std::function<void(const std::string& gameId, std::function<void(Game&)>)>;
103
104 // ==========================================================================
105 // Statistics
106 // ==========================================================================
107
108 struct Stats
109 {
111 int gamesFixed = 0;
113 time_t lastSyncTime = 0;
114 };
115
116 Stats getStats() const;
117
118 private:
119 // Singleton - private constructor
120 GameStateSync();
121 ~GameStateSync();
122 GameStateSync(const GameStateSync&) = delete;
123 GameStateSync& operator=(const GameStateSync&) = delete;
124
125 // ==========================================================================
126 // Internal Methods
127 // ==========================================================================
128
129 void syncThreadFunction(); // Main sync loop
130 void syncGame(Game* game); // Sync a single game
131 bool verifySteamInstallation(Game* game); // Check if Steam game is actually installed
132 bool verifyLocalInstallation(Game* game); // Check if non-Steam game files exist
133 void fixTransitionalState(Game* game); // Auto-fix stuck states
134 void updateSteamMetadata(Game* game); // Refresh Steam-specific data
135
136 void transitionGameState(Game* game, GameStatus newStatus);
137
138 // ==========================================================================
139 // Member Variables
140 // ==========================================================================
141
142 GameLibrary* library_;
143 SteamLibraryScanner* steamScanner_;
144
145 std::thread syncThread_;
146 std::atomic<bool> shouldStop_;
147 std::atomic<bool> requestFullSync_;
148
149 std::mutex gameIdsMutex_;
150 std::set<std::string> gamesNeedingSync_; // Games that need immediate sync
151
152 std::atomic<int> syncIntervalSeconds_{10};
153 std::atomic<int> transitionalStateTimeoutSeconds_{60};
154 std::atomic<bool> autoRecoveryEnabled_{true};
155
156 StateChangeCallback stateChangeCallback_;
157 GameUpdateCallback gameUpdateCallback_;
158 mutable std::mutex callbackMutex_;
159
160 // Track when games entered transitional states
161 std::map<std::string, time_t> transitionalStateStartTimes_;
162 std::mutex transitionalStatesMutex_;
163
164 Stats stats_;
165 mutable std::mutex statsMutex_;
166};
Central database for managing all games in the launcher.
void setStateChangeCallback(StateChangeCallback callback)
void requestFullSync()
Request immediate sync of all games.
void setTransitionalStateTimeout(int timeoutSeconds)
Set timeout for transitional states (default: 60 seconds).
void setSyncInterval(int intervalSeconds)
Set sync interval (default: 10 seconds).
void start(GameLibrary *library, SteamLibraryScanner *steamScanner)
Start the background sync thread.
Stats getStats() const
std::function< void(const std::string &gameId, std::function< void(Game &)>)> GameUpdateCallback
void setAutoRecoveryEnabled(bool enabled)
Enable/disable automatic status recovery.
void requestImmediateSync(const std::string &gameId)
Request immediate sync of a specific game.
void stop()
Stop the background sync thread.
void setGameUpdateCallback(GameUpdateCallback callback)
std::function< void(const std::string &gameId, GameStatus oldStatus, GameStatus newStatus)> StateChangeCallback
static GameStateSync & instance()
Complete representation of a game in the library.
Definition game.h:767
Scans Steam installations to discover games.
GameStatus
Current installation and execution status of the game.
Definition game.h:97