|
1 | 1 | import ntpath |
2 | 2 | from ..streams import EndianBinaryReader |
3 | | -from ..files import File |
4 | 3 |
|
| 4 | +from typing import TYPE_CHECKING |
| 5 | +if TYPE_CHECKING: |
| 6 | + from ..files.SerializedFile import SerializedFile |
5 | 7 |
|
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 - |
18 | 8 |
|
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) |
33 | 26 | for possible_name in possible_names: |
34 | 27 | reader = environment.get_cab(possible_name) |
35 | 28 | if reader: |
36 | 29 | break |
37 | 30 | 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) |
49 | 33 |
|
| 34 | + |
| 35 | +def _get_resource_data(reader: EndianBinaryReader, offset: int, size: int): |
50 | 36 | reader.Position = offset |
51 | 37 | return reader.read_bytes(size) |
0 commit comments