From 72a4c3c5d64f3761ee4154820c1b3eb3afbb5a89 Mon Sep 17 00:00:00 2001 From: yarik0chka Date: Tue, 16 Jul 2024 02:47:42 +0500 Subject: [PATCH] [Blb3] Fixed some blocks parsing, some refactoring --- AssetStudio.GUI/Studio.cs | 5 ++- AssetStudio/BlbFile.cs | 77 +++++++++++++++++++------------------ AssetStudio/OffsetStream.cs | 57 +++------------------------ 3 files changed, 50 insertions(+), 89 deletions(-) diff --git a/AssetStudio.GUI/Studio.cs b/AssetStudio.GUI/Studio.cs index 36a90c4..5ee653b 100644 --- a/AssetStudio.GUI/Studio.cs +++ b/AssetStudio.GUI/Studio.cs @@ -173,7 +173,10 @@ private static int ExtractBlockFile(FileReader reader, string savePath) var subSavePath = Path.Combine(savePath, reader.FileName + "_unpacked"); var dummyPath = Path.Combine(reader.FullPath, stream.AbsolutePosition.ToString("X8")); var subReader = new FileReader(dummyPath, stream, true); - total += ExtractBundleFile(subReader, subSavePath); + if (subReader.FileType == FileType.Blb3File) + total += ExtractBlb3File(subReader, subSavePath); + else + total += ExtractBundleFile(subReader, subSavePath); } while (stream.Remaining > 0); return total; } diff --git a/AssetStudio/BlbFile.cs b/AssetStudio/BlbFile.cs index 719d755..7c5070c 100644 --- a/AssetStudio/BlbFile.cs +++ b/AssetStudio/BlbFile.cs @@ -15,7 +15,6 @@ public class Blb3File private byte[] Header; private static string ModuleName = "UnityPlayer.dll"; - private static IntPtr Module = IntPtr.Zero; public BundleFile.Header m_Header; public List fileList; @@ -27,35 +26,36 @@ public class Blb3File [DllImport("kernel32.dll")] static extern IntPtr LoadLibrary(string lpFileName); - - [DllImport("kernel32.dll")] - static extern IntPtr GetModuleHandle(string lpModuleName); - - public delegate int DecryptBlb(ref byte buffer, ulong size, ref byte header, ulong headerSize, uint idk); + + private delegate int DecryptBlb(ref byte buffer, ulong size, ref byte header, ulong headerSize, uint idk); + + private static DecryptBlb _decrypt; public static void Decrypt(byte[] header, Span buffer) { - if (Module == IntPtr.Zero) + if (_decrypt == null) { - Module = LoadLibrary(ModuleName); + var module = LoadLibrary(ModuleName); byte[] key1 = new byte[256] { 0x63, 0x7d, 0x75, 0x78, 0xf6, 0x6e, 0x69, 0xc2, 0x38, 0x08, 0x6d, 0x20, 0xf2, 0xda, 0xa5, 0x79, 0xda, 0x93, 0xdb, 0x6e, 0xee, 0x4c, 0x51, 0xe7, 0xb5, 0xcd, 0xb8, 0xb4, 0x80, 0xb9, 0x6c, 0xdf, 0x97, 0xdc, 0xb1, 0x05, 0x12, 0x1a, 0xd1, 0xeb, 0x1c, 0x8c, 0xcf, 0xda, 0x5d, 0xf5, 0x1f, 0x3a, 0x34, 0xf6, 0x11, 0xf0, 0x2c, 0xa3, 0x33, 0xad, 0x3f, 0x2b, 0xba, 0xd9, 0xd7, 0x1a, 0x8c, 0x4a, 0x49, 0xc2, 0x6e, 0x59, 0x5f, 0x2b, 0x1c, 0xe7, 0x1a, 0x72, 0x9c, 0xf8, 0x65, 0xae, 0x61, 0xcb, 0x03, 0x80, 0x52, 0xbe, 0x74, 0xa9, 0xe7, 0x0c, 0x32, 0x92, 0xe4, 0x62, 0x16, 0x11, 0x06, 0x90, 0xb0, 0x8e, 0xc8, 0x98, 0x27, 0x28, 0x55, 0xe2, 0x2d, 0x90, 0x68, 0x14, 0x3c, 0x51, 0xf1, 0xc7, 0x21, 0xd2, 0x32, 0xfc, 0xe6, 0xe8, 0x4e, 0x82, 0xc4, 0xcf, 0xa0, 0x5a, 0x6c, 0x82, 0x8d, 0xad, 0x4d, 0x8d, 0x91, 0x6f, 0xdb, 0x12, 0xc2, 0x90, 0x4c, 0x2e, 0xf4, 0xb6, 0xe8, 0xd0, 0x97, 0xfc, 0xf0, 0x10, 0xdd, 0x4f, 0xb6, 0xbf, 0x06, 0x1f, 0xde, 0x77, 0x22, 0x8f, 0x42, 0xc3, 0x95, 0x44, 0x40, 0x93, 0x98, 0xa9, 0xed, 0xa3, 0x82, 0xfb, 0x6a, 0x7a, 0x06, 0xc9, 0x3d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x91, 0x9d, 0xe1, 0x8b, 0x1f, 0xda, 0xb0, 0xca, 0x99, 0x02, 0xb9, 0x72, 0x9d, 0x49, 0x2c, 0x80, 0x7e, 0xc5, 0x99, 0xd5, 0xe9, 0x80, 0xb2, 0xea, 0xc9, 0xcc, 0x53, 0xbf, 0x67, 0xd6, 0xbf, 0x14, 0xd6, 0x7e, 0x2d, 0xdc, 0x8e, 0x66, 0x83, 0xef, 0x57, 0x49, 0x61, 0xff, 0x69, 0x8f, 0x61, 0xcd, 0xd1, 0x1e, 0x9d, 0x9c, 0x16, 0x72, 0x72, 0xe6, 0x1d, 0xf0, 0x84, 0x4f, 0x4a, 0x77, 0x02, 0xd7, 0xe8, 0x39, 0x2c, 0x53, 0xcb, 0xc9, 0x12, 0x1e, 0x33, 0x74, 0x9e, 0x0c, 0xf4, 0xd5, 0xd4, 0x9f, 0xd4, 0xa4, 0x59, 0x7e, 0x35, 0xcf, 0x32, 0x22, 0xf4, 0xcc, 0xcf, 0xd3, 0x90, 0x2d, 0x48, 0xd3, 0x8f, 0x75, 0xe6, 0xd9, 0x1d, 0x2a, 0xe5, 0xc0, 0xf7, 0x2b, 0x78, 0x81, 0x87, 0x44, 0x0e, 0x5f, 0x50, 0x00, 0xd4, 0x61, 0x8d, 0xbe, 0x7b, 0x05, 0x15, 0x07, 0x3b, 0x33, 0x82, 0x1f, 0x18, 0x70, 0x92, 0xda, 0x64, 0x54, 0xce, 0xb1, 0x85, 0x3e, 0x69, 0x15, 0xf8, 0x46, 0x6a, 0x04, 0x96, 0x73, 0x0e, 0xd9, 0x16, 0x2f, 0x67, 0x68, 0xd4, 0xf7, 0x4a, 0x4a, 0xd0, 0x57, 0x68, 0x76 }; - IntPtr key1Ptr = GetModuleHandle(ModuleName) + 0x1FF7090; - IntPtr key2Ptr = GetModuleHandle(ModuleName) + 0x1FF6F90; + IntPtr key1Ptr = module + 0x1FF7090; + IntPtr key2Ptr = module + 0x1FF6F90; OverrideBytes(key1Ptr, key1, 256); OverrideBytes(key2Ptr, key2, 256); + + IntPtr function_raw = module + 0xFC730; + + _decrypt = Marshal.GetDelegateForFunctionPointer(function_raw); } - IntPtr function_raw = Module + 0xFC730; - DecryptBlb decrypt = (DecryptBlb)Marshal.GetDelegateForFunctionPointer(function_raw, typeof(DecryptBlb)); ulong header_size = (ulong)header.Length; ulong buffer_size = (ulong)buffer.Length; - decrypt(ref buffer[0], buffer_size > 128 ? 128 : buffer_size, ref header[0], header_size, 0); + _decrypt(ref buffer[0], buffer_size > 128 ? 128 : buffer_size, ref header[0], header_size, 0); } public Blb3File(FileReader reader, string path) @@ -138,7 +138,8 @@ private void ReadBlocksInfoAndDirectory(byte[] header) } for (int i = m_BlocksInfo.Count - 1; i > 0; i--) { - m_BlocksInfo[i].compressedSize = m_BlocksInfo[i].compressedSize - m_BlocksInfo[i - 1].compressedSize; + m_BlocksInfo[i].compressedSize -= m_BlocksInfo[i - 1].compressedSize; + m_BlocksInfo[i].flags = (StorageBlockFlags)(m_BlocksInfo[i].compressedSize == m_BlocksInfo[i].uncompressedSize ? CompressionType.None : compressionType); } reader.Position = nodesInfoOffset; @@ -195,42 +196,44 @@ private void ReadBlocks(FileReader reader, Stream blocksStream) { case CompressionType.None: //None { - reader.BaseStream.CopyTo(blocksStream, blockInfo.compressedSize); + var size = (int)blockInfo.uncompressedSize; + var buffer = reader.ReadBytes(size); + Decrypt(Header, buffer); + blocksStream.Write(buffer); break; } case CompressionType.Oodle: //Oodle { - { - var compressedSize = (int)blockInfo.compressedSize; - var uncompressedSize = (int)blockInfo.uncompressedSize; - - var compressedBytes = ArrayPool.Shared.Rent(compressedSize); - var uncompressedBytes = ArrayPool.Shared.Rent(uncompressedSize); + var compressedSize = (int)blockInfo.compressedSize; + var uncompressedSize = (int)blockInfo.uncompressedSize; - var compressedBytesSpan = compressedBytes.AsSpan(0, compressedSize); - var uncompressedBytesSpan = uncompressedBytes.AsSpan(0, uncompressedSize); + var compressedBytes = ArrayPool.Shared.Rent(compressedSize); + var uncompressedBytes = ArrayPool.Shared.Rent(uncompressedSize); - try - { + var compressedBytesSpan = compressedBytes.AsSpan(0, compressedSize); + var uncompressedBytesSpan = uncompressedBytes.AsSpan(0, uncompressedSize); - reader.Read(compressedBytesSpan); + try + { + reader.Read(compressedBytesSpan); + + if (compressedSize > 6) Decrypt(Header, compressedBytesSpan); - var numWrite = OodleHelper.Decompress(compressedBytesSpan, uncompressedBytesSpan); - if (numWrite != uncompressedSize) - { - Logger.Warning($"Oodle decompression error, write {numWrite} bytes but expected {uncompressedSize} bytes"); - } - } - finally + var numWrite = OodleHelper.Decompress(compressedBytesSpan, uncompressedBytesSpan); + if (numWrite != uncompressedSize) { - blocksStream.Write(uncompressedBytesSpan); - ArrayPool.Shared.Return(compressedBytes, true); - ArrayPool.Shared.Return(uncompressedBytes, true); + Logger.Warning($"Oodle decompression error, write {numWrite} bytes but expected {uncompressedSize} bytes"); } - break; } + finally + { + blocksStream.Write(uncompressedBytesSpan); + ArrayPool.Shared.Return(compressedBytes, true); + ArrayPool.Shared.Return(uncompressedBytes, true); + } + break; } case CompressionType.Lzma: //LZMA { diff --git a/AssetStudio/OffsetStream.cs b/AssetStudio/OffsetStream.cs index fafb94d..365076c 100644 --- a/AssetStudio/OffsetStream.cs +++ b/AssetStudio/OffsetStream.cs @@ -79,61 +79,16 @@ public IEnumerable GetOffsets(string path) } else { - using var reader = new FileReader(path, this, true); - - var readSignature = reader.FileType; - var signature = ""; - - if (readSignature == FileType.BundleFile || - readSignature == FileType.MhyFile || - readSignature == FileType.Blb3File) - { - switch (reader.FileType) - { - case FileType.BundleFile: - signature = "UnityFS\x00"; - break; - case FileType.MhyFile: - signature = "mhy0"; - break; - case FileType.Blb3File: - signature = "Blb\x03"; - break; - } - - Logger.Verbose($"Parsed signature: {signature}"); - - var signatureBytes = Encoding.UTF8.GetBytes(signature); - var buffer = ArrayPool.Shared.Rent((int)reader.Length); - while (Remaining > 0) - { - var index = 0; - var absOffset = AbsolutePosition; - var read = Read(buffer); - while (index < read) - { - index = buffer.AsSpan(0, read).Search(signatureBytes, index); - if (index == -1) break; - var offset = absOffset + index; - Offset = offset; - yield return offset; - index++; - } - } - ArrayPool.Shared.Return(buffer); - } else + while (Remaining > 0) { - while (Remaining > 0) + Offset = AbsolutePosition; + yield return AbsolutePosition; + if (Offset == AbsolutePosition) { - Offset = AbsolutePosition; - yield return AbsolutePosition; - if (Offset == AbsolutePosition) - { - break; - } + break; } } } } } -} +} \ No newline at end of file