Skip to content

Commit 2f9f647

Browse files
isHarryhK0lb3
authored andcommitted
fix(helpers): type hint, import, typo
1 parent 9fa7542 commit 2f9f647

File tree

8 files changed

+34
-33
lines changed

8 files changed

+34
-33
lines changed

UnityPy/helpers/ArchiveStorageManager.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# based on: https://github.com/Razmoth/PGRStudio/blob/master/AssetStudio/PGR/PGR.cs
22
import re
3-
from typing import Tuple, Union
3+
from typing import Optional, Tuple, Union
44

55
from ..streams import EndianBinaryReader
66

@@ -10,7 +10,7 @@
1010
UnityPyBoost = None
1111

1212
UNITY3D_SIGNATURE = b"#$unity3dchina!@"
13-
DECRYPT_KEY: bytes = None
13+
DECRYPT_KEY: Optional[bytes] = None
1414

1515

1616
def set_assetbundle_decrypt_key(key: Union[bytes, str]):
@@ -66,7 +66,7 @@ class ArchiveStorageDecryptor:
6666
index: bytes
6767
substitute: bytes = bytes(0x10)
6868

69-
def __init__(self, reader: EndianBinaryReader) -> None:
69+
def __init__(self, reader: EndianBinaryReader):
7070
self.unknown_1 = reader.read_u_int()
7171

7272
# read vector data/key vectors
@@ -100,7 +100,6 @@ def __init__(self, reader: EndianBinaryReader) -> None:
100100
)
101101

102102
def decrypt_block(self, data: bytes, index: int):
103-
104103
if UnityPyBoost:
105104
return UnityPyBoost.decrypt_block(self.index, self.substitute, data, index)
106105

@@ -113,7 +112,7 @@ def decrypt_block(self, data: bytes, index: int):
113112
index += 1
114113
return data
115114

116-
def decrypt_byte(self, view: bytearray, offset: int, index: int):
115+
def decrypt_byte(self, view: Union[bytearray, memoryview], offset: int, index: int):
117116
b = (
118117
self.substitute[((index >> 2) & 3) + 4]
119118
+ self.substitute[index & 3]
@@ -127,7 +126,7 @@ def decrypt_byte(self, view: bytearray, offset: int, index: int):
127126
b = view[offset]
128127
return b, offset + 1, index + 1
129128

130-
def decrypt(self, data: bytearray, index: int, remaining: int):
129+
def decrypt(self, data: Union[bytearray, memoryview], index: int, remaining: int):
131130
offset = 0
132131

133132
curByte, offset, index = self.decrypt_byte(data, offset, index)

UnityPy/helpers/CompressionHelper.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
import brotli
12
import gzip
23
import lzma
4+
import lz4.block
35
import struct
46
from typing import Tuple
57

6-
import brotli
7-
import lz4.block
8-
98
GZIP_MAGIC: bytes = b"\x1f\x8b"
109
BROTLI_MAGIC: bytes = b"brotli"
1110

UnityPy/helpers/ImportHelper.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
2+
23
import os
3-
from typing import Union, List
4+
from typing import Union, List, Optional, Tuple
45
from .CompressionHelper import BROTLI_MAGIC, GZIP_MAGIC
56
from ..enums import FileType
67
from ..streams import EndianBinaryReader
@@ -18,7 +19,7 @@ def list_all_files(directory: str) -> List[str]:
1819
val
1920
for sublist in [
2021
[os.path.join(dir_path, filename) for filename in filenames]
21-
for (dir_path, dirn_ames, filenames) in os.walk(directory)
22+
for (dir_path, dirnames, filenames) in os.walk(directory)
2223
if ".git" not in dir_path
2324
]
2425
for val in sublist
@@ -34,14 +35,14 @@ def find_all_files(directory: str, search_str: str) -> List[str]:
3435
for filename in filenames
3536
if search_str in filename
3637
]
37-
for (dir_path, dirn_ames, filenames) in os.walk(directory)
38+
for (dir_path, dirnames, filenames) in os.walk(directory)
3839
if ".git" not in dir_path
3940
]
4041
for val in sublist
4142
]
4243

4344

44-
def check_file_type(input_) -> Union[FileType, EndianBinaryReader]:
45+
def check_file_type(input_) -> Tuple[Optional[FileType], Optional[EndianBinaryReader]]:
4546
if isinstance(input_, str) and os.path.isfile(input_):
4647
reader = EndianBinaryReader(open(input_, "rb"))
4748
elif isinstance(input_, EndianBinaryReader):
@@ -124,10 +125,10 @@ def check_file_type(input_) -> Union[FileType, EndianBinaryReader]:
124125

125126
def parse_file(
126127
reader: EndianBinaryReader,
127-
parent,
128+
parent: files.File,
128129
name: str,
129-
typ: FileType = None,
130-
is_dependency=False,
130+
typ: Optional[FileType] = None,
131+
is_dependency: bool = False
131132
) -> Union[files.File, EndianBinaryReader]:
132133
if typ is None:
133134
typ, _ = check_file_type(reader)

UnityPy/helpers/MeshHelper.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import annotations
2+
23
import math
34
import struct
4-
from typing import Optional, List, Tuple, Union, TypeVar
5+
from typing import Optional, List, Sequence, Tuple, Union, TypeVar
56

