Skip to content

Commit

Permalink
feat: 优化 Python 绑定 (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO authored Jan 10, 2024
2 parents dc928dd + 11da461 commit e10d239
Show file tree
Hide file tree
Showing 13 changed files with 641 additions and 178 deletions.
3 changes: 2 additions & 1 deletion sample/python/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Tuple
from maa.library import Library
from maa.resource import Resource
from maa.controller import AdbController
Expand Down Expand Up @@ -49,7 +50,7 @@ async def main():
class MyRecognizer(CustomRecognizer):
def analyze(
self, context, image, task_name, custom_param
) -> (bool, (int, int, int, int), str):
) -> Tuple[bool, Tuple[int, int, int, int], str]:
return True, (0, 0, 100, 100), "Hello World!"


Expand Down
89 changes: 48 additions & 41 deletions source/binding/Python/maa/buffer.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import ctypes
import numpy
from typing import Tuple, Union, Optional
from typing import List, Tuple, Union, Optional
from PIL import Image

from .define import MaaBool
from .define import *
from .library import Library


class StringBuffer:
_handle: ctypes.c_void_p
_handle: MaaStringBufferHandle
_own: bool

def __init__(self, c_handle: Optional[ctypes.c_void_p] = None):
def __init__(self, c_handle: Optional[MaaStringBufferHandle] = None):
if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
Expand Down Expand Up @@ -56,40 +56,40 @@ def _set_api_properties():
return
StringBuffer._api_properties_initialized = True

Library.framework.MaaCreateStringBuffer.restype = ctypes.c_void_p
Library.framework.MaaCreateStringBuffer.argtypes = []
Library.framework.MaaCreateStringBuffer.restype = MaaStringBufferHandle
Library.framework.MaaCreateStringBuffer.argtypes = None

Library.framework.MaaDestroyStringBuffer.restype = None
Library.framework.MaaDestroyStringBuffer.argtypes = [ctypes.c_void_p]
Library.framework.MaaDestroyStringBuffer.argtypes = [MaaStringBufferHandle]

Library.framework.MaaIsStringEmpty.restype = MaaBool
Library.framework.MaaIsStringEmpty.argtypes = [ctypes.c_void_p]
Library.framework.MaaIsStringEmpty.argtypes = [MaaStringBufferHandle]

Library.framework.MaaClearString.restype = MaaBool
Library.framework.MaaClearString.argtypes = [ctypes.c_void_p]
Library.framework.MaaClearString.argtypes = [MaaStringBufferHandle]

Library.framework.MaaGetString.restype = ctypes.c_char_p
Library.framework.MaaGetString.argtypes = [ctypes.c_void_p]
Library.framework.MaaGetString.restype = MaaStringView
Library.framework.MaaGetString.argtypes = [MaaStringBufferHandle]

Library.framework.MaaGetStringSize.restype = ctypes.c_size_t
Library.framework.MaaGetStringSize.argtypes = [ctypes.c_void_p]
Library.framework.MaaGetStringSize.restype = MaaSize
Library.framework.MaaGetStringSize.argtypes = [MaaStringBufferHandle]

Library.framework.MaaSetString.restype = MaaBool
Library.framework.MaaSetString.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
Library.framework.MaaSetString.argtypes = [MaaStringBufferHandle, MaaStringView]

Library.framework.MaaSetStringEx.restype = MaaBool
Library.framework.MaaSetStringEx.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_size_t,
MaaStringBufferHandle,
MaaStringView,
MaaSize,
]


class ImageBuffer:
_handle: ctypes.c_void_p
_handle: MaaImageBufferHandle
_own: bool

def __init__(self, c_handle: Optional[ctypes.c_void_p] = None):
def __init__(self, c_handle: Optional[MaaImageBufferHandle] = None):
if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
Expand Down Expand Up @@ -133,7 +133,7 @@ def set(self, value: Union[numpy.ndarray, Image.Image]) -> bool:
value.ctypes.data,
value.shape[1],
value.shape[0],
16, # CV_8UC3
16, # CV_8UC3
)
)

Expand All @@ -151,45 +151,45 @@ def _set_api_properties():
return
ImageBuffer._api_properties_initialized = True

Library.framework.MaaCreateImageBuffer.restype = ctypes.c_void_p
Library.framework.MaaCreateImageBuffer.restype = MaaImageBufferHandle
Library.framework.MaaCreateImageBuffer.argtypes = []

Library.framework.MaaDestroyImageBuffer.restype = None
Library.framework.MaaDestroyImageBuffer.argtypes = [ctypes.c_void_p]
Library.framework.MaaDestroyImageBuffer.argtypes = [MaaImageBufferHandle]

