This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_zip.py
65 lines (52 loc) · 2.18 KB
/
extract_zip.py
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
import json
from argparse import ArgumentParser
from collections import defaultdict
from pathlib import Path
from shutil import copyfileobj
from zipfile import ZipFile
def main() -> None:
parser = ArgumentParser()
parser.add_argument(
"zbd_dir",
type=lambda value: Path(value).resolve(strict=True),
)
parser.add_argument(
"png_dir",
type=lambda value: Path(value).resolve(strict=True),
)
args = parser.parse_args()
texture_atlas = defaultdict(list)
for mission_zbd_dir in args.zbd_dir.glob("m*"):
mission_name = mission_zbd_dir.name
for package_path in mission_zbd_dir.glob("*texture*.zip"):
package_name = package_path.stem
print(mission_name, package_name)
with ZipFile(package_path, mode="r") as zip:
# load manifest
with zip.open("manifest.json", mode="r") as f:
manifest = json.load(f)
texture_infos = {ti["name"]: ti for ti in manifest["texture_infos"]}
# load textures
for info in zip.infolist():
if info.filename == "manifest.json":
continue
texture_name, _, ext = info.filename.rpartition(".")
assert ext == "png", info.filename
ti = texture_infos[texture_name]
ti["mission"] = mission_name
ti["package"] = package_name
del ti["image_loaded"]
del ti["alpha_loaded"]
del ti["palette_loaded"]
texture_dir = args.png_dir / texture_name
texture_dir.mkdir(exist_ok=True)
png_name = f"{texture_name}-{mission_name}-{package_name}.png"
png_path = texture_dir / png_name
with zip.open(info) as fsrc, png_path.open("wb") as fdst:
copyfileobj(fsrc, fdst)
texture_atlas[texture_name].append(ti)
atlas_path = args.png_dir / "atlas.json"
with atlas_path.open("w") as f:
json.dump(texture_atlas, f, indent=2)
if __name__ == "__main__":
main()