Skip to content

Commit e9558ac

Browse files
authored
Fix save_fs flag confusion (K0lb3#123)
* Fix save_fs flag argument confusion The names of the two flags in save_fs were the exact opposite of their names in BundleFile fields and BundleFile.save. In particular, bundle_file.save("original") would end up swapping the flags, which not only was counterintuitive but could even make the output files unreadable in Unity proper (because most of the flags aren't validated or enforced by UnityPy). Rename the save_fs arguments to match BundleFile fields. Keep them in the same order so that any user-written calls without named arguments continue to work. * save_fs: raise exception if data_flag suggests no DirectoryInfo UnityPy unconditionally writes the DirectoryInfo, but if the flags don't reflect this, the asset bundle can't be loaded in Unity. I don't know what files without this flag are supposed to look like, so just require the flag to be set always.
1 parent 54382cc commit e9558ac

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

UnityPy/files/BundleFile.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,18 @@ def save(self, packer=None):
176176
elif packer == "original":
177177
self.save_fs(
178178
writer,
179-
block_info_flag=self._block_info_flags,
180179
data_flag=self._data_flags,
180+
block_info_flag=self._block_info_flags,
181181
)
182182
elif packer == "lz4":
183-
self.save_fs(writer, block_info_flag=194, data_flag=2)
183+
self.save_fs(writer, data_flag=194, block_info_flag=2)
184184
elif isinstance(packer, tuple):
185185
self.save_fs(writer, *packer)
186186
else:
187187
raise NotImplemented("UnityFS - Packer:", packer)
188188
return writer.bytes
189189

190-
def save_fs(self, writer: EndianBinaryWriter, block_info_flag: int, data_flag: int):
190+
def save_fs(self, writer: EndianBinaryWriter, data_flag: int, block_info_flag: int):
191191
# header
192192
# compressed blockinfo (block details & directionary)
193193
# compressed assets
@@ -256,7 +256,7 @@ def save_fs(self, writer: EndianBinaryWriter, block_info_flag: int, data_flag: i
256256
uncompressed_data_size = len(file_data)
257257

258258
# compress the data
259-
switch = data_flag & 0x3F
259+
switch = block_info_flag & 0x3F
260260
if switch == 1: # LZMA
261261
file_data = CompressionHelper.compress_lzma(file_data)
262262
elif switch in [2, 3]: # LZ4, LZ4HC
@@ -277,9 +277,11 @@ def save_fs(self, writer: EndianBinaryWriter, block_info_flag: int, data_flag: i
277277
# compressed size
278278
block_writer.write_u_int(compressed_data_size)
279279
# flag
280-
block_writer.write_u_short(data_flag)
280+
block_writer.write_u_short(block_info_flag)
281281

282282
# file block info
283+
if not data_flag & 0x40:
284+
raise NotImplementedError("UnityPy always writes DirectoryInfo, so data_flag must include 0x40")
283285
# file count
284286
block_writer.write_int(len(files))
285287
offset = 0
@@ -300,7 +302,7 @@ def save_fs(self, writer: EndianBinaryWriter, block_info_flag: int, data_flag: i
300302

301303
uncompressed_block_data_size = len(block_data)
302304

303-
switch = block_info_flag & 0x3F
305+
switch = data_flag & 0x3F
304306
if switch == 1: # LZMA
305307
block_data = CompressionHelper.compress_lzma(block_data)
306308
elif switch in [2, 3]: # LZ4, LZ4HC
@@ -318,8 +320,8 @@ def save_fs(self, writer: EndianBinaryWriter, block_info_flag: int, data_flag: i
318320
writer.write_u_int(compressed_block_data_size)
319321
# uncompressed size
320322
writer.write_u_int(uncompressed_block_data_size)
321-
# compression flag
322-
writer.write_u_int(block_info_flag)
323+
# compression and file layout flag
324+
writer.write_u_int(data_flag)
323325

324326
if self.version >= 7:
325327
# UnityFS\x00 - 8
@@ -329,7 +331,7 @@ def save_fs(self, writer: EndianBinaryWriter, block_info_flag: int, data_flag: i
329331
# sum : 28 -> +8 alignment
330332
writer.align_stream(16)
331333

332-
if (block_info_flag & 0x80) != 0: # at end of file
334+
if data_flag & 0x80: # at end of file
333335
if data_flag & 0x200:
334336
writer.align_stream(16)
335337
writer.write(file_data)

0 commit comments

Comments
 (0)