Library.framework.MaaGetImageRawData.restype = ctypes.c_void_p
Library.framework.MaaGetImageRawData.argtypes = [ctypes.c_void_p]
Library.framework.MaaGetImageRawData.restype = MaaImageRawData
Library.framework.MaaGetImageRawData.argtypes = [MaaImageBufferHandle]

Library.framework.MaaGetImageWidth.restype = ctypes.c_int32
Library.framework.MaaGetImageWidth.argtypes = [ctypes.c_void_p]
Library.framework.MaaGetImageWidth.argtypes = [MaaImageBufferHandle]

Library.framework.MaaGetImageHeight.restype = ctypes.c_int32
Library.framework.MaaGetImageHeight.argtypes = [ctypes.c_void_p]
Library.framework.MaaGetImageHeight.argtypes = [MaaImageBufferHandle]

Library.framework.MaaGetImageType.restype = ctypes.c_int32
Library.framework.MaaGetImageType.argtypes = [ctypes.c_void_p]
Library.framework.MaaGetImageType.argtypes = [MaaImageBufferHandle]

Library.framework.MaaSetImageRawData.restype = MaaBool
Library.framework.MaaSetImageRawData.argtypes = [
ctypes.c_void_p,
ctypes.c_void_p,
MaaImageBufferHandle,
MaaImageRawData,
ctypes.c_int32,
ctypes.c_int32,
ctypes.c_int32,
]

Library.framework.MaaIsImageEmpty.restype = MaaBool
Library.framework.MaaIsImageEmpty.argtypes = [ctypes.c_void_p]
Library.framework.MaaIsImageEmpty.argtypes = [MaaImageBufferHandle]

Library.framework.MaaClearImage.restype = MaaBool
Library.framework.MaaClearImage.argtypes = [ctypes.c_void_p]
Library.framework.MaaClearImage.argtypes = [MaaImageBufferHandle]


class RectBuffer:
_handle: ctypes.c_void_p
_handle: MaaRectHandle
_own: bool

def __init__(self, c_handle: Optional[ctypes.c_void_p] = None):
def __init__(self, c_handle: Optional[MaaRectHandle] = None):
if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
Expand Down Expand Up @@ -219,7 +219,14 @@ def get(self) -> Tuple[int, int, int, int]:

return x, y, w, h

def set(self, value: Union[numpy.ndarray, tuple, list]) -> bool:
def set(
self,
value: Union[
numpy.ndarray,
Tuple[int, int, int, int],
List[int],
],
) -> bool:
if isinstance(value, numpy.ndarray):
if value.ndim != 1:
raise ValueError("value must be a 1D array")
Expand Down Expand Up @@ -248,27 +255,27 @@ def _set_api_properties():
return
RectBuffer._api_properties_initialized = True

Library.framework.MaaCreateRectBuffer.restype = ctypes.c_void_p
Library.framework.MaaCreateRectBuffer.restype = MaaRectHandle
Library.framework.MaaCreateRectBuffer.argtypes = []

Library.framework.MaaDestroyRectBuffer.restype = None
Library.framework.MaaDestroyRectBuffer.argtypes = [ctypes.c_void_p]
Library.framework.MaaDestroyRectBuffer.argtypes = [MaaRectHandle]

Library.framework.MaaGetRectX.restype = ctypes.c_int32
Library.framework.MaaGetRectX.argtypes = [ctypes.c_void_p]
Library.framework.MaaGetRectX.argtypes = [MaaRectHandle]

Library.framework.MaaGetRectY.restype = ctypes.c_int32
Library.framework.MaaGetRectY.argtypes = [ctypes.c_void_p]
Library.framework.MaaGetRectY.argtypes = [MaaRectHandle]

Library.framework.MaaGetRectW.restype = ctypes.c_int32
Library.framework.MaaGetRectW.argtypes = [ctypes.c_void_p]
Library.framework.MaaGetRectW.argtypes = [MaaRectHandle]

Library.framework.MaaGetRectH.restype = ctypes.c_int32
Library.framework.MaaGetRectH.argtypes = [ctypes.c_void_p]
Library.framework.MaaGetRectH.argtypes = [MaaRectHandle]

