forked from K0lb3/UnityPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceReader.py
More file actions
77 lines (65 loc) · 2.46 KB
/
ResourceReader.py
File metadata and controls
77 lines (65 loc) · 2.46 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
import os, glob
from ..streams import EndianBinaryReader
from ..files import File
def get_resource_data(*args):
"""
Input:
Option 1:
0 - path - file path
1 - assets_file - SerializedFile
2 - offset -
3 - size -
Option 2:
0 - reader - EndianBinaryReader
1 - offset -
2 - size -
-> -2 = offset, -1 = size
"""
if len(args) == 4:
reader = search_resource(res_path=args[0], assets_file=args[1])
elif len(args) == 3:
reader = args[0]
else:
raise TypeError(f"3 or 4 arguments required, but only {len(args)} given")
reader.Position = args[-2]
return reader.read_bytes(args[-1])
def search_resource(res_path, assets_file):
# try to find the resource in the Unity packages
base_name = os.path.basename(res_path)
if os.path.splitext(base_name)[1] == ".resource":
base_name2 = base_name.replace('.resource', '.assets.resS')
else:
base_name2 = base_name.replace('.assets.resS', '.resource')
for p in [res_path, base_name, base_name2]:
reader = assets_file.parent.files.get(p)
if reader:
if isinstance(reader, File.File):
# in case the import helper accidentally detected a resource file as something else
reader = reader.reader
return reader
# try to find it in the dir environment
c = assets_file
path = getattr(assets_file, "path", None)
while not path:
c = getattr(c,"parent",None)
if c == None:
raise FileNotFoundError(
f"Can't find the resource file {res_path}"
)
path = getattr(c, "path", None)
current_directory = path
resource_file_path = os.path.join(current_directory, *res_path.split("/"))
if not os.path.isfile(resource_file_path):
resource_file_path = search_resource_file(current_directory, base_name)
if not os.path.isfile(resource_file_path):
resource_file_path = search_resource_file(current_directory, base_name.replace('.assets.resS', '.resource'))
if os.path.isfile(resource_file_path):
return EndianBinaryReader(open(resource_file_path, "rb"))
else:
raise FileNotFoundError(
f"Can't find the resource file {res_path}"
)
def search_resource_file(path, name):
#print("real file", path, name)
files = glob.glob(os.path.join(path, "**", name), recursive=True)
return files[0] if len(files) else ""