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

View File

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

View File

@ -45,13 +45,15 @@ namespace RobloxFiles.BinaryFormat.Chunks
foreach (var pair in Lookup) foreach (var pair in Lookup)
{ {
string key = pair.Key; string key = pair.Key;
byte[] md5 = Convert.FromBase64String(key); byte[] md5 = Convert.FromBase64String(key);
uint id = pair.Value;
string value = Strings[id];
writer.Write(md5); 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(); return writer.FinishWritingChunk();

View File

@ -44,20 +44,15 @@ namespace RobloxFiles
public Instance Instance { get; internal set; } public Instance Instance { get; internal set; }
public PropertyType Type; public PropertyType Type;
public object Value;
public string XmlToken = ""; public string XmlToken = "";
public byte[] RawBuffer { get; internal set; } public byte[] RawBuffer;
internal BinaryRobloxFileWriter CurrentWriter; 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) switch (Type)
{ {
case PropertyType.Int: case PropertyType.Int:
@ -78,9 +73,35 @@ namespace RobloxFiles
case PropertyType.SharedString: case PropertyType.SharedString:
RawBuffer = Convert.FromBase64String((string)Value); RawBuffer = Convert.FromBase64String((string)Value);
break; 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); return (RawBuffer != null);
} }
} }
@ -138,8 +159,8 @@ namespace RobloxFiles
throw new Exception("Property.CurrentWriter must be set to use WriteValue<T>"); throw new Exception("Property.CurrentWriter must be set to use WriteValue<T>");
T value = CastValue<T>(); T value = CastValue<T>();
byte[] bytes = BinaryRobloxFileWriter.GetBytes(value); byte[] bytes = BinaryRobloxFileWriter.GetBytes(value);
CurrentWriter.Write(bytes); CurrentWriter.Write(bytes);
} }
} }