Roblox-File-Format/XmlFormat/XmlRobloxFile.cs

168 lines
5.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
namespace RobloxFiles.XmlFormat
{
2019-05-19 04:44:51 +00:00
public class XmlRobloxFile : RobloxFile
{
// Runtime Specific
public readonly XmlDocument Root = new XmlDocument();
2019-05-19 04:44:51 +00:00
internal Dictionary<string, Instance> Instances = new Dictionary<string, Instance>();
internal Dictionary<string, string> SharedStrings = new Dictionary<string, string>();
internal XmlRobloxFile()
{
Name = "XmlRobloxFile";
ParentLocked = true;
}
2019-05-19 04:44:51 +00:00
protected override void ReadFile(byte[] buffer)
{
try
{
string xml = Encoding.UTF8.GetString(buffer);
Root.LoadXml(xml);
}
catch
{
throw new Exception("XmlRobloxFile: Could not read provided buffer as XML!");
}
XmlNode roblox = Root.FirstChild;
if (roblox != null && roblox.Name == "roblox")
{
// Verify the version we are using.
XmlNode version = roblox.Attributes.GetNamedItem("version");
int schemaVersion;
if (version == null || !int.TryParse(version.Value, out schemaVersion))
throw new Exception("XmlRobloxFile: No version number defined!");
else if (schemaVersion < 4)
throw new Exception("XmlRobloxFile: Provided version must be at least 4!");
// Process the instances.
foreach (XmlNode child in roblox.ChildNodes)
{
if (child.Name == "Item")
{
2019-05-19 04:44:51 +00:00
Instance item = XmlRobloxFileReader.ReadInstance(child, this);
item.Parent = this;
}
else if (child.Name == "SharedStrings")
{
2019-05-19 04:44:51 +00:00
XmlRobloxFileReader.ReadSharedStrings(child, this);
}
}
// Query the properties.
2019-05-19 04:44:51 +00:00
var allProps = Instances.Values
.SelectMany(inst => inst.Properties)
.Select(pair => pair.Value);
// Resolve referent properties.
2019-05-19 04:44:51 +00:00
var refProps = allProps.Where(prop => prop.Type == PropertyType.Ref);
foreach (Property refProp in refProps)
{
string refId = refProp.Value as string;
if (Instances.ContainsKey(refId))
{
Instance refInst = Instances[refId];
refProp.Value = refInst;
}
else if (refId != "null")
{
string name = refProp.GetFullName();
Console.WriteLine("XmlRobloxFile: Could not resolve reference for {0}", name);
2019-05-25 23:45:54 +00:00
refProp.Value = null;
}
}
// Resolve shared strings.
2019-05-19 04:44:51 +00:00
var sharedProps = allProps.Where(prop => prop.Type == PropertyType.SharedString);
foreach (Property sharedProp in sharedProps)
{
string md5 = sharedProp.Value as string;
if (SharedStrings.ContainsKey(md5))
{
string value = SharedStrings[md5];
sharedProp.Value = value;
byte[] data = Convert.FromBase64String(value);
2019-05-19 04:44:51 +00:00
sharedProp.RawBuffer = data;
}
else
{
string name = sharedProp.GetFullName();
Console.WriteLine("XmlRobloxFile: Could not resolve shared string for {0}", name);
}
}
}
else
{
throw new Exception("XmlRobloxFile: No 'roblox' element found!");
}
}
2019-05-19 04:44:51 +00:00
public override void Save(Stream stream)
{
XmlDocument doc = new XmlDocument();
2019-05-19 04:44:51 +00:00
XmlElement roblox = doc.CreateElement("roblox");
roblox.SetAttribute("version", "4");
doc.AppendChild(roblox);
Instances.Clear();
SharedStrings.Clear();
2019-05-19 04:44:51 +00:00
Instance[] children = GetChildren();
// First, record all of the instances.
2019-05-19 04:44:51 +00:00
foreach (Instance inst in children)
XmlRobloxFileWriter.RecordInstances(this, inst);
// Now append them into the document.
2019-05-19 04:44:51 +00:00
foreach (Instance inst in children)
{
2019-05-19 04:44:51 +00:00
XmlNode instNode = XmlRobloxFileWriter.WriteInstance(inst, doc, this);
roblox.AppendChild(instNode);
}
// Append the shared strings.
if (SharedStrings.Count > 0)
{
2019-05-19 04:44:51 +00:00
XmlNode sharedStrings = XmlRobloxFileWriter.WriteSharedStrings(doc, this);
roblox.AppendChild(sharedStrings);
}
// Write the XML file.
using (StringWriter buffer = new StringWriter())
{
2019-05-19 04:44:51 +00:00
XmlWriterSettings settings = XmlRobloxFileWriter.Settings;
using (XmlWriter xmlWriter = XmlWriter.Create(buffer, settings))
doc.WriteContentTo(xmlWriter);
string result = buffer.ToString()
.Replace("<![CDATA[]]>", "");
using (BinaryWriter writer = new BinaryWriter(stream))
{
byte[] data = Encoding.UTF8.GetBytes(result);
2019-05-25 23:45:54 +00:00
stream.SetLength(0);
writer.Write(data);
}
}
}
}
}