forked from K0lb3/UnityPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
131 lines (103 loc) · 3.63 KB
/
Copy pathtest_main.py
File metadata and controls
131 lines (103 loc) · 3.63 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
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.read()
def test_read_batch():
env = UnityPy.load(SAMPLES)
for obj in env.objects:
obj.read()
def test_save_dict():
env = UnityPy.load(SAMPLES)
for obj in env.objects:
data = obj.get_raw_data()
item = obj.read_typetree(wrap=False)
assert isinstance(item, dict)
re_data = obj.save_typetree(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.read_typetree(wrap=True)
assert not isinstance(item, dict)
re_data = obj.save_typetree(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.read()
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":
obj.read().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():
# as not platforms are supported by FMOD
# we have to check if the platform is supported first
try:
from UnityPy.export import AudioClipConverter
AudioClipConverter.import_pyfmodex()
except NotImplementedError:
return
except OSError:
# cibuildwheel doesn't copy the .so files
# so we have to skip the test on it
print("Failed to load the fmod lib for your system.")
print("Skipping the audioclip test.")
return
if AudioClipConverter.pyfmodex is False:
return
env = UnityPy.load(os.path.join(SAMPLES, "char_118_yuki.ab"))
for obj in env.objects:
if obj.type.name == "AudioClip":
clip = obj.read()
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.read()
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")