diff --git a/ClaudeCode.md b/ClaudeCode.md index 6ce842d..2b031b7 100644 --- a/ClaudeCode.md +++ b/ClaudeCode.md @@ -644,6 +644,10 @@ Wolf Chat 是一個基於 MCP (Modular Capability Provider) 框架的聊天機 - 使用回調函數 (`callback`) 與調用者(即 `Setup.py`)通信,例如在遊戲重啟完成時。 - 保留了獨立運行模式,以便在直接執行時仍能工作(主要用於測試或舊版兼容)。 - 程式碼註解和日誌訊息已更新為英文。 + - **新增遊戲崩潰自動恢復 (2025-05-15)**: + - 在 `_monitor_loop` 方法中,優先檢查遊戲進程 (`_is_game_running`) 是否仍在運行。 + - 如果進程消失,會記錄警告並嘗試重新啟動遊戲 (`_start_game_process`)。 + - 新增 `_is_game_running` 方法,使用 `psutil` 檢查具有指定進程名稱的遊戲是否正在運行。 - **`Setup.py` (修改)**: - 導入 `game_manager`。 - 在 `WolfChatSetup` 類的 `__init__` 方法中初始化 `self.game_monitor = None`。 diff --git a/game_manager.py b/game_manager.py index 390edf2..1e0fb34 100644 --- a/game_manager.py +++ b/game_manager.py @@ -132,6 +132,17 @@ class GameMonitor: while not self.stop_event.is_set(): try: + # Add to _monitor_loop method - just 7 lines that matter + if not self._is_game_running(): + self.logger.warning("Game process disappeared - restarting") + time.sleep(2) # Let resources release + if self._start_game_process(): + self.logger.info("Game restarted successfully") + else: + self.logger.error("Game restart failed") + time.sleep(self.monitor_interval) # Wait before next check after a restart attempt + continue + # Check for scheduled restart if self.next_restart_time and time.time() >= self.next_restart_time: self.logger.info("Scheduled restart time reached. Performing restart...") @@ -239,6 +250,17 @@ class GameMonitor: self.logger.info("Game window monitoring loop finished") + def _is_game_running(self): + """Check if game is running""" + if not HAS_PSUTIL: + self.logger.warning("_is_game_running: psutil not available, cannot check process status.") + return True # Assume running if psutil is not available to avoid unintended restarts + try: + return any(p.name().lower() == self.game_process_name.lower() for p in psutil.process_iter(['name'])) + except Exception as e: + self.logger.error(f"Error checking game process: {e}") + return False # Assume not running on error + def _find_game_window(self): """Find the game window with the specified title""" try: