From 9bcde7e92268c84442557d6e5d95b91edfac4d53 Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Tue, 1 Feb 2022 17:21:58 +0530 Subject: [PATCH] window server reworked for improved threading --- fishy/__main__.py | 2 ++ fishy/engine/common/IEngine.py | 4 ++++ fishy/engine/common/event_handler.py | 7 ++++++- fishy/engine/common/window.py | 2 +- fishy/engine/common/window_server.py | 11 ++++++----- fishy/engine/fullautofisher/engine.py | 8 -------- fishy/engine/semifisher/engine.py | 28 +++++++++++++-------------- fishy/gui/log_config.py | 1 - 8 files changed, 32 insertions(+), 31 deletions(-) diff --git a/fishy/__main__.py b/fishy/__main__.py index 1b66f90..f3b855e 100644 --- a/fishy/__main__.py +++ b/fishy/__main__.py @@ -79,6 +79,8 @@ def main(): pil_logger = logging.getLogger('PIL') pil_logger.setLevel(logging.INFO) + # todo set log level info for d3dshot too + window_to_hide = win32gui.GetForegroundWindow() if not gui.check_eula(): diff --git a/fishy/engine/common/IEngine.py b/fishy/engine/common/IEngine.py index 18db5d7..b544e39 100644 --- a/fishy/engine/common/IEngine.py +++ b/fishy/engine/common/IEngine.py @@ -4,8 +4,10 @@ import typing from threading import Thread from typing import Callable +import cv2 from playsound import playsound +from fishy.engine.common.window import WindowClient from fishy.gui.funcs import GUIFuncsMock from fishy.helper import helper @@ -58,6 +60,7 @@ class IEngine: # noinspection PyBroadException def _crash_safe(self): + self.window = WindowClient(color=cv2.COLOR_RGB2GRAY, show_name="fishy debug") self.gui.bot_started(True) try: self.run() @@ -65,6 +68,7 @@ class IEngine: traceback.print_exc() self.state = 0 self.gui.bot_started(False) + self.window.destroy() def run(self): raise NotImplementedError diff --git a/fishy/engine/common/event_handler.py b/fishy/engine/common/event_handler.py index c8105a5..ba1b03d 100644 --- a/fishy/engine/common/event_handler.py +++ b/fishy/engine/common/event_handler.py @@ -5,6 +5,7 @@ from fishy.engine import SemiFisherEngine from fishy.engine.fullautofisher.engine import FullAuto +# to test only gui without engine code interfering class IEngineHandler: def __init__(self): ... @@ -58,7 +59,11 @@ class EngineEventHandler(IEngineHandler): def quit(self): def func(): - self.semi_fisher_engine.start = False + if self.semi_fisher_engine.start: + self.semi_fisher_engine.turn_off() + if self.full_fisher_engine.start: + self.semi_fisher_engine.turn_off() + self.event_handler_running = False self.event.append(func) diff --git a/fishy/engine/common/window.py b/fishy/engine/common/window.py index fb44f14..215baf3 100644 --- a/fishy/engine/common/window.py +++ b/fishy/engine/common/window.py @@ -28,7 +28,7 @@ class WindowClient: window_server.start() WindowClient.clients.append(self) - def destory(self): + def destroy(self): if self in WindowClient.clients: WindowClient.clients.remove(self) if len(WindowClient.clients) == 0: diff --git a/fishy/engine/common/window_server.py b/fishy/engine/common/window_server.py index 0cc4133..d46c530 100644 --- a/fishy/engine/common/window_server.py +++ b/fishy/engine/common/window_server.py @@ -84,20 +84,21 @@ def loop(): WindowServer.status = Status.CRASHED -def loop_end(): - cv2.waitKey(25) - - # noinspection PyBroadException def run(): # todo use config + logging.debug("window server started") while WindowServer.status == Status.RUNNING: try: loop() except Exception: traceback.print_exc() WindowServer.status = Status.CRASHED - loop_end() + + if WindowServer.status == Status.CRASHED: + logging.debug("window server crashed") + elif WindowServer.status == Status.STOPPED: + logging.debug("window server stopped") def start(): diff --git a/fishy/engine/fullautofisher/engine.py b/fishy/engine/fullautofisher/engine.py index 446f10b..a6fb4c8 100644 --- a/fishy/engine/fullautofisher/engine.py +++ b/fishy/engine/fullautofisher/engine.py @@ -43,8 +43,6 @@ class FullAuto(IEngine): self.mode = None def run(self): - self.window = WindowClient(color=cv2.COLOR_RGB2GRAY, show_name="Full auto debug") - self.mode = None if config.get("calibrate", False): self.mode = Calibrator(self) @@ -199,12 +197,6 @@ class FullAuto(IEngine): time.sleep(0.05) self._curr_rotate_y -= 0.05 - def toggle_start(self): - self.start = not self.start - if self.start: - self.thread = Thread(target=self.run) - self.thread.start() - if __name__ == '__main__': logging.getLogger("").setLevel(logging.DEBUG) diff --git a/fishy/engine/semifisher/engine.py b/fishy/engine/semifisher/engine.py index e525de9..1fb0956 100644 --- a/fishy/engine/semifisher/engine.py +++ b/fishy/engine/semifisher/engine.py @@ -1,22 +1,17 @@ import logging import time +import traceback import typing from threading import Thread from typing import Callable, Optional -import cv2 from fishy.engine.semifisher.fishing_mode import FishingMode -from fishy.helper.helper import log_raise -from playsound import playsound - from fishy.engine.common.IEngine import IEngine from fishy.engine.common.qr_detection import get_qr_location, get_values_from_image, image_pre_process from fishy.engine.common.window import WindowClient from fishy.engine.semifisher import fishing_event, fishing_mode from fishy.engine.semifisher.fishing_event import FishEvent -from fishy.helper import helper -from fishy.helper.luaparser import sv_color_extract if typing.TYPE_CHECKING: from fishy.gui import GUI @@ -32,8 +27,6 @@ class SemiFisherEngine(IEngine): Starts the fishing code explained in comments in detail """ - self.window = WindowClient(color=cv2.COLOR_RGB2GRAY, show_name="semifisher debug") - if self.get_gui: logging.info("Starting the bot engine, look at the fishing hole to start fishing") Thread(target=self._wait_and_check).start() @@ -44,6 +37,16 @@ class SemiFisherEngine(IEngine): return fishing_event.init() + # noinspection PyBroadException + try: + self._engine_loop() + except Exception: + logging.error("exception occurred while running engine loop") + traceback.print_exc() + + fishing_event.unsubscribe() + + def _engine_loop(self): skip_count = 0 while self.state == 1 and WindowClient.running(): capture = self.window.processed_image(func=image_pre_process) @@ -51,8 +54,7 @@ class SemiFisherEngine(IEngine): # if window server crashed if not capture: logging.error("Couldn't capture window stopping engine") - self.turn_off() - continue + return # crop qr and get the values from it values = get_values_from_image(capture) @@ -62,8 +64,7 @@ class SemiFisherEngine(IEngine): logging.error(f"Couldn't read values from FishyQR, skipping {skip_count}/5") if skip_count >= 5: logging.error("Stopping engine...") - self.turn_off() - continue + return else: skip_count = 0 @@ -71,9 +72,6 @@ class SemiFisherEngine(IEngine): fishing_mode.loop(values[3]) time.sleep(0.1) - logging.info("Fishing engine stopped") - fishing_event.unsubscribe() - def _wait_and_check(self): time.sleep(10) if not FishEvent.FishingStarted and self.state == 1: diff --git a/fishy/gui/log_config.py b/fishy/gui/log_config.py index d304666..4b4cc6a 100644 --- a/fishy/gui/log_config.py +++ b/fishy/gui/log_config.py @@ -12,7 +12,6 @@ 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))