Skip to content

Commit fd42f9c

Browse files
isHarryhK0lb3
authored andcommitted
refactor(helpers): get_resource_data in ResourceReader
1 parent 2f9f647 commit fd42f9c

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

UnityPy/helpers/ResourceReader.py

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,37 @@
11
import ntpath
22
from ..streams import EndianBinaryReader
3-
from ..files import File
43

4+
from typing import TYPE_CHECKING
5+
if TYPE_CHECKING:
6+
from ..files.SerializedFile import SerializedFile
57

6-
def get_resource_data(*args):
7-
"""
8-
Input:
9-
Option 1:
10-
0 - path - file path
11-
1 - assets_file - SerializedFile
12-
2 - offset -
13-
3 - size -
14-
Option 2:
15-
0 - reader - EndianBinaryReader
16-
1 - offset -
17-
2 - size -
188

19-
-> -2 = offset, -1 = size
20-
"""
21-
if len(args) == 4:
22-
res_path, assets_file, offset, size = args
23-
basename = ntpath.basename(res_path)
24-
name, ext = ntpath.splitext(basename)
25-
possible_names = [
26-
basename,
27-
f"{name}.resource",
28-
f"{name}.assets.resS",
29-
f"{name}.resS",
30-
]
31-
environment = assets_file.environment
32-
reader = None
9+
def get_resource_data(res_path: str, assets_file: "SerializedFile", offset: int, size: int):
10+
basename = ntpath.basename(res_path)
11+
name, ext = ntpath.splitext(basename)
12+
possible_names = [
13+
basename,
14+
f"{name}.resource",
15+
f"{name}.assets.resS",
16+
f"{name}.resS",
17+
]
18+
environment = assets_file.environment
19+
reader = None
20+
for possible_name in possible_names:
21+
reader = environment.get_cab(possible_name)
22+
if reader:
23+
break
24+
if not reader:
25+
assets_file.load_dependencies(possible_names)
3326
for possible_name in possible_names:
3427
reader = environment.get_cab(possible_name)
3528
if reader:
3629
break
3730
if not reader:
38-
assets_file.load_dependencies(possible_names)
39-
for possible_name in possible_names:
40-
reader = environment.get_cab(possible_name)
41-
if reader:
42-
break
43-
if not reader:
44-
raise FileNotFoundError(f"Resource file {basename} not found")
45-
elif len(args) == 3:
46-
reader, offset, size = args
47-
else:
48-
raise TypeError(f"3 or 4 arguments required, but only {len(args)} given")
31+
raise FileNotFoundError(f"Resource file {basename} not found")
32+
return _get_resource_data(reader, offset, size)
4933

34+
35+
def _get_resource_data(reader: EndianBinaryReader, offset: int, size: int):
5036
reader.Position = offset
5137
return reader.read_bytes(size)

0 commit comments

Comments
 (0)