-
Notifications
You must be signed in to change notification settings - Fork 123
/
instance.py
663 lines (533 loc) · 18.8 KB
/
instance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
import asyncio
import ctypes
import json
from pathlib import Path
from typing import Any, Dict, Optional, Union
from .buffer import ImageListBuffer, RectBuffer, StringBuffer, ImageBuffer
from .callback_agent import Callback, CallbackAgent
from .controller import Controller
from .custom_action import CustomAction
from .custom_recognizer import CustomRecognizer
from .define import *
from .future import Future, MaaStatusEnum
from .library import Library
from .resource import Resource
@dataclass
class RecognitionDetail:
reco_id: int
name: str
hit_box: Optional[Rect]
detail: Dict
raw: numpy.ndarray # Instance.set_debug_message(true) to get this
draws: List[numpy.ndarray] # Instance.set_debug_message(true) to get this
@dataclass
class NodeDetail:
node_id: int
name: str
recognition: RecognitionDetail
run_completed: bool
@dataclass
class TaskDetail:
task_id: int
entry: str
node_details: List[NodeDetail]
class TaskFuture(Future):
def __init__(self, maaid: MaaId, status_func, set_param_func, query_detail_func):
super().__init__(maaid, status_func)
self._set_param_func = set_param_func
self._query_detail_func = query_detail_func
def set_param(self, param: Any) -> bool:
"""
Set the param of the task.
:param param: The param of the task.
:return: True if the param was successfully set, False otherwise.
"""
return self._set_param_func(self._maaid, param)
def get(self) -> Optional[TaskDetail]:
"""
get the detail of the task.
:return: The detail of the task.
"""
return self._query_detail_func(self._maaid)
class Instance:
_callback_agent: CallbackAgent
_handle: MaaInstanceHandle
def __init__(self, callback: Optional[Callback] = None, callback_arg: Any = None):
"""
Create a new MAA task manager.
:param callback: The callback function.
:param callback_arg: The callback argument.
"""
if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
)
self._set_api_properties()
self._callback_agent = CallbackAgent(callback, callback_arg)
self._handle = Library.framework.MaaCreate(
self._callback_agent.c_callback, self._callback_agent.c_callback_arg
)
if not self._handle:
raise RuntimeError("Failed to create resource.")
def __del__(self):
"""
Destroy the MAA task manager.
"""
if self._handle:
Library.framework.MaaDestroy(self._handle)
def bind(self, resource: Resource, controller: Controller) -> bool:
"""
Bind the resource and controller to the instance.
:param resource: The resource to bind.
:param controller: The controller to bind.
:return: True if the resource and controller were successfully bound, False otherwise.
"""
return bool(
Library.framework.MaaBindResource(self._handle, resource._handle)
) and bool(
Library.framework.MaaBindController(self._handle, controller._handle)
)
@property
def inited(self) -> bool:
"""
Check if the instance is inited.
:return: True if the instance is inited, False otherwise.
"""
return bool(Library.framework.MaaInited(self._handle))
async def run_task(self, task_type: str, param: Dict = {}) -> Optional[TaskDetail]:
"""
Async run a task.
:param task_type: The name of the task.
:param param: The param of the task.
:return: details of the task.
"""
future = self.post_task(task_type, param)
await future.wait()
return future.get()
async def run_recognition(
self, task_type: str, param: Dict = {}
) -> Optional[RecognitionDetail]:
"""
Async run a recognition.
:param task_type: The name of the recognition.
:param param: The param of the recognition.
:return: details of the recognition.
"""
future = self.post_recognition(task_type, param)
await future.wait()
detail = future.get()
if not detail or not detail.node_details:
return None
return detail.node_details[0].recognition
async def run_action(
self, task_type: str, param: Dict = {}
) -> Optional[NodeDetail]:
"""
Async run a action.
:param task_type: The name of the action.
:param param: The param of the action.
:return: details of the node.
"""
future = self.post_action(task_type, param)
await future.wait()
detail = future.get()
if not detail or not detail.node_details:
return None
return detail.node_details[0]
def post_task(self, task_type: str, param: Any = {}) -> TaskFuture:
"""
Post a task to the instance. (run in background)
:param task_type: The name of the task.
:param param: The param of the task.
:return: The id of the posted task.
"""
maaid = Library.framework.MaaPostTask(
self._handle,
task_type.encode("utf-8"),
json.dumps(param, ensure_ascii=False).encode("utf-8"),
)
return TaskFuture(
maaid, self._status, self._set_task_param, self.query_task_detail
)
def post_recognition(self, task_type: str, param: Any = {}) -> TaskFuture:
"""
Post a recognition to the instance. (run in background)
:param task_type: The name of the recognition.
:param param: The param of the recognition.
:return: The id of the posted recognition.
"""
maaid = Library.framework.MaaPostRecognition(
self._handle,
task_type.encode("utf-8"),
json.dumps(param, ensure_ascii=False).encode("utf-8"),
)
return TaskFuture(
maaid, self._status, self._set_task_param, self.query_task_detail
)
def post_action(self, task_type: str, param: Any = {}) -> TaskFuture:
"""
Post a action to the instance. (run in background)
:param task_type: The name of the action.
:param param: The param of the action.
:return: The id of the posted action.
"""
maaid = Library.framework.MaaPostAction(
self._handle,
task_type.encode("utf-8"),
json.dumps(param, ensure_ascii=False).encode("utf-8"),
)
return TaskFuture(
maaid, self._status, self._set_task_param, self.query_task_detail
)
async def wait_all(self):
"""
Wait for all tasks to complete.
"""
while self.running():
await asyncio.sleep(0)
def running(self) -> bool:
"""
Is running
:return: True if running, False otherwise.
"""
return bool(Library.framework.MaaRunning(self._handle))
async def stop(self) -> bool:
"""
Async stop all tasks.
:return: True if all tasks were successfully stopped, False otherwise.
"""
return await self.post_stop().wait()
def post_stop(self) -> Future:
"""
Stop all tasks.
:return: True if all tasks were successfully stopped, False otherwise.
"""
Library.framework.MaaPostStop(self._handle)
return Future(0, self._stop_status)
def register_recognizer(self, name: str, recognizer: CustomRecognizer) -> bool:
"""
Register a custom recognizer.
:param name: The name of the custom recognizer.
:param recognizer: The custom recognizer.
:return: True if the custom recognizer was successfully registered, False otherwise.
"""
return bool(
Library.framework.MaaRegisterCustomRecognizer(
self._handle,
name.encode("utf-8"),
recognizer.c_handle,
recognizer.c_arg,
)
)
def register_action(self, name: str, action: CustomAction) -> bool:
"""
Register a custom action.
:param name: The name of the custom action.
:param action: The custom action.
:return: True if the custom action was successfully registered, False otherwise.
"""
return bool(
Library.framework.MaaRegisterCustomAction(
self._handle,
name.encode("utf-8"),
action.c_handle,
action.c_arg,
)
)
@property
def c_handle(self) -> ctypes.c_void_p:
"""
Get the C handle of the instance.
:return: The C handle of the instance.
"""
return self._handle
def _status(self, id: int) -> ctypes.c_int32:
return Library.framework.MaaTaskStatus(self._handle, id)
def _stop_status(self, id: int) -> ctypes.c_int32:
return MaaStatusEnum.success if not self.running() else MaaStatusEnum.running
def _set_task_param(self, id: int, param: Dict) -> bool:
return bool(
Library.framework.MaaSetTaskParam(
self._handle, id, json.dumps(param, ensure_ascii=False).encode("utf-8")
)
)
@staticmethod
def set_log_dir(path: Union[Path, str]) -> bool:
"""
Set the log directory.
:param path: The path to the log directory.
"""
return bool(
Library.framework.MaaSetGlobalOption(
MaaGlobalOptionEnum.LogDir,
str(path).encode("utf-8"),
len(path),
)
)
@staticmethod
def set_save_draw(save_draw: bool) -> bool:
"""
Set whether to save draw.
:param save_draw: Whether to save draw.
"""
cbool = ctypes.c_bool(save_draw)
return bool(
Library.framework.MaaSetGlobalOption(
MaaGlobalOptionEnum.SaveDraw,
ctypes.pointer(cbool),
ctypes.sizeof(ctypes.c_bool),
)
)
@staticmethod
def set_recording(recording: bool) -> bool:
"""
Set whether to dump all screenshots and actions.
:param recording: Whether to dump all screenshots and actions.
"""
cbool = ctypes.c_bool(recording)
return bool(
Library.framework.MaaSetGlobalOption(
MaaGlobalOptionEnum.Recording,
ctypes.pointer(cbool),
ctypes.sizeof(ctypes.c_bool),
)
)
@staticmethod
def set_stdout_level(level: MaaLoggingLevelEunm) -> bool:
"""
Set the level of log output to stdout.
:param level: The level of log output to stdout.
"""
cbool = ctypes.c_bool(level)
return bool(
Library.framework.MaaSetGlobalOption(
MaaGlobalOptionEnum.StdoutLevel,
ctypes.pointer(cbool),
ctypes.sizeof(MaaLoggingLevel),
)
)
@staticmethod
def set_show_hit_draw(show_hit_draw: bool) -> bool:
"""
Set whether to show hit draw.
:param show_hit_draw: Whether to show hit draw.
"""
cbool = ctypes.c_bool(show_hit_draw)
return bool(
Library.framework.MaaSetGlobalOption(
MaaGlobalOptionEnum.ShowHitDraw,
ctypes.pointer(cbool),
ctypes.sizeof(ctypes.c_bool),
)
)
@staticmethod
def set_debug_message(debug_message: bool) -> bool:
"""
Set whether to callback debug message.
:param debug_message: Whether to callback debug message.
"""
cbool = ctypes.c_bool(debug_message)
return bool(
Library.framework.MaaSetGlobalOption(
MaaGlobalOptionEnum.DebugMessage,
ctypes.pointer(cbool),
ctypes.sizeof(ctypes.c_bool),
)
)
@staticmethod
def query_recognition_detail(reco_id: int) -> Optional[RecognitionDetail]:
"""
Query recognition detail.
:param reco_id: The recognition id.
:return: The recognition detail.
"""
hit = MaaBool()
name = StringBuffer()
hit_box = RectBuffer()
detail_json = StringBuffer()
raw = ImageBuffer()
draws = ImageListBuffer()
ret = bool(
Library.framework.MaaQueryRecognitionDetail(
reco_id,
name.c_handle,
ctypes.pointer(hit),
hit_box.c_handle,
detail_json.c_handle,
raw.c_handle,
draws.c_handle,
)
)
if not ret:
return None
return RecognitionDetail(
reco_id=reco_id,
name=name.get(),
hit_box=bool(hit) and hit_box.get() or None,
detail=json.loads(detail_json.get()),
raw=raw.get(),
draws=draws.get(),
)
@staticmethod
def query_node_detail(node_id: int) -> Optional[NodeDetail]:
"""
Query running detail.
:param node_id: The running id.
:return: The running detail.
"""
reco_id = MaaRecoId()
name = StringBuffer()
run_completed = MaaBool()
ret = bool(
Library.framework.MaaQueryNodeDetail(
node_id,
name.c_handle,
ctypes.pointer(reco_id),
ctypes.pointer(run_completed),
)
)
if not ret:
return None
recognition = Instance.query_recognition_detail(reco_id.value)
if not recognition:
return None
return NodeDetail(
node_id=node_id,
name=name.get(),
recognition=recognition,
run_completed=bool(run_completed),
)
@staticmethod
def query_task_detail(task_id: int) -> Optional[TaskDetail]:
"""
Query task detail.
:param task_id: The task id.
:return: The task detail.
"""
size = MaaSize()
ret = bool(
Library.framework.MaaQueryTaskDetail(
task_id, None, None, ctypes.pointer(size)
)
)
if not ret:
return None
entry = StringBuffer()
node_id_list = (MaaNodeId * size.value)()
ret = bool(
Library.framework.MaaQueryTaskDetail(
task_id, entry.c_handle, node_id_list, ctypes.pointer(size)
)
)
if not ret:
return None
node_details = []
for i in range(size.value):
detail = Instance.query_node_detail(node_id_list[i])
node_details.append(detail)
return TaskDetail(task_id=task_id, entry=entry.get(), node_details=node_details)
_api_properties_initialized: bool = False
@staticmethod
def _set_api_properties():
"""
Set the API properties.
"""
if Instance._api_properties_initialized:
return
Instance._api_properties_initialized = True
Library.framework.MaaCreate.restype = MaaInstanceHandle
Library.framework.MaaCreate.argtypes = [
MaaInstanceCallback,
MaaCallbackTransparentArg,
]
Library.framework.MaaDestroy.restype = None
Library.framework.MaaDestroy.argtypes = [MaaInstanceHandle]
Library.framework.MaaBindResource.restype = MaaBool
Library.framework.MaaBindResource.argtypes = [
MaaInstanceHandle,
MaaResourceHandle,
]
Library.framework.MaaBindController.restype = MaaBool
Library.framework.MaaBindController.argtypes = [
MaaInstanceHandle,
MaaControllerHandle,
]
Library.framework.MaaInited.restype = MaaBool
Library.framework.MaaInited.argtypes = [MaaInstanceHandle]
Library.framework.MaaPostTask.restype = MaaId
Library.framework.MaaPostTask.argtypes = [
MaaInstanceHandle,
MaaStringView,
MaaStringView,
]
Library.framework.MaaPostRecognition.restype = MaaId
Library.framework.MaaPostRecognition.argtypes = [
MaaInstanceHandle,
MaaStringView,
MaaStringView,
]
Library.framework.MaaPostAction.restype = MaaId
Library.framework.MaaPostAction.argtypes = [
MaaInstanceHandle,
MaaStringView,
MaaStringView,
]
Library.framework.MaaSetTaskParam.restype = MaaBool
Library.framework.MaaSetTaskParam.argtypes = [
MaaInstanceHandle,
MaaTaskId,
MaaStringView,
]
Library.framework.MaaTaskStatus.restype = MaaStatus
Library.framework.MaaTaskStatus.argtypes = [
MaaInstanceHandle,
MaaTaskId,
]
Library.framework.MaaRunning.restype = MaaBool
Library.framework.MaaRunning.argtypes = [MaaInstanceHandle]
Library.framework.MaaPostStop.restype = MaaBool
Library.framework.MaaPostStop.argtypes = [MaaInstanceHandle]
Library.framework.MaaRegisterCustomRecognizer.restype = MaaBool
Library.framework.MaaRegisterCustomRecognizer.argtypes = [
MaaInstanceHandle,
MaaStringView,
MaaCustomRecognizerHandle,
MaaTransparentArg,
]
Library.framework.MaaRegisterCustomAction.restype = MaaBool
Library.framework.MaaRegisterCustomAction.argtypes = [
MaaInstanceHandle,
MaaStringView,
MaaCustomActionHandle,
MaaTransparentArg,
]
Library.framework.MaaSetGlobalOption.restype = MaaBool
Library.framework.MaaSetGlobalOption.argtypes = [
MaaGlobalOption,
MaaOptionValue,
MaaOptionValueSize,
]
Library.framework.MaaQueryRecognitionDetail.restype = MaaBool
Library.framework.MaaQueryRecognitionDetail.argtypes = [
MaaRecoId,
MaaStringBufferHandle,
ctypes.POINTER(MaaBool),
MaaRectHandle,
MaaStringBufferHandle,
MaaImageBufferHandle,
MaaImageListBufferHandle,
]
Library.framework.MaaQueryNodeDetail.restype = MaaBool
Library.framework.MaaQueryNodeDetail.argtypes = [
MaaNodeId,
MaaStringBufferHandle,
ctypes.POINTER(MaaRecoId),
ctypes.POINTER(MaaBool),
]
Library.framework.MaaQueryTaskDetail.restype = MaaBool
Library.framework.MaaQueryTaskDetail.argtypes = [
MaaTaskId,
MaaStringBufferHandle,
ctypes.POINTER(MaaRecoId),
ctypes.POINTER(MaaSize),
]