-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack.py
More file actions
170 lines (129 loc) · 4.25 KB
/
pack.py
File metadata and controls
170 lines (129 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
import json
import shutil
import zipfile
from pathlib import Path
# =====================
# 配置
# =====================
ADDON_NAME = "Wolf_Auto_Attack_V2"
TEMP_DIR = "tmp_pack"
MANIFEST_FILE = "manifest.json"
ICON_FILE = "pack_icon.png"
ENTITIES_DIR = "entities"
OPTIONAL_DIRS = [
"items",
"blocks",
"functions",
"animations",
"animation_controllers",
"render_controllers",
"textures",
"sounds",
]
# =====================
# 颜色输出(Windows 10+ 支持)
# =====================
GREEN = "\033[0;32m"
YELLOW = "\033[1;33m"
RED = "\033[0;31m"
NC = "\033[0m"
def print_green(msg):
print(f"{GREEN}{msg}{NC}")
def print_yellow(msg):
print(f"{YELLOW}{msg}{NC}")
def print_red(msg):
print(f"{RED}{msg}{NC}")
# =====================
# 工具函数
# =====================
def check_exists(path: Path, is_dir=False):
if is_dir and not path.is_dir():
print_red(f"错误: {path} 目录不存在!")
exit(1)
if not is_dir and not path.is_file():
print_red(f"错误: {path} 文件不存在!")
exit(1)
def read_version_from_manifest(path: Path) -> str:
try:
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
# Bedrock manifest 常见结构:
# header.version = [1, 0, 0]
header = data.get("header", {})
version_list = header.get("version")
if isinstance(version_list, list):
return ".".join(str(v) for v in version_list)
except Exception:
pass
print_yellow("警告: 无法从 manifest.json 读取版本信息,使用默认版本")
return "2.0.0"
def copy_if_exists(src: Path, dst: Path):
if src.is_dir():
print_yellow(f"找到 {src.name} 目录,正在复制...")
shutil.copytree(src, dst / src.name)
elif src.is_file():
shutil.copy2(src, dst)
def zip_dir(src_dir: Path, output_file: Path):
with zipfile.ZipFile(output_file, "w", zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(src_dir):
for file in files:
full_path = Path(root) / file
rel_path = full_path.relative_to(src_dir)
zipf.write(full_path, rel_path)
def get_file_size(path: Path) -> str:
size = path.stat().st_size
for unit in ["B", "KB", "MB", "GB"]:
if size < 1024:
return f"{size:.2f}{unit}"
size /= 1024
return f"{size:.2f}TB"
# =====================
# 主流程
# =====================
def main():
print_green("=== Minecraft Bedrock Add-on 打包工具 ===")
base_dir = Path.cwd()
manifest_path = base_dir / MANIFEST_FILE
icon_path = base_dir / ICON_FILE
entities_path = base_dir / ENTITIES_DIR
temp_path = base_dir / TEMP_DIR
print_yellow("检查必要文件...")
check_exists(manifest_path)
check_exists(icon_path)
check_exists(entities_path, is_dir=True)
print_green("✓ 所有必要文件都存在")
version = read_version_from_manifest(manifest_path)
print_yellow(f"Add-on 版本: {version}")
output_file = base_dir / f"{ADDON_NAME}_v{version}.mcpack"
if output_file.exists():
print_yellow("删除旧的打包文件...")
output_file.unlink()
print_yellow("创建临时目录...")
if temp_path.exists():
shutil.rmtree(temp_path)
temp_path.mkdir(parents=True)
print_yellow("复制文件...")
shutil.copy2(manifest_path, temp_path)
shutil.copy2(icon_path, temp_path)
shutil.copytree(entities_path, temp_path / ENTITIES_DIR)
for d in OPTIONAL_DIRS:
copy_if_exists(base_dir / d, temp_path)
print_yellow("正在打包...")
zip_dir(temp_path, output_file)
if output_file.exists():
print_green("✓ 打包成功!")
print_green(f"文件: {output_file.name}")
print_green(f"大小: {get_file_size(output_file)}")
else:
print_red("✗ 打包失败!")
exit(1)
print_yellow("清理临时文件...")
shutil.rmtree(temp_path)
print_green("=== 打包完成 ===")
print_yellow(f"你可以将 '{output_file.name}' 文件导入到 Minecraft Bedrock 版中使用")
if __name__ == "__main__":
# Windows 终端启用 ANSI 颜色支持
if os.name == "nt":
os.system("")
main()