diff --git a/AssetStudio.GUI/MainForm.cs b/AssetStudio.GUI/MainForm.cs index 1453ef7..985477e 100644 --- a/AssetStudio.GUI/MainForm.cs +++ b/AssetStudio.GUI/MainForm.cs @@ -85,7 +85,7 @@ namespace AssetStudio.GUI { Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); InitializeComponent(); - Text = $"Studio v{Application.ProductVersion}"; + Text = $"YarikStudio v{Application.ProductVersion}"; InitializeExportOptions(); InitializeProgressBar(); InitializeLogger(); @@ -308,7 +308,7 @@ namespace AssetStudio.GUI } } - Text = $"Studio v{Application.ProductVersion} - {productName} - {assetsManager.assetsFileList[0].unityVersion} - {assetsManager.assetsFileList[0].m_TargetPlatform}"; + Text = $"YarikStudio v{Application.ProductVersion} - {productName} - {assetsManager.assetsFileList[0].unityVersion} - {assetsManager.assetsFileList[0].m_TargetPlatform}"; assetListView.VirtualListSize = visibleAssets.Count; @@ -1484,7 +1484,7 @@ namespace AssetStudio.GUI public void ResetForm() { - Text = $"Studio v{Application.ProductVersion}"; + Text = $"YarikStudio v{Application.ProductVersion}"; assetsManager.Clear(); assemblyLoader.Clear(); exportableAssets.Clear(); diff --git a/AssetStudio.GUI/Studio.cs b/AssetStudio.GUI/Studio.cs index ba1b130..5bb379e 100644 --- a/AssetStudio.GUI/Studio.cs +++ b/AssetStudio.GUI/Studio.cs @@ -73,11 +73,31 @@ namespace AssetStudio.GUI extractedCount += ExtractBlkFile(reader, savePath); else if (reader.FileType == FileType.BlockFile) extractedCount += ExtractBlockFile(reader, savePath); + else if (reader.FileType == FileType.Blb3File) + extractedCount += ExtractBlb3File(reader, savePath); else reader.Dispose(); return extractedCount; } - + private static int ExtractBlb3File(FileReader reader, string savePath) + { + StatusStripUpdate($"Decompressing {reader.FileName} ..."); + try + { + var bundleFile = new Blb3File(reader, reader.FullPath); + reader.Dispose(); + if (bundleFile.fileList.Count > 0) + { + var extractPath = Path.Combine(savePath, reader.FileName + "_unpacked"); + return ExtractStreamFile(extractPath, bundleFile.fileList); + } + } + catch (InvalidCastException) + { + Logger.Error($"Game type mismatch, Expected {nameof(Mr0k)} but got {Game.Name} ({Game.GetType().Name}) !!"); + } + return 0; + } private static int ExtractBundleFile(FileReader reader, string savePath) { StatusStripUpdate($"Decompressing {reader.FileName} ..."); diff --git a/AssetStudio/AssetStudio.csproj b/AssetStudio/AssetStudio.csproj index 9024066..774c6f8 100644 --- a/AssetStudio/AssetStudio.csproj +++ b/AssetStudio/AssetStudio.csproj @@ -19,6 +19,12 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + diff --git a/AssetStudio/AssetsManager.cs b/AssetStudio/AssetsManager.cs index ea279e4..461c769 100644 --- a/AssetStudio/AssetsManager.cs +++ b/AssetStudio/AssetsManager.cs @@ -429,8 +429,8 @@ namespace AssetStudio case FileType.BundleFile: LoadBundleFile(subReader, reader.FullPath, offset, false); break; - case FileType.BlbFile: - LoadBlbFile(subReader, reader.FullPath, offset, false); + case FileType.Blb3File: + LoadBlb3File(subReader, reader.FullPath, offset, false); break; case FileType.MhyFile: LoadMhyFile(subReader, reader.FullPath, offset, false); @@ -528,7 +528,7 @@ namespace AssetStudio } } - private void LoadBlbFile(FileReader reader, string originalPath = null, long originalOffset = 0, bool log = true) + private void LoadBlb3File(FileReader reader, string originalPath = null, long originalOffset = 0, bool log = true) { if (log) { @@ -536,7 +536,7 @@ namespace AssetStudio } try { - var blbFile = new BlbFile(reader, reader.FullPath); + var blbFile = new Blb3File(reader, reader.FullPath); foreach (var file in blbFile.fileList) { var dummyPath = Path.Combine(Path.GetDirectoryName(reader.FullPath), file.fileName); diff --git a/AssetStudio/BlbFile.cs b/AssetStudio/BlbFile.cs index 7bcf708..719d755 100644 --- a/AssetStudio/BlbFile.cs +++ b/AssetStudio/BlbFile.cs @@ -3,28 +3,70 @@ using System.Buffers; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.InteropServices; +using System.Text; namespace AssetStudio { - public class BlbFile + public class Blb3File { private List m_BlocksInfo; private List m_DirectoryInfo; + private byte[] Header; + + private static string ModuleName = "UnityPlayer.dll"; + private static IntPtr Module = IntPtr.Zero; public BundleFile.Header m_Header; public List fileList; public long Offset; - public BlbFile(FileReader reader, string path) + [DllImport("kernel32.dll")] + static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect); + + [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); + + public static void Decrypt(byte[] header, Span buffer) + { + if (Module == IntPtr.Zero) + { + 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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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; + + OverrideBytes(key1Ptr, key1, 256); + OverrideBytes(key2Ptr, key2, 256); + } + 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); + } + + public Blb3File(FileReader reader, string path) { Offset = reader.Position; reader.Endian = EndianType.LittleEndian; var signature = reader.ReadStringToNull(4); Logger.Verbose($"Parsed signature {signature}"); - if (signature != "Blb\x02") - throw new Exception("not a Blb file"); + if (signature != "Blb\x03") + throw new Exception("not a Blb3 file"); var size = reader.ReadUInt32(); m_Header = new BundleFile.Header @@ -38,13 +80,25 @@ namespace AssetStudio m_Header.uncompressedBlocksInfoSize = size; Logger.Verbose($"Header: {m_Header}"); + reader.ReadUInt32(); + Header = reader.ReadBytes(16); var header = reader.ReadBytes((int)m_Header.compressedBlocksInfoSize); + + Decrypt(Header, header); + ReadBlocksInfoAndDirectory(header); using var blocksStream = CreateBlocksStream(path); ReadBlocks(reader, blocksStream); ReadFiles(blocksStream, path); } + private static void OverrideBytes(IntPtr address, byte[] bytes, int length) + { + uint oldprotect; + VirtualProtect(address, (uint)length, 0x40, out oldprotect); + Marshal.Copy(bytes, 0, address, length); + VirtualProtect(address, (uint)length, oldprotect, out oldprotect); + } private void ReadBlocksInfoAndDirectory(byte[] header) { @@ -82,6 +136,10 @@ namespace AssetStudio Logger.Verbose($"Block {i} Info: {m_BlocksInfo[i]}"); } + for (int i = m_BlocksInfo.Count - 1; i > 0; i--) + { + m_BlocksInfo[i].compressedSize = m_BlocksInfo[i].compressedSize - m_BlocksInfo[i - 1].compressedSize; + } reader.Position = nodesInfoOffset; m_DirectoryInfo = new List(); @@ -140,6 +198,40 @@ namespace AssetStudio reader.BaseStream.CopyTo(blocksStream, blockInfo.compressedSize); 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 compressedBytesSpan = compressedBytes.AsSpan(0, compressedSize); + var uncompressedBytesSpan = uncompressedBytes.AsSpan(0, uncompressedSize); + + try + { + + reader.Read(compressedBytesSpan); + + 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 + { + blocksStream.Write(uncompressedBytesSpan); + ArrayPool.Shared.Return(compressedBytes, true); + ArrayPool.Shared.Return(uncompressedBytes, true); + } + break; + } + } case CompressionType.Lzma: //LZMA { SevenZipHelper.StreamDecompress(reader.BaseStream, blocksStream, blockInfo.compressedSize, blockInfo.uncompressedSize); @@ -154,21 +246,27 @@ namespace AssetStudio var compressedBytes = ArrayPool.Shared.Rent(compressedSize); var uncompressedBytes = ArrayPool.Shared.Rent(uncompressedSize); + var compressedBytesSpan = compressedBytes.AsSpan(0, compressedSize); + var uncompressedBytesSpan = uncompressedBytes.AsSpan(0, uncompressedSize); + try { - var compressedBytesSpan = compressedBytes.AsSpan(0, compressedSize); - var uncompressedBytesSpan = uncompressedBytes.AsSpan(0, uncompressedSize); reader.Read(compressedBytesSpan); + Decrypt(Header, compressedBytesSpan); var numWrite = LZ4.Instance.Decompress(compressedBytesSpan, uncompressedBytesSpan); if (numWrite != uncompressedSize) { - throw new IOException($"Lz4 decompression error, write {numWrite} bytes but expected {uncompressedSize} bytes"); + Logger.Warning($"Lz4 decompression error, write {numWrite} bytes but expected {uncompressedSize} bytes"); } - blocksStream.Write(uncompressedBytesSpan); + } + catch (Exception e) + { + Logger.Error($"Lz4 decompression error {e.Message}"); } finally { + blocksStream.Write(uncompressedBytesSpan); ArrayPool.Shared.Return(compressedBytes, true); ArrayPool.Shared.Return(uncompressedBytes, true); } diff --git a/AssetStudio/BundleFile.cs b/AssetStudio/BundleFile.cs index 86c306f..e730c14 100644 --- a/AssetStudio/BundleFile.cs +++ b/AssetStudio/BundleFile.cs @@ -40,6 +40,7 @@ namespace AssetStudio Zstd = 5, Lz4Lit4 = 4, Lz4Lit5 = 5, + Oodle = 9, } public class BundleFile diff --git a/AssetStudio/FileReader.cs b/AssetStudio/FileReader.cs index 9d614e3..3ed08bd 100644 --- a/AssetStudio/FileReader.cs +++ b/AssetStudio/FileReader.cs @@ -16,7 +16,8 @@ namespace AssetStudio private static readonly byte[] zipMagic = { 0x50, 0x4B, 0x03, 0x04 }; private static readonly byte[] zipSpannedMagic = { 0x50, 0x4B, 0x07, 0x08 }; private static readonly byte[] mhy0Magic = { 0x6D, 0x68, 0x79, 0x30 }; - private static readonly byte[] blbMagic = { 0x42, 0x6C, 0x62, 0x02 }; + private static readonly byte[] blb2Magic = { 0x42, 0x6C, 0x62, 0x02 }; + private static readonly byte[] blb3Magic = { 0x42, 0x6C, 0x62, 0x03 }; private static readonly byte[] narakaMagic = { 0x15, 0x1E, 0x1C, 0x0D, 0x0D, 0x23, 0x21 }; private static readonly byte[] gunfireMagic = { 0x7C, 0x6D, 0x79, 0x72, 0x27, 0x7A, 0x73, 0x78, 0x3F }; @@ -86,11 +87,16 @@ namespace AssetStudio return FileType.MhyFile; } Logger.Verbose($"Parsed signature does not match with expected signature {Convert.ToHexString(mhy0Magic)}"); - if (blbMagic.SequenceEqual(magic)) + if (blb2Magic.SequenceEqual(magic)) { - return FileType.BlbFile; + return FileType.Blb2File; } - Logger.Verbose($"Parsed signature does not match with expected signature {Convert.ToHexString(mhy0Magic)}"); + Logger.Verbose($"Parsed signature does not match with expected signature {Convert.ToHexString(blb2Magic)}"); + if (blb3Magic.SequenceEqual(magic)) + { + return FileType.Blb3File; + } + Logger.Verbose($"Parsed signature does not match with expected signature {Convert.ToHexString(blb3Magic)}"); magic = ReadBytes(7); Position = 0; Logger.Verbose($"Parsed signature is {Convert.ToHexString(magic)}"); @@ -229,7 +235,7 @@ namespace AssetStudio break; } } - if (reader.FileType == FileType.BundleFile && game.Type.IsBlockFile() || reader.FileType == FileType.ENCRFile || reader.FileType == FileType.BlbFile) + if (reader.FileType == FileType.BundleFile && game.Type.IsBlockFile() || reader.FileType == FileType.ENCRFile || reader.FileType == FileType.Blb2File || reader.FileType == FileType.Blb3File) { Logger.Verbose("File might have multiple bundles !!"); try diff --git a/AssetStudio/FileType.cs b/AssetStudio/FileType.cs index 5fd3540..6e55366 100644 --- a/AssetStudio/FileType.cs +++ b/AssetStudio/FileType.cs @@ -17,7 +17,8 @@ namespace AssetStudio ZipFile, BlkFile, MhyFile, - BlbFile, + Blb2File, + Blb3File, ENCRFile, BlockFile } diff --git a/AssetStudio/OffsetStream.cs b/AssetStudio/OffsetStream.cs index 0205bfd..9ba4f4a 100644 --- a/AssetStudio/OffsetStream.cs +++ b/AssetStudio/OffsetStream.cs @@ -8,7 +8,6 @@ namespace AssetStudio { public class OffsetStream : Stream { - private const int BufferSize = 0x10000; private readonly Stream _baseStream; private long _offset; @@ -81,15 +80,35 @@ namespace AssetStudio } else { + using var reader = new FileReader(path, this, true); + var signature = reader.FileType switch + { + FileType.BundleFile => "UnityFS\x00", + FileType.MhyFile => "mhy", + FileType.Blb3File => "Blb\x03", + _ => throw new InvalidOperationException() + }; + + Logger.Verbose($"Parsed signature: {signature}"); + + var signatureBytes = Encoding.UTF8.GetBytes(signature); + var buffer = ArrayPool.Shared.Rent((int)reader.Length); while (Remaining > 0) { - Offset = AbsolutePosition; - yield return AbsolutePosition; - if (Offset == AbsolutePosition) + var index = 0; + var absOffset = AbsolutePosition; + var read = Read(buffer); + while (index < read) { - break; + 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); } } } diff --git a/AssetStudio/OodleHelper.cs b/AssetStudio/OodleHelper.cs new file mode 100644 index 0000000..df0139c --- /dev/null +++ b/AssetStudio/OodleHelper.cs @@ -0,0 +1,25 @@ +using System; +using System.IO; +using System.Runtime.InteropServices; + +namespace AssetStudio; +public static class OodleHelper +{ + [DllImport(@"oo2core_9_win64.dll")] + static extern int OodleLZ_Decompress(ref byte compressedBuffer, int compressedBufferSize, ref byte decompressedBuffer, int decompressedBufferSize, int fuzzSafe, int checkCRC, int verbosity, IntPtr rawBuffer, int rawBufferSize, IntPtr fpCallback, IntPtr callbackUserData, IntPtr decoderMemory, IntPtr decoderMemorySize, int threadPhase); + + public static int Decompress(Span compressed, Span decompressed) + { + int numWrite = -1; + try + { + numWrite = OodleLZ_Decompress(ref compressed[0], compressed.Length, ref decompressed[0], decompressed.Length, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3); + } + catch (Exception) + { + throw new IOException($"Oodle decompression error, write {numWrite} bytes but expected {decompressed.Length} bytes"); + } + + return numWrite; + } +} \ No newline at end of file diff --git a/AssetStudio/UnityPlayer.dll b/AssetStudio/UnityPlayer.dll new file mode 100644 index 0000000..a22898b Binary files /dev/null and b/AssetStudio/UnityPlayer.dll differ diff --git a/AssetStudio/oo2core_9_win64.dll b/AssetStudio/oo2core_9_win64.dll new file mode 100644 index 0000000..5abd61c Binary files /dev/null and b/AssetStudio/oo2core_9_win64.dll differ