Library.framework.MaaSetRect.restype = MaaBool
Library.framework.MaaSetRect.argtypes = [
ctypes.c_void_p,
MaaRectHandle,
ctypes.c_int32,
ctypes.c_int32,
ctypes.c_int32,
Expand Down
66 changes: 33 additions & 33 deletions source/binding/Python/maa/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
import numpy
import json

from typing import Any, Optional, Tuple
from typing import Any, Dict, Optional, Tuple

from .library import Library
from .define import MaaBool
from .buffer import *


class SyncContext:
_handle: ctypes.c_void_p
_handle: MaaSyncContextHandle

def __init__(self, handle: ctypes.c_void_p):
def __init__(self, handle: MaaSyncContextHandle):
self._set_api_properties()

self._handle = handle
Expand All @@ -38,7 +38,7 @@ def run_recognizer(
self,
image: numpy.ndarray,
task_name: str,
task_param: Any,
task_param: Dict,
) -> Optional[Tuple[Tuple[int, int, int, int], str]]:
"""
Sync context run recognizer.
Expand Down Expand Up @@ -76,8 +76,8 @@ def run_recognizer(
def run_action(
self,
task_name: str,
task_param: Any,
cur_box: (int, int, int, int),
task_param: Dict,
cur_box: Tuple[int, int, int, int],
cur_rec_detail: str,
) -> Optional[str]:
"""
Expand Down Expand Up @@ -242,40 +242,40 @@ def _set_api_properties():

Library.framework.MaaSyncContextRunTask.restype = MaaBool
Library.framework.MaaSyncContextRunTask.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
MaaSyncContextHandle,
MaaStringView,
MaaStringView,
]

Library.framework.MaaSyncContextRunRecognizer.restype = MaaBool
Library.framework.MaaSyncContextRunRecognizer.argtypes = [
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_void_p,
ctypes.c_void_p,
MaaSyncContextHandle,
MaaStringBufferHandle,
MaaStringView,
MaaStringView,
MaaRectHandle,
MaaStringBufferHandle,
]

Library.framework.MaaSyncContextRunAction.restype = MaaBool
Library.framework.MaaSyncContextRunAction.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_void_p,
ctypes.c_char_p,
MaaSyncContextHandle,
MaaStringView,
MaaStringView,
MaaRectHandle,
MaaStringBufferHandle,
]

Library.framework.MaaSyncContextClick.restype = MaaBool
Library.framework.MaaSyncContextClick.argtypes = [
ctypes.c_void_p,
MaaSyncContextHandle,
ctypes.c_int32,
ctypes.c_int32,
]

Library.framework.MaaSyncContextSwipe.restype = MaaBool
Library.framework.MaaSyncContextSwipe.argtypes = [
ctypes.c_void_p,
MaaSyncContextHandle,
ctypes.c_int32,
ctypes.c_int32,
ctypes.c_int32,
Expand All @@ -285,19 +285,19 @@ def _set_api_properties():

Library.framework.MaaSyncContextPressKey.restype = MaaBool
Library.framework.MaaSyncContextPressKey.argtypes = [
ctypes.c_void_p,
MaaSyncContextHandle,
ctypes.c_int32,
]

Library.framework.MaaSyncContextInputText.restype = MaaBool
Library.framework.MaaSyncContextInputText.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
MaaSyncContextHandle,
MaaStringView,
]

Library.framework.MaaSyncContextTouchDown.restype = MaaBool
Library.framework.MaaSyncContextTouchDown.argtypes = [
ctypes.c_void_p,
MaaSyncContextHandle,
ctypes.c_int32,
ctypes.c_int32,
ctypes.c_int32,
Expand All @@ -306,7 +306,7 @@ def _set_api_properties():

Library.framework.MaaSyncContextTouchMove.restype = MaaBool
Library.framework.MaaSyncContextTouchMove.argtypes = [
ctypes.c_void_p,
MaaSyncContextHandle,
ctypes.c_int32,
ctypes.c_int32,
ctypes.c_int32,
Expand All @@ -315,19 +315,19 @@ def _set_api_properties():

Library.framework.MaaSyncContextTouchUp.restype = MaaBool
Library.framework.MaaSyncContextTouchUp.argtypes = [
ctypes.c_void_p,
MaaSyncContextHandle,
ctypes.c_int32,
]

Library.framework.MaaSyncContextScreencap.restype = MaaBool
Library.framework.MaaSyncContextScreencap.argtypes = [
ctypes.c_void_p,
ctypes.c_void_p,
MaaSyncContextHandle,
MaaImageBufferHandle,
]

Library.framework.MaaSyncContextGetTaskResult.restype = MaaBool
Library.framework.MaaSyncContextGetTaskResult.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_void_p,
MaaSyncContextHandle,
MaaStringView,
MaaStringBufferHandle,
]
Loading

0 comments on commit e10d239

Please sign in to comment.