29GameStateSync::GameStateSync()
30 : library_(nullptr), steamScanner_(nullptr), shouldStop_(false), requestFullSync_(false)
35GameStateSync::~GameStateSync()
46 if (syncThread_.joinable())
53 steamScanner_ = steamScanner;
56 syncThread_ = std::thread(&GameStateSync::syncThreadFunction,
this);
58 Logger::info(
"[GameStateSync] Started background sync thread (interval: {}s)",
59 syncIntervalSeconds_);
64 if (!syncThread_.joinable())
72 if (syncThread_.joinable())
82 std::lock_guard<std::mutex> lock(gameIdsMutex_);
83 gamesNeedingSync_.insert(gameId);
88 requestFullSync_ =
true;
97 syncIntervalSeconds_ = std::max(1, intervalSeconds);
102 transitionalStateTimeoutSeconds_ = std::max(5, timeoutSeconds);
107 autoRecoveryEnabled_ = enabled;
112 std::lock_guard<std::mutex> lock(callbackMutex_);
113 stateChangeCallback_ = std::move(callback);
118 std::lock_guard<std::mutex> lock(callbackMutex_);
119 gameUpdateCallback_ = std::move(callback);
128 std::lock_guard<std::mutex> lock(statsMutex_);
136void GameStateSync::syncThreadFunction()
146 std::this_thread::sleep_for(std::chrono::seconds(1));
151 std::set<std::string> gamesToSync;
152 bool doFullSync = requestFullSync_.exchange(
false);
155 std::lock_guard<std::mutex> lock(gameIdsMutex_);
156 gamesToSync = gamesNeedingSync_;
157 gamesNeedingSync_.clear();
160 if (doFullSync || gamesToSync.empty())
166 std::vector<std::string> gameIds;
168 auto allGames = library_->getAllGames();
169 gameIds.reserve(allGames.size());
170 for (
const auto* g : allGames)
171 gameIds.push_back(g->id);
173 for (
const auto& gid : gameIds)
175 auto gameOpt = library_->getGame(gid);
176 if (gameOpt.has_value())
181 std::lock_guard<std::mutex> lock(statsMutex_);
182 stats_.totalSyncCycles++;
183 stats_.lastSyncTime = time(
nullptr);
189 for (
const auto& gameId : gamesToSync)
191 auto gameOpt = library_->getGame(gameId);
192 if (gameOpt.has_value())
199 std::lock_guard<std::mutex> lock(statsMutex_);
200 stats_.totalSyncCycles++;
201 stats_.lastSyncTime = time(
nullptr);
205 catch (
const std::exception& e)
207 Logger::error(
"[GameStateSync] Error in sync thread: {}", e.what());
211 for (
int i = 0; i < syncIntervalSeconds_ && !shouldStop_; ++i)
213 std::this_thread::sleep_for(std::chrono::seconds(1));
224void GameStateSync::syncGame(
Game* game)
226 if (!game || !autoRecoveryEnabled_)
235 fixTransitionalState(game);
242 if (verifySteamInstallation(game))
250 verifyLocalInstallation(game);
258void GameStateSync::fixTransitionalState(
Game* game)
261 std::string gameId = game->
id;
262 time_t now = time(
nullptr);
263 time_t stateStartTime = 0;
266 std::lock_guard<std::mutex> lock(transitionalStatesMutex_);
267 auto it = transitionalStateStartTimes_.find(gameId);
268 if (it == transitionalStateStartTimes_.end())
271 transitionalStateStartTimes_[gameId] = now;
274 stateStartTime = it->second;
278 double timeInState = difftime(now, stateStartTime);
279 if (timeInState < transitionalStateTimeoutSeconds_)
286 Logger::info(
"[GameStateSync] Fixing stuck state for: {} (stuck in {} for {}s)", game->
name,
311 transitionGameState(game, newStatus);
315 std::lock_guard<std::mutex> lock(transitionalStatesMutex_);
316 transitionalStateStartTimes_.erase(gameId);
321 std::lock_guard<std::mutex> lock(statsMutex_);
330bool GameStateSync::verifySteamInstallation(
Game* game)
351 "[GameStateSync] Correcting installation status for: {} (was: {}, should be: {})",
355 transitionGameState(game, expectedStatus);
366bool GameStateSync::verifyLocalInstallation(
Game* game)
372 bool shouldBeInstalled = executableExists || !game->
installPath.empty();
383 Logger::info(
"[GameStateSync] Correcting local game status for: {} (executable {})",
384 game->
name, (executableExists ?
"exists" :
"missing"));
386 transitionGameState(game, expectedStatus);
390 return executableExists;
397void GameStateSync::transitionGameState(
Game* game,
GameStatus newStatus)
401 if (oldStatus == newStatus)
410 std::lock_guard<std::mutex> lock(callbackMutex_);
411 stateCb = stateChangeCallback_;
412 updateCb = gameUpdateCallback_;
421 std::string gameId = game->
id;
422 updateCb(gameId, [newStatus](
Game& g) { g.
status = newStatus; });
431 std::lock_guard<std::mutex> lock(statsMutex_);
432 stats_.statusChanges++;
437 stateCb(game->
id, oldStatus, newStatus);
441 static_cast<int>(oldStatus),
static_cast<int>(newStatus));
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.
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.
GameMetadata metadata
Descriptive information and media.
std::string installPath
Installation directory path.
std::string name
Display name shown in UI.
std::string id
Unique identifier (auto-generated or user-specified).
GameRunnerConfig runnerConfig
How to launch and run the game.
GameStatus status
Current installation and execution status.
std::string getStatusName() const
Get human-readable status name.
static void info(const std::string &message, Args &&... args)
Log info message (general information).
static void error(const std::string &message, Args &&... args)
Log error message (recoverable failure).
Scans Steam installations to discover games.
GameStatus
Current installation and execution status of the game.
@ Installing
Game is being installed.
@ NotInstalled
Game is in library but not installed.
@ Running
Game is currently running.
@ Closing
Game is shutting down (waiting for process to terminate).
@ Updating
Game is being updated.
@ Installed
Game is installed and ready to play.
@ Launching
Game is being launched (waiting for process to start).
std::string executablePath
Full path to game executable.
@ Game
Game-specific fixes.