Fixed some binary file save problems.

This commit is contained in:
CloneTrooper1019 2019-06-10 20:27:57 -05:00
parent 5b85043194
commit 41c84dc49c
4 changed files with 72 additions and 48 deletions

View File

@ -32,7 +32,7 @@ namespace RobloxFiles.BinaryFormat
public Dictionary<string, string> Metadata => META?.Data;
public bool HasSharedStrings => (SSTR != null);
public Dictionary<uint, string> SharedStrings => SSTR?.Strings;
public IReadOnlyDictionary<uint, string> SharedStrings => SSTR?.Strings;
internal BinaryRobloxFile()
{
@ -130,20 +130,7 @@ namespace RobloxFiles.BinaryFormat
NumInstances = 0;
NumTypes = 0;
if (HasSharedStrings)
{
SSTR.NumHashes = 0;
SSTR.Lookup.Clear();
SSTR.Strings.Clear();
}
// Write the META chunk.
if (HasMetadata)
{
var metaChunk = META.SaveAsChunk(writer);
Chunks.Add(metaChunk);
}
SSTR = null;
// Record all instances and types.
writer.RecordInstances(Children);
@ -185,6 +172,13 @@ namespace RobloxFiles.BinaryFormat
Chunks.Insert(0, sharedStrings);
}
// Write the META chunk.
if (HasMetadata)
{
var metaChunk = META.SaveAsChunk(writer);
Chunks.Insert(0, metaChunk);
}
// Write the END_ chunk.
writer.StartWritingChunk("END\0");
writer.WriteString("</roblox>", true);

View File

@ -510,9 +510,16 @@ namespace RobloxFiles.BinaryFormat.Chunks
case PropertyType.String:
props.ForEach(prop =>
{
byte[] rawBuffer = prop.RawBuffer;
writer.Write(rawBuffer.Length);
writer.Write(rawBuffer);
byte[] buffer = prop.HasRawBuffer ? prop.RawBuffer : null;
if (buffer == null)
{
string value = prop.CastValue<string>();
buffer = Encoding.UTF8.GetBytes(value);
}
writer.Write(buffer.Length);
writer.Write(buffer);
});
break;

View File

@ -45,13 +45,15 @@ namespace RobloxFiles.BinaryFormat.Chunks
foreach (var pair in Lookup)
{
string key = pair.Key;
byte[] md5 = Convert.FromBase64String(key);
uint id = pair.Value;
string value = Strings[id];
writer.Write(md5);
writer.WriteString(value);
string value = Strings[pair.Value];
byte[] buffer = Convert.FromBase64String(value);
writer.Write(buffer.Length);
writer.Write(buffer);
}
return writer.FinishWritingChunk();

View File

@ -44,20 +44,15 @@ namespace RobloxFiles
public Instance Instance { get; internal set; }
public PropertyType Type;
public object Value;
public string XmlToken = "";
public byte[] RawBuffer { get; internal set; }
public byte[] RawBuffer;
internal BinaryRobloxFileWriter CurrentWriter;
internal object RawValue;
public bool HasRawBuffer
private void ImproviseRawBuffer()
{
get
{
if (RawBuffer == null && Value != null)
{
// Improvise what the buffer should be if this is a primitive.
switch (Type)
{
case PropertyType.Int:
@ -78,9 +73,35 @@ namespace RobloxFiles
case PropertyType.SharedString:
RawBuffer = Convert.FromBase64String((string)Value);
break;
//
}
}
public object Value
{
get
{
return RawValue;
}
set
{
RawValue = value;
RawBuffer = null;
ImproviseRawBuffer();
}
}
public bool HasRawBuffer
{
get
{
if (RawBuffer == null && Value != null)
{
// Improvise what the buffer should be if this is a primitive.
ImproviseRawBuffer();
}
return (RawBuffer != null);
}
}
@ -138,8 +159,8 @@ namespace RobloxFiles
throw new Exception("Property.CurrentWriter must be set to use WriteValue<T>");
T value = CastValue<T>();
byte[] bytes = BinaryRobloxFileWriter.GetBytes(value);
CurrentWriter.Write(bytes);
}
}