Skip to content

Commit

Permalink
chore: 调整 Python Binding 初始化行为
Browse files Browse the repository at this point in the history
  • Loading branch information
weinibuliu committed Dec 4, 2024
1 parent 5b0ae32 commit ca12499
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 39 deletions.
15 changes: 4 additions & 11 deletions source/binding/Python/maa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
18 changes: 0 additions & 18 deletions source/binding/Python/maa/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
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))

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

0 comments on commit ca12499

Please sign in to comment.