MaaFw 4.5.1__py3-none-manylinux2014_x86_64.whl → 5.4.0b1__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.
maa/agent/agent_server.py CHANGED
@@ -1,15 +1,38 @@
1
1
  import ctypes
2
- import sys
3
2
 
4
3
  from ..define import *
5
4
  from ..library import Library
6
- from ..buffer import StringBuffer
5
+ from ..event_sink import EventSink
7
6
 
8
7
 
9
8
  class AgentServer:
9
+ """Agent 服务端 / Agent server
10
+
11
+ 用于在独立进程中托管自定义识别器和动作,与 AgentClient 配合使用。
12
+ 这允许将复杂的自定义逻辑与主程序分离运行。
13
+ Used to host custom recognitions and actions in a separate process, working with AgentClient.
14
+ This allows separating complex custom logic from the main program.
15
+
16
+ Example:
17
+ @AgentServer.custom_recognition("MyReco")
18
+ class MyRecognition(CustomRecognition):
19
+ def analyze(self, context, argv):
20
+ return (100, 100, 50, 50)
21
+
22
+ AgentServer.start_up(sock_id)
23
+ AgentServer.join()
24
+ """
10
25
 
11
26
  @staticmethod
12
27
  def custom_recognition(name: str):
28
+ """自定义识别器装饰器 / Custom recognition decorator
29
+
30
+ Args:
31
+ name: 识别器名称,需与 Pipeline 中的 custom_recognition 字段匹配 / Recognition name, should match the custom_recognition field in Pipeline
32
+
33
+ Returns:
34
+ 装饰器函数 / Decorator function
35
+ """
13
36
 
14
37
  def wrapper_recognition(recognition):
