MaaFw 4.5.5__py3-none-manylinux2014_x86_64.whl → 5.4.0__py3-none-manylinux2014_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of MaaFw might be problematic. Click here for more details.

maa/define.py CHANGED
@@ -16,7 +16,9 @@ MaaCtrlId = MaaId
16
16
  MaaResId = MaaId
17
17
  MaaTaskId = MaaId
18
18
  MaaRecoId = MaaId
19
+ MaaActId = MaaId
19
20
  MaaNodeId = MaaId
21
+ MaaSinkId = MaaId
20
22
  MaaInvalidId = MaaId(0)
21
23
 
22
24
  MaaStringBufferHandle = ctypes.c_void_p
@@ -89,6 +91,23 @@ class MaaGlobalOptionEnum(IntEnum):
89
91
  # value: bool, eg: true; val_size: sizeof(bool)
90
92
  DebugMode = 6
91
93
 
94
+ # Whether to save screenshot on error
95
+ #
96
+ # value: bool, eg: true; val_size: sizeof(bool)
97
+ SaveOnError = 7
98
+
99
+ # Image quality for draw images
100
+ #
101
+ # value: int, eg: 85; val_size: sizeof(int)
102
+ # default value is 85, range: [0, 100]
103
+ DrawQuality = 8
104
+
105
+ # Recognition image cache limit
106
+ #
107
+ # value: size_t, eg: 4096; val_size: sizeof(size_t)
108
+ # default value is 4096
109
+ RecoImageCacheLimit = 9
110
+
92
111
 
93
112
  class MaaCtrlOptionEnum(IntEnum):
94
113
  Invalid = 0
@@ -165,8 +184,25 @@ MaaAdbScreencapMethod = ctypes.c_uint64
165
184
 
166
185
  class MaaAdbScreencapMethodEnum(IntEnum):
167
186
  """
168
- Use bitwise OR to set the method you need
169
- MaaFramework will test their speed and use the fastest one.
187
+ Adb screencap method flags.
188
+
189
+ Use bitwise OR to set the methods you need.
190
+ MaaFramework will test all provided methods and use the fastest available one.
191
+
192
+ Default: All methods except RawByNetcat, MinicapDirect, MinicapStream
193
+
194
+ Note: MinicapDirect and MinicapStream use lossy JPEG encoding, which may
195
+ significantly reduce template matching accuracy. Not recommended.
196
+
197
+ | Method | Speed | Compatibility | Encoding | Notes |
198
+ |-----------------------|------------|---------------|----------|-----------------------------------|
199
+ | EncodeToFileAndPull | Slow | High | Lossless | |
200
+ | Encode | Slow | High | Lossless | |
201
+ | RawWithGzip | Medium | High | Lossless | |
202
+ | RawByNetcat | Fast | Low | Lossless | |
203
+ | MinicapDirect | Fast | Low | Lossy | |
204
+ | MinicapStream | Very Fast | Low | Lossy | |
205
+ | EmulatorExtras | Very Fast | Low | Lossless | Emulators only: MuMu 12, LDPlayer 9 |
170
206
  """
171
207
 
172
208
  Null = 0
@@ -188,9 +224,21 @@ MaaAdbInputMethod = ctypes.c_uint64
188
224
 
189
225
  class MaaAdbInputMethodEnum(IntEnum):
190
226
  """
191
- Use bitwise OR to set the method you need
192
- MaaFramework will select the available ones according to priority.
193
- The priority is: EmulatorExtras > Maatouch > MinitouchAndAdbKey > AdbShell
227
+ Adb input method flags.
228
+
229
+ Use bitwise OR to set the methods you need.
230
+ MaaFramework will select the first available method according to priority.
231
+
232
+ Priority (high to low): EmulatorExtras > Maatouch > MinitouchAndAdbKey > AdbShell
233
+
234
+ Default: All methods except EmulatorExtras
235
+
236
+ | Method | Speed | Compatibility | Notes |
237
+ |----------------------|-------|---------------|---------------------------------------|
238
+ | AdbShell | Slow | High | |
239
+ | MinitouchAndAdbKey | Fast | Medium | Key press still uses AdbShell |
240
+ | Maatouch | Fast | Medium | |
241
+ | EmulatorExtras | Fast | Low | Emulators only: MuMu 12 |
194
242
  """
