Project Chimera
Меню

Исходники

210 строк · 8 КиБ

code/crates/chl-epic/tests/fixtures/make_fixture.py

1# Генератор эталонного манифеста Epic.
2#
3# Кодировщик здесь — чужой (legendary/sw_dlm.py, только чтение, импортом),
4# декодировщик — наш, на Rust. Это и есть смысл: тест сверяет две
5# независимые реализации формата, а не нашу с самой собой.
6#
7# Запуск:
8# PYTHONPATH=/home/fanchji/Games/StartWine/data/scripts python3 make_fixture.py <outdir>
9
10import json
11import os
12import sys
13import hashlib
14
15import sw_dlm as L
16
17
18def guid(a, b, c, d):
19 return (a, b, c, d)
20
21
22def build():
23 m = L.Manifest()
24
25 meta = L.ManifestMeta()
26 meta.data_version = 2 # включает build_id + uninstall action
27 meta.feature_level = 21
28 meta.is_file_data = False
29 meta.app_id = 4242
30 meta.app_name = "TestGame"
31 meta.build_version = "1.2.3-Windows"
32 meta.launch_exe = "Binaries/Win64/TestGame.exe"
33 meta.launch_command = "-nosplash"
34 meta.prereq_ids = ["prereq-one", "prereq-two"]
35 meta.prereq_name = "Visual C++ Redist"
36 meta.prereq_path = "Redist/vcredist.exe"
37 meta.prereq_args = "/quiet"
38 meta.uninstall_action_path = "Redist/uninstall.exe"
39 meta.uninstall_action_args = "/S"
40 m.meta = meta
41
42 cdl = L.CDL()
43 cdl.version = 0
44 specs = [
45 (guid(0x11111111, 0x22222222, 0x33333333, 0x44444444), 0x0123456789ABCDEF, 7, 1024 * 1024, 500000),
46 (guid(0xAABBCCDD, 0x00112233, 0x44556677, 0x8899AABB), 0xFEDCBA9876543210, 42, 1024 * 1024, 250000),
47 (guid(0x00000001, 0x00000002, 0x00000003, 0x00000004), 0x1, 0, 1024 * 1024, 131072),
48 ]
49 for g, h, grp, win, fsize in specs:
50 ci = L.ChunkInfo(manifest_version=21)
51 ci.guid = g
52 ci.hash = h
53 ci.sha_hash = hashlib.sha1(repr(g).encode()).digest()
54 ci.group_num = grp
55 ci.window_size = win
56 ci.file_size = fsize
57 cdl.elements.append(ci)
58 m.chunk_data_list = cdl
59
60 fml = L.FML()
61 fml.version = 2 # включает md5, mime_type, sha256
62
63 def mkfile(name, symlink, flags, tags, parts, md5=b"", mime="", sha256=b""):
64 fm = L.FileManifest()
65 fm.filename = name
66 fm.symlink_target = symlink
67 fm.hash = hashlib.sha1(name.encode()).digest()
68 fm.flags = flags
69 fm.install_tags = tags
70 off = 0
71 for g, o, s in parts:
72 cp = L.ChunkPart()
73 cp.guid = g
74 cp.offset = o
75 cp.size = s
76 cp.file_offset = off
77 off += s
78 fm.chunk_parts.append(cp)
79 fm.file_size = off
80 fm.hash_md5 = md5
81 fm.mime_type = mime
82 fm.hash_sha256 = sha256 or hashlib.sha256(name.encode()).digest()
83 return fm
84
85 fml.elements = [
86 # обычный исполняемый файл из двух частей разных чанков
87 mkfile(
88 "Binaries/Win64/TestGame.exe", "", 0x4, ["base"],
89 [(specs[0][0], 0, 300000), (specs[1][0], 1000, 200000)],
90 md5=hashlib.md5(b"exe").digest(), mime="application/x-msdownload",
91 ),
92 # имя вне ASCII — это заставляет legendary писать UTF-16 fstring
93 # с отрицательной длиной; ровно тот случай, на котором наивный
94 # разбор ломается молча.
95 mkfile(
96 "Content/Тексты/описание.txt", "", 0x1, [],
97 [(specs[2][0], 0, 4096)],
98 mime="text/plain",
99 ),
100 # симлинк и две метки установки
101 mkfile(
102 "Engine/lib/libfoo.so", "libfoo.so.1", 0x0, ["base", "extra"],
103 [(specs[2][0], 4096, 8192)],
104 ),
105 ]
106 m.file_manifest_list = fml
107
108 cf = L.CustomFields()
109 cf["CloudSaveFolder"] = "{appdata}/TestGame/Saves"
110 cf["FolderName"] = "TestGame"
111 m.custom_fields = cf
112
113 return m
114
115
116def expected(m):
117 meta, cdl, fml, cf = m.meta, m.chunk_data_list, m.file_manifest_list, m.custom_fields
118 return {
119 "meta": {
120 "data_version": meta.data_version,
121 "feature_level": meta.feature_level,
122 "is_file_data": meta.is_file_data,
123 "app_id": meta.app_id,
124 "app_name": meta.app_name,
125 "build_version": meta.build_version,
126 "launch_exe": meta.launch_exe,
127 "launch_command": meta.launch_command,
128 "prereq_ids": meta.prereq_ids,
129 "prereq_name": meta.prereq_name,
130 "prereq_path": meta.prereq_path,
131 "prereq_args": meta.prereq_args,
132 "uninstall_action_path": meta.uninstall_action_path,
133 "uninstall_action_args": meta.uninstall_action_args,
134 "build_id": meta.build_id,
135 },
136 "chunks": [
137 {
138 "guid_str": c.guid_str,
139 "guid_num": str(c.guid_num),
140 "hash": str(c.hash),
141 "sha_hash": c.sha_hash.hex(),
142 "group_num": c.group_num,
143 "window_size": c.window_size,
144 "file_size": c.file_size,
145 "path": c.path,
146 }
147 for c in cdl.elements
148 ],
149 "files": [
150 {
151 "filename": f.filename,
152 "symlink_target": f.symlink_target,
153 "hash": f.hash.hex(),
154 "flags": f.flags,
155 "read_only": bool(f.read_only),
156 "compressed": bool(f.compressed),
157 "executable": bool(f.executable),
158 "install_tags": f.install_tags,
159 "file_size": f.file_size,
160 "hash_md5": f.hash_md5.hex(),
161 "mime_type": f.mime_type,
162 "hash_sha256": f.hash_sha256.hex(),
163 "chunk_parts": [
164 {
165 "guid_str": p.guid_str,
166 "guid_num": str(p.guid_num),
167 "offset": p.offset,
168 "size": p.size,
169 "file_offset": p.file_offset,
170 }
171 for p in f.chunk_parts
172 ],
173 }
174 for f in fml.elements
175 ],
176 "custom_fields": dict(cf.items()),
177 }
178
179
180def main():
181 outdir = sys.argv[1]
182 os.makedirs(outdir, exist_ok=True)
183
184 m = build()
185 packed = m.write(compress=True)
186 with open(os.path.join(outdir, "sample.manifest"), "wb") as f:
187 f.write(packed)
188
189 # Тот же манифест без сжатия — отдельный путь в разборе (stored_as & 1).
190 m2 = build()
191 raw = m2.write(compress=False)
192 with open(os.path.join(outdir, "sample-uncompressed.manifest"), "wb") as f:
193 f.write(raw)
194
195 # Эталон читается обратно ЧУЖИМ разбором, а не берётся из того, что мы
196 # записали, — так в ожидания попадает то, что legendary реально
197 # вычитывает из этих байтов.
198 back = L.Manifest.read_all(packed)
199 with open(os.path.join(outdir, "sample.expected.json"), "w", encoding="utf-8") as f:
200 json.dump(expected(back), f, ensure_ascii=False, indent=2, sort_keys=True)
201
202 print("compressed:", len(packed), "bytes")
203 print("uncompressed:", len(raw), "bytes")
204 print("chunk path[0]:", back.chunk_data_list.elements[0].path)
205 print("build_id:", back.meta.build_id)
206
207
208if __name__ == "__main__":
209 main()
210