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 2 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
17 changes: 12 additions & 5 deletions source/binding/Python/maa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import os
from pathlib import Path

from .library import Library

__PATH = os.path.join(os.path.dirname(__file__), "bin")
__PATH = Path(Path(__file__).parent, "bin")

if os.path.exists(__PATH):
# If you want to print maafw version,please use `from maa import ver`
ver = None

Copy link
Member

Choose a reason for hiding this comment

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

这个不需要,有 Library.version()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个只是对 ver 进行初始化来着(虽然没什么意义,但这样或许是符合规范的?)
而且使用 ver 来获取版本信息从结果来看也比较直观,因为 open() 的返回值实际上就是 version() 的返回值,所以直接访问 maa.ver 的好处在于无需再运行一遍 version() 函数,并且代码中不需要 import maa.Library (不过需要 from maa import ver),玛丽佬觉得呢

Copy link
Member

Choose a reason for hiding this comment

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

我感觉没有必要,就是一个简单的获取接口,py 这边没必要缓存一份

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我感觉没有必要,就是一个简单的获取接口,py 这边没必要缓存一份

因为原设计就是缓存了一份 ver ,或许已经有人这么用了?如果不缓存 ver ,完全可以省略掉这个变量

Copy link
Member

Choose a reason for hiding this comment

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

去掉吧没必要搞个这个

if __PATH.exists():
ver = Library.open(__PATH)
if ver:
print(f"MaaFw version: {ver}")
if not ver:
raise RuntimeError(
"Library not initialized, please call `library.open()` first."
)
else:
raise FileNotFoundError(f"{__PATH} is not exists.")
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