195
243
 
196
244
  Null = 0
@@ -207,29 +255,176 @@ class MaaAdbInputMethodEnum(IntEnum):
207
255
  MaaWin32ScreencapMethod = ctypes.c_uint64
208
256
 
209
257
 
210
- # No bitwise OR, just set it
211
258
  class MaaWin32ScreencapMethodEnum(IntEnum):
259
+ """
260
+ Win32 screencap method.
261
+
262
+ No bitwise OR, select ONE method only.
263
+
264
+ No default value. Client should choose one as default.
265
+
266
+ Different applications use different rendering methods, there is no universal solution.
267
+
268
+ | Method | Speed | Compatibility | Require Admin | Background Support | Notes |
269
+ |-------------------------|-----------|---------------|---------------|--------------------|----------------------------------|
270
+ | GDI | Fast | Medium | No | No | |
271
+ | FramePool | Very Fast | Medium | No | Yes | Requires Windows 10 1903+ |
272
+ | DXGI_DesktopDup | Very Fast | Low | No | No | Desktop duplication (full screen)|
273
+ | DXGI_DesktopDup_Window | Very Fast | Low | No | No | Desktop duplication then crop |
274
+ | PrintWindow | Medium | Medium | No | Yes | |
275
+ | ScreenDC | Fast | High | No | No | |
276
+
277
+ Note: When a window is minimized on Windows, all screencap methods will fail.
278
+ Avoid minimizing the target window.
279
+ """
280
+
212
281
  Null = 0
213
282
 
214
283
  GDI = 1
215
284
  FramePool = 1 << 1
216
285
  DXGI_DesktopDup = 1 << 2
286
+ DXGI_DesktopDup_Window = 1 << 3
287
+ PrintWindow = 1 << 4
288
+ ScreenDC = 1 << 5
217
289
 
218
290
 
219
291
  MaaWin32InputMethod = ctypes.c_uint64
220
292
 
221
293
 
222
- # No bitwise OR, just set it
223
294
  class MaaWin32InputMethodEnum(IntEnum):
295
+ """
296
+ Win32 input method.
297
+
298
+ No bitwise OR, select ONE method only.
299
+
300
+ No default value. Client should choose one as default.
301
+
302
+ Different applications process input differently, there is no universal solution.
303
+
304
+ | Method | Compatibility | Require Admin | Seize Mouse | Background Support | Notes |
305
+ |------------------------------|---------------|---------------|--------------|--------------------|-------------------------------------------------------------|
306
+ | Seize | High | No | Yes | No | |
307
+ | SendMessage | Medium | Maybe | No | Yes | |
308
+ | PostMessage | Medium | Maybe | No | Yes | |
309
+ | LegacyEvent | Low | No | Yes | No | |
310
+ | PostThreadMessage | Low | Maybe | No | Yes | |
311
+ | SendMessageWithCursorPos | Medium | Maybe | Briefly | Yes | Designed for apps that check real cursor position |
312
+ | PostMessageWithCursorPos | Medium | Maybe | Briefly | Yes | Designed for apps that check real cursor position |
313
+
314
+ Note:
315
+ - Admin rights mainly depend on the target application's privilege level.
316
+ If the target runs as admin, MaaFramework should also run as admin for compatibility.
317
+ - "WithCursorPos" methods briefly move the cursor to target position, send message,
318
+ then restore cursor position. This "briefly" seizes the mouse but won't block user operations.
319
+ """
320
+
224
321
  Null = 0
225
322
 
226
323
  Seize = 1
227
324
  SendMessage = 1 << 1