15
38
  AgentServer.register_custom_recognition(
@@ -25,6 +48,15 @@ class AgentServer:
25
48
  def register_custom_recognition(
26
49
  name: str, recognition: "CustomRecognition" # type: ignore
27
50
  ) -> bool:
51
+ """注册自定义识别器 / Register custom recognition
52
+
53
+ Args:
54
+ name: 识别器名称 / Recognition name
55
+ recognition: 自定义识别器实例 / Custom recognition instance
56
+
57
+ Returns:
58
+ bool: 是否成功 / Whether successful
59
+ """
28
60
 
29
61
  AgentServer._set_api_properties()
30
62
 
@@ -41,6 +73,14 @@ class AgentServer:
41
73
 
42
74
  @staticmethod
43
75
  def custom_action(name: str):
76
+ """自定义动作装饰器 / Custom action decorator
77
+
78
+ Args:
79
+ name: 动作名称,需与 Pipeline 中的 custom_action 字段匹配 / Action name, should match the custom_action field in Pipeline
80
+
81
+ Returns:
82
+ 装饰器函数 / Decorator function
83
+ """
44
84
 
45
85
  def wrapper_action(action):
46
86
  AgentServer.register_custom_action(name=name, action=action())
@@ -52,6 +92,15 @@ class AgentServer:
52
92
 
53
93
  @staticmethod
54
94
  def register_custom_action(name: str, action: "CustomAction") -> bool: # type: ignore
95
+ """注册自定义动作 / Register custom action
96
+
97
+ Args:
98
+ name: 动作名称 / Action name
99
+ action: 自定义动作实例 / Custom action instance
100
+
101
+ Returns:
102
+ bool: 是否成功 / Whether successful
103
+ """
55
104
 
56
105
  AgentServer._set_api_properties()
57
106
 
@@ -68,6 +117,14 @@ class AgentServer:
68
117
 
69
118
  @staticmethod
70
119
  def start_up(identifier: str) -> bool:
120
+ """启动 Agent 服务 / Start Agent service
121
+
122
+ Args:
123
+ identifier: 连接标识符,用于与 AgentClient 匹配 / Connection identifier for matching with AgentClient
124
+
125
+ Returns:
126
+ bool: 是否成功 / Whether successful
127
+ """
71
128
 
72
129
  AgentServer._set_api_properties()
73
130
 
@@ -75,6 +132,7 @@ class AgentServer:
75
132
 
76
133
  @staticmethod
77
134
  def shut_down() -> None:
135
+ """关闭 Agent 服务 / Shut down Agent service"""
78
136
 
79
137
  AgentServer._set_api_properties()
80
138
 
@@ -82,18 +140,154 @@ class AgentServer:
82
140
 
83
141
  @staticmethod
84
142
  def join() -> None:
143
+ """等待 Agent 服务结束 / Wait for Agent service to end
144
+
145
+ 阻塞当前线程直到服务结束。
146
+ Blocks the current thread until the service ends.
147
+ """
85
148
 
86
149
  AgentServer._set_api_properties()
87
150
 
88
151
  Library.agent_server().MaaAgentServerJoin()
89
152
 
90
153
  @staticmethod
91
- def detach(self) -> None:
154
+ def detach() -> None:
155
+ """分离 Agent 服务 / Detach Agent service
156
+
157
+ 允许服务在后台运行而不阻塞。
158
+ Allows the service to run in the background without blocking.
159
+ """
92
160
 
93
161
  AgentServer._set_api_properties()
94
162
 
95
163
  Library.agent_server().MaaAgentServerDetach()
96
164
 
165
+ _sink_holder: Dict[int, "EventSink"] = {}
166
+
167
+ @staticmethod
168
+ def resource_sink():
169
+ """资源事件监听器装饰器 / Resource event sink decorator
170
+
171
+ Returns:
172
+ 装饰器函数 / Decorator function
173
+ """
174
+
175
+ def wrapper_sink(sink):
176
+ AgentServer.add_resource_sink(sink=sink())
177
+ return sink
178
+
179
+ return wrapper_sink
180
+
181
+ @staticmethod
182
+ def add_resource_sink(sink: "ResourceEventSink") -> None:
183
+ """添加资源事件监听器 / Add resource event sink
184
+
185
+ Args:
186
+ sink: 资源事件监听器 / Resource event sink
187
+ """
188
+ sink_id = int(
189
+ Library.agent_server().MaaAgentServerAddResourceSink(
190
+ *EventSink._gen_c_param(sink)
191
+ )
192
+ )
193
+ if sink_id == MaaInvalidId:
194
+ return None
195
+
196
+ AgentServer._sink_holder[sink_id] = sink
197
+
198
+ @staticmethod
199
+ def controller_sink():
200
+ """控制器事件监听器装饰器 / Controller event sink decorator
201
+
202
+ Returns:
203
+ 装饰器函数 / Decorator function
204
+ """
205
+
206
+ def wrapper_sink(sink):
207
+ AgentServer.add_controller_sink(sink=sink())
208
+ return sink
209
+
210
+ return wrapper_sink
211
+
212
+ @staticmethod
213
+ def add_controller_sink(sink: "ControllerEventSink") -> None:
214
+ """添加控制器事件监听器 / Add controller event sink
215
+
216
+ Args:
217
+ sink: 控制器事件监听器 / Controller event sink
218
+ """
219
+ sink_id = int(
220
+ Library.agent_server().MaaAgentServerAddControllerSink(
221
+ *EventSink._gen_c_param(sink)
222
+ )
223
+ )
224
+ if sink_id == MaaInvalidId:
225
+ return None
226
+
227
+ AgentServer._sink_holder[sink_id] = sink
228
+
229
+ @staticmethod
230
+ def tasker_sink():
231
+ """任务器事件监听器装饰器 / Tasker event sink decorator
232
+
233
+ Returns:
234
+ 装饰器函数 / Decorator function
235
+ """
236
+
237
+ def wrapper_sink(sink):
238
+ AgentServer.add_tasker_sink(sink=sink())
239
+ return sink
240
+
241
+ return wrapper_sink
242
+
243
+ @staticmethod
244
+ def add_tasker_sink(sink: "TaskerEventSink") -> None:
245
+ """添加任务器事件监听器 / Add tasker event sink
246
+
247
+ Args:
248
+ sink: 任务器事件监听器 / Tasker event sink
249
+ """
250
+ sink_id = int(
251
+ Library.agent_server().MaaAgentServerAddTaskerSink(
252
+ *EventSink._gen_c_param(sink)
253
+ )
254
+ )
255
+ if sink_id == MaaInvalidId:
256
+ return None
257
+
258
+ AgentServer._sink_holder[sink_id] = sink
259
+
260
+ @staticmethod
261
+ def context_sink():
262
+ """上下文事件监听器装饰器 / Context event sink decorator
263
+
264
+ Returns:
265
+ 装饰器函数 / Decorator function
266
+ """
267
+
268
+ def wrapper_sink(sink):
269
+ AgentServer.add_context_sink(sink=sink())
270
+ return sink
271
+
272
+ return wrapper_sink
273
+
274
+ @staticmethod
275
+ def add_context_sink(sink: "ContextEventSink") -> None:
276
+ """添加上下文事件监听器 / Add context event sink
277
+
278
+ Args:
279
+ sink: 上下文事件监听器 / Context event sink
280
+ """
281
+ sink_id = int(
282
+ Library.agent_server().MaaAgentServerAddContextSink(
283
+ *EventSink._gen_c_param(sink)
284
+ )
285
+ )
286
+ if sink_id == MaaInvalidId:
287
+ return None
288
+
289
+ AgentServer._sink_holder[sink_id] = sink
290
+
97
291
  _api_properties_initialized: bool = False
98
292
 
99
293
  @staticmethod
@@ -130,3 +324,27 @@ class AgentServer:
130
324
 
131
325
  Library.agent_server().MaaAgentServerDetach.restype = None
132
326
  Library.agent_server().MaaAgentServerDetach.argtypes = []
327
+
328
+ Library.agent_server().MaaAgentServerAddResourceSink.restype = MaaSinkId
329
+ Library.agent_server().MaaAgentServerAddResourceSink.argtypes = [
330
+ MaaEventCallback,
331
+ ctypes.c_void_p,
332
+ ]
333
+
334
+ Library.agent_server().MaaAgentServerAddControllerSink.restype = MaaSinkId
335
+ Library.agent_server().MaaAgentServerAddControllerSink.argtypes = [
336
+ MaaEventCallback,
337
+ ctypes.c_void_p,
338
+ ]
339
+
340
+ Library.agent_server().MaaAgentServerAddTaskerSink.restype = MaaSinkId
341
+ Library.agent_server().MaaAgentServerAddTaskerSink.argtypes = [
342
+ MaaEventCallback,
343
+ ctypes.c_void_p,
344
+ ]
345
+
346
+ Library.agent_server().MaaAgentServerAddContextSink.restype = MaaSinkId
347
+ Library.agent_server().MaaAgentServerAddContextSink.argtypes = [
348
+ MaaEventCallback,
349
+ ctypes.c_void_p,
350
+ ]
maa/agent_client.py CHANGED
@@ -1,15 +1,34 @@
1
1
  import ctypes
2
+ from typing import List
2
3
 
3
4
  from .define import *
4
5
  from .library import Library
5
6
  from .resource import Resource
6
- from .buffer import StringBuffer
7
+ from .controller import Controller
8
+ from .tasker import Tasker
9
+ from .buffer import StringBuffer, StringListBuffer
7
10
 
8
11
 
9
12
  class AgentClient:
13
+ """Agent 客户端 / Agent client
14
+
15
+ 用于连接到 AgentServer,将自定义识别器和动作的执行委托给独立进程。
16
+ 这允许将 MaaFW 本体与 Custom 逻辑分离至独立进程运行。
17
+ Used to connect to AgentServer, delegating custom recognition and action execution to a separate process.
18
+ This allows separating MaaFW core from Custom logic into independent processes.
19
+ """
20
+
10
21
  _handle: MaaAgentClientHandle
11
22
 
12
23
  def __init__(self, identifier: Optional[str] = None):
24
+ """创建 Agent 客户端 / Create Agent client
25
+
26
+ Args:
27
+ identifier: 可选的连接标识符,用于匹配特定的 AgentServer / Optional connection identifier for matching specific AgentServer
28
+
29
+ Raises:
30
+ RuntimeError: 如果创建失败
31
+ """
13
32
  self._set_api_properties()
14
33
 
15
34
  if identifier:
@@ -29,6 +48,11 @@ class AgentClient:
29
48
 
30
49
  @property
31
50
  def identifier(self) -> Optional[str]:
51
+ """获取连接标识符 / Get connection identifier
52
+
53
+ Returns:
54
+ Optional[str]: 连接标识符,如果未设置则返回 None / Connection identifier, or None if not set
55
+ """
32
56
  id_buffer = StringBuffer()
33
57
  if not Library.agent_client().MaaAgentClientIdentifier(
34
58
  self._handle, id_buffer._handle
@@ -38,6 +62,17 @@ class AgentClient:
38
62
  return id_buffer.get()
39
63
 
40
64
  def bind(self, resource: Resource) -> bool:
65
+ """绑定资源 / Bind resource
66
+
67
+ 将 AgentServer 中注册的自定义识别器和动作绑定到资源上。
68
+ Bind custom recognitions and actions registered in AgentServer to the resource.
69
+
70
+ Args:
71
+ resource: 资源对象 / Resource object
72
+
73
+ Returns:
74
+ bool: 是否成功 / Whether successful
75
+ """
41
76
  # avoid gc
42
77
  self._resource = resource
43
78
 
@@ -47,22 +82,125 @@ class AgentClient:
47
82
  )
48
83
  )
