Skip to content

Commit cd5b98f

Browse files
authored
refactor: 重构基础控件 (#1)
1 parent c6a6e19 commit cd5b98f

File tree

8 files changed

+454
-126
lines changed

8 files changed

+454
-126
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,7 @@ cython_debug/
161161

162162
# nicegui
163163
.nicegui/
164+
165+
# maafw
166+
config
167+
debug

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
from source.webpage import index
44

5-
ui.run(title="Maa Debugger", storage_secret="maadbg")
5+
ui.run(title="Maa Debugger", storage_secret="maadbg")#, root_path="/proxy/8080")

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
### from MaaFw
12
numpy
23
Pillow
34

5+
### from MaaDebugger
46
nicegui
7+
asyncer

source/control/runner.py

Lines changed: 0 additions & 91 deletions
This file was deleted.

source/interaction/maafw.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import asyncio
2+
import threading
3+
4+
import source.interaction.maafw as maafw
5+
6+
7+
class Screenshotter(threading.Thread):
8+
def __init__(self):
9+
super().__init__()
10+
self.source = None
11+
self.active = False
12+
13+
def __del__(self):
14+
self.active = False
15+
self.source = None
16+
17+
def run(self):
18+
while self.active:
19+
im = asyncio.run(maafw.screencap())
20+
if not im:
21+
continue
22+
23+
self.source = im
24+
25+
def start(self):
26+
self.active = True
27+
super().start()
28+
29+
def stop(self):
30+
self.active = False
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from nicegui import ui
2+
from enum import Enum, auto
3+
4+
5+
class Status(Enum):
6+
PENDING = auto()
7+
RUNNING = auto()
8+
SUCCESS = auto()
9+
FAILURE = auto()
10+
11+
12+
class StatusIndicator:
13+
def __init__(self, target_object, target_name):
14+
self._label = ui.label().bind_text_from(
15+
target_object,
16+
target_name,
17+
backward=lambda s: StatusIndicator._text_backward(s),
18+
)
19+
20+
def label(self):
21+
return self._label
22+
23+
@staticmethod
24+
def _text_backward(status: Status) -> str:
25+
match status:
26+
case Status.PENDING:
27+
return "🟡"
28+
case Status.RUNNING:
29+
return "👀"
30+
case Status.SUCCESS:
31+
return "✅"
32+
case Status.FAILURE:
33+
return "❌"

0 commit comments

Comments
 (0)