mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-16 01:40:46 +08:00
格式化工具部分: 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>
262 lines
7.7 KiB
Python
262 lines
7.7 KiB
Python
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import re
|
|
import struct
|
|
from argparse import ArgumentParser
|
|
|
|
import numpy as np
|
|
from PIL import Image
|
|
from tqdm import tqdm
|
|
|
|
|
|
def remove_auxiliary_data(input_file, output_file):
|
|
with open(input_file, "rb") as f:
|
|
data = f.read()
|
|
|
|
output_data = bytearray()
|
|
output_data.extend(data[:8]) # PNG signature
|
|
|
|
# Locate and copy critical chunks
|
|
start = 8
|
|
while start < len(data):
|
|
length, chunk_type = struct.unpack(">I4s", data[start : start + 8])
|
|
start += 8
|
|
chunk_data = data[start : start + length]
|
|
start += length
|
|
crc = data[start : start + 4]
|
|
start += 4
|
|
|
|
if chunk_type in [b"IHDR", b"PLTE", b"IDAT", b"IEND"]:
|
|
output_data.extend(struct.pack(">I4s", length, chunk_type))
|
|
output_data.extend(chunk_data)
|
|
output_data.extend(crc)
|
|
|
|
with open(output_file, "wb") as f:
|
|
f.write(output_data)
|
|
|
|
|
|
def get_file_id(file_path: str):
|
|
file_id = file_path.replace("\\", "/")
|
|
# resource\\global\\{SERVERNAME}\\resource\\template\\xxx.png-> {SERVERNAME}/xxx
|
|
m = re.search(r"global/(\w+)/resource/template/([^/]+)\.png", file_id)
|
|
if m:
|
|
return f"{m.group(1)}/{m.group(2)}"
|
|
# resource\\template\\xxx.png-> official/xxx
|
|
m = re.search(r"template/([^/]+)\.png", file_id)
|
|
if m:
|
|
return f"official/{m.group(1)}"
|
|
# resource\\PATH\\TO\\xxx.png-> resource/PATH/TO/xxx
|
|
m = re.search(r"resource/(.*)\.png", file_id)
|
|
if m:
|
|
return f"resource/{m.group(1)}"
|
|
# docs\\.vuepress\\public\\image\\PATH\\TO\\xxx.png -> docs/PATH/TO/xxx
|
|
m = re.search(r"docs/.vuepress/public/images/(.*)\.png", file_id)
|
|
if m:
|
|
return f"docs/{m.group(1)}"
|
|
# website\\apps\\web\\PATH\\TO\\xxx.png -> web/PATH/TO/xxx
|
|
m = re.search(r"website/apps/web/(.*)\.png", file_id)
|
|
if m:
|
|
return f"web/{m.group(1)}"
|
|
|
|
return None
|
|
|
|
|
|
def check_png_need_update(file_path: str, perfect_pngs: dict, quiet: bool):
|
|
# convert to absolute path
|
|
file_path = str(pathlib.Path(file_path).resolve())
|
|
|
|
if not file_path.endswith(".png"):
|
|
return False
|
|
|
|
file_id = get_file_id(file_path)
|
|
if not file_id:
|
|
print("unknown file path", file_path)
|
|
return False
|
|
|
|
if file_id in perfect_pngs:
|
|
with open(file_path, "rb") as f:
|
|
data = f.read()
|
|
sha256 = hashlib.sha256(data).hexdigest()
|
|
if sha256 == perfect_pngs[file_id]:
|
|
if not quiet:
|
|
print("skip", file_path)
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
def update_png_with_optipng(file_path: str, perfect_pngs: dict, quiet: bool):
|
|
if check_png_need_update(file_path, perfect_pngs, quiet):
|
|
if not quiet:
|
|
print("updating", file_path)
|
|
else:
|
|
return False
|
|
|
|
file_path = str(pathlib.Path(file_path).resolve())
|
|
file_id = get_file_id(file_path)
|
|
sz_before = os.stat(file_path).st_size
|
|
|
|
input_file = output_file = file_path
|
|
remove_auxiliary_data(input_file, output_file)
|
|
if quiet:
|
|
os.system(f'optipng -quiet -o7 -zm1-9 -fix "{output_file}"')
|
|
else:
|
|
os.system(f'optipng -o7 -zm1-9 -fix "{output_file}"')
|
|
|
|
sz_after = os.stat(file_path).st_size
|
|
|
|
if not quiet:
|
|
print(
|
|
f"before: {sz_before} Bytes, after: {sz_after} Bytes, diff: {sz_before - sz_after} Bytes"
|
|
)
|
|
|
|
# update file sha256
|
|
with open(file_path, "rb") as f:
|
|
data = f.read()
|
|
sha256 = hashlib.sha256(data).hexdigest()
|
|
perfect_pngs.update({file_id: sha256})
|
|
|
|
if not quiet:
|
|
print("updated", file_path)
|
|
update_perfect_png_dict(perfect_pngs)
|
|
return sz_before - sz_after
|
|
|
|
|
|
def update_png_with_oxipng(file_path: str, perfect_pngs: dict, quiet: bool):
|
|
if check_png_need_update(file_path, perfect_pngs, quiet):
|
|
if not quiet:
|
|
print("updating", file_path)
|
|
else:
|
|
return False
|
|
|
|
file_path = str(pathlib.Path(file_path).resolve())
|
|
file_id = get_file_id(file_path)
|
|
sz_before = os.stat(file_path).st_size
|
|
|
|
img_before = np.array(Image.open(file_path).convert("L"))
|
|
|
|
if quiet:
|
|
os.system(f'oxipng -o max --fast -Z -s -q "{file_path}"')
|
|
os.system(f'oxipng -o 2 -s -q "{file_path}"')
|
|
else:
|
|
os.system(f'oxipng -o max --fast -Z -s "{file_path}"')
|
|
os.system(f'oxipng -o 2 -s "{file_path}"')
|
|
|
|
sz_after = os.stat(file_path).st_size
|
|
|
|
if not quiet:
|
|
print(
|
|
f"before: {sz_before} Bytes, after: {sz_after} Bytes, diff: {sz_before - sz_after} Bytes"
|
|
)
|
|
|
|
# update file sha256
|
|
with open(file_path, "rb") as f:
|
|
data = f.read()
|
|
sha256 = hashlib.sha256(data).hexdigest()
|
|
perfect_pngs.update({file_id: sha256})
|
|
|
|
img_after = np.array(Image.open(file_path).convert("L"))
|
|
if not (img_before == img_after).all():
|
|
return -1
|
|
|
|
if not quiet:
|
|
print("updated", file_path)
|
|
update_perfect_png_dict(perfect_pngs)
|
|
return sz_before - sz_after
|
|
|
|
|
|
cur_dir = pathlib.Path(__file__).parent.resolve()
|
|
perfect_pngs_path = str(cur_dir / "optimize_templates.json")
|
|
|
|
|
|
def update_perfect_png_dict(perfect_pngs: dict):
|
|
with open(perfect_pngs_path, "w") as f:
|
|
json.dump(perfect_pngs, f, indent=4)
|
|
|
|
|
|
def ArgParser():
|
|
parser = ArgumentParser()
|
|
parser.add_argument(
|
|
"-p",
|
|
"--path",
|
|
dest="path",
|
|
nargs="*",
|
|
help="the paths of png files or folders",
|
|
default=[],
|
|
)
|
|
parser.add_argument("-q", "--quiet", dest="quiet", action="store_true")
|
|
return parser
|
|
|
|
|
|
if __name__ == "__main__":
|
|
args = ArgParser().parse_args()
|
|
paths = args.path
|
|
quiet = args.quiet
|
|
|
|
perfect_pngs = {}
|
|
optimized_png_paths = []
|
|
|
|
if os.path.exists(perfect_pngs_path):
|
|
with open(perfect_pngs_path, "r") as f:
|
|
perfect_pngs = json.load(f)
|
|
|
|
if len(paths) == 0:
|
|
docs_dir = cur_dir.parent.parent / "docs"
|
|
website_dir = cur_dir.parent.parent / "website"
|
|
resource_dir = cur_dir.parent.parent / "resource"
|
|
paths = [
|
|
str(resource_dir / "global"),
|
|
str(resource_dir / "template"),
|
|
str(docs_dir),
|
|
str(website_dir),
|
|
]
|
|
print("no path specified, use default paths:", paths)
|
|
|
|
files = []
|
|
tqdm_total = 0
|
|
|
|
for path in paths:
|
|
if pathlib.Path(path).is_dir():
|
|
for root, _, _files in os.walk(path):
|
|
for f in _files:
|
|
file = os.path.join(root, f)
|
|
if check_png_need_update(file, perfect_pngs, quiet):
|
|
files.append(file)
|
|
tqdm_total += os.stat(file).st_size ** 2 // 2**20
|
|
elif pathlib.Path(path).is_file():
|
|
files.append(path)
|
|
tqdm_total += os.stat(path).st_size ** 2 // 2**20
|
|
|
|
total_diff_sz = 0
|
|
if quiet:
|
|
t = tqdm(files, total=tqdm_total)
|
|
for i, file in enumerate(files):
|
|
cur_file_sz = os.stat(file).st_size
|
|
diff_sz = update_png_with_oxipng(file, perfect_pngs, quiet)
|
|
if diff_sz == -1:
|
|
print("error.")
|
|
exit(-1)
|
|
if diff_sz:
|
|
total_diff_sz += diff_sz
|
|
optimized_png_paths.append(file)
|
|
|
|
if total_diff_sz < 1024:
|
|
total_diff_sz_str = f"{total_diff_sz} Bytes"
|
|
elif total_diff_sz < 1048576:
|
|
total_diff_sz_str = f"{(total_diff_sz / 1024):.2f} KiB"
|
|
else:
|
|
total_diff_sz_str = f"{(total_diff_sz / 1048576):.2f} MiB"
|
|
|
|
if quiet:
|
|
t.update(cur_file_sz**2 // 2**20)
|
|
t.set_postfix(
|
|
file_counts=f"{i+1}/{len(files)}", reduced_size=total_diff_sz_str
|
|
)
|
|
t.refresh()
|
|
else:
|
|
print(
|
|
f"file counts: {i+1}/{len(files)}, reduced pngs size: {total_diff_sz_str}"
|
|
)
|