-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
98 lines (76 loc) · 2.85 KB
/
main.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
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
import os
import json
import argparse
from lib.glTF import glTF, ObjectProcessor
from lib.odin import SupercellOdinGLTF
debug = False
required_folders = {
"sc_input": "In-SC-glTF",
"sc_output": "Out-SC-glTF",
"def_input": "In-glTF",
"def_output": "Out-glTF",
}
if (debug):
required_folders["in_debug"] = "In-Debug"
required_folders["out_debug"] = "Out-Debug"
def decode(post_process: bool):
files = os.scandir(required_folders["sc_input"])
for filepath in files:
gltf = glTF()
try:
with open(filepath.path, "rb") as file:
gltf.read(file.read())
except ValueError as e:
print(f"Error: {e}")
print(f"Failed to read file by name \"{filepath.name}\". Skip...")
continue
for chunk in gltf.chunks:
chunk.deserialize_json()
if (post_process):
odin = SupercellOdinGLTF(gltf)
gltf = odin.process()
if debug:
if chunks_data := [
chunk.data
for chunk in gltf.chunks
if chunk.name == "JSON" and not isinstance(chunk.data, bytes)
]:
open(os.path.join(required_folders["out_debug"], filepath.name) + ".json", "wb").write(bytes(json.dumps(chunks_data, cls=ObjectProcessor, indent=4), "utf8"))
print(f"Successful: {filepath.name}")
with open(os.path.join(required_folders["def_output"], filepath.name), "wb") as file:
file.write(gltf.write())
def encode():
files = os.scandir(required_folders["def_input"])
for filepath in files:
print(f"Reading: {filepath.name}")
gltf = glTF()
with open(filepath.path, "rb") as file:
gltf.read(file.read())
for chunk in gltf.chunks:
chunk.serialize_json()
if debug:
open(
os.path.join(
required_folders["in_debug"], f"{filepath.name}.bin"
),
"wb",
).write(
[chunk.data for chunk in gltf.chunks if chunk.name == "FLA2"][0]
)
print(f"Successful: {filepath.name}")
with open(os.path.join(required_folders["sc_output"], filepath.name), "wb") as file:
file.write(gltf.write())
if __name__ == "__main__":
for name in required_folders.values():
os.makedirs(name, exist_ok=True)
parser = argparse.ArgumentParser(
prog="scglTF Converter", description="Tool for converting Supercell glTF files to usual ones and vice versa"
)
parser.add_argument("mode", type=str, choices=["decode", "decodeRaw", "encode"])
args = parser.parse_args()
if (args.mode == "decode"):
decode(post_process=True)
if (args.mode == "decodeRaw"):
decode(post_process=False)
elif (args.mode == "encode"):
encode()