6-
from ..enums.MeshTopology import MeshTopology
77
from ..classes.generated import (
88
ChannelInfo,
99
StreamInfo,
@@ -13,8 +13,7 @@
1313
Vector3f,
1414
Vector4f,
1515
)
16-
from .PackedBitVector import unpack_floats, unpack_ints
17-
16+
from ..enums.MeshTopology import MeshTopology
1817
from ..enums.VertexFormat import (
1918
VertexChannelFormat,
2019
VertexFormat2017,
@@ -23,6 +22,7 @@
2322
VERTEX_FORMAT_2017_STRUCT_TYPE_MAP,
2423
VERTEX_FORMAT_STRUCT_TYPE_MAP,
2524
)
25+
from .PackedBitVector import unpack_floats, unpack_ints
2626
from .ResourceReader import get_resource_data
2727

2828
try:
@@ -37,11 +37,11 @@
3737
T = TypeVar("T")
3838

3939

40-
def flat_list_to_tuples(data: List[T], item_size: int) -> List[tuple[T]]:
40+
def flat_list_to_tuples(data: Sequence[T], item_size: int) -> List[tuple[T, ...]]:
4141
return [tuple(data[i : i + item_size]) for i in range(0, len(data), item_size)]
4242

4343

44-
def vector_list_to_tuples(data: List[Vector2f, Vector3f, Vector4f]) -> List[tuple]:
44+
def vector_list_to_tuples(data: List[Union[Vector2f, Vector3f, Vector4f]]) -> List[tuple]:
4545
if isinstance(data[0], Vector2f):
4646
return [(v.x, v.y) for v in data]
4747
elif isinstance(data[0], Vector3f):
@@ -52,7 +52,7 @@ def vector_list_to_tuples(data: List[Vector2f, Vector3f, Vector4f]) -> List[tupl
5252
raise ValueError("Unknown vector type")
5353

5454

55-
def zeros(shape: Tuple[int, ...]) -> list:
55+
def zeros(shape: Union[Tuple[int], Tuple[int, int]]) -> Union[List, List[List]]:
5656
if len(shape) == 1:
5757
return [0] * shape[0]
5858
elif len(shape) == 2:
@@ -97,7 +97,7 @@ def __init__(
9797
src: Union[Mesh, SpriteRenderData],
9898
version: Optional[Tuple[int, int, int, int]] = None,
9999
endianess: str = "<",
100-
) -> None:
100+
):
101101
self.src = src
102102
self.endianess = endianess
103103
if version is not None:

UnityPy/helpers/PackedBitVector.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import Optional, List, TYPE_CHECKING, Tuple
22

3-
# from ..objects.math import Quaternionf
43
if TYPE_CHECKING:
54
from ..classes.generated import PackedBitVector
65

UnityPy/helpers/Tpk.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from importlib.resources import open_binary
55
from io import BytesIO
66
from struct import Struct
7-
from typing import Any, Dict, List, Tuple
7+
from typing import Any, Dict, List, Optional, Tuple
88

99
from .TypeTreeHelper import TypeTreeNode
1010

@@ -19,7 +19,9 @@ def init():
1919

2020
global TPKTYPETREE
2121
with BytesIO(data) as stream:
22-
TPKTYPETREE = TpkFile(stream).GetDataBlob()
22+
blob = TpkFile(stream).GetDataBlob()
23+
assert isinstance(blob, TpkTypeTreeBlob)
24+
TPKTYPETREE = blob
2325

2426

2527
def get_typetree_node(class_id: int, version: tuple):
@@ -338,8 +340,8 @@ class TpkUnityClass:
338340
Name: int
339341
Base: int
340342
Flags: TpkUnityClassFlags
341-
EditorRootNode: int
342-
ReleaseRootNode: int
343+
EditorRootNode: Optional[int]
344+
ReleaseRootNode: Optional[int]
343345

344346
def __init__(self, stream: BytesIO) -> None:
345347
self.Name, self.Base, Flags = TpkUnityClass.Struct.unpack(
@@ -473,7 +475,7 @@ def Count(self) -> int:
473475
class TpkCommonString:
474476
__slots__ = ("VersionInformation", "StringBufferIndices")
475477
VersionInformation: List[Tuple[UnityVersion, int]]
476-
StringBufferIndices: List[int]
478+
StringBufferIndices: Tuple[int]
477479

478480
def __init__(self, stream: BytesIO) -> None:
479481
(versionCount,) = INT32.unpack(stream.read(INT32.size))

UnityPy/helpers/TypeTreeHelper.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from __future__ import annotations
1+
from __future__ import annotations
22

33
import re
44
from typing import TYPE_CHECKING, Any, Optional, Union
@@ -49,6 +49,7 @@
4949
"string": EndianBinaryReader.read_aligned_string,
5050
"TypelessData": EndianBinaryReader.read_byte_array,
5151
}
52+
5253
FUNCTION_READ_MAP_ARRAY = {
5354
"SInt8": EndianBinaryReader.read_byte_array,
5455
"UInt8": EndianBinaryReader.read_u_byte_array,
@@ -83,7 +84,7 @@ def copy(self) -> TypeTreeConfig:
8384
return TypeTreeConfig(self.as_dict, self.assetsfile, self.has_registry)
8485

8586

86-
def get_ref_type_node(ref_object: dict, assetfile: SerializedFile) -> TypeTreeNode:
87+
def get_ref_type_node(ref_object: dict, assetfile: SerializedFile) -> Optional[TypeTreeNode]:
8788
typ = ref_object["type"]
8889
if isinstance(typ, dict):
8990
cls = typ["class"]

UnityPy/helpers/TypeTreeNode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import re
44
from struct import Struct
5-
from typing import TYPE_CHECKING, Dict, Iterator, List, Optional, Tuple, Any, Union
5+
from typing import TYPE_CHECKING, Dict, Iterator, List, Optional, Tuple, Union
66

77
from attrs import define, field
88

0 commit comments

Comments
 (0)