From da6ccc6250b86ded11afd27ef138fec71f86e8fa Mon Sep 17 00:00:00 2001 From: pokzcodes <98141051+pokzcodes@users.noreply.github.com> Date: Sat, 5 Feb 2022 13:52:41 -0500 Subject: [PATCH] Kill d2r when botty cannot recover and shuts down (#511) --- src/game_controller.py | 10 +++++++--- src/utils/restart.py | 8 ++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/game_controller.py b/src/game_controller.py index 317c22e..9b0952c 100644 --- a/src/game_controller.py +++ b/src/game_controller.py @@ -14,7 +14,7 @@ from health_manager import HealthManager from logger import Logger from messages import Messenger from screen import Screen -from utils.restart import restart_game +from utils.restart import restart_game, kill_game from utils.misc import kill_thread, set_d2r_always_on_top, restore_d2r_window_visibility @@ -68,7 +68,7 @@ class GameController: Logger.error(msg) if self._config.general["custom_message_hook"]: messenger.send_message(msg) - os._exit(1) + self.safe_exit(1) else: do_restart = self.game_recovery.go_to_hero_selection() break @@ -101,7 +101,7 @@ class GameController: Logger.error("Could not recover from a max game length violation. Quitting botty.") if self._config.general["custom_message_hook"]: messenger.send_message("Got stuck and will now quit botty") - os._exit(1) + self.safe_exit(1) def start(self): # Check if we user should update the d2r settings @@ -156,3 +156,7 @@ class GameController: def toggle_pause_bot(self): if self.bot: self.bot.toggle_pause() + + def safe_exit(self, error_code=0): + kill_game() + os._exit(error_code) diff --git a/src/utils/restart.py b/src/utils/restart.py index f2297c7..7c789bd 100644 --- a/src/utils/restart.py +++ b/src/utils/restart.py @@ -15,13 +15,17 @@ def process_exists(process_name): # because Fail message could be translated return last_line.lower().startswith(process_name.lower()) +def kill_game(): + while process_exists("D2R.exe"): + os.system("taskkill /f /im D2R.exe") + wait(1.0, 1.5) + def restart_game(d2_path = None): if not d2_path: path = "C:\Program Files (x86)\Diablo II Resurrected\D2R.exe" else: path = d2_path - if process_exists("D2R.exe"): - os.system("taskkill /f /im D2R.exe") + kill_game() wait(1.0, 1.5) # This method should function similar to opening the exe via double-click os.startfile(path)