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 13, 2024
1 parent ca62afe commit ec0dbf2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 66 deletions.
4 changes: 4 additions & 0 deletions sample/python/pi_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ def run(
# if reco_detail xxxx

return CustomAction.RunResult(success=True)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions source/binding/Python/maa/custom_action.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ctypes
from dataclasses import dataclass
from abc import ABC, abstractmethod

from .buffer import RectBuffer
Expand Down
1 change: 1 addition & 0 deletions source/binding/Python/maa/custom_recognizer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ctypes
from dataclasses import dataclass
from abc import ABC, abstractmethod
from typing import Tuple

Expand Down
46 changes: 23 additions & 23 deletions source/binding/Python/maa/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@


class Library:
@classmethod
def open(cls, path: Union[pathlib.Path, str]) -> Optional[str]:
@staticmethod
def open(path: Union[pathlib.Path, str]) -> Optional[str]:
platform_values = {
"windows": ("MaaFramework.dll", "MaaToolkit.dll"),
"darwin": ("libMaaFramework.dylib", "libMaaToolkit.dylib"),
Expand All @@ -24,40 +24,40 @@ def open(cls, path: Union[pathlib.Path, str]) -> Optional[str]:
lib_import = ctypes.CDLL

try:
cls.framework_libpath = (
Library.framework_libpath = (
pathlib.Path(path) / platform_values[platform_type][0]
)
cls.framework = lib_import(str(cls.framework_libpath))
Library.framework = lib_import(str(Library.framework_libpath))
except OSError:
cls.framework_libpath = ctypes.util.find_library("MaaFramework")
cls.framework = lib_import(str(cls.framework_libpath))
Library.framework_libpath = ctypes.util.find_library("MaaFramework")
Library.framework = lib_import(str(Library.framework_libpath))

try:
cls.toolkit_libpath = pathlib.Path(path) / platform_values[platform_type][1]
cls.toolkit = lib_import(str(cls.toolkit_libpath))
Library.toolkit_libpath = pathlib.Path(path) / platform_values[platform_type][1]
Library.toolkit = lib_import(str(Library.toolkit_libpath))
except OSError:
cls.toolkit_libpath = ctypes.util.find_library("MaaToolkit")
cls.toolkit = lib_import(str(cls.toolkit_libpath))
Library.toolkit_libpath = ctypes.util.find_library("MaaToolkit")
Library.toolkit = lib_import(str(Library.toolkit_libpath))

if not cls.framework or not cls.toolkit:
cls.initialized = False
if not Library.framework or not Library.toolkit:
Library.initialized = False
return None

cls._set_api_properties()
cls.initialized = True
Library._set_api_properties()
Library.initialized = True

return cls.version()
return Library.version()

@classmethod
def version(cls) -> str:
if not cls.initialized:
@staticmethod
def version() -> str:
if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
)

return cls.framework.MaaVersion().decode("utf-8")
return Library.framework.MaaVersion().decode("utf-8")

@classmethod
def _set_api_properties(cls):
cls.framework.MaaVersion.restype = ctypes.c_char_p
cls.framework.MaaVersion.argtypes = None
@staticmethod
def _set_api_properties():
Library.framework.MaaVersion.restype = ctypes.c_char_p
Library.framework.MaaVersion.argtypes = None
77 changes: 34 additions & 43 deletions source/binding/Python/maa/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, List, Union, Optional, Any
from collections import defaultdict

from .define import *
from .library import Library
Expand All @@ -16,7 +17,7 @@ class AdbDevice:
address: str
screencap_methods: MaaAdbScreencapMethod
input_methods: MaaAdbInputMethod
config: str
config: Dict[str, Any]


@dataclass
Expand All @@ -30,11 +31,11 @@ class Toolkit:

### public ###

@classmethod
@staticmethod
def init_option(
cls, user_path: Union[str, Path], default_config: Dict = {}
user_path: Union[str, Path], default_config: Dict = {}
) -> bool:
cls._set_api_properties()
Toolkit._set_api_properties()

return bool(
Library.toolkit.MaaToolkitConfigInitOption(
Expand All @@ -43,14 +44,13 @@ def init_option(
)
)

@classmethod
@staticmethod
def find_adb_devices(
cls, specified_adb: Union[str, Path] = None
specified_adb: Union[str, Path] = None
) -> List[AdbDevice]:
cls._set_api_properties()
Toolkit._set_api_properties()

list_handle = Library.toolkit.MaaToolkitAdbDeviceListCreate()
Library.toolkit.MaaToolkitAdbDeviceListDestroy(list_handle)

if specified_adb:
Library.toolkit.MaaToolkitAdbDeviceFindSpecified(
Expand Down Expand Up @@ -82,24 +82,25 @@ def find_adb_devices(
input_methods = Library.toolkit.MaaToolkitAdbDeviceGetInputMethods(
device_handle
)
config = Library.toolkit.MaaToolkitAdbDeviceGetConfig(device_handle).decode(
config = json.loads(Library.toolkit.MaaToolkitAdbDeviceGetConfig(device_handle).decode(
"utf-8"
)
))

devices.append(
AdbDevice(
name, adb_path, address, screencap_methods, input_methods, config
)
)

Library.toolkit.MaaToolkitAdbDeviceListDestroy(list_handle)

return devices

@classmethod
def find_desktop_windows(cls) -> List[DesktopWindow]:
cls._set_api_properties()
@staticmethod
def find_desktop_windows() -> List[DesktopWindow]:
Toolkit._set_api_properties()

list_handle = Library.toolkit.MaaToolkitDesktopWindowListCreate()
Library.toolkit.MaaToolkitDesktopWindowListDestroy(list_handle)

Library.toolkit.MaaToolkitDesktopWindowFindAll(list_handle)

Expand All @@ -120,84 +121,74 @@ def find_desktop_windows(cls) -> List[DesktopWindow]:

windows.append(DesktopWindow(hwnd, class_name, window_name))

Library.toolkit.MaaToolkitDesktopWindowListDestroy(list_handle)
return windows

@classmethod
@staticmethod
def register_pi_custom_recognition(
cls, name: str, recognizer: "CustomRecognizer", inst_id: int = 0
name: str, recognizer: "CustomRecognizer", inst_id: int = 0
) -> None:
cls._set_api_properties()

if not cls._custom_recognizer_holder:
cls._custom_recognizer_holder = {}

if not cls._custom_recognizer_holder[inst_id]:
cls._custom_recognizer_holder[inst_id] = {}

Toolkit._set_api_properties()

# avoid gc
cls._custom_recognizer_holder[inst_id][name] = recognizer
Toolkit._custom_recognizer_holder[inst_id][name] = recognizer

return bool(
Library.framework.MaaToolkitProjectInterfaceRegisterCustomRecognition(
Library.toolkit.MaaToolkitProjectInterfaceRegisterCustomRecognition(
ctypes.c_uint64(inst_id),
name.encode("utf-8"),
recognizer.c_handle,
recognizer.c_arg,
)
)

@classmethod
@staticmethod
def register_pi_custom_action(
cls, name: str, action: "CustomAction", inst_id: int = 0
name: str, action: "CustomAction", inst_id: int = 0
) -> None:
cls._set_api_properties()

if not cls._custom_action_holder:
cls._custom_action_holder = {}

if not cls._custom_action_holder[inst_id]:
cls._custom_action_holder[inst_id] = {}
Toolkit._set_api_properties()

# avoid gc
cls._custom_recognizer_holder[inst_id][name] = action
Toolkit._custom_recognizer_holder[inst_id][name] = action

return bool(
Library.framework.MaaToolkitProjectInterfaceRegisterCustomAction(
Library.toolkit.MaaToolkitProjectInterfaceRegisterCustomAction(
ctypes.c_uint64(inst_id),
name.encode("utf-8"),
action.c_handle,
action.c_arg,
),
)

@classmethod
@staticmethod
def run_pi_cli(
cls,
resource_path: Union[str, Path],
user_path: Union[str, Path],
directly: bool = False,
callback: Optional[Callback] = None,
callback_arg: Any = None,
inst_id: int = 0,
) -> bool:
cls._set_api_properties()
Toolkit._set_api_properties()

cls._callback_agent = CallbackAgent(callback, callback_arg)
Toolkit._callback_agent = CallbackAgent(callback, callback_arg)

return bool(
Library.toolkit.MaaToolkitProjectInterfaceRunCli(
ctypes.c_uint64(inst_id),
str(resource_path).encode("utf-8"),
str(user_path).encode("utf-8"),
directly,
cls._callback_agent.c_callback,
cls._callback_agent.c_callback_arg,
Toolkit._callback_agent.c_callback,
Toolkit._callback_agent.c_callback_arg,
)
)

### private ###

_api_properties_initialized: bool = False
_custom_recognizer_holder = defaultdict(dict)
_custom_action_holder = defaultdict(dict)

@staticmethod
def _set_api_properties():
Expand Down

0 comments on commit ec0dbf2

Please sign in to comment.