24SteamProcessMonitor::SteamProcessMonitor()
25 : currentState_(State::NOT_RUNNING), shouldStop_(false),
26 transitionTime_(std::chrono::steady_clock::now())
31SteamProcessMonitor::~SteamProcessMonitor()
42 if (monitorThread_.joinable())
52 if (checkSteamProcess())
61 monitorThread_ = std::thread(&SteamProcessMonitor::monitorThread,
this);
66 if (!monitorThread_.joinable())
75 std::lock_guard<std::mutex> lock(stateMutex_);
78 stateCV_.notify_all();
80 if (monitorThread_.joinable())
82 monitorThread_.join();
93 std::lock_guard<std::mutex> lock(stateMutex_);
94 stateCallback_ =
nullptr;
101 std::lock_guard<std::mutex> lock(stateMutex_);
102 State current = currentState_.load();
118 transitionTime_ = std::chrono::steady_clock::now();
119 stateCV_.notify_all();
129 std::lock_guard<std::mutex> lock(stateMutex_);
130 State current = currentState_.load();
144 Logger::info(
"[SteamMonitor] Requesting Steam shutdown");
146 transitionTime_ = std::chrono::steady_clock::now();
147 stateCV_.notify_all();
156 std::unique_lock<std::mutex> lock(stateMutex_);
158 return stateCV_.wait_for(lock, std::chrono::milliseconds(timeoutMs),
159 [
this, targetState]() {
return currentState_.load() == targetState; });
168 return currentState_.load();
187 if (steamRoot.empty())
193 std::filesystem::path pipePath = steamRoot /
"steam.pipe";
194 if (std::filesystem::exists(pipePath))
200 std::filesystem::path registryPath = steamRoot /
"registry.vdf";
201 if (std::filesystem::exists(registryPath))
219 Logger::info(
"[SteamMonitor] Waiting for Steam to be fully initialized...");
221 auto startTime = std::chrono::steady_clock::now();
222 int pollIntervalMs = 250;
228 Logger::info(
"[SteamMonitor] Steam is fully initialized");
232 auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
233 std::chrono::steady_clock::now() - startTime)
236 if (elapsed >= timeoutMs)
238 Logger::warn(
"[SteamMonitor] Timeout waiting for Steam initialization after {}ms",
243 std::this_thread::sleep_for(std::chrono::milliseconds(pollIntervalMs));
255 std::lock_guard<std::mutex> lock(stateMutex_);
256 stateCallback_ = std::move(callback);
263void SteamProcessMonitor::monitorThread()
269 State current = currentState_.load();
270 bool processExists = checkSteamProcess();
287 Logger::info(
"[SteamMonitor] Steam started successfully");
293 handleStartingTimeout();
310 Logger::info(
"[SteamMonitor] Steam shut down successfully");
316 handleStoppingTimeout();
332 std::unique_lock<std::mutex> lock(stateMutex_);
333 stateCV_.wait_for(lock, std::chrono::milliseconds(500),
334 [
this] {
return shouldStop_.load(); });
341bool SteamProcessMonitor::checkSteamProcess()
346void SteamProcessMonitor::transitionTo(State newState)
348 State oldState = currentState_.exchange(newState);
350 if (oldState == newState)
358 std::lock_guard<std::mutex> lock(stateMutex_);
359 transitionTime_ = std::chrono::steady_clock::now();
361 Logger::info(
"[SteamMonitor] State transition: {} -> {}",
static_cast<int>(oldState),
362 static_cast<int>(newState));
365 stateCV_.notify_all();
367 callbackCopy = stateCallback_;
372 callbackCopy(oldState, newState);
376void SteamProcessMonitor::handleStartingTimeout()
379 std::chrono::steady_clock::time_point t;
381 std::lock_guard<std::mutex> lock(stateMutex_);
384 auto now = std::chrono::steady_clock::now();
385 auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - t).count();
389 Logger::error(
"[SteamMonitor] Timeout waiting for Steam to start");
394void SteamProcessMonitor::handleStoppingTimeout()
396 std::chrono::steady_clock::time_point t;
398 std::lock_guard<std::mutex> lock(stateMutex_);
401 auto now = std::chrono::steady_clock::now();
402 auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - t).count();
406 Logger::error(
"[SteamMonitor] Timeout waiting for Steam to shut down");
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).
static void warn(const std::string &message, Args &&... args)
Log warning message (non-critical issue).
static ProcessResult executeBackground(const std::string &command)
Execute command in background (non-blocking).
static ProcessResult execute(const std::string &command, bool silent=false)
Execute command and check exit code.
static bool isProcessRunning(const std::string &processName)
Check if process is running by name.
static std::filesystem::path getPrimarySteamRoot()
Get primary Steam installation directory.
void setStateCallback(StateCallback callback)
State getCurrentState() const
bool isSteamRunning() const
static SteamProcessMonitor & instance()
bool waitForSteamInitialized(int timeoutMs=10000)
void requestSteamShutdown()
bool isSteamFullyInitialized() const
bool waitForState(State targetState, int timeoutMs=10000)
std::function< void(State oldState, State newState)> StateCallback
constexpr int START_TIMEOUT_SEC
Maximum time to wait for Steam to start before timeout.
constexpr int SHUTDOWN_TIMEOUT_SEC
Maximum time to wait for Steam to shutdown before timeout.