window server reworked for improved threading

This commit is contained in:
Adam Saudagar 2022-02-01 17:21:58 +05:30
parent 245493fbc4
commit 9bcde7e922
8 changed files with 32 additions and 31 deletions

View File

@ -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():

View File

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

View File

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

View File

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

View File

@ -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():

View File

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

View File

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

View File

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