Skip to content

Commit 2037172

Browse files
committed
feat: 允许上传文件作为待匹配图片
1 parent 5f376d4 commit 2037172

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/MaaDebugger/maafw/__init__.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
11
import re
2+
import io
23
from pathlib import Path
34
from typing import Callable, List, Optional, Tuple, Union
45

56
from asyncify import asyncify
67
from PIL import Image
7-
from maa.controller import AdbController, Win32Controller
8+
from maa.controller import AdbController, Win32Controller, CustomController
89
from maa.context import Context, ContextEventSink
910
from maa.tasker import Tasker, RecognitionDetail
1011
from maa.resource import Resource, ResourceEventSink
1112
from maa.toolkit import Toolkit, AdbDevice, DesktopWindow
1213
from maa.agent_client import AgentClient
1314
from maa.library import Library
1415
from maa.event_sink import NotificationType
16+
import numpy as np
1517

1618
from ..utils import cvmat_to_image
1719

1820

21+
class MyCustomController(CustomController):
22+
def __init__(self, img_bytes: bytes):
23+
super().__init__()
24+
25+
img = Image.open(io.BytesIO(img_bytes))
26+
self.ndarray = np.array(img)
27+
28+
def connect(self) -> bool:
29+
return True
30+
31+
def request_uuid(self) -> str:
32+
return "0"
33+
34+
def screencap(self) -> np.ndarray:
35+
return self.ndarray
36+
37+
1938
class MaaFW:
2039

2140
resource: Optional[Resource]
22-
controller: Union[AdbController, Win32Controller, None]
41+
controller: Union[AdbController, Win32Controller, CustomController, None]
2342
tasker: Optional[Tasker]
2443
agent: Optional[AgentClient]
2544
context_event_sink: Optional[ContextEventSink]
@@ -89,6 +108,12 @@ def connect_win32hwnd(
89108

90109
return True, None
91110

111+
def connect_custom_controller(self, img_bytes) -> Tuple[bool, Optional[str]]:
112+
self.controller = MyCustomController(img_bytes)
113+
self.controller.post_connection().wait()
114+
115+
return True, None
116+
92117
@asyncify
93118
def load_resource(self, dir: List[Path]) -> Tuple[bool, Optional[str]]:
94119
if not self.resource:
@@ -149,7 +174,20 @@ def run_task(
149174
if not AgentClient().register_sink(self.resource, self.controller, self.tasker):
150175
return False, "Failed to register Agent sink."
151176

152-
return self.tasker.post_task(entry, pipeline_override).wait().succeeded, None
177+
if isinstance(self.controller, CustomController):
178+
# disable action
179+
pipeline_override.update(
180+
{entry: {"action": {"type": "DoNothing"}, "next": []}}
181+
)
182+
return (
183+
self.tasker.post_task(entry, pipeline_override).wait().succeeded,
184+
None,
185+
)
186+
else:
187+
return (
188+
self.tasker.post_task(entry, pipeline_override).wait().succeeded,
189+
None,
190+
)
153191

154192
@asyncify
155193
def stop_task(self) -> None:
@@ -176,6 +214,9 @@ def click(self, x, y) -> bool:
176214
if not self.controller:
177215
return False
178216

217+
if isinstance(self.controller, CustomController):
218+
return False
219+
179220
return self.controller.post_click(x, y).wait().succeeded
180221

181222
@asyncify

src/MaaDebugger/webpage/index_page/master_control.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def connect_control():
3939
with ui.tabs() as tabs:
4040
adb = ui.tab("Adb")
4141
win32 = ui.tab("Win32")
42+
custom = ui.tab("Custom")
4243

4344
tab_panels = ui.tab_panels(tabs, value="Adb").bind_value(STORAGE, "controller_type")
4445
with tab_panels:
@@ -49,6 +50,10 @@ def connect_control():
4950
with ui.row(align_items="center").classes("w-full"):
5051
connect_win32_control()
5152

53+
with ui.tab_panel(custom):
54+
with ui.row(align_items="center").classes("w-full"):
55+
connect_custom_control()
56+
5257
os_type = system.get_os_type()
5358
if os_type != system.OSTypeEnum.Windows:
5459
win32.disable()
@@ -301,6 +306,22 @@ def on_change_hwnd_select(value: Optional[str]):
301306
hwnd_input.value = value
302307

303308

309+
def connect_custom_control():
310+
def on_upload(e):
311+
GlobalStatus.ctrl_connecting = Status.RUNNING
312+
try:
313+
maafw.connect_custom_controller(e.content.read())
314+
except:
315+
GlobalStatus.ctrl_connecting = Status.FAILED
316+
ui.notify("Failed to load image.", position="bottom-right", type="negative")
317+
return
318+
319+
GlobalStatus.ctrl_connecting = Status.SUCCEEDED
320+
321+
StatusIndicator(GlobalStatus, "ctrl_connecting")
322+
ui.upload(auto_upload=True, on_upload=lambda e: on_upload(e))
323+
324+
304325
def screenshot_control():
305326
with (
306327
ui.row()

0 commit comments

Comments
 (0)