From 6326a8343473e34f605e3004b45ca29ec03a05a5 Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Sun, 30 Jan 2022 19:58:17 +0530 Subject: [PATCH 1/3] added multi monitor support for window module --- fishy/engine/common/window_server.py | 35 +++++++++++++++++++--------- requirements.txt | 1 + 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/fishy/engine/common/window_server.py b/fishy/engine/common/window_server.py index ad433db..1951886 100644 --- a/fishy/engine/common/window_server.py +++ b/fishy/engine/common/window_server.py @@ -5,11 +5,12 @@ from enum import Enum from threading import Thread import cv2 -import numpy as np +import d3dshot import pywintypes +import win32api import win32gui -from PIL import ImageGrab -from win32api import GetSystemMetrics +from d3dshot import D3DShot +from ctypes import windll from fishy.helper.config import config @@ -28,6 +29,8 @@ class WindowServer: windowOffset = None hwnd = None status = Status.STOPPED + d3: D3DShot = None + monitor_top_left = None def init(): @@ -37,10 +40,19 @@ def init(): """ try: WindowServer.hwnd = win32gui.FindWindow(None, "Elder Scrolls Online") + + monitor_id = windll.user32.MonitorFromWindow(WindowServer.hwnd, 2) + WindowServer.monitor_top_left = win32api.GetMonitorInfo(monitor_id)["Monitor"][:2] + rect = win32gui.GetWindowRect(WindowServer.hwnd) client_rect = win32gui.GetClientRect(WindowServer.hwnd) WindowServer.windowOffset = math.floor(((rect[2] - rect[0]) - client_rect[2]) / 2) WindowServer.status = Status.RUNNING + + d3 = d3dshot.create(capture_output="numpy") + d3.display = next((m for m in d3.displays if m.hmonitor == monitor_id), None) + WindowServer.d3 = d3 + except pywintypes.error: logging.error("Game window not found") WindowServer.status = Status.CRASHED @@ -51,21 +63,22 @@ def loop(): Executed in the start of the main loop finds the game window location and captures it """ - bbox = (0, 0, GetSystemMetrics(0), GetSystemMetrics(1)) - temp_screen = np.array(ImageGrab.grab(bbox=bbox)) + temp_screen = WindowServer.d3.screenshot() rect = win32gui.GetWindowRect(WindowServer.hwnd) client_rect = win32gui.GetClientRect(WindowServer.hwnd) - fullscreen = GetSystemMetrics(1) == (rect[3] - rect[1]) - titleOffset = ((rect[3] - rect[1]) - client_rect[3]) - WindowServer.windowOffset if not fullscreen else 0 - + fullscreen = WindowServer.d3.display.resolution[1] == (rect[3] - rect[1]) + title_offset = ((rect[3] - rect[1]) - client_rect[3]) - WindowServer.windowOffset if not fullscreen else 0 crop = ( - rect[0] + WindowServer.windowOffset, rect[1] + titleOffset, rect[2] - WindowServer.windowOffset, - rect[3] - WindowServer.windowOffset) + rect[0] + WindowServer.windowOffset - WindowServer.monitor_top_left[0], + rect[1] + title_offset - WindowServer.monitor_top_left[1], + rect[2] - WindowServer.windowOffset - WindowServer.monitor_top_left[0], + rect[3] - WindowServer.windowOffset - WindowServer.monitor_top_left[1] + ) - WindowServer.Screen = temp_screen[crop[1]:crop[3], crop[0]:crop[2]] + WindowServer.Screen = temp_screen[crop[1]:crop[3], crop[0]:crop[2]] if not fullscreen else temp_screen if WindowServer.Screen.size == 0: logging.error("Don't minimize or drag game window outside the screen") diff --git a/requirements.txt b/requirements.txt index 8df1eac..e604915 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,3 +15,4 @@ playsound==1.2.2 event-scheduler pyzbar mouse +d3dshot \ No newline at end of file From b745fd915bc3baaafeb2f7902d226c2cfd767737 Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Sun, 30 Jan 2022 19:58:30 +0530 Subject: [PATCH 2/3] turned off debug logging --- fishy/gui/log_config.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fishy/gui/log_config.py b/fishy/gui/log_config.py index 0a93050..d304666 100644 --- a/fishy/gui/log_config.py +++ b/fishy/gui/log_config.py @@ -1,5 +1,6 @@ +import logging import typing -from logging import StreamHandler +from logging import StreamHandler, Formatter if typing.TYPE_CHECKING: from . import GUI @@ -11,6 +12,7 @@ class GUIStreamHandler(StreamHandler): self.gui = gui def emit(self, record): + self.setLevel(logging.INFO) msg = self.format(record) self.gui.call_in_thread(lambda: _write_to_console(self.gui, msg)) From 3d393cef93dee8e571972c2a0de666f083ff63d1 Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Mon, 31 Jan 2022 11:14:27 +0530 Subject: [PATCH 3/3] setup process will no long try to import libraries --- fishy/__init__.py | 8 +++++++- fishy/engine/common/window_server.py | 3 +-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/fishy/__init__.py b/fishy/__init__.py index 124b664..ac2cc6f 100644 --- a/fishy/__init__.py +++ b/fishy/__init__.py @@ -1,4 +1,10 @@ from fishy import constants -from fishy.__main__ import main + +# this prevents importing from package while setup +def main(): + from fishy.__main__ import main as actual_main + actual_main() + + __version__ = constants.version diff --git a/fishy/engine/common/window_server.py b/fishy/engine/common/window_server.py index 1951886..0cc4133 100644 --- a/fishy/engine/common/window_server.py +++ b/fishy/engine/common/window_server.py @@ -9,7 +9,6 @@ import d3dshot import pywintypes import win32api import win32gui -from d3dshot import D3DShot from ctypes import windll from fishy.helper.config import config @@ -29,7 +28,7 @@ class WindowServer: windowOffset = None hwnd = None status = Status.STOPPED - d3: D3DShot = None + d3: d3dshot.D3DShot = None monitor_top_left = None