-
-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathenvironment.py
More file actions
350 lines (296 loc) · 11.8 KB
/
environment.py
File metadata and controls
350 lines (296 loc) · 11.8 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
import io
import ntpath
import os
import re
from typing import TYPE_CHECKING, BinaryIO, Callable, Dict, List, Optional, Union, cast
from zipfile import ZipFile
from fsspec import AbstractFileSystem
from fsspec.implementations.local import LocalFileSystem
from .enums import FileType
from .files import BundleFile, File, ObjectReader, SerializedFile, WebFile
from .helpers.ContainerHelper import ContainerHelper
from .helpers.ImportHelper import (
FileSourceType,
check_file_type,
find_sensitive_path,
parse_file,
)
from .streams import EndianBinaryReader
if TYPE_CHECKING:
from UnityPy.helpers.TypeTreeGenerator import TypeTreeGenerator
reSplit = re.compile(r"(.*?([^\/\\]+?))\.split\d+")
class Environment:
files: Dict[str, Union[SerializedFile, BundleFile, WebFile, EndianBinaryReader]]
cabs: Dict[str, Union[SerializedFile, EndianBinaryReader]]
path: str
local_files: List[str]
local_files_simple: List[str]
typetree_generator: Optional["TypeTreeGenerator"] = None
def __init__(self, *args: FileSourceType, fs: Optional[AbstractFileSystem] = None, path: Optional[str] = None):
self.files = {}
self.cabs = {}
self.fs = fs or LocalFileSystem()
self.local_files = []
self.local_files_simple = []
if path is None:
# if no path is given, use the current working directory
if isinstance(self.fs, LocalFileSystem):
self.path = os.getcwd()
else:
self.path = ""
else:
self.path = path
if args:
for arg in args:
if isinstance(arg, str):
if self.fs.isfile(arg):
if ntpath.splitext(arg)[-1] in [".apk", ".zip"]:
self.load_zip_file(arg)
else:
self.path = ntpath.dirname(arg) or ntpath.curdir
if reSplit.match(arg):
self.load_files([arg])
else:
self.load_file(arg)
elif self.fs.isdir(arg):
self.path = arg
self.load_folder(arg)
else:
self.load_file(file=arg)
if len(self.files) == 1:
self.file = list(self.files.values())[0]
def load_files(self, files: List[str]):
"""Loads all files (list) into the Environment and merges .split files for common usage."""
self.load_assets(files, lambda x: open(x, "rb"))
def load_folder(self, path: str):
"""Loads all files in the given path and its subdirs into the Environment."""
self.load_files([self.fs.sep.join([root, f]) for root, dirs, files in self.fs.walk(path) for f in files])
def load(self, files: List[str]):
"""Loads all files into the Environment."""
self.files.update(
{ntpath.basename(f): self.load_file(self.fs.open(f, "rb"), self, f) for f in files if self.fs.exists(f)}
)
def _load_split_file(self, basename: str) -> bytes:
file: List[bytes] = []
for i in range(0, 999):
item = f"{basename}.split{i}"
if self.fs.exists(item):
with self.fs.open(item, "rb") as f:
file.append(f.read()) # type: ignore
elif i:
break
return b"".join(file)
def load_file(
self,
file: FileSourceType,
parent: Optional[Union["Environment", File]] = None,
name: Optional[str] = None,
is_dependency: bool = False,
):
if not parent:
parent = self
if isinstance(file, str):
split_match = reSplit.match(file)
if split_match:
basepath, _basename = split_match.groups()
assert isinstance(basepath, str)
name = basepath
file = self._load_split_file(basepath)
else:
name = file
if not os.path.exists(file):
# relative paths are in the asset directory, not the cwd
if not os.path.isabs(file):
file = os.path.join(self.path, file)
# for dependency loading of split files
if os.path.exists(f"{file}.split0"):
file = self._load_split_file(file)
# Unity paths are case insensitive,
# so we need to find "Resources/Foo.asset" when the record says "resources/foo.asset"
elif not os.path.exists(file):
file_path = find_sensitive_path(self.path, file)
if file_path:
file = file_path
else:
return None
# raise FileNotFoundError(f"File {file} not found in {self.path}")
if isinstance(file, str):
file = self.fs.open(file, "rb")
typ, reader = check_file_type(file)
stream_name = (
name
if name
else getattr(
file,
"name",
str(file.__hash__()) if hasattr(file, "__hash__") else "", # type: ignore
)
)
if typ == FileType.ZIP:
f = self.load_zip_file(file)
else:
f = parse_file(reader, self, name=stream_name, typ=typ, is_dependency=is_dependency)
if isinstance(f, (SerializedFile, EndianBinaryReader)):
self.register_cab(stream_name, f)
self.files[stream_name] = f
return f
def load_zip_file(self, value):
if isinstance(value, str) and self.fs.exists(value):
buffer = cast(io.BufferedReader, self.fs.open(value, "rb"))
elif isinstance(value, (bytes, bytearray, memoryview)):
buffer = io.BytesIO(value)
elif isinstance(value, (io.BufferedReader, io.BufferedIOBase)):
buffer = value
else:
raise TypeError("Unsupported type for loading zip file")
z = ZipFile(buffer)
self.load_assets(z.namelist(), lambda x: z.open(x, "r")) # type: ignore
z.close()
def save(self, pack="none", out_path="output"):
"""Saves all changed assets.
Mark assets as changed using `.mark_changed()`.
pack = "none" (default) or "lz4"
"""
for fname, fitem in self.files.items():
if getattr(fitem, "is_changed", False):
with open(self.fs.sep.join([out_path, ntpath.basename(fname)]), "wb") as out:
out.write(fitem.save(packer=pack))
@property
def objects(self) -> List[ObjectReader]:
"""Returns a list of all objects in the Environment."""
def search(item):
ret = []
if not isinstance(item, Environment) and getattr(item, "objects", None):
# serialized file
if getattr(item, "is_dependency", False):
return []
return [val for val in item.objects.values()]
elif getattr(item, "files", None): # WebBundle and BundleFile
# bundle
for sub_item in item.files.values():
ret.extend(search(sub_item))
return ret
return ret
return search(self)
@property
def container(self) -> ContainerHelper:
"""Returns a dictionary of all objects in the Environment."""
container = []
for f in self.cabs.values():
if isinstance(f, SerializedFile) and not f.is_dependency:
container.extend(f.container.container)
return ContainerHelper(container)
@property
def assets(self) -> list:
"""
Lists all assets / SerializedFiles within this environment.
"""
def gen_all_asset_files(file, ret: Optional[list] = None):
if ret is None:
ret = []
for f in getattr(file, "files", {}).values():
if getattr(f, "is_dependency", False):
continue
if isinstance(f, SerializedFile):
ret.append(f)
else:
gen_all_asset_files(f, ret)
return ret
return gen_all_asset_files(self)
def get(self, key: str, default=None):
return getattr(self, key, default)
def register_cab(self, name: str, item: Union[SerializedFile, EndianBinaryReader]) -> None:
"""
Registers a cab file.
Parameters
----------
name : str
The name of the cab file.
item : File
The file to register.
"""
self.cabs[simplify_name(name)] = item
def get_cab(self, name: str) -> Union[SerializedFile, EndianBinaryReader, None]:
"""
Returns the cab file with the given name.
Parameters
----------
name : str
The name of the cab file.
Returns
-------
File
The cab file.
"""
return self.cabs.get(simplify_name(name), None)
def load_assets(self, assets: List[str], open_f: Callable[[str], BinaryIO]):
"""
Load all assets from a list of files via the given open_f function.
Parameters
----------
assets : List[str]
List of files to load.
open_f : Callable[[str], io.IOBase]
Function to open the files.
The function takes a file path and returns an io.IOBase object.
"""
split_files = []
for path in assets:
splitMatch = reSplit.match(path)
if splitMatch:
basepath, _basename = splitMatch.groups()
if basepath in split_files:
continue
split_files.append(basepath)
data = self._load_split_file(basepath)
path = basepath
else:
data = open_f(path)
self.load_file(data, name=path)
def find_file(self, name: str, is_dependency: bool = True) -> Union[File, None]:
"""
Finds a file in the environment.
Parameters
----------
name : str
The name of the file.
is_dependency : bool
Whether the file is a dependency.
Returns
-------
File | None
The file if it was found, otherwise None.
"""
simple_name = simplify_name(name)
cab = self.get_cab(simple_name)
if cab:
return cab
fp = self.fs.sep.join([self.path, name])
if self.fs.exists(fp):
return self.load_file(fp, name=name, is_dependency=is_dependency)
if len(self.local_files) == 0 and self.path:
for root, _, files in self.fs.walk(self.path):
for f in files:
self.local_files.append(self.fs.sep.join([root, f]))
self.local_files_simple.append(self.fs.sep.join([root, simplify_name(f)]))
if name in self.local_files:
fp = name
elif simple_name in self.local_files_simple:
fp = self.local_files[self.local_files_simple.index(simple_name)]
else:
fp = next((f for f in self.local_files if f.endswith(name)), None)
if not fp:
fp = next(
(f for f in self.local_files_simple if f.endswith(simple_name)),
None,
)
if not fp:
raise FileNotFoundError(f"File {name} not found in {self.path}")
return self.load_file(fp, name=name, is_dependency=is_dependency)
def simplify_name(name: str) -> str:
"""Simplifies a name by:
- removing the extension
- removing the path
- converting to lowercase
"""
return ntpath.basename(name).lower()