From f6f6bfad7082704cc012c5aaac3a3c872e2e3949 Mon Sep 17 00:00:00 2001 From: Femi Lawal Date: Mon, 12 Feb 2024 19:30:45 +0100 Subject: [PATCH 1/3] make PyAutoGUI default --- fishy/engine/common/screenshot.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/fishy/engine/common/screenshot.py b/fishy/engine/common/screenshot.py index 03e8e17..7122f06 100644 --- a/fishy/engine/common/screenshot.py +++ b/fishy/engine/common/screenshot.py @@ -94,11 +94,25 @@ class D3DShot(IScreenShot): def grab(self) -> ndarray: return self.d3.screenshot() - -LIBS = [MSS, PyAutoGUI, D3DShot] - +LIBS = [PyAutoGUI, MSS, D3DShot] def create() -> IScreenShot: - lib = LIBS[config.get("sslib", 0)] - logging.debug(f"Using {lib.__name__} screenshot lib") - return lib() + # Attempt to use D3DShot if performance is critical and it's specified in the config + preferred_lib = config.get("sslib", None) + if preferred_lib == "d3dshot": + logging.debug("Attempting to use D3DShot (fast) as the screenshot library...") + d3dshot_instance = D3DShot() + if d3dshot_instance.setup(): + return d3dshot_instance + else: + logging.warning("D3DShot setup failed. Falling back to default method...") + + # Default to PyAutoGUI if D3DShot is not explicitly chosen or fails to set up + for lib in LIBS: + lib_instance = lib() + if lib_instance.setup(): + logging.debug(f"Using {lib.__name__} as the screenshot library.") + return lib_instance + + logging.error("No suitable screenshot library found. Please check your configuration.") + raise Exception("Failed to initialize a screenshot library.") From 270abc5167483b0cb6d7d98d0ee4893e0c1d6510 Mon Sep 17 00:00:00 2001 From: Femi Lawal Date: Sun, 3 Mar 2024 18:40:27 +0100 Subject: [PATCH 2/3] remove error thrown when no suitable library found --- fishy/engine/common/screenshot.py | 34 +++++++++++++++---------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/fishy/engine/common/screenshot.py b/fishy/engine/common/screenshot.py index 7122f06..bb71b56 100644 --- a/fishy/engine/common/screenshot.py +++ b/fishy/engine/common/screenshot.py @@ -97,22 +97,20 @@ class D3DShot(IScreenShot): LIBS = [PyAutoGUI, MSS, D3DShot] def create() -> IScreenShot: - # Attempt to use D3DShot if performance is critical and it's specified in the config - preferred_lib = config.get("sslib", None) - if preferred_lib == "d3dshot": - logging.debug("Attempting to use D3DShot (fast) as the screenshot library...") - d3dshot_instance = D3DShot() - if d3dshot_instance.setup(): - return d3dshot_instance - else: - logging.warning("D3DShot setup failed. Falling back to default method...") - - # Default to PyAutoGUI if D3DShot is not explicitly chosen or fails to set up - for lib in LIBS: - lib_instance = lib() - if lib_instance.setup(): - logging.debug(f"Using {lib.__name__} as the screenshot library.") - return lib_instance - + # Initialize a variable to hold the preferred library index + preferred_lib_index = config.get("sslib", 0) + # Create a list of library indices to try, starting with the preferred one + lib_indices = [preferred_lib_index] + [i for i in range(len(LIBS)) if i != preferred_lib_index] + + for index in lib_indices: + lib = LIBS[index] + try: + lib_instance = lib() + if lib_instance.setup(): + logging.debug(f"Using {lib.__name__} as the screenshot library.") + return lib_instance + except Exception as e: + logging.warning(f"Setup failed for {lib.__name__} with error: {str(e)}. Trying next library...") + logging.error("No suitable screenshot library found. Please check your configuration.") - raise Exception("Failed to initialize a screenshot library.") + return None From a16474f613d13eea4bb261f66291671a05997f1b Mon Sep 17 00:00:00 2001 From: Femi Lawal Date: Mon, 11 Mar 2024 09:10:09 +0100 Subject: [PATCH 3/3] handled None in screenshot.create() --- fishy/engine/common/window_server.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/fishy/engine/common/window_server.py b/fishy/engine/common/window_server.py index 66f60a1..e79f10b 100644 --- a/fishy/engine/common/window_server.py +++ b/fishy/engine/common/window_server.py @@ -36,6 +36,12 @@ def init(): Finds the game window, and calculates the offset to remove the title bar """ WindowServer.sslib = screenshot.create() + # Check if the screenshot library was successfully created + if WindowServer.sslib is None: + logging.error("Failed to create screenshot library instance") + WindowServer.status = Status.CRASHED + return + WindowServer.status = Status.RUNNING WindowServer.crop = os_services.get_game_window_rect()