Skip to content

Commit

Permalink
Opt: Early init minitouch and MaaTouch for faster startup
Browse files Browse the repository at this point in the history
  • Loading branch information
LmeSzinc committed Apr 15, 2024
1 parent 39af236 commit 6ec8df8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
4 changes: 4 additions & 0 deletions module/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ def close_game(self):
self.data, keys="Alas.Optimization.CloseGameDuringWait", default=False
)

@property
def is_actual_task(self):
return self.task.command.lower() not in ['alas', 'template']

@property
def is_cloud_game(self):
return deep_get(
Expand Down
7 changes: 7 additions & 0 deletions module/device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ def __init__(self, *args, **kwargs):
if not self.config.is_template_config and self.config.Emulator_ScreenshotMethod == 'auto':
self.run_simple_screenshot_benchmark()

# Early init
if self.config.is_actual_task:
if self.config.Emulator_ControlMethod == 'MaaTouch':
self.early_maatouch_init()
if self.config.Emulator_ControlMethod == 'minitouch':
self.early_minitouch_init()

# SRC only, use nemu_ipc if available
available = self.nemu_ipc_available()
logger.attr('nemu_ipc_available', available)
Expand Down
43 changes: 35 additions & 8 deletions module/device/method/maatouch.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import socket
import threading
from functools import wraps

from adbutils.errors import AdbError

from module.base.decorator import cached_property, del_cached_property
from module.base.decorator import cached_property, del_cached_property, has_cached_property
from module.base.timer import Timer
from module.base.utils import *
from module.device.connection import Connection
from module.device.method.minitouch import CommandBuilder, insert_swipe
from module.device.method.utils import RETRY_TRIES, retry_sleep, handle_adb_error
from module.device.method.utils import RETRY_TRIES, handle_adb_error, retry_sleep
from module.exception import RequestHumanTakeover
from module.logger import logger

Expand Down Expand Up @@ -36,20 +37,20 @@ def retry_wrapper(self, *args, **kwargs):

def init():
self.adb_reconnect()
del_cached_property(self, 'maatouch_builder')
del_cached_property(self, '_maatouch_builder')
# Emulator closed
except ConnectionAbortedError as e:
logger.error(e)

def init():
self.adb_reconnect()
del_cached_property(self, 'maatouch_builder')
del_cached_property(self, '_maatouch_builder')
# AdbError
except AdbError as e:
if handle_adb_error(e):
def init():
self.adb_reconnect()
del_cached_property(self, 'maatouch_builder')
del_cached_property(self, '_maatouch_builder')
else:
break
# MaaTouchNotInstalledError: Received "Aborted" from MaaTouch
Expand All @@ -58,12 +59,12 @@ def init():

def init():
self.maatouch_install()
del_cached_property(self, 'maatouch_builder')
del_cached_property(self, '_maatouch_builder')
except BrokenPipeError as e:
logger.error(e)

def init():
del_cached_property(self, 'maatouch_builder')
del_cached_property(self, '_maatouch_builder')
# Unknown, probably a trucked image
except Exception as e:
logger.exception(e)
Expand Down Expand Up @@ -103,12 +104,38 @@ class MaaTouch(Connection):
max_y: int
_maatouch_stream = socket.socket
_maatouch_stream_storage = None
_maatouch_init_thread = None

@cached_property
def maatouch_builder(self):
def _maatouch_builder(self):
self.maatouch_init()
return MaatouchBuilder(self)

@property
def maatouch_builder(self):
# Wait init thread
if self._maatouch_init_thread is not None:
self._maatouch_init_thread.join()
del self._maatouch_init_thread
self._maatouch_init_thread = None

return self._maatouch_builder

def early_maatouch_init(self):
"""
Start a thread to init maatouch connection while the Alas instance just starting to take screenshots
This would speed up the first click 0.2 ~ 0.4s.
"""
if has_cached_property(self, '_maatouch_builder'):
return

def early_maatouch_init_func():
_ = self._maatouch_builder

thread = threading.Thread(target=early_maatouch_init_func, daemon=True)
self._maatouch_init_thread = thread
thread.start()

def maatouch_init(self):
logger.hr('MaaTouch init')
max_x, max_y = 1280, 720
Expand Down
40 changes: 33 additions & 7 deletions module/device/method/minitouch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import json
import re
import socket
import threading
import time
from functools import wraps
from typing import List
Expand All @@ -10,11 +10,11 @@
from adbutils.errors import AdbError
from uiautomator2 import _Service

from module.base.decorator import Config, cached_property, del_cached_property
from module.base.decorator import Config, cached_property, del_cached_property, has_cached_property
from module.base.timer import Timer
from module.base.utils import *
from module.device.connection import Connection
from module.device.method.utils import RETRY_TRIES, retry_sleep, handle_adb_error
from module.device.method.utils import RETRY_TRIES, handle_adb_error, retry_sleep
from module.exception import RequestHumanTakeover, ScriptError
from module.logger import logger

Expand Down Expand Up @@ -328,7 +328,7 @@ def init():
self.install_uiautomator2()
if self._minitouch_port:
self.adb_forward_remove(f'tcp:{self._minitouch_port}')
del_cached_property(self, 'minitouch_builder')
del_cached_property(self, '_minitouch_builder')
# MinitouchOccupiedError: Timeout when connecting to minitouch
except MinitouchOccupiedError as e:
logger.error(e)
Expand All @@ -337,7 +337,7 @@ def init():
self.restart_atx()
if self._minitouch_port:
self.adb_forward_remove(f'tcp:{self._minitouch_port}')
del_cached_property(self, 'minitouch_builder')
del_cached_property(self, '_minitouch_builder')
# AdbError
except AdbError as e:
if handle_adb_error(e):
Expand All @@ -349,7 +349,7 @@ def init():
logger.error(e)

def init():
del_cached_property(self, 'minitouch_builder')
del_cached_property(self, '_minitouch_builder')
# Unknown, probably a trucked image
except Exception as e:
logger.exception(e)
Expand All @@ -370,12 +370,38 @@ class Minitouch(Connection):
_minitouch_ws: websockets.WebSocketClientProtocol
max_x: int
max_y: int
_minitouch_init_thread = None

@cached_property
def minitouch_builder(self):
def _minitouch_builder(self):
self.minitouch_init()
return CommandBuilder(self)

@property
def minitouch_builder(self):
# Wait init thread
if self._minitouch_init_thread is not None:
self._minitouch_init_thread.join()
del self._minitouch_init_thread
self._minitouch_init_thread = None

return self._minitouch_builder

def early_minitouch_init(self):
"""
Start a thread to init minitouch connection while the Alas instance just starting to take screenshots
This would speed up the first click 0.05s.
"""
if has_cached_property(self, '_minitouch_builder'):
return

def early_minitouch_init_func():
_ = self._minitouch_builder

thread = threading.Thread(target=early_minitouch_init_func, daemon=True)
self._minitouch_init_thread = thread
thread.start()

@Config.when(DEVICE_OVER_HTTP=False)
def minitouch_init(self):
logger.hr('MiniTouch init')
Expand Down

0 comments on commit 6ec8df8

Please sign in to comment.