49
84
 
85
+ def register_sink(
86
+ self, resource: Resource, controller: Controller, tasker: Tasker
87
+ ) -> bool:
88
+ """注册事件监听器 / Register event sinks
89
+
90
+ 将资源、控制器、任务器的事件转发给 AgentServer。
91
+ Forward resource, controller, and tasker events to AgentServer.
92
+
93
+ Args:
94
+ resource: 资源对象 / Resource object
95
+ controller: 控制器对象 / Controller object
96
+ tasker: 任务器对象 / Tasker object
97
+
98
+ Returns:
99
+ bool: 是否全部注册成功 / Whether all registrations succeeded
100
+ """
101
+ # avoid gc
102
+ self._sinks = [resource, controller, tasker]
103
+
104
+ return (
105
+ bool(
106
+ Library.agent_client().MaaAgentClientRegisterResourceSink(
107
+ self._handle, resource._handle
108
+ )
109
+ )
110
+ and bool(
111
+ Library.agent_client().MaaAgentClientRegisterControllerSink(
112
+ self._handle, controller._handle
113
+ )
114
+ )
115
+ and bool(
116
+ Library.agent_client().MaaAgentClientRegisterTaskerSink(
117
+ self._handle, tasker._handle
118
+ )
119
+ )
120
+ )
121
+
50
122
  def connect(self) -> bool:
