Roblox-File-Format/BinaryFormat/BinaryFileChunk.cs

58 lines
1.5 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Text;
using LZ4;
namespace RobloxFiles.BinaryFormat
{
/// <summary>
2019-05-19 04:44:51 +00:00
/// BinaryRobloxFileChunk represents a generic LZ4-compressed chunk
/// of data in Roblox's Binary File Format.
/// </summary>
2019-05-19 04:44:51 +00:00
public class BinaryRobloxFileChunk
{
public readonly string ChunkType;
2019-05-19 04:44:51 +00:00
public readonly byte[] Reserved;
public readonly int CompressedSize;
public readonly int Size;
public readonly byte[] CompressedData;
public readonly byte[] Data;
public bool HasCompressedData => (CompressedSize > 0);
2019-05-19 04:44:51 +00:00
public BinaryRobloxFileReader GetDataReader()
{
2019-05-19 04:44:51 +00:00
MemoryStream buffer = new MemoryStream(Data);
return new BinaryRobloxFileReader(buffer);
}
2019-05-19 04:44:51 +00:00
public override string ToString()
{
2019-05-19 04:44:51 +00:00
return ChunkType + " Chunk [" + Size + " bytes]";
}
2019-05-19 04:44:51 +00:00
public BinaryRobloxFileChunk(BinaryRobloxFileReader reader)
{
byte[] bChunkType = reader.ReadBytes(4);
ChunkType = Encoding.ASCII.GetString(bChunkType);
CompressedSize = reader.ReadInt32();
Size = reader.ReadInt32();
2019-05-19 04:44:51 +00:00
2019-01-31 10:53:28 +00:00
Reserved = reader.ReadBytes(4);
if (HasCompressedData)
{
CompressedData = reader.ReadBytes(CompressedSize);
Data = LZ4Codec.Decode(CompressedData, 0, CompressedSize, Size);
}
else
{
Data = reader.ReadBytes(Size);
}
}
}
}