Repository navigation
Expand file tree
/
Copy pathtest_main.py
More file actions
126 lines (97 loc) · 3.4 KB
/
Copy pathtest_main.py
File metadata and controls
126 lines (97 loc) · 3.4 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
import io
import os
import platform
from PIL import Image
import UnityPy
from UnityPy.streams import EndianBinaryReader
SAMPLES = os.path.join(os.path.dirname(os.path.abspath(__file__)), "samples")
def test_read_single():
for f in os.listdir(SAMPLES):
env = UnityPy.load(os.path.join(SAMPLES, f))
for obj in env.objects:
obj.parse_as_object()
obj.parse_as_dict()
def test_read_batch():
env = UnityPy.load(SAMPLES)
for obj in env.objects:
obj.parse_as_object()
obj.parse_as_dict()
def test_save_dict():
env = UnityPy.load(SAMPLES)
for obj in env.objects:
data = obj.get_raw_data()
item = obj.parse_as_dict()
assert isinstance(item, dict)
re_data = obj.patch(item)
assert data == re_data
def test_save_wrap():
env = UnityPy.load(SAMPLES)
for obj in env.objects:
data = obj.get_raw_data()
item = obj.parse_as_object()
assert not isinstance(item, dict)
re_data = obj.patch(item)
assert data == re_data
def test_texture2d():
for f in os.listdir(SAMPLES):
env = UnityPy.load(os.path.join(SAMPLES, f))
for obj in env.objects:
if obj.type.name == "Texture2D":
data = obj.parse_as_object()
data.image.save(io.BytesIO(), format="PNG")
data.image = data.image.transpose(Image.ROTATE_90)
data.save()
def test_sprite():
for f in os.listdir(SAMPLES):
env = UnityPy.load(os.path.join(SAMPLES, f))
for obj in env.objects:
if obj.type.name == "Sprite":
sprite = obj.parse_as_object()
sprite.image.save(io.BytesIO(), format="PNG")
if platform.system() == "Darwin":
# crunch issue on macos leading to segfault
del test_texture2d
del test_sprite
def test_audioclip():
from fmod_toolkit.importer import import_pyfmodex
try:
import_pyfmodex()
except ValueError:
print("FMOD toolkit not available, skipping AudioClip tests")
return
env = UnityPy.load(os.path.join(SAMPLES, "char_118_yuki.ab"))
for obj in env.objects:
if obj.type.name == "AudioClip":
clip = obj.parse_as_object()
assert len(clip.samples) == 1
def test_mesh():
env = UnityPy.load(os.path.join(SAMPLES, "xinzexi_2_n_tex"))
with open(os.path.join(SAMPLES, "xinzexi_2_n_tex_mesh"), "rb") as f:
wanted = f.read().replace(b"\r", b"")
for obj in env.objects:
if obj.type.name == "Mesh":
mesh = obj.parse_as_object()
data = mesh.export()
if isinstance(data, str):
data = data.encode("utf8").replace(b"\r", b"")
assert data == wanted
def test_read_typetree():
env = UnityPy.load(SAMPLES)
for obj in env.objects:
obj.read_typetree()
def test_save():
env = UnityPy.load(SAMPLES)
# TODO - check against original
# this only makes sure
# that the save function still produces a readable file
for name, file in env.files.items():
if isinstance(file, EndianBinaryReader):
continue
save1 = file.save()
save2 = UnityPy.load(save1).file.save()
assert save1 == save2, f"Failed to save {name} correctly"
if __name__ == "__main__":
for x in list(locals()):
if str(x)[:4] == "test":
locals()[x]()
input("All Tests Passed")