diff --git a/source/binding/Python/maa/__init__.py b/source/binding/Python/maa/__init__.py index 6faf3cf6c..3e99f522f 100644 --- a/source/binding/Python/maa/__init__.py +++ b/source/binding/Python/maa/__init__.py @@ -3,16 +3,9 @@ from .library import Library -__PATH = Path(Path(__file__).parent, "bin") -if not __PATH.exists(): - # sys.argv is designed for Github Action,we seldom use this. - if len(sys.argv) < 2: - raise FileNotFoundError(f"`{__PATH}` is not existed.") - +if len(sys.argv) > 2: __PATH = Path(Path(sys.argv[1]).resolve(), "bin") - if not __PATH.exists(): - raise FileNotFoundError(f"`{__PATH}` is not existed.") +else: + __PATH = Path(Path(__file__).parent, "bin") -ver = Library.open(__PATH) -if not ver: - raise RuntimeError("Fail to open the library.") +Library.open(__PATH) diff --git a/source/binding/Python/maa/buffer.py b/source/binding/Python/maa/buffer.py index 7698032ea..7c14efc4e 100644 --- a/source/binding/Python/maa/buffer.py +++ b/source/binding/Python/maa/buffer.py @@ -93,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: @@ -199,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: @@ -305,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: @@ -409,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: diff --git a/source/binding/Python/maa/library.py b/source/binding/Python/maa/library.py index 423454729..27839dd0f 100644 --- a/source/binding/Python/maa/library.py +++ b/source/binding/Python/maa/library.py @@ -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"), @@ -45,21 +46,14 @@ def open(path: Union[pathlib.Path, str]) -> Optional[str]: Library.toolkit = lib_import(str(Library.toolkit_libpath)) 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