diff --git a/fishy/engine/common/screenshot.py b/fishy/engine/common/screenshot.py index 1ee59ca..af51fe2 100644 --- a/fishy/engine/common/screenshot.py +++ b/fishy/engine/common/screenshot.py @@ -1,5 +1,6 @@ import logging import subprocess +import traceback from abc import ABC, abstractmethod from functools import partial from typing import Optional @@ -98,7 +99,24 @@ class D3DShot(IScreenShot): LIBS = [PyAutoGUI, MSS, D3DShot] -def create() -> IScreenShot: - lib = LIBS[config.get("sslib", 0)] - logging.debug(f"Using {lib.__name__} screenshot lib") - return lib() \ No newline at end of file +# noinspection PyBroadException +def create() -> Optional[IScreenShot]: + # 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(): + # testing grab once + ss = lib_instance.grab() + if ss.shape: + logging.debug(f"Using {lib.__name__} as the screenshot library.") + return lib_instance + except Exception: + logging.warning(f"Setup failed for {lib.__name__} with error: {traceback.format_exc()}. Trying next library...") + + return None diff --git a/fishy/engine/common/window_server.py b/fishy/engine/common/window_server.py index e79f10b..1d0db8d 100644 --- a/fishy/engine/common/window_server.py +++ b/fishy/engine/common/window_server.py @@ -42,14 +42,15 @@ def init(): WindowServer.status = Status.CRASHED return - WindowServer.status = Status.RUNNING - WindowServer.crop = os_services.get_game_window_rect() - - if WindowServer.crop is None or not WindowServer.sslib.setup(): + crop = os_services.get_game_window_rect() + if crop is None or not WindowServer.sslib.setup(): logging.error("Game window not found by window_server") WindowServer.status = Status.CRASHED return + WindowServer.crop = crop + WindowServer.status = Status.RUNNING + def get_cropped_screenshot(): ss = WindowServer.sslib.grab() diff --git a/fishy/version.txt b/fishy/version.txt index c98d62a..0ef0d69 100644 --- a/fishy/version.txt +++ b/fishy/version.txt @@ -1 +1 @@ -0.5.25 +0.5.26