AssetStudio/AssetStudio/BundleFile.cs

307 lines
12 KiB
C#
Raw Normal View History

2020-04-07 08:13:04 +00:00
using System;
using System.Collections.Generic;
2015-10-30 02:41:37 +00:00
using System.IO;
2018-07-14 15:46:32 +00:00
using System.Linq;
2015-10-30 02:41:37 +00:00
using Lz4;
2018-04-02 22:51:22 +00:00
namespace AssetStudio
2015-10-30 02:41:37 +00:00
{
2020-04-07 08:13:04 +00:00
public class BundleFile
2018-03-04 18:35:53 +00:00
{
2020-04-07 08:13:04 +00:00
public class Header
{
public string signature;
public uint version;
public string unityVersion;
public string unityRevision;
public long size;
public uint compressedBlocksInfoSize;
public uint uncompressedBlocksInfoSize;
public uint flags;
}
2018-07-14 15:46:32 +00:00
2020-04-07 08:13:04 +00:00
public class StorageBlock
{
public uint compressedSize;
public uint uncompressedSize;
public ushort flags;
}
2018-03-04 18:35:53 +00:00
2020-04-07 08:13:04 +00:00
public class Node
{
public long offset;
public long size;
public uint flags;
public string path;
}
2015-10-30 02:41:37 +00:00
2020-04-07 08:13:04 +00:00
public Header m_Header;
private StorageBlock[] m_BlocksInfo;
private Node[] m_DirectoryInfo;
public StreamFile[] fileList;
public BundleFile(EndianBinaryReader reader, string path)
{
2020-04-07 08:13:04 +00:00
m_Header = new Header();
m_Header.signature = reader.ReadStringToNull();
switch (m_Header.signature)
2015-10-30 02:41:37 +00:00
{
2020-04-07 08:13:04 +00:00
case "UnityArchive":
break; //TODO
2017-11-09 19:36:08 +00:00
case "UnityWeb":
case "UnityRaw":
2020-04-07 08:13:04 +00:00
ReadHeaderAndBlocksInfo(reader);
using (var blocksStream = CreateBlocksStream(path))
2017-11-09 19:36:08 +00:00
{
2020-04-07 08:13:04 +00:00
ReadBlocksAndDirectory(reader, blocksStream);
ReadFiles(blocksStream, path);
}
break;
case "UnityFS":
ReadHeader(reader);
ReadBlocksInfoAndDirectory(reader);
using (var blocksStream = CreateBlocksStream(path))
{
ReadBlocks(reader, blocksStream);
ReadFiles(blocksStream, path);
}
break;
}
}
2015-10-30 02:41:37 +00:00
2020-04-07 08:13:04 +00:00
private void ReadHeaderAndBlocksInfo(EndianBinaryReader reader)
{
var isCompressed = m_Header.signature == "UnityWeb";
m_Header.version = reader.ReadUInt32();
m_Header.unityVersion = reader.ReadStringToNull();
m_Header.unityRevision = reader.ReadStringToNull();
if (m_Header.version >= 4)
{
var hash = reader.ReadBytes(16);
var crc = reader.ReadUInt32();
}
var minimumStreamedBytes = reader.ReadUInt32();
var headerSize = reader.ReadUInt32();
var numberOfLevelsToDownloadBeforeStreaming = reader.ReadUInt32();
var levelCount = reader.ReadInt32();
2020-04-10 10:20:45 +00:00
m_BlocksInfo = new StorageBlock[1];
2020-04-07 08:13:04 +00:00
for (int i = 0; i < levelCount; i++)
{
2020-04-10 10:20:45 +00:00
var storageBlock = new StorageBlock()
2020-04-07 08:13:04 +00:00
{
compressedSize = reader.ReadUInt32(),
uncompressedSize = reader.ReadUInt32(),
flags = (ushort)(isCompressed ? 1 : 0)
};
2020-04-10 10:20:45 +00:00
if (i == levelCount - 1)
{
m_BlocksInfo[0] = storageBlock;
}
2020-04-07 08:13:04 +00:00
}
if (m_Header.version >= 2)
{
var completeFileSize = reader.ReadUInt32();
}
if (m_Header.version >= 3)
{
var fileInfoHeaderSize = reader.ReadUInt32();
}
reader.Position = headerSize;
}
2015-10-30 02:41:37 +00:00
2020-04-07 08:13:04 +00:00
private Stream CreateBlocksStream(string path)
{
Stream blocksStream;
var uncompressedSizeSum = m_BlocksInfo.Sum(x => x.uncompressedSize);
if (uncompressedSizeSum >= int.MaxValue)
{
/*var memoryMappedFile = MemoryMappedFile.CreateNew(Path.GetFileName(path), uncompressedSizeSum);
assetsDataStream = memoryMappedFile.CreateViewStream();*/
blocksStream = new FileStream(path + ".temp", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.DeleteOnClose);
}
else
{
blocksStream = new MemoryStream((int)uncompressedSizeSum);
}
return blocksStream;
}
2017-11-09 19:36:08 +00:00
2020-04-07 08:13:04 +00:00
private void ReadBlocksAndDirectory(EndianBinaryReader reader, Stream blocksStream)
{
foreach (var blockInfo in m_BlocksInfo)
{
var uncompressedBytes = reader.ReadBytes((int)blockInfo.compressedSize);
if (blockInfo.flags == 1)
{
using (var memoryStream = new MemoryStream(uncompressedBytes))
2017-11-09 19:36:08 +00:00
{
2020-04-07 08:13:04 +00:00
using (var decompressStream = SevenZipHelper.StreamDecompress(memoryStream))
2018-07-14 15:46:32 +00:00
{
2020-04-07 08:13:04 +00:00
uncompressedBytes = decompressStream.ToArray();
2018-07-14 15:46:32 +00:00
}
2017-11-09 19:36:08 +00:00
}
2020-04-07 08:13:04 +00:00
}
blocksStream.Write(uncompressedBytes, 0, uncompressedBytes.Length);
}
blocksStream.Position = 0;
var blocksReader = new EndianBinaryReader(blocksStream);
var nodesCount = blocksReader.ReadInt32();
m_DirectoryInfo = new Node[nodesCount];
for (int i = 0; i < nodesCount; i++)
{
m_DirectoryInfo[i] = new Node
{
path = blocksReader.ReadStringToNull(),
offset = blocksReader.ReadUInt32(),
size = blocksReader.ReadUInt32()
};
2017-02-16 07:30:11 +00:00
}
}
2020-04-07 08:13:04 +00:00
public void ReadFiles(Stream blocksStream, string path)
2017-02-16 07:30:11 +00:00
{
2020-04-07 08:13:04 +00:00
fileList = new StreamFile[m_DirectoryInfo.Length];
for (int i = 0; i < m_DirectoryInfo.Length; i++)
2017-02-16 07:30:11 +00:00
{
2020-04-07 08:13:04 +00:00
var node = m_DirectoryInfo[i];
2018-07-14 15:46:32 +00:00
var file = new StreamFile();
2020-04-07 08:13:04 +00:00
fileList[i] = file;
file.fileName = Path.GetFileName(node.path);
if (node.size >= int.MaxValue)
{
/*var memoryMappedFile = MemoryMappedFile.CreateNew(file.fileName, entryinfo_size);
file.stream = memoryMappedFile.CreateViewStream();*/
var extractPath = path + "_unpacked" + Path.DirectorySeparatorChar;
Directory.CreateDirectory(extractPath);
2020-09-26 15:29:38 +00:00
file.stream = new FileStream(extractPath + file.fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
2020-04-07 08:13:04 +00:00
}
else
{
file.stream = new MemoryStream((int)node.size);
}
blocksStream.Position = node.offset;
blocksStream.CopyTo(file.stream, node.size);
file.stream.Position = 0;
2017-02-16 07:30:11 +00:00
}
}
2020-04-07 08:13:04 +00:00
private void ReadHeader(EndianBinaryReader reader)
{
m_Header.version = reader.ReadUInt32();
m_Header.unityVersion = reader.ReadStringToNull();
m_Header.unityRevision = reader.ReadStringToNull();
m_Header.size = reader.ReadInt64();
m_Header.compressedBlocksInfoSize = reader.ReadUInt32();
m_Header.uncompressedBlocksInfoSize = reader.ReadUInt32();
m_Header.flags = reader.ReadUInt32();
}
private void ReadBlocksInfoAndDirectory(EndianBinaryReader reader)
2017-02-16 07:30:11 +00:00
{
byte[] blocksInfoBytes;
2020-09-28 23:08:14 +00:00
if (m_Header.version >= 7)
{
reader.AlignStream(16);
}
2020-04-07 08:13:04 +00:00
if ((m_Header.flags & 0x80) != 0) //kArchiveBlocksInfoAtTheEnd
2017-02-16 07:30:11 +00:00
{
2020-04-07 08:13:04 +00:00
var position = reader.Position;
reader.Position = reader.BaseStream.Length - m_Header.compressedBlocksInfoSize;
blocksInfoBytes = reader.ReadBytes((int)m_Header.compressedBlocksInfoSize);
reader.Position = position;
2017-02-16 07:30:11 +00:00
}
2020-04-07 08:13:04 +00:00
else //0x40 kArchiveBlocksAndDirectoryInfoCombined
2017-02-16 07:30:11 +00:00
{
2020-04-07 08:13:04 +00:00
blocksInfoBytes = reader.ReadBytes((int)m_Header.compressedBlocksInfoSize);
2017-02-16 07:30:11 +00:00
}
2020-03-25 19:44:07 +00:00
var blocksInfoCompressedStream = new MemoryStream(blocksInfoBytes);
2020-04-07 08:13:04 +00:00
MemoryStream blocksInfoUncompresseddStream;
switch (m_Header.flags & 0x3F) //kArchiveCompressionTypeMask
2017-02-16 07:30:11 +00:00
{
2020-04-07 08:13:04 +00:00
default: //None
{
2020-04-07 08:13:04 +00:00
blocksInfoUncompresseddStream = blocksInfoCompressedStream;
2017-02-16 07:30:11 +00:00
break;
}
2020-04-07 08:13:04 +00:00
case 1: //LZMA
{
2020-04-07 08:13:04 +00:00
blocksInfoUncompresseddStream = SevenZipHelper.StreamDecompress(blocksInfoCompressedStream);
2020-03-25 19:44:07 +00:00
blocksInfoCompressedStream.Close();
2017-02-16 07:30:11 +00:00
break;
}
2020-04-07 08:13:04 +00:00
case 2: //LZ4
case 3: //LZ4HC
2017-02-16 07:30:11 +00:00
{
2020-04-07 08:13:04 +00:00
var uncompressedBytes = new byte[m_Header.uncompressedBlocksInfoSize];
2020-03-25 19:44:07 +00:00
using (var decoder = new Lz4DecoderStream(blocksInfoCompressedStream))
2017-02-16 07:30:11 +00:00
{
2020-04-07 08:13:04 +00:00
decoder.Read(uncompressedBytes, 0, uncompressedBytes.Length);
2017-02-16 07:30:11 +00:00
}
2020-04-07 08:13:04 +00:00
blocksInfoUncompresseddStream = new MemoryStream(uncompressedBytes);
2017-02-16 07:30:11 +00:00
break;
}
}
2020-04-07 08:13:04 +00:00
using (var blocksInfoReader = new EndianBinaryReader(blocksInfoUncompresseddStream))
2017-02-16 07:30:11 +00:00
{
2020-04-07 08:13:04 +00:00
var uncompressedDataHash = blocksInfoReader.ReadBytes(16);
var blocksInfoCount = blocksInfoReader.ReadInt32();
m_BlocksInfo = new StorageBlock[blocksInfoCount];
for (int i = 0; i < blocksInfoCount; i++)
2017-02-16 07:30:11 +00:00
{
2020-04-07 08:13:04 +00:00
m_BlocksInfo[i] = new StorageBlock
2018-07-14 15:46:32 +00:00
{
uncompressedSize = blocksInfoReader.ReadUInt32(),
compressedSize = blocksInfoReader.ReadUInt32(),
2020-04-07 08:13:04 +00:00
flags = blocksInfoReader.ReadUInt16()
2018-07-14 15:46:32 +00:00
};
}
2020-04-07 08:13:04 +00:00
var nodesCount = blocksInfoReader.ReadInt32();
m_DirectoryInfo = new Node[nodesCount];
for (int i = 0; i < nodesCount; i++)
2018-07-14 15:46:32 +00:00
{
2020-04-07 08:13:04 +00:00
m_DirectoryInfo[i] = new Node
{
2020-04-07 08:13:04 +00:00
offset = blocksInfoReader.ReadInt64(),
size = blocksInfoReader.ReadInt64(),
flags = blocksInfoReader.ReadUInt32(),
path = blocksInfoReader.ReadStringToNull(),
};
2017-02-16 07:30:11 +00:00
}
2020-04-07 08:13:04 +00:00
}
}
private void ReadBlocks(EndianBinaryReader reader, Stream blocksStream)
{
foreach (var blockInfo in m_BlocksInfo)
{
switch (blockInfo.flags & 0x3F) //kStorageBlockCompressionTypeMask
2017-02-16 07:30:11 +00:00
{
2020-04-07 08:13:04 +00:00
default: //None
2018-07-14 15:46:32 +00:00
{
2020-04-07 08:13:04 +00:00
reader.BaseStream.CopyTo(blocksStream, blockInfo.compressedSize);
break;
2018-07-14 15:46:32 +00:00
}
2020-04-07 08:13:04 +00:00
case 1: //LZMA
2018-07-14 15:46:32 +00:00
{
2020-04-07 08:13:04 +00:00
SevenZipHelper.StreamDecompress(reader.BaseStream, blocksStream, blockInfo.compressedSize, blockInfo.uncompressedSize);
break;
}
case 2: //LZ4
case 3: //LZ4HC
{
var compressedStream = new MemoryStream(reader.ReadBytes((int)blockInfo.compressedSize));
using (var lz4Stream = new Lz4DecoderStream(compressedStream))
{
lz4Stream.CopyTo(blocksStream, blockInfo.uncompressedSize);
}
break;
2018-07-14 15:46:32 +00:00
}
}
}
2020-04-07 08:13:04 +00:00
blocksStream.Position = 0;
2015-10-30 02:41:37 +00:00
}
}
}