|
| 1 | +from pathlib import Path |
| 2 | +from typing import List, Optional |
| 3 | +from asyncer import asyncify |
| 4 | +from PIL import Image |
| 5 | + |
| 6 | +import sys |
| 7 | + |
| 8 | +async def import_maa(binding_dir: Path, bin_dir: Path) -> bool: |
| 9 | + if not binding_dir.exists(): |
| 10 | + print("Binding directory does not exist") |
| 11 | + return False |
| 12 | + |
| 13 | + if not bin_dir.exists(): |
| 14 | + print("Bin dir does not exist") |
| 15 | + return False |
| 16 | + |
| 17 | + binding_dir = str(binding_dir) |
| 18 | + if binding_dir not in sys.path: |
| 19 | + sys.path.insert(0, binding_dir) |
| 20 | + |
| 21 | + try: |
| 22 | + from maa.library import Library |
| 23 | + from maa.toolkit import Toolkit |
| 24 | + except ModuleNotFoundError as err: |
| 25 | + print(err) |
| 26 | + return False |
| 27 | + |
| 28 | + version = await asyncify(Library.open)(bin_dir) |
| 29 | + if not version: |
| 30 | + print("Failed to open MaaFramework") |
| 31 | + return False |
| 32 | + |
| 33 | + print(f"Import MAA successfully, version: {version}") |
| 34 | + |
| 35 | + Toolkit.init_option("./") |
| 36 | + |
| 37 | + return True |
| 38 | + |
| 39 | + |
| 40 | +async def detect_adb() -> List["AdbDevice"]: |
| 41 | + from maa.toolkit import Toolkit |
| 42 | + |
| 43 | + return await Toolkit.adb_devices() |
| 44 | + |
| 45 | + |
| 46 | +resource = None |
| 47 | +controller = None |
| 48 | +instance = None |
| 49 | + |
| 50 | + |
| 51 | +async def connect_adb(path: Path, address: str) -> bool: |
| 52 | + global controller |
| 53 | + |
| 54 | + from maa.controller import AdbController |
| 55 | + |
| 56 | + controller = AdbController(path, address) |
| 57 | + connected = await controller.connect() |
| 58 | + if not connected: |
| 59 | + print(f"Failed to connect {path} {address}") |
| 60 | + return False |
| 61 | + |
| 62 | + return True |
| 63 | + |
| 64 | + |
| 65 | +async def load_resource(dir: Path) -> bool: |
| 66 | + global resource |
| 67 | + |
| 68 | + from maa.resource import Resource |
| 69 | + |
| 70 | + if not resource: |
| 71 | + resource = Resource() |
| 72 | + |
| 73 | + return resource.clear() and await resource.load(dir) |
| 74 | + |
| 75 | + |
| 76 | +async def run_task(entry: str, param: dict = {}) -> bool: |
| 77 | + global controller, resource, instance |
| 78 | + |
| 79 | + from maa.instance import Instance |
| 80 | + |
| 81 | + if not instance: |
| 82 | + instance = Instance() |
| 83 | + |
| 84 | + instance.bind(resource, controller) |
| 85 | + if not instance.inited: |
| 86 | + print("Failed to init MaaFramework instance") |
| 87 | + return False |
| 88 | + |
| 89 | + return await instance.run_task(entry, param) |
| 90 | + |
| 91 | + |
| 92 | +async def stop_task(): |
| 93 | + global instance |
| 94 | + |
| 95 | + if not instance: |
| 96 | + return |
| 97 | + |
| 98 | + await instance.stop() |
| 99 | + |
| 100 | + |
| 101 | +async def screencap() -> Optional[Image]: |
| 102 | + global controller |
| 103 | + if not controller: |
| 104 | + return None |
| 105 | + |
| 106 | + im = await controller.screencap() |
| 107 | + if im is None: |
| 108 | + return None |
| 109 | + |
| 110 | + pil = Image.fromarray(im) |
| 111 | + b, g, r = pil.split() |
| 112 | + return Image.merge("RGB", (r, g, b)) |
| 113 | + |
| 114 | + |
| 115 | +async def click(x, y) -> None: |
| 116 | + global controller |
| 117 | + if not controller: |
| 118 | + return None |
| 119 | + |
| 120 | + await controller.click(x, y) |
0 commit comments