From 4ea27ae7daa94467817a4d97a6e50256b8ee82db Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Tue, 1 Feb 2022 22:20:57 +0530 Subject: [PATCH] added a null check in get_coords changed print_exec to print in both console and fishy fixed engine couldn't be stoped with toggle fixed full auto stop getting called multiple times due to inactive stop set logging for d3dshot to info, to remove debug logs caused by it --- fishy/__main__.py | 13 ++++++------- fishy/engine/common/IEngine.py | 5 +++-- fishy/engine/common/qr_detection.py | 2 +- fishy/engine/common/window_server.py | 3 ++- fishy/engine/fullautofisher/engine.py | 16 +++++++++------- fishy/engine/fullautofisher/mode/calibrator.py | 4 +++- fishy/engine/fullautofisher/mode/player.py | 1 + fishy/engine/fullautofisher/test.py | 2 +- fishy/engine/semifisher/engine.py | 15 ++++++++++----- fishy/gui/gui.py | 3 +-- fishy/gui/log_config.py | 3 +++ fishy/helper/helper.py | 9 +++++++-- 12 files changed, 47 insertions(+), 29 deletions(-) diff --git a/fishy/__main__.py b/fishy/__main__.py index f3b855e..8a614f8 100644 --- a/fishy/__main__.py +++ b/fishy/__main__.py @@ -2,19 +2,18 @@ import ctypes import logging import os import sys -import traceback import win32con import win32gui import fishy from fishy import gui, helper, web -from fishy.constants import chalutier, lam2, fishyqr, libgps from fishy.engine.common.event_handler import EngineEventHandler from fishy.gui import GUI, splash, update_dialog from fishy.helper import hotkey from fishy.helper.active_poll import active from fishy.helper.config import config +from fishy.helper.helper import print_exc from fishy.helper.hotkey.hotkey_process import hotkey from fishy.helper.migration import Migration @@ -58,7 +57,7 @@ def initialize(window_to_hide): if update_now: helper.auto_upgrade() except Exception: - logging.error(traceback.format_exc()) + print_exc() if not config.get("debug", False) and check_window_name(win32gui.GetWindowText(window_to_hide)): win32gui.ShowWindow(window_to_hide, win32con.SW_HIDE) @@ -76,10 +75,10 @@ def main(): print("launching please wait...") - pil_logger = logging.getLogger('PIL') - pil_logger.setLevel(logging.INFO) - - # todo set log level info for d3dshot too + info_logger = ["comtypes", "PIL"] + for i in info_logger: + pil_logger = logging.getLogger(i) + pil_logger.setLevel(logging.INFO) window_to_hide = win32gui.GetForegroundWindow() diff --git a/fishy/engine/common/IEngine.py b/fishy/engine/common/IEngine.py index b544e39..445b03e 100644 --- a/fishy/engine/common/IEngine.py +++ b/fishy/engine/common/IEngine.py @@ -10,6 +10,7 @@ from playsound import playsound from fishy.engine.common.window import WindowClient from fishy.gui.funcs import GUIFuncsMock from fishy.helper import helper +from fishy.helper.helper import print_exc if typing.TYPE_CHECKING: from fishy.gui import GUI @@ -39,7 +40,7 @@ class IEngine: if self.state == 0: self.turn_on() else: - self.turn_on() + self.turn_off() def turn_on(self): self.state = 1 @@ -65,7 +66,7 @@ class IEngine: try: self.run() except Exception: - traceback.print_exc() + print_exc() self.state = 0 self.gui.bot_started(False) self.window.destroy() diff --git a/fishy/engine/common/qr_detection.py b/fishy/engine/common/qr_detection.py index 1db1ad2..5284329 100644 --- a/fishy/engine/common/qr_detection.py +++ b/fishy/engine/common/qr_detection.py @@ -38,7 +38,7 @@ def get_qr_location(og_img): cv2.drawContours(mask, cnt, i, 255, -1) x, y, w, h = cv2.boundingRect(cnt[i]) qr_result = decode(og_img[y:h + y, x:w + x], - symbols=[ZBarSymbol.QRCODE]) + symbols=[ZBarSymbol.QRCODE]) if qr_result: valid_crops.append(((x, y, x + w, y + h), area)) diff --git a/fishy/engine/common/window_server.py b/fishy/engine/common/window_server.py index d46c530..cb9e136 100644 --- a/fishy/engine/common/window_server.py +++ b/fishy/engine/common/window_server.py @@ -12,6 +12,7 @@ import win32gui from ctypes import windll from fishy.helper.config import config +from fishy.helper.helper import print_exc class Status(Enum): @@ -92,7 +93,7 @@ def run(): try: loop() except Exception: - traceback.print_exc() + print_exc() WindowServer.status = Status.CRASHED if WindowServer.status == Status.CRASHED: diff --git a/fishy/engine/fullautofisher/engine.py b/fishy/engine/fullautofisher/engine.py index a6fb4c8..f0bbcd4 100644 --- a/fishy/engine/fullautofisher/engine.py +++ b/fishy/engine/fullautofisher/engine.py @@ -20,7 +20,7 @@ from fishy.engine.semifisher import fishing_event, fishing_mode from fishy.engine.semifisher.fishing_mode import FishingMode from fishy.helper import hotkey from fishy.helper.config import config -from fishy.helper.helper import wait_until, is_eso_active, sign +from fishy.helper.helper import wait_until, is_eso_active, sign, print_exc mse = mouse.Controller() kb = keyboard.Controller() @@ -73,7 +73,7 @@ class FullAuto(IEngine): self.mode.run() except Exception: logging.error("exception occurred while running full auto mode") - traceback.print_exc() + print_exc() def _pre_run_checks(self): if self.window.get_capture() is None: @@ -99,10 +99,11 @@ class FullAuto(IEngine): def stop_on_inactive(self): def func(): - logging.info("stop on inactive started") - wait_until(lambda: not is_eso_active() or self.start != 1) - self.turn_off() - logging.info("stop on inactive stopped") + logging.debug("stop on inactive started") + wait_until(lambda: not is_eso_active() or not self.start) + if self.start and not is_eso_active(): + self.turn_off() + logging.debug("stop on inactive stopped") Thread(target=func).start() def get_coords(self): @@ -113,7 +114,8 @@ class FullAuto(IEngine): todo its waiting for qr which doesn't block the engine when commanded to close """ img = self.window.processed_image(func=image_pre_process) - return get_values_from_image(img)[:3] + values = get_values_from_image(img) + return values[:3] if values else None def move_to(self, target) -> bool: current = self.get_coords() diff --git a/fishy/engine/fullautofisher/mode/calibrator.py b/fishy/engine/fullautofisher/mode/calibrator.py index 05c00f2..86cb887 100644 --- a/fishy/engine/fullautofisher/mode/calibrator.py +++ b/fishy/engine/fullautofisher/mode/calibrator.py @@ -20,7 +20,7 @@ kb = keyboard.Controller() offset = 0 -def get_crop_coods(window): +def get_crop_coords(window): img = window.get_capture() img = cv2.inRange(img, 0, 1) @@ -37,6 +37,8 @@ def get_crop_coods(window): x, y, w, h = cv2.boundingRect(cnt[i]) return x, y + offset, x + w, y + h - offset + return None + def _update_factor(key, value): full_auto_factors = config.get("full_auto_factors", {}) diff --git a/fishy/engine/fullautofisher/mode/player.py b/fishy/engine/fullautofisher/mode/player.py index 7d534ea..ed6d4c5 100644 --- a/fishy/engine/fullautofisher/mode/player.py +++ b/fishy/engine/fullautofisher/mode/player.py @@ -75,6 +75,7 @@ class Player(IMode): self.i = find_nearest(self.timeline, coords)[0] logging.info("starting player") + return True def _loop(self): action = self.timeline[self.i] diff --git a/fishy/engine/fullautofisher/test.py b/fishy/engine/fullautofisher/test.py index 07c48c9..e4f3c12 100644 --- a/fishy/engine/fullautofisher/test.py +++ b/fishy/engine/fullautofisher/test.py @@ -9,7 +9,7 @@ class Test: self.target = None # noinspection PyProtectedMember - def print_coods(self): + def print_coords(self): logging.info(self.engine.get_coords()) def set_target(self): diff --git a/fishy/engine/semifisher/engine.py b/fishy/engine/semifisher/engine.py index 1fb0956..6e2892d 100644 --- a/fishy/engine/semifisher/engine.py +++ b/fishy/engine/semifisher/engine.py @@ -12,6 +12,7 @@ from fishy.engine.common.qr_detection import get_qr_location, get_values_from_im 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.helper import print_exc if typing.TYPE_CHECKING: from fishy.gui import GUI @@ -31,7 +32,12 @@ class SemiFisherEngine(IEngine): logging.info("Starting the bot engine, look at the fishing hole to start fishing") Thread(target=self._wait_and_check).start() - self.window.crop = get_qr_location(self.window.get_capture()) + capture = self.window.get_capture() + if capture is None: + logging.error("couldn't get game capture") + return + + self.window.crop = get_qr_location(capture) if not self.window.crop: logging.error("FishyQR not found, try to drag it around and try again") return @@ -42,7 +48,7 @@ class SemiFisherEngine(IEngine): self._engine_loop() except Exception: logging.error("exception occurred while running engine loop") - traceback.print_exc() + print_exc() fishing_event.unsubscribe() @@ -52,7 +58,7 @@ class SemiFisherEngine(IEngine): capture = self.window.processed_image(func=image_pre_process) # if window server crashed - if not capture: + if capture is None: logging.error("Couldn't capture window stopping engine") return @@ -61,9 +67,8 @@ class SemiFisherEngine(IEngine): # if fishyqr fails to get read multiple times, stop the bot if not values: skip_count += 1 - logging.error(f"Couldn't read values from FishyQR, skipping {skip_count}/5") if skip_count >= 5: - logging.error("Stopping engine...") + logging.error("Couldn't read values from FishyQR, Stopping engine...") return else: skip_count = 0 diff --git a/fishy/gui/gui.py b/fishy/gui/gui.py index 45af6d4..747b6f9 100644 --- a/fishy/gui/gui.py +++ b/fishy/gui/gui.py @@ -8,10 +8,9 @@ from dataclasses import dataclass from ttkthemes import ThemedTk -from fishy.engine.common.event_handler import EngineEventHandler, IEngineHandler +from fishy.engine.common.event_handler import IEngineHandler from fishy.gui import config_top from fishy.gui.funcs import GUIFuncs -from fishy.web import web from ..helper.config import config from ..helper.helper import wait_until diff --git a/fishy/gui/log_config.py b/fishy/gui/log_config.py index 4b4cc6a..e05c755 100644 --- a/fishy/gui/log_config.py +++ b/fishy/gui/log_config.py @@ -12,6 +12,9 @@ class GUIStreamHandler(StreamHandler): self.gui = gui def emit(self, record): + # todo implement verbose/debug option to show more info + # formatter = Formatter('%(name)s - %(levelname)s - %(message)s') + # self.setFormatter(formatter) msg = self.format(record) self.gui.call_in_thread(lambda: _write_to_console(self.gui, msg)) diff --git a/fishy/helper/helper.py b/fishy/helper/helper.py index 56a0a93..01c31c9 100644 --- a/fishy/helper/helper.py +++ b/fishy/helper/helper.py @@ -157,7 +157,7 @@ def create_shortcut(anti_ghosting: bool): logging.info("Shortcut created") except Exception: - traceback.print_exc() + print_exc() logging.error("Couldn't create shortcut") @@ -219,7 +219,7 @@ def install_addon(name, url, v=None): return 0 except Exception: logging.error("Could not install Add-On " + name + ", try doing it manually") - traceback.print_exc() + print_exc() return 1 @@ -276,3 +276,8 @@ def kill_thread(thread): if res > 1: ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, 0) print('Exception raise failure') + + +def print_exc(): + logging.error(traceback.format_exc()) + traceback.print_exc()