game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
launch_manager.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3
4#include "game.h"
5#include <atomic>
6#include <chrono>
7#include <condition_variable>
8#include <functional>
9#include <memory>
10#include <mutex>
11#include <string>
12#include <thread>
13
14/**
15 * @brief Manages the complete game launch pipeline with state tracking
16 *
17 * Handles the full launch sequence:
18 * 1. Check/sync Steam settings if needed
19 * 2. Wait for Steam to be ready
20 * 3. Launch the game
21 * 4. Monitor for successful start
22 *
23 * Provides callbacks at each stage for UI updates.
24 */
25class GameLaunchManager
26{
27 public:
28 enum class LaunchState
29 {
30 IDLE, // No launch in progress
31 CHECKING_SETTINGS, // Comparing current vs desired Steam settings
32 SYNCING_SETTINGS, // Writing settings to Steam VDF files
33 WAITING_FOR_STEAM, // Waiting for Steam to restart after sync
34 LAUNCHING, // Executing game launch command
35 VERIFYING_START, // Waiting to confirm game process started
36 RUNNING, // Game confirmed running
37 FAILED, // Launch failed at some stage
38 COMPLETED // Launch sequence completed successfully
39 };
40
41 // Callback types
43 std::function<void(LaunchState oldState, LaunchState newState, const std::string& message)>;
44 using CompletionCallback = std::function<void(bool success, const std::string& message)>;
45
46 // ==========================================================================
47 // Singleton Access
48 // ==========================================================================
49
50 static GameLaunchManager& instance();
51
52 // ==========================================================================
53 // Launch Control
54 // ==========================================================================
55
56 // Request a game launch (non-blocking, async operation)
57 void requestLaunch(const Game& game);
58
59 // Cancel current launch operation
60 void cancelLaunch();
61
62 // Get current launch state
64
65 // Get ID of game currently being launched
66 std::string getCurrentGameId() const;
67
68 // Check if a launch is in progress
69 bool isLaunchInProgress() const;
70
71 // ==========================================================================
72 // Callbacks
73 // ==========================================================================
74
75 void setStateCallback(StateCallback callback);
77
78 private:
79 // Singleton
80 GameLaunchManager();
81 ~GameLaunchManager();
82 GameLaunchManager(const GameLaunchManager&) = delete;
83 GameLaunchManager& operator=(const GameLaunchManager&) = delete;
84
85 // ==========================================================================
86 // Internal Methods
87 // ==========================================================================
88
89 void transitionTo(LaunchState newState, const std::string& message = "");
90 void performLaunch(); // Main launch logic
91
92 // Pipeline stages
93 bool checkAndSyncSettings();
94 bool waitForSteam();
95 bool executeLaunch();
96 bool verifyGameStarted();
97
98 /// Sleep on stateCV_ for `duration` or until shouldCancel_ flips. Returns
99 /// true if the sleep completed without cancellation, false if cancelled.
100 /// Used in place of std::this_thread::sleep_for() so the destructor's
101 /// join() doesn't stall waiting for arbitrary post-launch delays.
102 bool interruptibleSleep(std::chrono::milliseconds duration);
103
104 // ==========================================================================
105 // Member Variables
106 // ==========================================================================
107
108 std::atomic<LaunchState> currentState_;
109 std::atomic<bool> shouldCancel_;
110
111 mutable std::mutex stateMutex_; // mutable for const methods
112 std::condition_variable stateCV_;
113
114 std::unique_ptr<Game> currentGame_; // Game being launched
115 std::string currentGameId_;
116
117 StateCallback stateCallback_;
118 CompletionCallback completionCallback_;
119
120 std::thread launchThread_; // Thread running performLaunch()
121};
std::function< void(LaunchState oldState, LaunchState newState, const std::string &message)> StateCallback
void requestLaunch(const Game &game)
LaunchState getCurrentState() const
std::function< void(bool success, const std::string &message)> CompletionCallback
void setStateCallback(StateCallback callback)
static GameLaunchManager & instance()
void setCompletionCallback(CompletionCallback callback)
std::string getCurrentGameId() const
bool isLaunchInProgress() const
Complete representation of a game in the library.
Definition game.h:767