Files
MaaAssistantArknights/src/Python/asst/asst.py
Lucien Shaw e461208bf9 chore: 完善容器配置及依赖安装 (#14208)
格式化工具部分:
1. pre-commit 引入 python 的格式化工具,包括 black(代码格式化)和 isort(对“包导入顺序”的规范)
2. 允许 prettier 对文档站的 markdown 文件格式化
3. 不允许 prettier 对 markdown 文件中的代码块的代码本身进行格式化
4. 升级了 pre-commit 的各个 hook 的版本
5. 优化了 pre-commit 的日志文本显示

容器总览部分:
1. 由原来的单一轻量环境转为区分空环境、轻量环境和全量环境
2. 空环境是裸 Linux 镜像(Ubuntu),为默认环境
3. 轻量环境适合开发文档站前端
4. 全量环境适合开发 MaaCore
5. 目前,全量环境完整包含了轻量环境,轻量环境完整包含了空环境
6. 在仓库 README.md 中更新了三个环境的描述,并将链接分别设置为对应环境的创建链接
   **注意:没有修改文档站中的对应文件**
7. 在各个语言的开发指南的最后,移除了 Codespace 部分的“安装额外依赖”相关描述,且将链接设置为全量环境的创建链接
   **注意:没有添加“开发文档站”的指南和对应 codespace 的使用方式**

容器的轻量环境和全量环境共有部分:
1. 安装 black 和 isort 包
2. 调整 VS Code 设置,取消先前(对 markdown 文件单独指定 markdownlint 扩展作为格式化工具)的错误设置,现在 markdown 文件仍然使用默认的 prettier 扩展作为格式化工具
3. 引入 markdown-all-in-one 扩展作为语法提示工具
4. 将 node_modules 和 3rdparty 排除在 VS Code 的文本的搜索路径之外

容器的全量环境部分:
1. 为 tools 下的所有 python 脚本安装依赖
2. 使用 tools/maadeps-download.py 下载 maadeps,且将必要二进制文件软链接到 /usr/local/bin/
3. 使用 apt 安装 cmake 和 clangd-20,将后者通过 update-alternatives 设置为系统 clangd 的默认版本
4. 使用 cmake tools 扩展,并按照 Linux 编译方法进行配置
5. 使用 clang-format 作为 c/cpp 的格式化工具,clang-format 程序主体来自 maadeps(已经软链接到 /usr/local/bin/)
6. 使用 clangd 作为 c/cpp 的语法提示工具
7. 将 MaaDeps、install 和 build 排除在 VS Code 的文本搜索路径之外

其它手动调整:
1. 更新文档站的 package.json,指定 pnpm 包管理器的版本
2. 手动保证 markdown 文件中的列表前后有空行(注意到 MarkdownLint 官方规则不一定能精准定位所有“列表前后空行”的问题,详见:https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md#md032---lists-should-be-surrounded-by-blank-lines )
3. 修改了部分 markdown 文件中的 json 代码块的语法问题
   **注意:相同的问题并未全部发现,仅修改了两处**
4. 在 tools 目录中,一处 python 脚本的包名误用(本地包名和某个 pip 包重名),这里修改了相应代码
5. 在 tools 目录中,一处 python 脚本使用了弃用的包 cchardet 的问题,这里更换了推荐使用的功能相近的包并修改了相应代码

自动化脚本提交的修改:
1. 自动格式化了大量 tools 中的 python 脚本
2. 自动格式化了大量 docs 中的 markdown 文件

Commits:

* chore: pre-commit引入black和isort规范py文件

* chore: Auto update by pre-commit hooks [skip changelog]

* chore: devcontainer添加isort扩展,排序python导入

* chore: pre-commit任务命名及更名

* style: isort fix

* chore: Auto update by pre-commit hooks [skip changelog]

* chore: 更新pre-commit的hook版本

* fix: 模块名与第三方库重名,大忌

* chore: 容器构建时额外安装isort

* docs: md -> markdown

* chore: 容器安装python包和maadeps

* fix: 修复过时python包

* chore: 指定pnpm版本

* chore: container支持选择轻量环境

* chore: 去掉rust

* chore: add plain env

* chore: 使用clangd语言服务器

* chore: 无需单独设置markdown的格式化工具

* chore: 更新安装的clangd版本

* docs: 简易文档适配

* docs: 在仓库README中重新编排codespaces相关指引

* chore: Auto update by pre-commit hooks [skip changelog]

* style: 调整缩进

* chore: 格式化工具不用特意排除被gitignore忽略的文件

* chore: sh文件在gitattributes中单列一类

* chore: 格式化docs下的markdown文件

* chore: 暂时不修改md文件中的代码块

* style: 人为明确markdown中的部分列表相关格式

* docs: 补上部分markdown的json代码块中缺失的逗号

* chore: Auto update by pre-commit hooks [skip changelog]

* chore: Auto update by pre-commit hooks [skip changelog]

* fix: 补上tools的服务器排序相关代码中缺失的逗号

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* chore: 使用maadeps的clangd

* build: 更新maadeps工具链版本

* style: prettier fix

* revert: 还原maadeps版本

* revert: 取消使用maadeps的clangd依赖,改用系统apt安装

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-09-30 19:39:48 +08:00

341 lines
10 KiB
Python

import ctypes
import ctypes.util
import json
import os
import pathlib
import platform
from typing import Optional, Union
from .utils import JSON, InstanceOptionType, StaticOptionType
class Asst:
CallBackType = ctypes.CFUNCTYPE(
None, ctypes.c_int, ctypes.c_char_p, ctypes.c_void_p
)
"""
回调函数,使用实例可参照 my_callback
:params:
``param1 message``: 消息类型
``param2 details``: json string
``param3 arg``: 自定义参数
"""
@staticmethod
def load(
path: Union[pathlib.Path, str],
incremental_path: Optional[Union[pathlib.Path, str]] = None,
user_dir: Optional[Union[pathlib.Path, str]] = None,
) -> bool:
"""
加载 dll 及资源
:params:
``path``: DLL及资源所在文件夹路径
``incremental_path``: 增量资源所在文件夹路径
``user_dir``: 用户数据(日志、调试图片等)写入文件夹路径
"""
platform_values = {
"windows": {"libpath": "MaaCore.dll", "environ_var": "PATH"},
"darwin": {
"libpath": "libMaaCore.dylib",
"environ_var": "DYLD_LIBRARY_PATH",
},
"linux": {"libpath": "libMaaCore.so", "environ_var": "LD_LIBRARY_PATH"},
}
lib_import_func = None
platform_type = platform.system().lower()
if platform_type == "windows":
lib_import_func = ctypes.WinDLL
else:
lib_import_func = ctypes.CDLL
Asst.__libpath = pathlib.Path(path) / platform_values[platform_type]["libpath"]
try:
os.environ[
platform_values[platform_type]["environ_var"]
] += os.pathsep + str(path)
except KeyError:
os.environ[platform_values[platform_type]["environ_var"]] = (
os.pathsep + str(path)
)
try:
Asst.__lib = lib_import_func(str(Asst.__libpath))
except OSError:
Asst.__libpath = ctypes.util.find_library("MaaCore")
Asst.__lib = lib_import_func(str(Asst.__libpath))
Asst.__set_lib_properties()
ret: bool = True
if user_dir:
ret &= Asst.__lib.AsstSetUserDir(str(user_dir).encode("utf-8"))
ret &= Asst.__lib.AsstLoadResource(str(path).encode("utf-8"))
if incremental_path:
ret &= Asst.__lib.AsstLoadResource(str(incremental_path).encode("utf-8"))
return ret
def __init__(self, callback: CallBackType = None, arg=None):
"""
:params:
``callback``: 回调函数
``arg``: 自定义参数
"""
self.__callback = callback
if callback:
self.__ptr = Asst.__lib.AsstCreateEx(callback, arg)
else:
self.__ptr = Asst.__lib.AsstCreate()
def __del__(self):
Asst.__lib.AsstDestroy(self.__ptr)
self.__ptr = None
def set_instance_option(self, option_type: InstanceOptionType, option_value: str):
"""
设置额外配置
参见${MaaAssistantArknights}/src/MaaCore/Assistant.cpp#set_instance_option
:params:
``externa_config``: 额外配置类型
``config_value``: 额外配置的值
:return: 是否设置成功
"""
return Asst.__lib.AsstSetInstanceOption(
self.__ptr, int(option_type), option_value.encode("utf-8")
)
def set_static_option(option_type: StaticOptionType, option_value: str):
"""
设置进程级参数
参见${MaaAssistantArknights}/src/MaaCore/Assistant.cpp#set_static_option
:params:
``option_type``: 进程级参数类型
``option_value``: 进程级参数的值
:return: 是否设置成功
"""
return Asst.__lib.AsstSetStaticOption(
int(option_type), option_value.encode("utf-8")
)
def connect(self, adb_path: str, address: str, config: str = "General"):
"""
连接设备
:params:
``adb_path``: adb 程序的路径
``address``: adb 地址+端口
``config``: adb 配置,可参考 resource/config.json
:return: 是否连接成功
"""
return Asst.__lib.AsstConnect(
self.__ptr,
adb_path.encode("utf-8"),
address.encode("utf-8"),
config.encode("utf-8"),
)
def get_image(self, size: int) -> bytes | None:
"""
获取上次截图
:params:
``size``: 图像字节数, 如 1280*720*3
: return: 成功时图像的字节; 失败时 None
"""
buffer_type = ctypes.c_byte * size
buffer = buffer_type()
buffer.value = b"\000" * size
if (got := Asst.__lib.AsstGetImage(self.__ptr, buffer, size)) and got > 0:
return bytes(buffer)
else:
return None
def set_connection_extras(name: str, extras: JSON):
"""
连接模拟器端的Extras
:params:
``name``: Extras名称
``extras``: Extras配置
"""
Asst.__lib.AsstSetConnectionExtras(
name.encode("utf-8"), json.dumps(extras, ensure_ascii=False).encode("utf-8")
)
TaskId = int
def append_task(self, type_name: str, params: JSON = {}) -> TaskId:
"""
添加任务
:params:
``type_name``: 任务类型,请参考 docs/集成文档.md
``params``: 任务参数,请参考 docs/集成文档.md
:return: 任务 ID, 可用于 set_task_params 接口
"""
return Asst.__lib.AsstAppendTask(
self.__ptr,
type_name.encode("utf-8"),
json.dumps(params, ensure_ascii=False).encode("utf-8"),
)
def set_task_params(self, task_id: TaskId, params: JSON) -> bool:
"""
动态设置任务参数
:params:
``task_id``: 任务 ID, 使用 append_task 接口的返回值
``params``: 任务参数,同 append_task 接口,请参考 docs/集成文档.md
:return: 是否成功
"""
return Asst.__lib.AsstSetTaskParams(
self.__ptr, task_id, json.dumps(params, ensure_ascii=False).encode("utf-8")
)
def start(self) -> bool:
"""
开始任务
:return: 是否成功
"""
return Asst.__lib.AsstStart(self.__ptr)
def stop(self) -> bool:
"""
停止并清空所有任务
:return: 是否成功
"""
return Asst.__lib.AsstStop(self.__ptr)
def running(self) -> bool:
"""
是否正在运行
:return: 是否正在运行
"""
return Asst.__lib.AsstRunning(self.__ptr)
@staticmethod
def log(level: str, message: str) -> None:
"""
打印日志
:params:
``level``: 日志等级标签
``message``: 日志内容
"""
Asst.__lib.AsstLog(level.encode("utf-8"), message.encode("utf-8"))
def get_version(self) -> str:
"""
获取DLL版本号
: return: 版本号
"""
return Asst.__lib.AsstGetVersion().decode("utf-8")
@staticmethod
def __set_lib_properties():
Asst.__lib.AsstSetUserDir.restype = ctypes.c_bool
Asst.__lib.AsstSetUserDir.argtypes = (ctypes.c_char_p,)
Asst.__lib.AsstLoadResource.restype = ctypes.c_bool
Asst.__lib.AsstLoadResource.argtypes = (ctypes.c_char_p,)
Asst.__lib.AsstSetStaticOption.restype = ctypes.c_bool
Asst.__lib.AsstSetStaticOption.argtypes = (
ctypes.c_int,
ctypes.c_char_p,
)
Asst.__lib.AsstSetConnectionExtras.restype = ctypes.c_void_p
Asst.__lib.AsstSetConnectionExtras.argtypes = (
ctypes.c_char_p,
ctypes.c_char_p,
)
Asst.__lib.AsstGetImage.restype = ctypes.c_uint64
Asst.__lib.AsstGetImage.argtypes = (
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_uint64,
)
Asst.__lib.AsstCreate.restype = ctypes.c_void_p
Asst.__lib.AsstCreate.argtypes = ()
Asst.__lib.AsstCreateEx.restype = ctypes.c_void_p
Asst.__lib.AsstCreateEx.argtypes = (
ctypes.c_void_p,
ctypes.c_void_p,
)
Asst.__lib.AsstDestroy.argtypes = (ctypes.c_void_p,)
Asst.__lib.AsstSetInstanceOption.restype = ctypes.c_bool
Asst.__lib.AsstSetInstanceOption.argtypes = (
ctypes.c_void_p,
ctypes.c_int,
ctypes.c_char_p,
)
Asst.__lib.AsstConnect.restype = ctypes.c_bool
Asst.__lib.AsstConnect.argtypes = (
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
)
Asst.__lib.AsstAsyncConnect.restype = ctypes.c_int
Asst.__lib.AsstAsyncConnect.argtypes = (
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_bool,
)
Asst.__lib.AsstAppendTask.restype = ctypes.c_int
Asst.__lib.AsstAppendTask.argtypes = (
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
)
Asst.__lib.AsstSetTaskParams.restype = ctypes.c_bool
Asst.__lib.AsstSetTaskParams.argtypes = (
ctypes.c_void_p,
ctypes.c_int,
ctypes.c_char_p,
)
Asst.__lib.AsstStart.restype = ctypes.c_bool
Asst.__lib.AsstStart.argtypes = (ctypes.c_void_p,)
Asst.__lib.AsstStop.restype = ctypes.c_bool
Asst.__lib.AsstStop.argtypes = (ctypes.c_void_p,)
Asst.__lib.AsstRunning.restype = ctypes.c_bool
Asst.__lib.AsstRunning.argtypes = (ctypes.c_void_p,)
Asst.__lib.AsstGetVersion.restype = ctypes.c_char_p
Asst.__lib.AsstLog.restype = None
Asst.__lib.AsstLog.argtypes = (ctypes.c_char_p, ctypes.c_char_p)