325
+ PostMessage = 1 << 2
326
+ LegacyEvent = 1 << 3
327
+ PostThreadMessage = 1 << 4
328
+ SendMessageWithCursorPos = 1 << 5
329
+ PostMessageWithCursorPos = 1 << 6
228
330
 
229
331
 
230
332
  # No bitwise OR, just set it
231
333
  MaaDbgControllerType = ctypes.c_uint64
232
334
 
335
+ # No bitwise OR, just set it
336
+ MaaGamepadType = ctypes.c_uint64
337
+
338
+
339
+ class MaaGamepadTypeEnum(IntEnum):
340
+ """
341
+ Virtual gamepad type for GamepadController (Windows only).
342
+
343
+ No bitwise OR, select ONE type only.
344
+
345
+ Requires ViGEm Bus Driver to be installed.
346
+
347
+ | Type | Description |
348
+ |-------------|---------------------------------------|
349
+ | Xbox360 | Microsoft Xbox 360 Controller (wired) |
350
+ | DualShock4 | Sony DualShock 4 Controller (wired) |
351
+ """
352
+
353
+ Xbox360 = 0
354
+ DualShock4 = 1
355
+
356
+
357
+ class MaaGamepadButtonEnum(IntEnum):
358
+ """
359
+ Gamepad button flags (XUSB protocol values).
360
+
361
+ Use bitwise OR to combine multiple buttons.
362
+ DS4 face buttons are aliases to Xbox face buttons.
363
+ """
364
+
365
+ # D-pad
366
+ DPAD_UP = 0x0001
367
+ DPAD_DOWN = 0x0002
368
+ DPAD_LEFT = 0x0004
369
+ DPAD_RIGHT = 0x0008
370
+
371
+ # Control buttons
372
+ START = 0x0010
373
+ BACK = 0x0020
374
+ LEFT_THUMB = 0x0040 # L3
375
+ RIGHT_THUMB = 0x0080 # R3
376
+
377
+ # Shoulder buttons
378
+ LB = 0x0100 # Left Bumper / L1
379
+ RB = 0x0200 # Right Bumper / R1
380
+
381
+ # Guide button
382
+ GUIDE = 0x0400
383
+
384
+ # Face buttons (Xbox layout)
385
+ A = 0x1000
386
+ B = 0x2000
387
+ X = 0x4000
388
+ Y = 0x8000
389
+
390
+ # DS4 face buttons (aliases to Xbox buttons)
391
+ CROSS = A
392
+ CIRCLE = B
393
+ SQUARE = X
394
+ TRIANGLE = Y
395
+ L1 = LB
396
+ R1 = RB
397
+ L3 = LEFT_THUMB
398
+ R3 = RIGHT_THUMB
399
+ OPTIONS = START
400
+ SHARE = BACK
401
+
402
+ # DS4 special buttons (unique values)
403
+ PS = 0x10000
404
+ TOUCHPAD = 0x20000
405
+
406
+
407
+ class MaaGamepadContactEnum(IntEnum):
408
+ """
409
+ Gamepad contact (analog stick or trigger) mapping for touch_down/touch_move/touch_up.
410
+ """
411
+
412
+ LEFT_STICK = 0 # x: -32768~32767, y: -32768~32767
413
+ RIGHT_STICK = 1 # x: -32768~32767, y: -32768~32767
414
+ LEFT_TRIGGER = 2 # pressure: 0~255
415
+ RIGHT_TRIGGER = 3 # pressure: 0~255
416
+
417
+
418
+ MaaControllerFeature = ctypes.c_uint64
419
+
420
+
421
+ # Use bitwise OR to set the features you need
422
+ class MaaControllerFeatureEnum(IntEnum):
423
+ Null = 0
424
+
425
+ UseMouseDownAndUpInsteadOfClick = 1
426
+ UseKeyboardDownAndUpInsteadOfClick = 1 << 1
427
+
233
428
 
234
429
  class MaaDbgControllerTypeEnum(IntEnum):
235
430
  Null = 0
@@ -240,8 +435,8 @@ class MaaDbgControllerTypeEnum(IntEnum):
240
435
 
