Skip to content

Commit 10346b4

Browse files
authored
1.9.27 - BundleFile - alignment fix for Unity 2019.4.X
* BundleFile - check data for block_alignment * restrict alignment check * only check alignment data for version < 7 * use correct version check * improve error message on failed file parsing * bump version
1 parent c8d41de commit 10346b4

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

UnityPy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.9.26"
1+
__version__ = "1.9.27"
22

33
from .environment import Environment
44
from .helpers.ArchiveStorageManager import set_assetbundle_decrypt_key

UnityPy/environment.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ def load_file(
129129
return f
130130
except Exception as e:
131131
# just to be sure
132-
# cuz the SerializedFile detection isn't perfect
133-
print("Error loading, reverting to EndianBinaryReader:\n", str(e))
132+
# because the SerializedFile detection isn't perfect
133+
print(f"Error while loading {stream_name}: {e}")
134+
print("Interpreting it as ResourceFile...")
134135
return EndianBinaryReader(file)
135136

136137
def load_zip_file(self, value):

UnityPy/files/BundleFile.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BundleFile(File.File):
2323
version_player: str
2424
dataflags: Tuple[ArchiveFlags, ArchiveFlagsOld]
2525
decryptor: ArchiveStorageManager.ArchiveStorageDecryptor = None
26+
_uses_block_alignment: bool = False
2627

2728
def __init__(self, reader: EndianBinaryReader, parent: File, name: str = None):
2829
super().__init__(parent=parent, name=name)
@@ -109,8 +110,19 @@ def read_fs(self, reader: EndianBinaryReader):
109110
if self.dataflags & self.dataflags.UsesAssetBundleEncryption:
110111
self.decryptor = ArchiveStorageManager.ArchiveStorageDecryptor(reader)
111112

113+
# check if we need to align the reader
114+
# - align to 16 bytes and check if all are 0
115+
# - if not, reset the reader to the previous position
112116
if self.version >= 7:
113-
reader.align_stream(16)
117+
reader.Position = pre_align
118+
self._uses_block_alignment = True
119+
elif version >= (2019, 4):
120+
pre_align = reader.Position
121+
align_data = reader.read((16 - pre_align % 16) % 16)
122+
if any(align_data):
123+
reader.Position = pre_align
124+
else:
125+
self._uses_block_alignment = True
114126

115127
start = reader.Position
116128
if (
@@ -360,7 +372,7 @@ def save_fs(self, writer: EndianBinaryWriter, data_flag: int, block_info_flag: i
360372
# compression and file layout flag
361373
writer.write_u_int(data_flag)
362374

363-
if self.version >= 7:
375+
if self._uses_block_alignment:
364376
# UnityFS\x00 - 8
365377
# size 8
366378
# comp sizes 4+4

0 commit comments

Comments
 (0)