diff --git a/fishy/engine/common/window_server.py b/fishy/engine/common/window_server.py index e6fc693..2c0e128 100644 --- a/fishy/engine/common/window_server.py +++ b/fishy/engine/common/window_server.py @@ -39,9 +39,9 @@ def init(): WindowServer.sct = mss() WindowServer.crop = os_services.get_game_window_rect() - monitor_rect = os_services.get_monitor_rect() - if monitor_rect is None: + + if monitor_rect is None or WindowServer.crop is None: logging.error("Game window not found") WindowServer.status = Status.CRASHED @@ -50,10 +50,15 @@ def init(): WindowServer.monitor_id = i -def get_screenshot(): +def get_cropped_screenshot(): sct_img = WindowServer.sct.grab(WindowServer.sct.monitors[WindowServer.monitor_id]) # noinspection PyTypeChecker - return np.array(sct_img) + ss = np.array(sct_img) + crop = WindowServer.crop + cropped_ss = ss[crop[1]:crop[3], crop[0]:crop[2]] + if cropped_ss.size == 0: + return None + return cropped_ss def loop(): @@ -61,12 +66,10 @@ def loop(): Executed in the start of the main loop finds the game window location and captures it """ - ss = get_screenshot() - crop = WindowServer.crop - WindowServer.Screen = ss[crop[1]:crop[3], crop[0]:crop[2]] + WindowServer.Screen = get_cropped_screenshot() - if WindowServer.Screen.size == 0: - logging.error("Don't minimize or drag game window outside the screen") + if WindowServer.Screen is None: + logging.error("Couldn't find the game window") WindowServer.status = Status.CRASHED diff --git a/fishy/osservices/linux.py b/fishy/osservices/linux.py index 8cc82fd..a028910 100644 --- a/fishy/osservices/linux.py +++ b/fishy/osservices/linux.py @@ -1,4 +1,4 @@ -from typing import Tuple +from typing import Tuple, Optional from fishy.osservices.os_services import IOSServices @@ -25,5 +25,5 @@ class Linux(IOSServices): def get_monitor_rect(self): pass - def get_game_window_rect(self) -> Tuple[int, int, int, int]: + def get_game_window_rect(self) -> Optional[Tuple[int, int, int, int]]: pass diff --git a/fishy/osservices/os_services.py b/fishy/osservices/os_services.py index e4c52bb..182d9ba 100644 --- a/fishy/osservices/os_services.py +++ b/fishy/osservices/os_services.py @@ -52,7 +52,7 @@ class IOSServices(ABC): """ @abstractmethod - def get_game_window_rect(self) -> Tuple[int, int, int, int]: + def get_game_window_rect(self) -> Optional[Tuple[int, int, int, int]]: """ :return: location of the game window without any frame """ diff --git a/fishy/osservices/windows.py b/fishy/osservices/windows.py index 796bd22..c7dad66 100644 --- a/fishy/osservices/windows.py +++ b/fishy/osservices/windows.py @@ -3,7 +3,7 @@ import logging import math import os import sys -from typing import Tuple +from typing import Tuple, Optional import pywintypes import win32api @@ -89,20 +89,24 @@ class Windows(IOSServices): except pywintypes.error: return None - def get_game_window_rect(self) -> Tuple[int, int, int, int]: + def get_game_window_rect(self) -> Optional[Tuple[int, int, int, int]]: hwnd = win32gui.FindWindow(None, "Elder Scrolls Online") monitor_rect = self.get_monitor_rect() - rect = win32gui.GetWindowRect(hwnd) - client_rect = win32gui.GetClientRect(hwnd) - windowOffset = math.floor(((rect[2] - rect[0]) - client_rect[2]) / 2) - fullscreen = monitor_rect[3] == (rect[3] - rect[1]) - title_offset = ((rect[3] - rect[1]) - client_rect[3]) - windowOffset if not fullscreen else 0 + # noinspection PyUnresolvedReferences + try: + rect = win32gui.GetWindowRect(hwnd) + client_rect = win32gui.GetClientRect(hwnd) + windowOffset = math.floor(((rect[2] - rect[0]) - client_rect[2]) / 2) + fullscreen = monitor_rect[3] == (rect[3] - rect[1]) + title_offset = ((rect[3] - rect[1]) - client_rect[3]) - windowOffset if not fullscreen else 0 - game_rect = ( - rect[0] + windowOffset - monitor_rect[0], - rect[1] + title_offset - monitor_rect[1], - rect[2] - windowOffset - monitor_rect[0], - rect[3] - windowOffset - monitor_rect[1] - ) - return game_rect + game_rect = ( + rect[0] + windowOffset - monitor_rect[0], + rect[1] + title_offset - monitor_rect[1], + rect[2] - windowOffset - monitor_rect[0], + rect[3] - windowOffset - monitor_rect[1] + ) + return game_rect + except pywintypes.error: + return None