241
436
  FUNCTYPE = ctypes.WINFUNCTYPE if (platform.system() == "Windows") else ctypes.CFUNCTYPE
242
437
 
243
- MaaNotificationCallback = FUNCTYPE(
244
- None, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_void_p
438
+ MaaEventCallback = FUNCTYPE(
439
+ None, ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_void_p
245
440
  )
246
441
 
247
442
  MaaCustomRecognitionCallback = FUNCTYPE(
@@ -284,11 +479,19 @@ class MaaCustomControllerCallbacks(ctypes.Structure):
284
479
  MaaBool,
285
480
  ctypes.c_void_p,
286
481
  )
482
+ ConnectedFunc = FUNCTYPE(
483
+ MaaBool,
484
+ ctypes.c_void_p,
485
+ )
287
486
  RequestUuidFunc = FUNCTYPE(
288
487
  MaaBool,
289
488
  ctypes.c_void_p,
290
489
  MaaStringBufferHandle,
291
490
  )
491
+ GetFeaturesFunc = FUNCTYPE(
492
+ MaaControllerFeature,
493
+ ctypes.c_void_p,
494
+ )
292
495
  StartAppFunc = FUNCTYPE(
293
496
  MaaBool,
294
497
  ctypes.c_char_p,
@@ -360,9 +563,17 @@ class MaaCustomControllerCallbacks(ctypes.Structure):
360
563
  ctypes.c_int32,
361
564
  ctypes.c_void_p,
362
565
  )
566
+ ScrollFunc = FUNCTYPE(
567
+ MaaBool,
568
+ ctypes.c_int32,
569
+ ctypes.c_int32,
570
+ ctypes.c_void_p,
571
+ )
363
572
  _fields_ = [
364
573
  ("connect", ConnectFunc),
574
+ ("connected", ConnectedFunc),
365
575
  ("request_uuid", RequestUuidFunc),
576
+ ("get_features", GetFeaturesFunc),
366
577
  ("start_app", StartAppFunc),
367
578
  ("stop_app", StopAppFunc),
368
579
  ("screencap", ScreencapFunc),
@@ -375,6 +586,7 @@ class MaaCustomControllerCallbacks(ctypes.Structure):
375
586
  ("input_text", InputTextFunc),
376
587
  ("key_down", KeyDownFunc),
377
588
  ("key_up", KeyUpFunc),
589
+ ("scroll", ScrollFunc),
378
590
  ]
379
591
 
380
592
 
@@ -410,6 +622,41 @@ class Status:
410
622
  return self._status == MaaStatusEnum.running
411
623
 
412
624
 
625
+ @dataclass
626
+ class Point:
627
+ x: int = 0
628
+ y: int = 0
629
+
630
+ def __add__(
631
+ self,
632
+ other: Union[
633
+ "Point",
634
+ Tuple[int, int],
635
+ List[int],
636
+ ],
637
+ ):
638
+ if (
639
+ isinstance(other, Point)
640
+ or isinstance(other, tuple)
641
+ or (isinstance(other, list) and len(other) == 2)
642
+ ):
643
+ x1, y1 = self
644
+ x2, y2 = other
645
+ return Point(
646
+ x1 + x2,
647
+ y1 + y2,
648
+ )
649
+
650
+ raise TypeError(f"Cannot add {type(other).__name__} to Point")
651
+
652
+ def __iter__(self):
653
+ yield self.x
654
+ yield self.y
655
+
656
+ def __getitem__(self, key):
657
+ return list(self)[key]
658
+
659
+
413
660
  @dataclass
414
661
  class Rect:
415
662
  x: int = 0
@@ -448,12 +695,15 @@ class Rect:
448
695
  yield self.h
449
696
 
450
697
  def __getitem__(self, key):
451
- return self.roi[key]
698
+ return list(self)[key]
452
699
 
453
- @property
454
- def roi(self):
455
- return list(self)
456
700
 
701
+ PointType = Union[
702
+ Point,
703
+ List[int],
704
+ numpy.ndarray,
705
+ Tuple[int, int],
706
+ ]
457
707
 
458
708
  RectType = Union[
459
709
  Rect,
@@ -471,6 +721,31 @@ class AlgorithmEnum(StrEnum):
471
721
  OCR = "OCR"
472
722
  NeuralNetworkClassify = "NeuralNetworkClassify"
473
723
  NeuralNetworkDetect = "NeuralNetworkDetect"
724
+ And = "And"
725
+ Or = "Or"
726
+ Custom = "Custom"
727
+
728
+
729
+ class ActionEnum(StrEnum):
730
+ DoNothing = "DoNothing"
731
+ Click = "Click"
732
+ LongPress = "LongPress"
733
+ Swipe = "Swipe"
734
+ MultiSwipe = "MultiSwipe"
735
+ ClickKey = "ClickKey"
736
+ LongPressKey = "LongPressKey"
737
+ InputText = "InputText"
738
+ StartApp = "StartApp"
739
+ StopApp = "StopApp"
740
+ Scroll = "Scroll"
741
+ TouchDown = "TouchDown"
742
+ TouchMove = "TouchMove"
743
+ TouchUp = "TouchUp"
744
+ KeyDown = "KeyDown"
745
+ KeyUp = "KeyUp"
746
+ StopTask = "StopTask"
747
+ Command = "Command"
748
+ Shell = "Shell"
474
749
  Custom = "Custom"
475
750
 
476
751
 
@@ -516,6 +791,20 @@ class CustomRecognitionResult:
516
791
  detail: Union[str, Dict]
517
792
 
518
793
 
794
+ @dataclass
795
+ class AndRecognitionResult:
796
+ """And 算法识别结果,包含所有子识别的完整详情"""
797
+
798
+ sub_results: List["RecognitionDetail"]
799
+
800
+
801
+ @dataclass
802
+ class OrRecognitionResult:
803
+ """Or 算法识别结果,包含已执行子识别的完整详情"""
804
+
805
+ sub_results: List["RecognitionDetail"]
806
+
807
+
519
808
  RecognitionResult = Union[
520
809
  TemplateMatchResult,
521
810
  FeatureMatchResult,
@@ -523,6 +812,8 @@ RecognitionResult = Union[
523
812
  OCRResult,
524
813
  NeuralNetworkClassifyResult,
525
814
  NeuralNetworkDetectResult,
815
+ AndRecognitionResult,
816
+ OrRecognitionResult,
526
817
  CustomRecognitionResult,
527
818
  ]
528
819
 
@@ -534,6 +825,8 @@ AlgorithmResultDict = {
534
825
  AlgorithmEnum.OCR: OCRResult,
535
826
  AlgorithmEnum.NeuralNetworkClassify: NeuralNetworkClassifyResult,
536
827
  AlgorithmEnum.NeuralNetworkDetect: NeuralNetworkDetectResult,
828
+ AlgorithmEnum.And: AndRecognitionResult,
829
+ AlgorithmEnum.Or: OrRecognitionResult,
537
830
  AlgorithmEnum.Custom: CustomRecognitionResult,
538
831
  }
539
832
 
@@ -542,11 +835,12 @@ AlgorithmResultDict = {
542
835
  class RecognitionDetail:
543
836
  reco_id: int
544
837
  name: str
545
- algorithm: AlgorithmEnum
838
+ algorithm: Union[AlgorithmEnum, str]
839
+ hit: bool
546
840
  box: Optional[Rect]
547
841
 
548
842
  all_results: List[RecognitionResult]
549
- filterd_results: List[RecognitionResult]
843
+ filtered_results: List[RecognitionResult]
550
844
  best_result: Optional[RecognitionResult]
551
845
 
552
846
  raw_detail: Dict
@@ -554,11 +848,133 @@ class RecognitionDetail:
554
848
  draw_images: List[numpy.ndarray] # only valid in debug mode
555
849
 
556
850
 
851
+ @dataclass
852
+ class ClickActionResult:
853
+ point: Point
854
+ contact: int
855
+
856
+
857
+ @dataclass
858
+ class LongPressActionResult:
859
+ point: Point
860
+ duration: int
861
+ contact: int
862
+
863
+
864
+ @dataclass
865
+ class SwipeActionResult:
866
+ begin: Point
867
+ end: List[Point]
868
+ end_hold: List[int]
869
+ duration: List[int]
870
+ only_hover: bool
871
+ starting: int
872
+ contact: int
873
+
874
+
875
+ @dataclass
876
+ class MultiSwipeActionResult:
877
+ swipes: List[SwipeActionResult]
878
+
879
+
880
+ @dataclass
881
+ class ClickKeyActionResult:
882
+ keycode: List[int]
883
+
884
+
885
+ @dataclass
886
+ class LongPressKeyActionResult:
887
+ keycode: List[int]
888
+ duration: int
889
+
890
+
891
+ @dataclass
892
+ class InputTextActionResult:
893
+ text: str
894
+
895
+
896
+ @dataclass
897
+ class AppActionResult:
898
+ package: str
899
+
900
+
901
+ @dataclass
902
+ class ScrollActionResult:
903
+ dx: int
904
+ dy: int
905
+
906
+
907
+ @dataclass
908
+ class TouchActionResult:
909
+ contact: int
910
+ point: Point
911
+ pressure: int
912
+
913
+
914
+ @dataclass
915
+ class ShellActionResult:
916
+ cmd: str
917
+ timeout: int
918
+ success: bool
919
+ output: str
920
+
921
+
922
+ ActionResult = Union[
923
+ ClickActionResult,
924
+ LongPressActionResult,
925
+ SwipeActionResult,
926
+ MultiSwipeActionResult,
927
+ ClickKeyActionResult,
928
+ LongPressKeyActionResult,
929
+ InputTextActionResult,
930
+ AppActionResult,
931
+ ScrollActionResult,
932
+ TouchActionResult,
933
+ ShellActionResult,
934
+ None,
935
+ ]
936
+
937
+ ActionResultDict = {
938
+ ActionEnum.DoNothing: None,
939
+ ActionEnum.Click: ClickActionResult,
940
+ ActionEnum.LongPress: LongPressActionResult,
941
+ ActionEnum.Swipe: SwipeActionResult,
942
+ ActionEnum.MultiSwipe: MultiSwipeActionResult,
943
+ ActionEnum.ClickKey: ClickKeyActionResult,
944
+ ActionEnum.LongPressKey: LongPressKeyActionResult,
945
+ ActionEnum.InputText: InputTextActionResult,
946
+ ActionEnum.StartApp: AppActionResult,
947
+ ActionEnum.StopApp: AppActionResult,
948
+ ActionEnum.Scroll: ScrollActionResult,
949
+ ActionEnum.TouchDown: TouchActionResult,
950
+ ActionEnum.TouchMove: TouchActionResult,
951
+ ActionEnum.TouchUp: TouchActionResult,
952
+ ActionEnum.KeyDown: ClickKeyActionResult,
953
+ ActionEnum.KeyUp: ClickKeyActionResult,
954
+ ActionEnum.StopTask: None,
955
+ ActionEnum.Command: None,
956
+ ActionEnum.Shell: ShellActionResult,
957
+ ActionEnum.Custom: None,
958
+ }
959
+
960
+
961
+ @dataclass
962
+ class ActionDetail:
963
+ action_id: int
964
+ name: str
965
+ action: Union[ActionEnum, str]
966
+ box: Rect
967
+ success: bool
968
+ result: Optional[ActionResult]
969
+ raw_detail: Dict
970
+
971
+
557
972
  @dataclass
558
973
  class NodeDetail:
559
974
  node_id: int
560
975
  name: str
561
- recognition: RecognitionDetail
976
+ recognition: Optional[RecognitionDetail]
977
+ action: Optional[ActionDetail]
562
978
  completed: bool
563
979
 
564
980
 
maa/event_sink.py ADDED
@@ -0,0 +1,103 @@
1
+ import ctypes
2
+ import json
3
+ from abc import ABC
4
+ from typing import Tuple
5
+ from enum import IntEnum
6
+
7
+ from .define import MaaEventCallback
8
+
9
+
10
+ # class NotificationEvent(IntEnum):
11
+ # ResourceLoading = 1
12
+ # ControllerAction = 2
13
+ # TaskerTask = 3
14
+ # TaskNextList = 4
15
+ # TaskRecognition = 5
16
+ # TaskAction = 6
17
+
18
+
19
+ class NotificationType(IntEnum):
20
+ """通知类型枚举 / Notification type enumeration
21
+
22
+ 用于标识事件回调的状态类型。
23
+ Used to identify the status type of event callbacks.
24
+
25
+ Attributes:
26
+ Unknown: 未知类型 / Unknown type
27
+ Starting: 开始 / Starting
28
+ Succeeded: 成功 / Succeeded
29
+ Failed: 失败 / Failed
30
+ """
31
+
32
+ Unknown = 0
33
+ Starting = 1
34
+ Succeeded = 2
35
+ Failed = 3
36
+
37
+
38
+ class EventSink(ABC):
39
+ """事件监听器基类 / Event sink base class
40
+
41
+ 用于接收 MaaFramework 各种事件回调的抽象基类。
42
+ 派生类包括 ResourceEventSink、ControllerEventSink、TaskerEventSink、ContextEventSink。
43
+ Abstract base class for receiving various event callbacks from MaaFramework.
44
+ Derived classes include ResourceEventSink, ControllerEventSink, TaskerEventSink, ContextEventSink.
45
+ """
46
+
47
+ def on_unknown_notification(self, instance, msg: str, details: dict):
48
+ """处理未知类型的通知 / Handle unknown notification
49
+
50
+ 当收到无法识别的通知时调用。
51
+ Called when an unrecognized notification is received.
52
+
53
+ Args:
54
+ instance: 相关实例对象 / Related instance object
55
+ msg: 消息类型 / Message type
56
+ details: 消息详情 / Message details
57
+ """
58
+ pass
59
+
60
+ def _on_raw_notification(self, handle: ctypes.c_void_p, msg: str, details: dict):
61
+ pass
62
+
63
+ @property
64
+ def c_callback(self) -> MaaEventCallback:
65
+ return self._c_sink_agent
66
+
67
+ @property
68
+ def c_callback_arg(self) -> ctypes.c_void_p:
69
+ return ctypes.c_void_p.from_buffer(ctypes.py_object(self))
70
+
71
+ @staticmethod
72
+ def _gen_c_param(
73
+ sink: "EventSink",
74
+ ) -> Tuple[MaaEventCallback, ctypes.c_void_p]:
75
+ return sink.c_callback, sink.c_callback_arg
76
+
77
+ @staticmethod
78
+ def _notification_type(message: str) -> NotificationType:
79
+ if message.endswith(".Starting"):
80
+ return NotificationType.Starting
81
+ elif message.endswith(".Succeeded"):
82
+ return NotificationType.Succeeded
83
+ elif message.endswith(".Failed"):
84
+ return NotificationType.Failed
85
+ else:
86
+ return NotificationType.Unknown
87
+
88
+ @staticmethod
89
+ @MaaEventCallback
90
+ def _c_sink_agent(
91
+ handle: ctypes.c_void_p,
92
+ msg: ctypes.c_char_p,
93
+ details_json: ctypes.c_char_p,
94
+ callback_arg: ctypes.c_void_p,
95
+ ):
96
+ if not callback_arg:
97
+ return
98
+
99
+ self: EventSink = ctypes.cast(callback_arg, ctypes.py_object).value
100
+
101
+ self._on_raw_notification(
102
+ handle, msg.decode(), json.loads(details_json.decode())
103
+ )