-
-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathextractor.py
More file actions
368 lines (312 loc) · 12.3 KB
/
extractor.py
File metadata and controls
368 lines (312 loc) · 12.3 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import json
import os
from io import BytesIO
from pathlib import Path
from typing import Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union
import UnityPy
from UnityPy.classes import (
AudioClip,
Font,
GameObject,
Mesh,
MonoBehaviour,
Object,
PPtr,
Shader,
Sprite,
TextAsset,
Texture2D,
)
from UnityPy.enums.ClassIDType import ClassIDType
from UnityPy.files import ObjectReader, SerializedFile
def export_obj(
obj: Union[ObjectReader, PPtr],
fp: str,
append_name: bool = False,
append_path_id: bool = False,
export_unknown_as_typetree: bool = False,
asset_filter: Optional[Callable[[Object], bool]] = None,
) -> List[Tuple[SerializedFile, int]]:
"""Exports the given object to the given filepath.
Args:
obj (Object, PPtr): A valid Unity object or a reference to one.
fp (Path): A valid filepath where the object should be exported to.
append_name (bool, optional): Decides if the obj name will be appended to the filepath. Defaults to False.
append_path_id (bool, optional): Decides if the obj path id will be appended to the filepath. Defaults to False.
export_unknown_as_typetree (bool, optional): If set, then unimplemented objects will be exported
via their typetree or dumped as bin. Defaults to False.
asset_filter (func(Object)->bool, optional): Determines whether to export an object. Defaults to all objects.
Returns:
list: a list of exported object path_ids
"""
# figure out export function
export_func = EXPORT_TYPES.get(obj.type)
if not export_func:
if export_unknown_as_typetree:
export_func = exportMonoBehaviour
else:
return []
# set filepath
if isinstance(obj, PPtr):
obj = obj.deref()
instance = obj.parse_as_object()
# a filter that returned True during an earlier extract_assets check can return False now with more info from read()
if asset_filter and not asset_filter(instance):
return []
if append_name:
name = getattr(instance, "m_Name", obj.type.name)
fp = os.path.join(
fp,
name,
)
fp, extension = os.path.splitext(fp)
if append_path_id:
fp = f"{fp}_{obj.m_PathID}"
# export
return export_func(instance, fp, extension)
def extract_assets(
src: Union[Path, BytesIO, bytes, bytearray],
dst: Path,
use_container: bool = True,
ignore_first_container_dirs: int = 0,
append_path_id: bool = False,
export_unknown_as_typetree: bool = False,
asset_filter: Optional[Callable[[Object], bool]] = None,
) -> List[Tuple[SerializedFile, int]]:
"""Extracts some or all assets from the given source.
Args:
src (Union[Path, BytesIO, bytes, bytearray]): [description]
dst (Path): [description]
use_container (bool, optional): [description]. Defaults to True.
ignore_first_container_dirs (int, optional): [description]. Defaults to 0.
append_path_id (bool, optional): [description]. Defaults to False.
export_unknown_as_typetree (bool, optional): [description]. Defaults to False.
asset_filter (func(object)->bool, optional): Determines whether to export an object.
Defaults to all objects.
Returns:
List[Tuple[SerializedFile, int]]: [description]
"""
# load source
env = UnityPy.load(src)
exported = []
export_types_keys = list(EXPORT_TYPES.keys())
def defaulted_export_index(type: ClassIDType):
try:
return export_types_keys.index(type)
except (IndexError, ValueError):
return 999
if use_container:
container = sorted(env.container.items(), key=lambda x: defaulted_export_index(x[1].type))
for obj_path, obj in container:
# The filter here can only access metadata.
# The same filter may produce a different result later in extract_obj after obj.read()
if asset_filter is not None and not asset_filter(obj):
continue
# the check of the various sub directories is required to avoid // in the path
obj_dest = os.path.join(
dst,
*(x for x in obj_path.split("/")[ignore_first_container_dirs:] if x),
)
os.makedirs(os.path.dirname(obj_dest), exist_ok=True)
exported.extend(
export_obj(
obj,
obj_dest,
append_path_id=append_path_id,
export_unknown_as_typetree=export_unknown_as_typetree,
asset_filter=asset_filter,
)
)
else:
objects = sorted(env.objects, key=lambda x: defaulted_export_index(x.type))
for obj in objects:
if asset_filter is not None and not asset_filter(obj):
continue
if (obj.assets_file, obj.path_id) not in exported:
exported.extend(
export_obj(
obj,
dst,
append_name=True,
append_path_id=append_path_id,
export_unknown_as_typetree=export_unknown_as_typetree,
asset_filter=asset_filter,
)
)
return exported
###############################################################################
# EXPORT FUNCTIONS #
###############################################################################
def exportTextAsset(
obj: Union[TextAsset, ObjectReader], fp: str, extension: str = ".txt"
) -> List[Tuple[SerializedFile, int]]:
if isinstance(obj, ObjectReader):
obj = obj.parse_as_object()
if not extension:
extension = ".txt"
with open(f"{fp}{extension}", "wb") as f:
f.write(obj.m_Script.encode("utf-8", "surrogateescape"))
return [(obj.assets_file, obj.object_reader.path_id)]
def exportFont(obj: Union[Font, ObjectReader], fp: str, extension: str = "") -> List[Tuple[SerializedFile, int]]:
if isinstance(obj, ObjectReader):
obj = obj.parse_as_object()
# TODO - export glyphs
if obj.m_FontData:
extension = ".ttf"
if obj.m_FontData[0:4] == b"OTTO":
extension = ".otf"
with open(f"{fp}{extension}", "wb") as f:
f.write(bytes(obj.m_FontData))
return [(obj.assets_file, obj.object_reader.path_id)]
def exportMesh(obj: Union[Mesh, ObjectReader], fp: str, extension=".obj") -> List[Tuple[SerializedFile, int]]:
if isinstance(obj, ObjectReader):
obj = obj.parse_as_object()
if not extension:
extension = ".obj"
with open(f"{fp}{extension}", "wt", encoding="utf8", newline="") as f:
f.write(obj.export())
return [(obj.assets_file, obj.object_reader.path_id)]
def exportShader(obj: Union[Shader, ObjectReader], fp: str, extension=".txt") -> List[Tuple[SerializedFile, int]]:
if isinstance(obj, ObjectReader):
obj = obj.parse_as_object()
if not extension:
extension = ".txt"
with open(f"{fp}{extension}", "wt", encoding="utf8", newline="") as f:
f.write(obj.export())
return [(obj.assets_file, obj.object_reader.path_id)]
def exportMonoBehaviour(
obj: Union[MonoBehaviour, ObjectReader], fp: str, extension: str = ""
) -> List[Tuple[SerializedFile, int]]:
reader = obj.object_reader if isinstance(obj, MonoBehaviour) else obj
try:
export = reader.parse_as_dict()
extension = ".json"
export = json.dumps(export, indent=4, ensure_ascii=False).encode("utf8", errors="surrogateescape")
except Exception:
extension = ".bin"
export = reader.get_raw_data()
with open(f"{fp}{extension}", "wb") as f:
f.write(export)
return [(obj.assets_file, obj.path_id)]
def exportAudioClip(
obj: Union[AudioClip, ObjectReader], fp: str, extension: str = ""
) -> List[Tuple[SerializedFile, int]]:
if isinstance(obj, ObjectReader):
clip = obj.parse_as_object()
else:
clip = obj
obj = clip.object_reader
samples = clip.samples
if len(samples) == 0:
pass
elif len(samples) == 1:
with open(f"{fp}.wav", "wb") as f:
f.write(list(samples.values())[0])
else:
os.makedirs(fp, exist_ok=True)
for name, clip_data in samples.items():
with open(os.path.join(fp, f"{name}.wav"), "wb") as f:
f.write(clip_data)
return [(obj.assets_file, obj.path_id)]
def exportSprite(
obj: Union[Sprite, ObjectReader], fp: str, extension: str = ".png"
) -> List[Tuple[SerializedFile, int]]:
if isinstance(obj, ObjectReader):
sprite = obj.parse_as_object()
else:
sprite = obj
obj = sprite.object_reader
sprite.image.save(f"{fp}{extension}")
exported = [
(obj.assets_file, obj.path_id),
(sprite.m_RD.texture.assetsfile, sprite.m_RD.texture.path_id),
]
alpha_assets_file = getattr(sprite.m_RD.alphaTexture, "assets_file", None)
alpha_path_id = getattr(sprite.m_RD.alphaTexture, "path_id", None)
if alpha_path_id and alpha_assets_file:
exported.append((alpha_assets_file, alpha_path_id))
return exported
def exportTexture2D(
obj: Union[Texture2D, ObjectReader], fp: str, extension: str = ".png"
) -> List[Tuple[SerializedFile, int]]:
if isinstance(obj, ObjectReader):
obj = obj.parse_as_object()
if not extension:
extension = ".png"
if obj.m_Width:
# textures can be empty
obj.image.save(f"{fp}{extension}")
return [(obj.assets_file, obj.object_reader.path_id)]
def exportGameObject(
obj: Union[GameObject, ObjectReader], fp: str, extension: str = ""
) -> List[Tuple[SerializedFile, int]]:
if isinstance(obj, ObjectReader):
obj = obj.parse_as_object()
exported = [(obj.assets_file, obj.object_reader.path_id)]
refs = crawl_obj(obj)
if refs:
os.makedirs(fp, exist_ok=True)
for ref_id, ref in refs.items():
# Don't export already exported objects a second time
# and prevent circular calls by excluding other GameObjects.
# The other GameObjects were already exported in the this call.
if (ref.assets_file, ref_id) in exported or ref.type == ClassIDType.GameObject:
continue
try:
exported.extend(export_obj(ref, fp, True, True))
except Exception as e:
print(f"Failed to export {ref_id}")
print(e)
return exported
EXPORT_TYPES = {
# following types can include other objects
ClassIDType.GameObject: exportGameObject,
ClassIDType.Sprite: exportSprite,
# following types don't include other objects
ClassIDType.AudioClip: exportAudioClip,
ClassIDType.Font: exportFont,
ClassIDType.Mesh: exportMesh,
ClassIDType.MonoBehaviour: exportMonoBehaviour,
ClassIDType.Shader: exportShader,
ClassIDType.TextAsset: exportTextAsset,
ClassIDType.Texture2D: exportTexture2D,
}
ASSEMBLY_NAME_DLL = str
CLASS_NAME = str
MONOBEHAVIOUR_TYPETREES: Dict[ASSEMBLY_NAME_DLL, Dict[CLASS_NAME, List[Dict]]] = {}
def crawl_obj(obj: Union[Object, ObjectReader, PPtr], ret: Optional[dict] = None) -> Dict[int, Union[Object, PPtr]]:
"""Crawls through the data struture of the object
and returns a list of all the components.
"""
if not ret:
ret = {}
values: Sequence
if isinstance(obj, PPtr):
if obj.m_PathID == 0 and obj.m_FileID == 0 and obj.m_Index == -2:
return ret
try:
instance = obj.deref_parse_as_dict()
values = instance.values()
except AttributeError:
return ret
elif isinstance(obj, Object):
values = obj.__dict__.values()
elif isinstance(obj, ObjectReader):
values = obj.parse_as_dict().values()
else:
return ret
ret[obj.m_PathID] = obj
for value in flatten(values):
if isinstance(value, (Object, PPtr)):
if value.m_PathID in ret:
continue
crawl_obj(value, ret)
return ret
def flatten(seq: Sequence) -> Iterable:
for elem in list(seq):
if isinstance(elem, (list, tuple)):
yield from flatten(elem)
elif isinstance(elem, dict):
yield from flatten(elem.values()) # type: ignore
else:
yield elem