Skip to content

Commit

Permalink
fix: python binding
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Sep 20, 2024
1 parent a647269 commit 216207f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 72 deletions.
48 changes: 7 additions & 41 deletions source/binding/Python/maa/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,7 @@ def __init__(
MaaAdbInputMethod(input_methods),
json.dumps(config, ensure_ascii=False).encode(),
str(agent_path).encode(),
(
self._notification_handler.c_callback
if self._notification_handler
else None
),
(
self._notification_handler.c_callback_arg
if self._notification_handler
else None
),
*NotificationHandler._gen_c_param(self._notification_handler)
)

if not self._handle:
Expand Down Expand Up @@ -365,16 +356,7 @@ def __init__(
hWnd,
MaaWin32ScreencapMethod(screencap_method),
MaaWin32InputMethod(input_method),
(
self._notification_handler.c_callback
if self._notification_handler
else None
),
(
self._notification_handler.c_callback_arg
if self._notification_handler
else None
),
*NotificationHandler._gen_c_param(self._notification_handler)
)

if not self._handle:
Expand Down Expand Up @@ -410,16 +392,7 @@ def __init__(
str(write_path).encode(),
MaaDbgControllerType(dbg_type),
json.dumps(config, ensure_ascii=False).encode(),
(
self._notification_handler.c_callback
if self._notification_handler
else None
),
(
self._notification_handler.c_callback_arg
if self._notification_handler
else None
),
*NotificationHandler._gen_c_param(self._notification_handler)
)

if not self._handle:
Expand Down Expand Up @@ -468,16 +441,7 @@ def __init__(
self._handle = Library.framework.MaaCustomControllerCreate(
self.c_handle,
self.c_arg,
(
self._notification_handler.c_callback
if self._notification_handler
else None
),
(
self._notification_handler.c_callback_arg
if self._notification_handler
else None
),
*NotificationHandler._gen_c_param(self._notification_handler)
)

if not self._handle:
Expand Down Expand Up @@ -674,7 +638,9 @@ def _c_swipe_agent(
ctypes.py_object,
).value

return int(self.swipe(int(c_x1), int(c_y1), int(c_x2), int(c_y2), int(c_duration)))
return int(
self.swipe(int(c_x1), int(c_y1), int(c_x2), int(c_y2), int(c_duration))
)

@staticmethod
@MaaCustomControllerCallbacks.TouchDownFunc
Expand Down
12 changes: 11 additions & 1 deletion source/binding/Python/maa/notification_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ctypes
import json
from abc import ABC, abstractmethod
from abc import ABC
from typing import Optional, Tuple
from enum import Enum
from dataclasses import dataclass

Expand Down Expand Up @@ -153,6 +154,15 @@ def c_callback(self) -> MaaNotificationCallback:
def c_callback_arg(self) -> ctypes.c_void_p:
return ctypes.c_void_p.from_buffer(ctypes.py_object(self))

@staticmethod
def _gen_c_param(
handler: Optional["NotificationHandler"],
) -> Tuple[MaaNotificationCallback, ctypes.c_void_p]:
if handler:
return handler.c_callback, handler.c_callback_arg
else:
return NotificationHandler._c_notification_agent, ctypes.c_void_p()

@staticmethod
def _notification_type(message: str) -> NotificationType:
if message.endswith(".Starting"):
Expand Down
11 changes: 1 addition & 10 deletions source/binding/Python/maa/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,7 @@ def __init__(
else:
self._notification_handler = notification_handler
self._handle = Library.framework.MaaResourceCreate(
(
self._notification_handler.c_callback
if self._notification_handler
else None
),
(
self._notification_handler.c_callback_arg
if self._notification_handler
else None
),
*NotificationHandler._gen_c_param(self._notification_handler)
)
self._own = True

Expand Down
11 changes: 1 addition & 10 deletions source/binding/Python/maa/tasker.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,7 @@ def __init__(
else:
self._notification_handler = notification_handler
self._handle = Library.framework.MaaTaskerCreate(
(
self._notification_handler.c_callback
if self._notification_handler
else None
),
(
self._notification_handler.c_callback_arg
if self._notification_handler
else None
),
*NotificationHandler._gen_c_param(self._notification_handler)
)
self._own = True

Expand Down
11 changes: 1 addition & 10 deletions source/binding/Python/maa/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,7 @@ def pi_run_cli(
str(resource_path).encode(),
str(user_path).encode(),
directly,
(
Toolkit._pi_notification_handler.c_callback
if Toolkit._pi_notification_handler
else None
),
(
Toolkit._pi_notification_handler.c_callback_arg
if Toolkit._pi_notification_handler
else None
),
*NotificationHandler._gen_c_param(Toolkit._pi_notification_handler),
)
)

Expand Down
5 changes: 5 additions & 0 deletions test/python/binding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ def run(


def api_test():
r1 = Resource()
r2 = Resource()
t1 = Tasker()
t2 = Tasker()

resource = Resource(MyNotificationHandler())
print(f"resource: {resource}")

Expand Down

0 comments on commit 216207f

Please sign in to comment.