307 lines
16 KiB
C#
307 lines
16 KiB
C#
using System;
|
|
using System.Buffers;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
namespace AssetStudio
|
|
{
|
|
public class Blb3File
|
|
{
|
|
private List<BundleFile.StorageBlock> m_BlocksInfo;
|
|
private List<BundleFile.Node> m_DirectoryInfo;
|
|
private byte[] Header;
|
|
|
|
private static string ModuleName = "UnityPlayer.dll";
|
|
private static IntPtr Module = IntPtr.Zero;
|
|
|
|
public BundleFile.Header m_Header;
|
|
public List<StreamFile> fileList;
|
|
public long Offset;
|
|
|
|
|
|
[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<byte> 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, 0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 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;
|
|
|
|
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\x03")
|
|
throw new Exception("not a Blb3 file");
|
|
|
|
var size = reader.ReadUInt32();
|
|
m_Header = new BundleFile.Header
|
|
{
|
|
version = 6,
|
|
unityVersion = "5.x.x",
|
|
unityRevision = "2017.4.30f1",
|
|
flags = 0
|
|
};
|
|
m_Header.compressedBlocksInfoSize = size;
|
|
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)
|
|
{
|
|
using var stream = new MemoryStream(header);
|
|
using var reader = new EndianBinaryReader(stream, EndianType.LittleEndian);
|
|
|
|
m_Header.size = reader.ReadUInt32();
|
|
var lastUncompressedSize = reader.ReadUInt32();
|
|
|
|
reader.Position += 4;
|
|
var blobOffset = reader.ReadInt32();
|
|
var blobSize = reader.ReadUInt32();
|
|
var compressionType = (CompressionType)reader.ReadByte();
|
|
var uncompressedSize = (uint)1 << reader.ReadByte();
|
|
reader.AlignStream();
|
|
|
|
var blocksInfoCount = reader.ReadInt32();
|
|
var nodesCount = reader.ReadInt32();
|
|
|
|
var blocksInfoOffset = reader.Position + reader.ReadInt64();
|
|
var nodesInfoOffset = reader.Position + reader.ReadInt64();
|
|
var flagInfoOffset = reader.Position + reader.ReadInt64();
|
|
|
|
reader.Position = blocksInfoOffset;
|
|
m_BlocksInfo = new List<BundleFile.StorageBlock>();
|
|
Logger.Verbose($"Blocks count: {blocksInfoCount}");
|
|
for (int i = 0; i < blocksInfoCount; i++)
|
|
{
|
|
m_BlocksInfo.Add(new BundleFile.StorageBlock
|
|
{
|
|
compressedSize = reader.ReadUInt32(),
|
|
uncompressedSize = i == blocksInfoCount - 1 ? lastUncompressedSize : uncompressedSize,
|
|
flags = (StorageBlockFlags)compressionType
|
|
});
|
|
|
|
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<BundleFile.Node>();
|
|
Logger.Verbose($"Directory count: {nodesCount}");
|
|
for (int i = 0; i < nodesCount; i++)
|
|
{
|
|
m_DirectoryInfo.Add(new BundleFile.Node
|
|
{
|
|
offset = reader.ReadInt32(),
|
|
size = reader.ReadInt32()
|
|
});
|
|
|
|
var pos = reader.Position;
|
|
reader.Position = flagInfoOffset;
|
|
var flag = reader.ReadUInt32();
|
|
if (i >= 0x20)
|
|
{
|
|
flag = reader.ReadUInt32();
|
|
}
|
|
m_DirectoryInfo[i].flags = (uint)(flag & (1 << i)) * 4;
|
|
reader.Position = pos;
|
|
|
|
var pathOffset = reader.Position + reader.ReadInt64();
|
|
|
|
pos = reader.Position;
|
|
reader.Position = pathOffset;
|
|
m_DirectoryInfo[i].path = reader.ReadStringToNull();
|
|
reader.Position = pos;
|
|
|
|
Logger.Verbose($"Directory {i} Info: {m_DirectoryInfo[i]}");
|
|
}
|
|
}
|
|
|
|
private Stream CreateBlocksStream(string path)
|
|
{
|
|
Stream blocksStream;
|
|
var uncompressedSizeSum = (int)m_BlocksInfo.Sum(x => x.uncompressedSize);
|
|
Logger.Verbose($"Total size of decompressed blocks: 0x{uncompressedSizeSum:X8}");
|
|
if (uncompressedSizeSum >= int.MaxValue)
|
|
blocksStream = new FileStream(path + ".temp", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.DeleteOnClose);
|
|
else
|
|
blocksStream = new MemoryStream(uncompressedSizeSum);
|
|
return blocksStream;
|
|
}
|
|
|
|
private void ReadBlocks(FileReader reader, Stream blocksStream)
|
|
{
|
|
foreach (var blockInfo in m_BlocksInfo)
|
|
{
|
|
var compressionType = (CompressionType)(blockInfo.flags & StorageBlockFlags.CompressionTypeMask);
|
|
Logger.Verbose($"Block compression type {compressionType}");
|
|
switch (compressionType) //kStorageBlockCompressionTypeMask
|
|
{
|
|
case CompressionType.None: //None
|
|
{
|
|
reader.BaseStream.CopyTo(blocksStream, blockInfo.compressedSize);
|
|
break;
|
|
}
|
|
case CompressionType.Oodle: //Oodle
|
|
{
|
|
{
|
|
var compressedSize = (int)blockInfo.compressedSize;
|
|
var uncompressedSize = (int)blockInfo.uncompressedSize;
|
|
|
|
var compressedBytes = ArrayPool<byte>.Shared.Rent(compressedSize);
|
|
var uncompressedBytes = ArrayPool<byte>.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<byte>.Shared.Return(compressedBytes, true);
|
|
ArrayPool<byte>.Shared.Return(uncompressedBytes, true);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
case CompressionType.Lzma: //LZMA
|
|
{
|
|
SevenZipHelper.StreamDecompress(reader.BaseStream, blocksStream, blockInfo.compressedSize, blockInfo.uncompressedSize);
|
|
break;
|
|
}
|
|
case CompressionType.Lz4: //LZ4
|
|
case CompressionType.Lz4HC: //LZ4HC
|
|
{
|
|
var compressedSize = (int)blockInfo.compressedSize;
|
|
var uncompressedSize = (int)blockInfo.uncompressedSize;
|
|
|
|
var compressedBytes = ArrayPool<byte>.Shared.Rent(compressedSize);
|
|
var uncompressedBytes = ArrayPool<byte>.Shared.Rent(uncompressedSize);
|
|
|
|
var compressedBytesSpan = compressedBytes.AsSpan(0, compressedSize);
|
|
var uncompressedBytesSpan = uncompressedBytes.AsSpan(0, uncompressedSize);
|
|
|
|
try
|
|
{
|
|
|
|
reader.Read(compressedBytesSpan);
|
|
Decrypt(Header, compressedBytesSpan);
|
|
var numWrite = LZ4.Instance.Decompress(compressedBytesSpan, uncompressedBytesSpan);
|
|
if (numWrite != uncompressedSize)
|
|
{
|
|
Logger.Warning($"Lz4 decompression error, write {numWrite} bytes but expected {uncompressedSize} bytes");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logger.Error($"Lz4 decompression error {e.Message}");
|
|
}
|
|
finally
|
|
{
|
|
blocksStream.Write(uncompressedBytesSpan);
|
|
ArrayPool<byte>.Shared.Return(compressedBytes, true);
|
|
ArrayPool<byte>.Shared.Return(uncompressedBytes, true);
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
throw new IOException($"Unsupported compression type {compressionType}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ReadFiles(Stream blocksStream, string path)
|
|
{
|
|
Logger.Verbose($"Writing files from blocks stream...");
|
|
|
|
fileList = new List<StreamFile>();
|
|
for (int i = 0; i < m_DirectoryInfo.Count; i++)
|
|
{
|
|
var node = m_DirectoryInfo[i];
|
|
var file = new StreamFile();
|
|
fileList.Add(file);
|
|
file.path = node.path;
|
|
file.fileName = Path.GetFileName(node.path);
|
|
if (node.size >= int.MaxValue)
|
|
{
|
|
var extractPath = path + "_unpacked" + Path.DirectorySeparatorChar;
|
|
Directory.CreateDirectory(extractPath);
|
|
file.stream = new FileStream(extractPath + file.fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
|
|
}
|
|
else
|
|
file.stream = new MemoryStream((int)node.size);
|
|
blocksStream.Position = node.offset;
|
|
blocksStream.CopyTo(file.stream, node.size);
|
|
file.stream.Position = 0;
|
|
}
|
|
}
|
|
}
|
|
} |