Cleaning up some things.

This commit is contained in:
CloneTrooper1019
2019-05-18 23:44:51 -05:00
parent 34642f5656
commit 9c3a673d95
47 changed files with 303 additions and 351 deletions

View File

@ -6,46 +6,41 @@ using LZ4;
namespace RobloxFiles.BinaryFormat
{
/// <summary>
/// BinaryRobloxChunk represents a generic LZ4-compressed chunk
/// BinaryRobloxFileChunk represents a generic LZ4-compressed chunk
/// of data in Roblox's Binary File Format.
/// </summary>
public class BinaryRobloxChunk
public class BinaryRobloxFileChunk
{
public readonly string ChunkType;
public readonly byte[] Reserved;
public readonly int CompressedSize;
public readonly int Size;
public readonly byte[] Reserved;
public readonly byte[] CompressedData;
public readonly byte[] Data;
public bool HasCompressedData => (CompressedSize > 0);
public BinaryRobloxFileReader GetDataReader()
{
MemoryStream buffer = new MemoryStream(Data);
return new BinaryRobloxFileReader(buffer);
}
public override string ToString()
{
return ChunkType + " Chunk [" + Size + " bytes]";
}
public BinaryRobloxReader GetReader(string chunkType)
{
if (ChunkType == chunkType)
{
MemoryStream buffer = new MemoryStream(Data);
return new BinaryRobloxReader(buffer);
}
throw new Exception("Expected " + chunkType + " ChunkType from the input RobloxBinaryChunk");
}
public BinaryRobloxChunk(BinaryRobloxReader reader)
public BinaryRobloxFileChunk(BinaryRobloxFileReader reader)
{
byte[] bChunkType = reader.ReadBytes(4);
ChunkType = Encoding.ASCII.GetString(bChunkType);
CompressedSize = reader.ReadInt32();
Size = reader.ReadInt32();
Reserved = reader.ReadBytes(4);
if (HasCompressedData)

View File

@ -5,9 +5,9 @@ using System.Text;
namespace RobloxFiles.BinaryFormat
{
public class BinaryRobloxReader : BinaryReader
public class BinaryRobloxFileReader : BinaryReader
{
public BinaryRobloxReader(Stream stream) : base(stream) { }
public BinaryRobloxFileReader(Stream stream) : base(stream) { }
private byte[] lastStringBuffer = new byte[0] { };
// Reads 'count * sizeof(T)' interleaved bytes and converts

View File

@ -7,7 +7,7 @@ using RobloxFiles.BinaryFormat.Chunks;
namespace RobloxFiles.BinaryFormat
{
public class BinaryRobloxFile : IRobloxFile
public class BinaryRobloxFile : RobloxFile
{
// Header Specific
public const string MagicHeader = "<roblox!\x89\xff\x0d\x0a\x1a\x0a";
@ -17,12 +17,8 @@ namespace RobloxFiles.BinaryFormat
public uint NumInstances;
public byte[] Reserved;
// IRobloxFile
internal readonly Instance BinContents = new Instance("Folder", "BinaryRobloxFile");
public Instance Contents => BinContents;
// Runtime Specific
public List<BinaryRobloxChunk> Chunks = new List<BinaryRobloxChunk>();
public List<BinaryRobloxFileChunk> Chunks = new List<BinaryRobloxFileChunk>();
public override string ToString() => GetType().Name;
public Instance[] Instances;
@ -30,11 +26,17 @@ namespace RobloxFiles.BinaryFormat
public Dictionary<string, string> Metadata;
public Dictionary<uint, string> SharedStrings;
internal BinaryRobloxFile()
{
Name = "BinaryRobloxFile";
ParentLocked = true;
}
public void ReadFile(byte[] contents)
protected override void ReadFile(byte[] contents)
{
using (MemoryStream file = new MemoryStream(contents))
using (BinaryRobloxReader reader = new BinaryRobloxReader(file))
using (BinaryRobloxFileReader reader = new BinaryRobloxFileReader(file))
{
// Verify the signature of the file.
byte[] binSignature = reader.ReadBytes(14);
@ -59,7 +61,7 @@ namespace RobloxFiles.BinaryFormat
{
try
{
BinaryRobloxChunk chunk = new BinaryRobloxChunk(reader);
BinaryRobloxFileChunk chunk = new BinaryRobloxFileChunk(reader);
Chunks.Add(chunk);
switch (chunk.ChunkType)
@ -88,7 +90,7 @@ namespace RobloxFiles.BinaryFormat
reading = false;
break;
default:
Console.WriteLine("Unhandled chunk type: {0}!", chunk.ChunkType);
Console.WriteLine("BinaryRobloxFile: Unhandled chunk type: {0}!", chunk.ChunkType);
Chunks.Remove(chunk);
break;
}
@ -101,7 +103,7 @@ namespace RobloxFiles.BinaryFormat
}
}
public void WriteFile(Stream stream)
public override void Save(Stream stream)
{
throw new NotImplementedException("Not implemented yet!");
}

View File

@ -13,9 +13,9 @@
return TypeName;
}
public INST(BinaryRobloxChunk chunk)
public INST(BinaryRobloxFileChunk chunk)
{
using (BinaryRobloxReader reader = chunk.GetReader("INST"))
using (BinaryRobloxFileReader reader = chunk.GetDataReader())
{
TypeIndex = reader.ReadInt32();
TypeName = reader.ReadString();
@ -30,7 +30,7 @@
{
foreach (int instId in InstanceIds)
{
Instance inst = new Instance(TypeName);
Instance inst = new Instance() { ClassName = TypeName };
file.Instances[instId] = inst;
}

View File

@ -7,9 +7,9 @@ namespace RobloxFiles.BinaryFormat.Chunks
public int NumEntries;
public Dictionary<string, string> Data = new Dictionary<string, string>();
public META(BinaryRobloxChunk chunk)
public META(BinaryRobloxFileChunk chunk)
{
using (BinaryRobloxReader reader = chunk.GetReader("META"))
using (BinaryRobloxFileReader reader = chunk.GetDataReader())
{
NumEntries = reader.ReadInt32();

View File

@ -8,9 +8,9 @@
public readonly int[] ChildrenIds;
public readonly int[] ParentIds;
public PRNT(BinaryRobloxChunk chunk)
public PRNT(BinaryRobloxFileChunk chunk)
{
using (BinaryRobloxReader reader = chunk.GetReader("PRNT"))
using (BinaryRobloxFileReader reader = chunk.GetDataReader())
{
Format = reader.ReadByte();
NumRelations = reader.ReadInt32();
@ -28,14 +28,7 @@
int parentId = ParentIds[i];
Instance child = file.Instances[childId];
Instance parent = null;
if (parentId >= 0)
parent = file.Instances[parentId];
else
parent = file.BinContents;
child.Parent = parent;
child.Parent = (parentId >= 0 ? file.Instances[parentId] : file);
}
}
}

View File

@ -14,11 +14,11 @@ namespace RobloxFiles.BinaryFormat.Chunks
public readonly int TypeIndex;
public readonly PropertyType Type;
private BinaryRobloxReader Reader;
private BinaryRobloxFileReader Reader;
public PROP(BinaryRobloxChunk chunk)
public PROP(BinaryRobloxFileChunk chunk)
{
Reader = chunk.GetReader("PROP");
Reader = chunk.GetDataReader();
TypeIndex = Reader.ReadInt32();
Name = Reader.ReadString();
@ -78,7 +78,7 @@ namespace RobloxFiles.BinaryFormat.Chunks
// Leave an access point for the original byte sequence, in case this is a BinaryString.
// This will allow the developer to read the sequence without any mangling from C# strings.
byte[] buffer = Reader.GetLastStringBuffer();
props[i].SetRawBuffer(buffer);
props[i].RawBuffer = buffer;
return result;
});

View File

@ -11,9 +11,9 @@ namespace RobloxFiles.BinaryFormat.Chunks
public Dictionary<string, uint> Lookup = new Dictionary<string, uint>();
public Dictionary<uint, string> Strings = new Dictionary<uint, string>();
public SSTR(BinaryRobloxChunk chunk)
public SSTR(BinaryRobloxFileChunk chunk)
{
using (BinaryRobloxReader reader = chunk.GetReader("SSTR"))
using (BinaryRobloxFileReader reader = chunk.GetDataReader())
{
Version = reader.ReadInt32();
NumHashes = reader.ReadInt32();