diff --git a/sample/python/__main__.py b/sample/python/__main__.py index f93aa86cf..582e6de6e 100644 --- a/sample/python/__main__.py +++ b/sample/python/__main__.py @@ -1,3 +1,4 @@ +from typing import Tuple from maa.library import Library from maa.resource import Resource from maa.controller import AdbController @@ -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!" diff --git a/source/binding/Python/maa/buffer.py b/source/binding/Python/maa/buffer.py index f5ebf250a..514a78cac 100644 --- a/source/binding/Python/maa/buffer.py +++ b/source/binding/Python/maa/buffer.py @@ -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." @@ -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." @@ -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 ) ) @@ -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." @@ -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") @@ -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, diff --git a/source/binding/Python/maa/context.py b/source/binding/Python/maa/context.py index 0fa29d579..4e0675f4c 100644 --- a/source/binding/Python/maa/context.py +++ b/source/binding/Python/maa/context.py @@ -2,7 +2,7 @@ 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 @@ -10,9 +10,9 @@ 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 @@ -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. @@ -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]: """ @@ -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, @@ -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, @@ -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, @@ -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, ] diff --git a/source/binding/Python/maa/controller.py b/source/binding/Python/maa/controller.py index 5b51537dd..4fbf8942d 100644 --- a/source/binding/Python/maa/controller.py +++ b/source/binding/Python/maa/controller.py @@ -4,7 +4,7 @@ from abc import ABC from typing import Optional, Any, Dict -from .define import MaaApiCallback, MaaBool, MaaId +from .define import * from .future import Future from .library import Library from .callback_agent import CallbackAgent, Callback @@ -13,7 +13,7 @@ class Controller(ABC): - _handle: ctypes.c_void_p + _handle: MaaControllerHandle _callback_agent: CallbackAgent def __init__(self, callback: Optional[Callback] = None, callback_arg: Any = None): @@ -80,24 +80,27 @@ def _set_api_properties(): Controller._api_properties_initialized = True Library.framework.MaaControllerDestroy.restype = None - Library.framework.MaaControllerDestroy.argtypes = [ctypes.c_void_p] + Library.framework.MaaControllerDestroy.argtypes = [MaaControllerHandle] Library.framework.MaaControllerSetOption.restype = MaaBool Library.framework.MaaControllerSetOption.argtypes = [ - ctypes.c_void_p, - ctypes.c_int32, - ctypes.c_void_p, - ctypes.c_uint64, + MaaControllerHandle, + MaaCtrlOption, + MaaOptionValue, + MaaOptionValueSize, ] - Library.framework.MaaControllerPostConnection.restype = MaaId - Library.framework.MaaControllerPostConnection.argtypes = [ctypes.c_void_p] + Library.framework.MaaControllerPostConnection.restype = MaaCtrlId + Library.framework.MaaControllerPostConnection.argtypes = [MaaControllerHandle] - Library.framework.MaaControllerStatus.restype = ctypes.c_int32 - Library.framework.MaaControllerStatus.argtypes = [ctypes.c_void_p, MaaId] + Library.framework.MaaControllerStatus.restype = MaaStatus + Library.framework.MaaControllerStatus.argtypes = [ + MaaControllerHandle, + MaaCtrlId, + ] Library.framework.MaaControllerConnected.restype = MaaBool - Library.framework.MaaControllerConnected.argtypes = [ctypes.c_void_p] + Library.framework.MaaControllerConnected.argtypes = [MaaControllerHandle] class AdbController(Controller): @@ -146,13 +149,13 @@ def _set_adb_api_properties(self): Set the API properties for the ADB controller. """ - Library.framework.MaaAdbControllerCreateV2.restype = ctypes.c_void_p + Library.framework.MaaAdbControllerCreateV2.restype = MaaControllerHandle Library.framework.MaaAdbControllerCreateV2.argtypes = [ - ctypes.c_char_p, - ctypes.c_char_p, - ctypes.c_int32, - ctypes.c_char_p, - ctypes.c_char_p, - MaaApiCallback, - ctypes.c_void_p, + MaaStringView, + MaaStringView, + MaaAdbControllerType, + MaaStringView, + MaaStringView, + MaaControllerCallback, + MaaCallbackTransparentArg, ] diff --git a/source/binding/Python/maa/custom_action.py b/source/binding/Python/maa/custom_action.py index 13388255b..e86c9bdbf 100644 --- a/source/binding/Python/maa/custom_action.py +++ b/source/binding/Python/maa/custom_action.py @@ -6,6 +6,7 @@ from .buffer import RectBuffer, StringBuffer, ImageBuffer from .context import SyncContext +# TODO: Typing class CustomAction(ABC): _handle: MaaCustomAction @@ -48,7 +49,7 @@ def stop( raise NotImplementedError - def c_handle(self) -> ctypes._Pointer[MaaCustomAction]: + def c_handle(self) -> ctypes.POINTER(MaaCustomAction): return ctypes.pointer(self._handle) def c_arg(self) -> ctypes.c_void_p: diff --git a/source/binding/Python/maa/custom_recognizer.py b/source/binding/Python/maa/custom_recognizer.py index 723ab716c..2d51f247a 100644 --- a/source/binding/Python/maa/custom_recognizer.py +++ b/source/binding/Python/maa/custom_recognizer.py @@ -1,3 +1,4 @@ +from typing import Tuple import numpy import ctypes from abc import ABC, abstractmethod @@ -6,6 +7,8 @@ from .buffer import RectBuffer, StringBuffer, ImageBuffer from .context import SyncContext +# TODO: Typing + class CustomRecognizer(ABC): _handle: MaaCustomRecognizer @@ -16,11 +19,11 @@ def __init__(self): @abstractmethod def analyze( self, - context: ctypes.c_void_p, + context: SyncContext, image: numpy.ndarray, task_name: str, custom_param: str, - ) -> (bool, (int, int, int, int), str): + ) -> Tuple[bool, Tuple[int, int, int, int], str]: """ Analyze the given image. @@ -34,7 +37,7 @@ def analyze( raise NotImplementedError - def c_handle(self) -> ctypes._Pointer(MaaCustomRecognizer): + def c_handle(self) -> ctypes.POINTER(MaaCustomRecognizer): return ctypes.pointer(self._handle) def c_arg(self) -> ctypes.c_void_p: @@ -60,9 +63,7 @@ def _c_analyze_agent( task_name = c_task_name.decode("utf-8") custom_param = c_custom_param.decode("utf-8") - success, box, detail = self.analyze( - context, image, task_name, custom_param - ) + success, box, detail = self.analyze(context, image, task_name, custom_param) RectBuffer(c_out_box).set(box) StringBuffer(c_out_detail).set(detail) diff --git a/source/binding/Python/maa/define.py b/source/binding/Python/maa/define.py index 95394489a..2592afb8f 100644 --- a/source/binding/Python/maa/define.py +++ b/source/binding/Python/maa/define.py @@ -9,7 +9,7 @@ MaaId = ctypes.c_uint64 -class MaaStatus(Enum): +class MaaStatusEnum(Enum): invalid = 0 pending = 1000 running = 2000 @@ -51,3 +51,100 @@ class MaaCustomAction(ctypes.Structure): ("action", RunFunc), ("stop", StopFunc), ] + + +# TODO: tidy + +MaaOptionValueSize = ctypes.c_uint64 +MaaOptionValue = ctypes.c_void_p + +MaaOption = ctypes.c_int32 +MaaCtrlOption = MaaOption + + +class MaaCtrlOptionEnum: + Invalid: MaaCtrlOption = 0 + + # Only one of long and short side can be set, and the other is automatically scaled according to the aspect ratio. + # value: int, eg: 1920; val_size: sizeof(int) + ScreenshotTargetLongSide: MaaCtrlOption = 1 + + # Only one of long and short side can be set, and the other is automatically scaled according to the aspect ratio. + # value: int, eg: 1080; val_size: sizeof(int) + ScreenshotTargetShortSide: MaaCtrlOption = 2 + + # For StartApp + # value: string, eg: "com.hypergryph.arknights/com.u8.sdk.U8UnityContext"; val_size: string length + DefaultAppPackageEntry: MaaCtrlOption = 3 + + # For StopApp + # value: string, eg: "com.hypergryph.arknights"; val_size: string length + DefaultAppPackage: MaaCtrlOption = 4 + + # Dump all screenshots and actions + # this option will || with MaaGlobalOptionEnum.Recording + # value: bool, eg: true; val_size: sizeof(bool) + Recording: MaaCtrlOption = 5 + + +MaaControllerHandle = ctypes.c_void_p + +MaaStringView = ctypes.c_char_p +MaaStringBufferHandle = ctypes.c_void_p + +MaaSize = ctypes.c_size_t + +MaaImageBufferHandle = ctypes.c_void_p +MaaImageRawData = ctypes.c_void_p + +MaaRectHandle = ctypes.c_void_p + +MaaSyncContextHandle = ctypes.c_void_p + +MaaControllerHandle = ctypes.c_void_p + +MaaStatus = ctypes.c_int32 +MaaCtrlId = MaaId +MaaAdbControllerType = ctypes.c_int32 + + +class MaaAdbControllerTypeEnum: + Invalid: MaaAdbControllerType = 0 + + Touch_Adb: MaaAdbControllerType = 1 + Touch_MiniTouch: MaaAdbControllerType = 2 + Touch_MaaTouch: MaaAdbControllerType = 3 + Touch_Mask: MaaAdbControllerType = 0xFF + + Key_Adb: MaaAdbControllerType = 1 << 8 + Key_MaaTouch: MaaAdbControllerType = 2 << 8 + Key_Mask: MaaAdbControllerType = 0xFF00 + + Input_Preset_Adb: MaaAdbControllerType = Touch_Adb | Key_Adb + Input_Preset_Minitouch: MaaAdbControllerType = Touch_MiniTouch | Key_Adb + Input_Preset_Maatouch: MaaAdbControllerType = Touch_MaaTouch | Key_MaaTouch + + Screencap_FastestWay: MaaAdbControllerType = 1 << 16 + Screencap_RawByNetcat: MaaAdbControllerType = 2 << 16 + Screencap_RawWithGzip: MaaAdbControllerType = 3 << 16 + Screencap_Encode: MaaAdbControllerType = 4 << 16 + Screencap_EncodeToFile: MaaAdbControllerType = 5 << 16 + Screencap_MinicapDirect: MaaAdbControllerType = 6 << 16 + Screencap_MinicapStream: MaaAdbControllerType = 7 << 16 + Screencap_Mask: MaaAdbControllerType = 0xFF0000 + + +MaaControllerCallback = MaaApiCallback +MaaTransparentArg = ctypes.c_void_p +MaaCallbackTransparentArg = MaaTransparentArg + +MaaInstanceHandle = ctypes.c_void_p +MaaInstanceCallback = MaaApiCallback +MaaResourceHandle = ctypes.c_void_p + +MaaTaskId = MaaId +MaaCustomRecognizerHandle = ctypes.c_void_p +MaaCustomActionHandle = ctypes.c_void_p + +MaaResourceCallback = MaaApiCallback +MaaResId = MaaId diff --git a/source/binding/Python/maa/future.py b/source/binding/Python/maa/future.py index 408a1fc79..1fa913a80 100644 --- a/source/binding/Python/maa/future.py +++ b/source/binding/Python/maa/future.py @@ -3,14 +3,14 @@ import ctypes from typing import Union -from .define import MaaStatus, MaaId +from .define import MaaStatus, MaaStatusEnum, MaaId class Status: - _status: MaaStatus + _status: MaaStatusEnum - def __init__(self, status: Union[ctypes.c_int32, MaaStatus]): - self._status = MaaStatus(status) + def __init__(self, status: Union[MaaStatus, MaaStatusEnum]): + self._status = MaaStatusEnum(status) def done(self) -> bool: """ @@ -19,7 +19,7 @@ def done(self) -> bool: :return: True if the status is done, False otherwise. """ - return self._status in [MaaStatus.success, MaaStatus.failure] + return self._status in [MaaStatusEnum.success, MaaStatusEnum.failure] def success(self) -> bool: """ @@ -28,7 +28,7 @@ def success(self) -> bool: :return: True if the status is success, False otherwise. """ - return self._status == MaaStatus.success + return self._status == MaaStatusEnum.success def failure(self) -> bool: """ @@ -37,7 +37,7 @@ def failure(self) -> bool: :return: True if the status is failure, False otherwise. """ - return self._status == MaaStatus.failure + return self._status == MaaStatusEnum.failure def pending(self) -> bool: """ @@ -46,7 +46,7 @@ def pending(self) -> bool: :return: True if the status is pending, False otherwise. """ - return self._status == MaaStatus.pending + return self._status == MaaStatusEnum.pending def running(self) -> bool: """ @@ -55,7 +55,7 @@ def running(self) -> bool: :return: True if the status is running, False otherwise. """ - return self._status == MaaStatus.running + return self._status == MaaStatusEnum.running class Future(abc.ABC): diff --git a/source/binding/Python/maa/instance.py b/source/binding/Python/maa/instance.py index 039dd1e28..b7106f52e 100644 --- a/source/binding/Python/maa/instance.py +++ b/source/binding/Python/maa/instance.py @@ -1,10 +1,10 @@ import ctypes import json import asyncio -from typing import Union, Optional, Any +from typing import Dict, Union, Optional, Any -from .define import MaaApiCallback, MaaBool, MaaId -from .future import Future, MaaStatus +from .define import * +from .future import Future, MaaStatusEnum from .library import Library from .callback_agent import CallbackAgent, Callback from .controller import Controller @@ -15,7 +15,7 @@ class Instance: _callback_agent: CallbackAgent - _handle: ctypes.c_void_p + _handle: MaaInstanceHandle def __init__(self, callback: Optional[Callback] = None, callback_arg: Any = None): """ @@ -69,7 +69,7 @@ def inited(self) -> bool: return Library.framework.MaaInited(self._handle) - async def run_task(self, task_type: str, param: Any = {}) -> bool: + async def run_task(self, task_type: str, param: Dict = {}) -> bool: """ Async run a task. @@ -179,9 +179,9 @@ def _status(self, id: int) -> ctypes.c_int32: return Library.framework.MaaTaskStatus(self._handle, id) def _stop_status(self, id: int) -> ctypes.c_int32: - return MaaStatus.success if self.all_finished() else MaaStatus.running + return MaaStatusEnum.success if self.all_finished() else MaaStatusEnum.running - def _set_task_param(self, id: int, param: Any) -> bool: + def _set_task_param(self, id: int, param: Dict) -> bool: return Library.framework.MaaSetTaskParam( self._handle, id, json.dumps(param).encode("utf-8") ) @@ -197,61 +197,70 @@ def _set_api_properties(): return Instance._api_properties_initialized = True - Library.framework.MaaCreate.restype = ctypes.c_void_p - Library.framework.MaaCreate.argtypes = [MaaApiCallback, ctypes.c_void_p] + Library.framework.MaaCreate.restype = MaaInstanceHandle + Library.framework.MaaCreate.argtypes = [ + MaaInstanceCallback, + MaaCallbackTransparentArg, + ] Library.framework.MaaDestroy.restype = None - Library.framework.MaaDestroy.argtypes = [ctypes.c_void_p] + Library.framework.MaaDestroy.argtypes = [MaaInstanceHandle] Library.framework.MaaBindResource.restype = MaaBool - Library.framework.MaaBindResource.argtypes = [ctypes.c_void_p, ctypes.c_void_p] + Library.framework.MaaBindResource.argtypes = [ + MaaInstanceHandle, + MaaResourceHandle, + ] Library.framework.MaaBindController.restype = MaaBool Library.framework.MaaBindController.argtypes = [ - ctypes.c_void_p, - ctypes.c_void_p, + MaaInstanceHandle, + MaaControllerHandle, ] Library.framework.MaaInited.restype = MaaBool - Library.framework.MaaInited.argtypes = [ctypes.c_void_p] + Library.framework.MaaInited.argtypes = [MaaInstanceHandle] Library.framework.MaaPostTask.restype = MaaId Library.framework.MaaPostTask.argtypes = [ - ctypes.c_void_p, - ctypes.c_char_p, - ctypes.c_char_p, + MaaInstanceHandle, + MaaStringView, + MaaStringView, ] Library.framework.MaaSetTaskParam.restype = MaaBool Library.framework.MaaSetTaskParam.argtypes = [ - ctypes.c_void_p, - MaaId, - ctypes.c_char_p, + MaaInstanceHandle, + MaaTaskId, + MaaStringView, ] - Library.framework.MaaTaskStatus.restype = ctypes.c_int32 - Library.framework.MaaTaskStatus.argtypes = [ctypes.c_void_p, MaaId] + Library.framework.MaaTaskStatus.restype = MaaStatus + Library.framework.MaaTaskStatus.argtypes = [ + MaaInstanceHandle, + MaaTaskId, + ] Library.framework.MaaTaskAllFinished.restype = MaaBool - Library.framework.MaaTaskAllFinished.argtypes = [ctypes.c_void_p] + Library.framework.MaaTaskAllFinished.argtypes = [MaaInstanceHandle] Library.framework.MaaPostStop.restype = MaaBool - Library.framework.MaaPostStop.argtypes = [ctypes.c_void_p] + Library.framework.MaaPostStop.argtypes = [MaaInstanceHandle] Library.framework.MaaRegisterCustomRecognizer.restype = MaaBool Library.framework.MaaRegisterCustomRecognizer.argtypes = [ - ctypes.c_void_p, - ctypes.c_char_p, - ctypes.c_void_p, - ctypes.c_void_p, + MaaInstanceHandle, + MaaStringView, + MaaCustomRecognizerHandle, + MaaTransparentArg, ] Library.framework.MaaRegisterCustomAction.restype = MaaBool Library.framework.MaaRegisterCustomAction.argtypes = [ - ctypes.c_void_p, - ctypes.c_char_p, - ctypes.c_void_p, - ctypes.c_void_p, + MaaInstanceHandle, + MaaStringView, + MaaCustomActionHandle, + MaaTransparentArg, ] diff --git a/source/binding/Python/maa/library.py b/source/binding/Python/maa/library.py index b991c1e0d..c8f43a313 100644 --- a/source/binding/Python/maa/library.py +++ b/source/binding/Python/maa/library.py @@ -5,6 +5,8 @@ import platform from typing import Union, Optional +from .define import MaaStringView + class Library: @classmethod @@ -93,7 +95,7 @@ def version(cls) -> str: "Library not initialized, please call `library.open()` first." ) - cls.framework.MaaVersion.restype = ctypes.c_char_p + cls.framework.MaaVersion.restype = MaaStringView cls.framework.MaaVersion.argtypes = None return cls.framework.MaaVersion().decode("utf-8") diff --git a/source/binding/Python/maa/library.pyi b/source/binding/Python/maa/library.pyi new file mode 100644 index 000000000..064b84753 --- /dev/null +++ b/source/binding/Python/maa/library.pyi @@ -0,0 +1,335 @@ +from ctypes import c_int32 +import pathlib +from typing import Optional, Union + +from .define import * + +class _Framework: + # library.py + @staticmethod + def MaaVersion() -> MaaStringView: ... + + # buffer.py + # StringBuffer + @staticmethod + def MaaCreateStringBuffer() -> MaaStringBufferHandle: ... + @staticmethod + def MaaDestroyStringBuffer(handle: MaaStringBufferHandle) -> None: ... + @staticmethod + def MaaIsStringEmpty(handle: MaaStringBufferHandle) -> MaaBool: ... + @staticmethod + def MaaClearString(handle: MaaStringBufferHandle) -> MaaBool: ... + @staticmethod + def MaaGetString(handle: MaaStringBufferHandle) -> MaaStringView: ... + @staticmethod + def MaaGetStringSize(handle: MaaStringBufferHandle) -> MaaSize: ... + @staticmethod + def MaaSetString(handle: MaaStringBufferHandle, str: MaaStringView) -> MaaBool: ... + @staticmethod + def MaaSetStringEx( + handle: MaaStringBufferHandle, + str: MaaStringView, + size: MaaSize, + ) -> MaaBool: ... + # ImageBuffer + @staticmethod + def MaaCreateImageBuffer() -> MaaCreateImageBuffer: ... + @staticmethod + def MaaDestroyImageBuffer(handle: MaaImageBufferHandle) -> None: ... + @staticmethod + def MaaGetImageRawData(handle: MaaImageBufferHandle) -> MaaImageRawData: ... + @staticmethod + def MaaGetImageWidth(handle: MaaImageBufferHandle) -> c_int32: ... + @staticmethod + def MaaGetImageHeight(handle: MaaImageBufferHandle) -> c_int32: ... + @staticmethod + def MaaGetImageType(handle: MaaImageBufferHandle) -> c_int32: ... + @staticmethod + def MaaSetImageRawData( + handle: MaaImageBufferHandle, + data: MaaImageRawData, + width: c_int32, + height: c_int32, + type: c_int32, + ) -> MaaBool: ... + @staticmethod + def MaaIsImageEmpty(handle: MaaImageBufferHandle) -> MaaBool: ... + @staticmethod + def MaaClearImage(handle: MaaImageBufferHandle) -> MaaBool: ... + # RectBuffer + @staticmethod + def MaaCreateRectBuffer() -> MaaRectHandle: ... + @staticmethod + def MaaDestroyRectBuffer(handle: MaaRectHandle) -> None: ... + @staticmethod + def MaaGetRectX(handle: MaaRectHandle) -> c_int32: ... + @staticmethod + def MaaGetRectY(handle: MaaRectHandle) -> c_int32: ... + @staticmethod + def MaaGetRectW(handle: MaaRectHandle) -> c_int32: ... + @staticmethod + def MaaGetRectH(handle: MaaRectHandle) -> c_int32: ... + @staticmethod + def MaaSetRect( + handle: MaaRectHandle, + x: c_int32, + y: c_int32, + w: c_int32, + h: c_int32, + ) -> MaaBool: ... + + # context.py + @staticmethod + def MaaSyncContextRunTask( + sync_context: MaaSyncContextHandle, + task_name: MaaStringView, + param: MaaStringView, + ) -> MaaBool: ... + @staticmethod + def MaaSyncContextRunRecognizer( + sync_context: MaaSyncContextHandle, + image: MaaStringBufferHandle, + task_name: MaaStringView, + task_param: MaaStringView, + out_box: MaaRectHandle, + out_detail: MaaStringBufferHandle, + ) -> MaaBool: ... + @staticmethod + def MaaSyncContextRunAction( + sync_context: MaaSyncContextHandle, + task_name: MaaStringView, + task_param: MaaStringView, + cur_box: MaaRectHandle, + cur_rec_detail: MaaStringBufferHandle, + ) -> MaaBool: ... + @staticmethod + def MaaSyncContextClick( + sync_context: MaaSyncContextHandle, + x: c_int32, + y: c_int32, + ) -> MaaBool: ... + @staticmethod + def MaaSyncContextSwipe( + sync_context: MaaSyncContextHandle, + x1: c_int32, + y1: c_int32, + x2: c_int32, + y2: c_int32, + duration: c_int32, + ) -> MaaBool: ... + @staticmethod + def MaaSyncContextPressKey( + sync_context: MaaSyncContextHandle, + keycode: c_int32, + ) -> MaaBool: ... + @staticmethod + def MaaSyncContextInputText( + sync_context: MaaSyncContextHandle, + text: MaaStringView, + ) -> MaaBool: ... + @staticmethod + def MaaSyncContextTouchDown( + sync_context: MaaSyncContextHandle, + contact: c_int32, + x: c_int32, + y: c_int32, + pressure: c_int32, + ) -> MaaBool: ... + @staticmethod + def MaaSyncContextTouchMove( + sync_context: MaaSyncContextHandle, + contact: c_int32, + x: c_int32, + y: c_int32, + pressure: c_int32, + ) -> MaaBool: ... + @staticmethod + def MaaSyncContextTouchUp( + sync_context: MaaSyncContextHandle, + contact: c_int32, + ) -> MaaBool: ... + @staticmethod + def MaaSyncContextScreencap( + sync_context: MaaSyncContextHandle, + out_image: MaaImageBufferHandle, + ) -> MaaBool: ... + @staticmethod + def MaaSyncContextGetTaskResult( + sync_context: MaaSyncContextHandle, + task_name: MaaStringView, + out_task_result: MaaStringBufferHandle, + ) -> MaaBool: ... + + # controller.py + # Contorller + @staticmethod + def MaaControllerDestroy(ctrl: MaaControllerHandle) -> None: ... + @staticmethod + def MaaControllerSetOption( + ctrl: MaaControllerHandle, + key: MaaCtrlOption, + value: MaaOptionValue, + val_size: MaaOptionValueSize, + ) -> MaaBool: ... + @staticmethod + def MaaControllerPostConnection(ctrl: MaaControllerHandle) -> MaaCtrlId: ... + @staticmethod + def MaaControllerStatus( + ctrl: MaaControllerHandle, + id: MaaCtrlId, + ) -> MaaStatus: ... + @staticmethod + def MaaControllerConnected(ctrl: MaaControllerHandle) -> MaaBool: ... + # AdbController + @staticmethod + def MaaAdbControllerCreateV2( + adb_path: MaaStringView, + address: MaaStringView, + type: MaaAdbControllerType, + config: MaaStringView, + agent_path: MaaStringView, + callback: MaaControllerCallback, + callback_arg: MaaCallbackTransparentArg, + ) -> MaaControllerHandle: ... + + # instance.py + @staticmethod + def MaaCreate( + callback: MaaInstanceCallback, + callback_arg: MaaCallbackTransparentArg, + ) -> MaaInstanceHandle: ... + @staticmethod + def MaaDestroy(inst: MaaInstanceHandle) -> None: ... + @staticmethod + def MaaBindResource( + inst: MaaInstanceHandle, + res: MaaResourceHandle, + ) -> MaaBool: ... + @staticmethod + def MaaBindController( + inst: MaaInstanceHandle, + ctrl: MaaControllerHandle, + ) -> MaaBool: ... + @staticmethod + def MaaInited(inst: MaaInstanceHandle) -> MaaBool: ... + @staticmethod + def MaaPostTask( + inst: MaaInstanceHandle, + entry: MaaStringView, + param: MaaStringView, + ) -> MaaTaskId: ... + @staticmethod + def MaaSetTaskParam( + inst: MaaInstanceHandle, + id: MaaTaskId, + param: MaaStringView, + ) -> MaaBool: ... + @staticmethod + def MaaTaskStatus( + inst: MaaInstanceHandle, + id: MaaTaskId, + ) -> MaaStatus: ... + @staticmethod + def MaaTaskAllFinished(inst: MaaInstanceHandle) -> MaaBool: ... + @staticmethod + def MaaPostStop(inst: MaaInstanceHandle) -> MaaBool: ... + @staticmethod + def MaaRegisterCustomRecognizer( + inst: MaaInstanceHandle, + name: MaaStringView, + recognizer: MaaCustomRecognizerHandle, + recognizer_arg: MaaTransparentArg, + ) -> MaaBool: ... + @staticmethod + def MaaRegisterCustomAction( + inst: MaaInstanceHandle, + name: MaaStringView, + action: MaaCustomActionHandle, + action_arg: MaaTransparentArg, + ) -> MaaBool: ... + + # resource.py + @staticmethod + def MaaResourceCreate( + callback: MaaResourceCallback, + callback_arg: MaaCallbackTransparentArg, + ) -> MaaResourceHandle: ... + @staticmethod + def MaaResourceDestroy(res: MaaResourceHandle) -> None: ... + @staticmethod + def MaaResourcePostPath( + res: MaaResourceHandle, + path: MaaStringView, + ) -> MaaResId: ... + @staticmethod + def MaaResourceStatus( + res: MaaResourceHandle, + id: MaaResId, + ) -> MaaStatus: ... + @staticmethod + def MaaResourceLoaded(res: MaaResourceHandle) -> MaaBool: ... + +class _Toolkit: + # toolkit.py + @staticmethod + def MaaToolkitInit() -> MaaBool: ... + @staticmethod + def MaaToolkitUninit() -> MaaBool: ... + @staticmethod + def MaaToolkitPostFindDevice() -> MaaBool: ... + @staticmethod + def MaaToolkitIsFindDeviceCompleted() -> MaaBool: ... + @staticmethod + def MaaToolkitGetDeviceCount() -> MaaSize: ... + @staticmethod + def MaaToolkitGetDeviceName(index: MaaSize) -> MaaStringView: ... + @staticmethod + def MaaToolkitGetDeviceAdbPath(index: MaaSize) -> MaaStringView: ... + @staticmethod + def MaaToolkitGetDeviceAdbSerial(index: MaaSize) -> MaaStringView: ... + @staticmethod + def MaaToolkitGetDeviceAdbControllerType( + index: MaaSize, + ) -> MaaAdbControllerType: ... + @staticmethod + def MaaToolkitGetDeviceAdbConfig(index: MaaSize) -> MaaStringView: ... + @staticmethod + def MaaToolkitRegisterCustomRecognizerExecutor( + handle: MaaInstanceHandle, + recognizer_name: MaaStringView, + recognizer_exec_path: MaaStringView, + recognizer_exec_param_json: MaaStringView, + ) -> MaaBool: ... + @staticmethod + def MaaToolkitRegisterCustomActionExecutor( + handle: MaaInstanceHandle, + action_name: MaaStringView, + action_exec_path: MaaStringView, + action_ex: MaaStringView, + ) -> MaaBool: ... + +class Library: + framework: _Framework + toolkit: _Toolkit + + @classmethod + def open( + cls, + path: Union[pathlib.Path, str], + toolkit: bool = True, + ) -> Optional[str]: + """ + Open the library at the given path. + + :param path: The path to the library. + :return: True if the library was successfully opened, False otherwise. + """ + pass + @classmethod + def version(cls) -> str: + """ + Get the version of the library. + + :return: The version of the library. + """ + pass diff --git a/source/binding/Python/maa/resource.py b/source/binding/Python/maa/resource.py index a33601edb..26fad9d61 100644 --- a/source/binding/Python/maa/resource.py +++ b/source/binding/Python/maa/resource.py @@ -3,14 +3,14 @@ import asyncio from typing import Union, Optional, Any -from .define import MaaApiCallback, MaaBool, MaaId +from .define import * from .future import Future from .library import Library from .callback_agent import CallbackAgent, Callback class Resource: - _handle: ctypes.c_void_p + _handle: MaaResourceHandle _callback_agent: CallbackAgent def __init__(self, callback: Optional[Callback] = None, callback_arg: Any = None): @@ -89,20 +89,26 @@ def _set_api_properties(): return Resource._api_properties_initialized = True - Library.framework.MaaResourceCreate.restype = ctypes.c_void_p - Library.framework.MaaResourceCreate.argtypes = [MaaApiCallback, ctypes.c_void_p] + Library.framework.MaaResourceCreate.restype = MaaResourceHandle + Library.framework.MaaResourceCreate.argtypes = [ + MaaResourceCallback, + MaaCallbackTransparentArg, + ] Library.framework.MaaResourceDestroy.restype = None - Library.framework.MaaResourceDestroy.argtypes = [ctypes.c_void_p] + Library.framework.MaaResourceDestroy.argtypes = [MaaResourceHandle] - Library.framework.MaaResourcePostPath.restype = MaaId + Library.framework.MaaResourcePostPath.restype = MaaResId Library.framework.MaaResourcePostPath.argtypes = [ - ctypes.c_void_p, - ctypes.c_char_p, + MaaResourceHandle, + MaaStringView, ] - Library.framework.MaaResourceStatus.restype = ctypes.c_int32 - Library.framework.MaaResourceStatus.argtypes = [ctypes.c_void_p, MaaId] + Library.framework.MaaResourceStatus.restype = MaaStatus + Library.framework.MaaResourceStatus.argtypes = [ + MaaResourceHandle, + MaaResId, + ] Library.framework.MaaResourceLoaded.restype = MaaBool - Library.framework.MaaResourceLoaded.argtypes = [ctypes.c_void_p] + Library.framework.MaaResourceLoaded.argtypes = [MaaResourceHandle] diff --git a/source/binding/Python/maa/toolkit.py b/source/binding/Python/maa/toolkit.py index f2c83b16c..ab7e17e1f 100644 --- a/source/binding/Python/maa/toolkit.py +++ b/source/binding/Python/maa/toolkit.py @@ -5,7 +5,7 @@ from typing import List from .library import Library -from .define import MaaBool +from .define import * from .instance import Instance @@ -122,38 +122,39 @@ def _set_api_properties(): Library.toolkit.MaaToolkitIsFindDeviceCompleted.restype = MaaBool Library.toolkit.MaaToolkitIsFindDeviceCompleted.argtypes = None - Library.toolkit.MaaToolkitGetDeviceCount.restype = ctypes.c_size_t + Library.toolkit.MaaToolkitGetDeviceCount.restype = MaaSize Library.toolkit.MaaToolkitGetDeviceCount.argtypes = None - Library.toolkit.MaaToolkitGetDeviceName.restype = ctypes.c_char_p - Library.toolkit.MaaToolkitGetDeviceName.argtypes = [ctypes.c_size_t] + Library.toolkit.MaaToolkitGetDeviceName.restype = MaaStringView + Library.toolkit.MaaToolkitGetDeviceName.argtypes = [MaaSize] - Library.toolkit.MaaToolkitGetDeviceAdbPath.restype = ctypes.c_char_p - Library.toolkit.MaaToolkitGetDeviceAdbPath.argtypes = [ctypes.c_size_t] + Library.toolkit.MaaToolkitGetDeviceAdbPath.restype = MaaStringView + Library.toolkit.MaaToolkitGetDeviceAdbPath.argtypes = [MaaSize] - Library.toolkit.MaaToolkitGetDeviceAdbSerial.restype = ctypes.c_char_p - Library.toolkit.MaaToolkitGetDeviceAdbSerial.argtypes = [ctypes.c_size_t] + Library.toolkit.MaaToolkitGetDeviceAdbSerial.restype = MaaStringView + Library.toolkit.MaaToolkitGetDeviceAdbSerial.argtypes = [MaaSize] - Library.toolkit.MaaToolkitGetDeviceAdbControllerType.restype = ctypes.c_int32 - Library.toolkit.MaaToolkitGetDeviceAdbControllerType.argtypes = [ - ctypes.c_size_t - ] + Library.toolkit.MaaToolkitGetDeviceAdbControllerType.restype = ( + MaaAdbControllerType + ) + + Library.toolkit.MaaToolkitGetDeviceAdbControllerType.argtypes = [MaaSize] - Library.toolkit.MaaToolkitGetDeviceAdbConfig.restype = ctypes.c_char_p - Library.toolkit.MaaToolkitGetDeviceAdbConfig.argtypes = [ctypes.c_size_t] + Library.toolkit.MaaToolkitGetDeviceAdbConfig.restype = MaaStringView + Library.toolkit.MaaToolkitGetDeviceAdbConfig.argtypes = [MaaSize] Library.toolkit.MaaToolkitRegisterCustomRecognizerExecutor.restype = MaaBool Library.toolkit.MaaToolkitRegisterCustomRecognizerExecutor.argtypes = [ - ctypes.c_void_p, - ctypes.c_char_p, - ctypes.c_char_p, - ctypes.c_char_p, + MaaInstanceHandle, + MaaStringView, + MaaStringView, + MaaStringView, ] Library.toolkit.MaaToolkitRegisterCustomActionExecutor.restype = MaaBool Library.toolkit.MaaToolkitRegisterCustomActionExecutor.argtypes = [ - ctypes.c_void_p, - ctypes.c_char_p, - ctypes.c_char_p, - ctypes.c_char_p, + MaaInstanceHandle, + MaaStringView, + MaaStringView, + MaaStringView, ]