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

chore: 调整 Python Binding 初始化行为 #444

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions source/binding/Python/maa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import os
from pathlib import Path
import sys

from .library import Library

__PATH = os.path.join(os.path.dirname(__file__), "bin")
__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 os.path.exists(__PATH):
ver = Library.open(__PATH)
if ver:
print(f"MaaFw version: {ver}")
__PATH = Path(Path(sys.argv[1]).resolve(), "bin")
if not __PATH.exists():
raise FileNotFoundError(f"`{__PATH}` is not existed.")

ver = Library.open(__PATH)
if not ver:
raise RuntimeError("Fail to open the library.")
4 changes: 0 additions & 4 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initialized 这个变量看看能不能也删了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initialized 这个变量看看能不能也删了

或许是可以的,这个变量的用途应该只是在其他文件中检查 library 初始化状态,现在在初始化失败时直接 raise 了

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

if 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
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
Loading