123
+ """连接到 AgentServer / Connect to AgentServer
124
+
125
+ Returns:
126
+ bool: 是否成功 / Whether successful
127
+ """
51
128
  return bool(Library.agent_client().MaaAgentClientConnect(self._handle))
52
129
 
53
130
  def disconnect(self) -> bool:
131
+ """断开与 AgentServer 的连接 / Disconnect from AgentServer
132
+
133
+ Returns:
134
+ bool: 是否成功 / Whether successful
135
+ """
54
136
  return bool(Library.agent_client().MaaAgentClientDisconnect(self._handle))
55
137
 
56
138
  @property
57
139
  def connected(self) -> bool:
140
+ """判断是否已连接 / Check if connected
141
+
142
+ Returns:
143
+ bool: 是否已连接 / Whether connected
144
+ """
58
145
  return bool(Library.agent_client().MaaAgentClientConnected(self._handle))
59
146
 
60
147
  @property
61
148
  def alive(self) -> bool:
149
+ """判断连接是否存活 / Check if connection is alive
150
+
151
+ Returns:
152
+ bool: 连接是否存活 / Whether connection is alive
153
+ """
62
154
  return bool(Library.agent_client().MaaAgentClientAlive(self._handle))
63
155
 
64
156
  def set_timeout(self, milliseconds: int) -> bool:
