Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: 调整 Python Binding 初始化行为 #445

Merged
merged 11 commits into from
Dec 4, 2024
10 changes: 5 additions & 5 deletions source/binding/Python/maa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
from pathlib import Path

from .library import Library

__PATH = os.path.join(os.path.dirname(__file__), "bin")
__PATH = Path(os.environ.get("MAAFW_BINARY_PATH"))
if not __PATH:
__PATH = Path(Path(__file__).parent, "bin")

if os.path.exists(__PATH):
ver = Library.open(__PATH)
if ver:
print(f"MaaFw version: {ver}")
Library.open(__PATH)
22 changes: 0 additions & 22 deletions source/binding/Python/maa/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ class StringBuffer:
_own: bool

def __init__(self, handle: Optional[MaaStringBufferHandle] = None):
if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
)
self._set_api_properties()

if handle:
Expand Down Expand Up @@ -97,10 +93,6 @@ class StringListBuffer:
_own: bool

def __init__(self, handle: Optional[MaaStringListBufferHandle] = None):
if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
)
self._set_api_properties()

if handle:
Expand Down Expand Up @@ -203,11 +195,6 @@ class ImageBuffer:
_own: bool

def __init__(self, c_handle: Optional[MaaImageBufferHandle] = None):
if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
)

self._set_api_properties()

if c_handle:
Expand Down Expand Up @@ -309,10 +296,6 @@ class ImageListBuffer:
_own: bool

def __init__(self, c_handle: Optional[MaaImageListBufferHandle] = None):
if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
)
self._set_api_properties()

if c_handle:
Expand Down Expand Up @@ -413,11 +396,6 @@ class RectBuffer:
_own: bool

def __init__(self, c_handle: Optional[MaaRectHandle] = None):
if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
)

self._set_api_properties()

if c_handle:
Expand Down
7 changes: 1 addition & 6 deletions source/binding/Python/maa/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ def __init__(
self,
handle: Optional[MaaControllerHandle] = None,
):
if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
)

self._set_api_properties()

if handle:
Expand Down Expand Up @@ -152,7 +147,7 @@ def set_screenshot_target_short_side(self, short_side: int) -> bool:
ctypes.sizeof(ctypes.c_int32),
)
)

def set_screenshot_use_raw_size(self, enable: bool) -> bool:
cbool = MaaBool(enable)
return bool(
Expand Down
14 changes: 4 additions & 10 deletions source/binding/Python/maa/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

class Library:

initialized = False

@staticmethod
def open(path: Union[pathlib.Path, str]) -> Optional[str]:
if not path.exists():
raise FileNotFoundError(f"`{path}` is not existed.")

platform_values = {
"windows": ("MaaFramework.dll", "MaaToolkit.dll"),
"darwin": ("libMaaFramework.dylib", "libMaaToolkit.dylib"),
Expand Down Expand Up @@ -45,21 +46,14 @@ def open(path: Union[pathlib.Path, str]) -> Optional[str]:
Library.toolkit = lib_import(str(Library.toolkit_libpath))
MistEO marked this conversation as resolved.
Show resolved Hide resolved

if not Library.framework or not Library.toolkit:
Library.initialized = False
return None
raise RuntimeError("Fail to open the library.")

Library._set_api_properties()
Library.initialized = True

return Library.version()

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

return Library.framework.MaaVersion().decode()

@staticmethod
Expand Down
6 changes: 0 additions & 6 deletions source/binding/Python/maa/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ def __init__(
notification_handler: Optional[NotificationHandler] = None,
handle: Optional[MaaResourceHandle] = None,
):

if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
)

self._set_api_properties()

if handle:
Expand Down
6 changes: 0 additions & 6 deletions source/binding/Python/maa/tasker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ctypes
import json
import time
from pathlib import Path
from typing import Any, Dict, Optional, Union

Expand All @@ -25,11 +24,6 @@ def __init__(
notification_handler: Optional[NotificationHandler] = None,
handle: Optional[MaaTaskerHandle] = None,
):
if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
)

self._set_api_properties()

if handle:
Expand Down
5 changes: 0 additions & 5 deletions source/binding/Python/maa/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,6 @@ def _set_api_properties():
return
Toolkit._api_properties_initialized = True

if not Library.initialized:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
)

Library.toolkit.MaaToolkitConfigInitOption.restype = MaaBool
Library.toolkit.MaaToolkitConfigInitOption.argtypes = [
ctypes.c_char_p,
Expand Down
21 changes: 13 additions & 8 deletions test/python/binding_test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import os
from pathlib import Path
import numpy
import sys

import numpy

if len(sys.argv) < 2:
print("Usage: python binding_test.py <install_dir>")
sys.exit(1)

install_dir = Path(sys.argv[1]).resolve()
print(f"install_dir: {install_dir}")
binding_dir = Path(install_dir, "binding", "Python")

binding_dir = str(install_dir / "binding" / "Python")
if binding_dir not in sys.path:
sys.path.insert(0, binding_dir)
os.environ["MAAFW_BINARY_PATH"] = str(f"{binding_dir}/bin")
MistEO marked this conversation as resolved.
Show resolved Hide resolved
print(f"install_dir: {install_dir}")
print(f"binding_dir: {binding_dir}")

if str(binding_dir) not in sys.path:
sys.path.insert(0, str(binding_dir))

from maa.library import Library
from maa.resource import Resource
Expand Down Expand Up @@ -257,7 +261,9 @@ def click(self, x: int, y: int) -> bool:
return True

def swipe(self, x1: int, y1: int, x2: int, y2: int, duration: int) -> bool:
print(f"on MyController.swipe, x1: {x1}, y1: {y1}, x2: {x2}, y2: {y2}, duration: {duration}")
print(
f"on MyController.swipe, x1: {x1}, y1: {y1}, x2: {x2}, y2: {y2}, duration: {duration}"
)
self.count += 1
return True

Expand Down Expand Up @@ -304,8 +310,7 @@ def input_text(self, text: str) -> bool:


if __name__ == "__main__":
version = Library.open(install_dir / "bin")
print(f"MaaFw Version: {version}")
print(f"MaaFw Version: {Library.version()}")

Toolkit.init_option(install_dir / "bin")

Expand Down
Loading