Optimizations and memory leak fixes

This commit is contained in:
CloneTrooper1019
2020-08-17 00:33:59 -05:00
parent 0ca6738cb9
commit 297426bdb5
20 changed files with 134 additions and 125 deletions

View File

@ -26,12 +26,6 @@ namespace RobloxFiles.BinaryFormat
public bool HasWriteBuffer { get; private set; }
public byte[] WriteBuffer { get; private set; }
public BinaryRobloxFileReader GetDataReader(BinaryRobloxFile file)
{
MemoryStream buffer = new MemoryStream(Data);
return new BinaryRobloxFileReader(file, buffer);
}
public override string ToString()
{
string chunkType = ChunkType.Replace('\0', ' ');
@ -81,7 +75,7 @@ namespace RobloxFiles.BinaryFormat
if (!compress || CompressedSize > Size)
{
CompressedSize = 0;
CompressedData = new byte[0];
CompressedData = Array.Empty<byte>();
}
ChunkType = writer.ChunkType;

View File

@ -28,9 +28,9 @@ namespace RobloxFiles
public Instance[] Instances { get; internal set; }
public INST[] Classes { get; internal set; }
internal META META = null;
internal SSTR SSTR = null;
internal SIGN SIGN = null;
internal META META;
internal SSTR SSTR;
internal SIGN SIGN;
public bool HasMetadata => (META != null);
public Dictionary<string, string> Metadata => META?.Data;
@ -104,18 +104,22 @@ namespace RobloxFiles
reading = false;
break;
case string unhandled:
Console.WriteLine("BinaryRobloxFile - Unhandled chunk-type: {0}!", unhandled);
Console.Error.WriteLine("BinaryRobloxFile - Unhandled chunk-type: {0}!", unhandled);
break;
default: break;
}
if (handler != null)
{
chunk.Handler = handler;
using (var dataReader = chunk.GetDataReader(this))
handler.Load(dataReader);
using (var readBuffer = new MemoryStream(chunk.Data))
{
using (var dataReader = new BinaryRobloxFileReader(this, readBuffer))
{
chunk.Handler = handler;
handler.Load(dataReader);
}
}
ChunksImpl.Add(chunk);
}
}
@ -133,16 +137,17 @@ namespace RobloxFiles
// Generate the chunk data.
//////////////////////////////////////////////////////////////////////////
using (var writer = new BinaryRobloxFileWriter(this))
using (var workBuffer = new MemoryStream())
using (var writer = new BinaryRobloxFileWriter(this, workBuffer))
{
// Clear the existing data.
Referent = "-1";
ChunksImpl.Clear();
NumInstances = 0;
NumClasses = 0;
SSTR = null;
// Recursively capture all instances and classes.
writer.RecordInstances(Children);
@ -168,7 +173,7 @@ namespace RobloxFiles
// Write the PRNT chunk.
var parents = new PRNT();
writer.SaveChunk(parents);
// Write the SSTR chunk.
if (HasSharedStrings)
writer.SaveChunk(SSTR, 0);

View File

@ -10,7 +10,7 @@ namespace RobloxFiles.BinaryFormat
public class BinaryRobloxFileReader : BinaryReader
{
public readonly BinaryRobloxFile File;
private byte[] lastStringBuffer = new byte[0] { };
private byte[] lastStringBuffer = Array.Empty<byte>();
public BinaryRobloxFileReader(BinaryRobloxFile file, Stream stream) : base(stream)
{

View File

@ -28,7 +28,7 @@ namespace RobloxFiles.BinaryFormat
// Instances in child->parent order
internal List<Instance> PostInstances { get; private set; }
public BinaryRobloxFileWriter(BinaryRobloxFile file, Stream workBuffer = null) : base(workBuffer ?? new MemoryStream())
public BinaryRobloxFileWriter(BinaryRobloxFile file, Stream workBuffer) : base(workBuffer)
{
File = file;
@ -167,14 +167,13 @@ namespace RobloxFiles.BinaryFormat
if (!instance.Archivable)
continue;
int instId = (int)(File.NumInstances++);
instance.Referent = instId.ToString();
int instId = (int)File.NumInstances++;
string className = instance.ClassName;
instance.Referent = instId.ToInvariantString();
Instances.Add(instance);
string className = instance.ClassName;
INST inst;
if (!ClassMap.TryGetValue(className, out inst))
if (!ClassMap.TryGetValue(className, out INST inst))
{
inst = new INST()
{