65
- return bool(Library.agent_client().MaaAgentClientSetTimeout(self._handle, ctypes.c_int64(milliseconds)))
157
+ """设置超时时间 / Set timeout
158
+
159
+ Args:
160
+ milliseconds: 超时时间(毫秒) / Timeout in milliseconds
161
+
162
+ Returns:
163
+ bool: 是否成功 / Whether successful
164
+ """
165
+ return bool(
166
+ Library.agent_client().MaaAgentClientSetTimeout(
167
+ self._handle, ctypes.c_int64(milliseconds)
168
+ )
169
+ )
170
+
171
+ @property
172
+ def custom_recognition_list(self) -> List[str]:
173
+ """获取已注册的自定义识别器列表 / Get registered custom recognizer list
174
+
175
+ Returns:
176
+ list[str]: 自定义识别器名列表 / List of custom recognizer names
177
+
178
+ Raises:
179
+ RuntimeError: 如果获取失败
180
+ """
181
+ buffer = StringListBuffer()
182
+ if not Library.agent_client().MaaAgentClientGetCustomRecognitionList(
183
+ self._handle, buffer._handle
184
+ ):
185
+ raise RuntimeError("Failed to get custom recognition list.")
186
+ return buffer.get()
187
+
188
+ @property
189
+ def custom_action_list(self) -> List[str]:
190
+ """获取已注册的自定义操作列表 / Get registered custom action list
191
+
192
+ Returns:
193
+ list[str]: 自定义操作名列表 / List of custom action names
194
+
195
+ Raises:
196
+ RuntimeError: 如果获取失败
197
+ """
198
+ buffer = StringListBuffer()
199
+ if not Library.agent_client().MaaAgentClientGetCustomActionList(
200
+ self._handle, buffer._handle
201
+ ):
202
+ raise RuntimeError("Failed to get custom action list.")
203
+ return buffer.get()
66
204
 
67
205
  _api_properties_initialized: bool = False
68
206
 
@@ -96,6 +234,24 @@ class AgentClient:
96
234
  MaaResourceHandle,
97
235
  ]
98
236
 
237
+ Library.agent_client().MaaAgentClientRegisterResourceSink.restype = MaaBool
238
+ Library.agent_client().MaaAgentClientRegisterResourceSink.argtypes = [
239
+ MaaAgentClientHandle,
240
+ MaaResourceHandle,
241
+ ]
242
+
243
+ Library.agent_client().MaaAgentClientRegisterControllerSink.restype = MaaBool
244
+ Library.agent_client().MaaAgentClientRegisterControllerSink.argtypes = [
245
+ MaaAgentClientHandle,
246
+ MaaControllerHandle,
247
+ ]
248
+
249
+ Library.agent_client().MaaAgentClientRegisterTaskerSink.restype = MaaBool
250
+ Library.agent_client().MaaAgentClientRegisterTaskerSink.argtypes = [
251
+ MaaAgentClientHandle,
252
+ MaaTaskerHandle,
253
+ ]
254
+
99
255
  Library.agent_client().MaaAgentClientConnect.restype = MaaBool
100
256
  Library.agent_client().MaaAgentClientConnect.argtypes = [
101
257
  MaaAgentClientHandle,
@@ -121,3 +277,15 @@ class AgentClient:
121
277
  MaaAgentClientHandle,
122
278
  ctypes.c_int64,
123
279
  ]
280
+
281
+ Library.agent_client().MaaAgentClientGetCustomRecognitionList.restype = MaaBool
282
+ Library.agent_client().MaaAgentClientGetCustomRecognitionList.argtypes = [
283
+ MaaAgentClientHandle,
284
+ MaaStringListBufferHandle,
285
+ ]
286
+
287
+ Library.agent_client().MaaAgentClientGetCustomActionList.restype = MaaBool
288
+ Library.agent_client().MaaAgentClientGetCustomActionList.argtypes = [
289
+ MaaAgentClientHandle,
290
+ MaaStringListBufferHandle,
291
+ ]
Binary file
Binary file
Binary file
Binary file
Binary file
maa/bin/libMaaToolkit.so CHANGED
Binary file
maa/bin/libMaaUtils.so CHANGED
Binary file
maa/bin/libc++.so.1 CHANGED
Binary file
maa/bin/libc++abi.so.1 CHANGED
Binary file
Binary file
Binary file
Binary file
maa/bin/libunwind.so.1 CHANGED
Binary file
Binary file