handled case when game is not running

This commit is contained in:
Adam Saudagar 2023-03-06 23:18:09 +05:30
parent a5499475f6
commit 901ce6c346
4 changed files with 33 additions and 26 deletions

View File

@ -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

View File

@ -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

View File

@ -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
"""

View File

@ -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