Roblox-File-Format/XmlFormat/XmlRobloxFile.cs
CloneTrooper1019 de8df15d3f Large scale refactor to add class support!
Instance classes are now strongly typed with real property fields that
are derived from the JSON API Dump! This required a lot of reworking
across the board:

- Classes and Enums are auto-generated in the 'Generated' folder now.
This is done using a custom built-in plugin, which can be found in
the Plugins folder of this project.
- Property objects are now tied to .NET's reflection system. Reading
and writing from them will try to redirect into a field of the
Instance they are bound to.
- Property types that were loosely defined now have proper data types
(such as Color3uint8, Content, ProtectedString, SharedString, etc)
- Fixed an error with the CFrame directional vectors.
- The binary PRNT chunk now writes instances in child->parent order.
- Enums are now generated correctly, with up-to-date values.
- INST chunks are now referred to as 'Classes' instead of 'Types'.
- Unary operator added to Vector2 and Vector3.
- CollectionService tags can now be manipulated per-instance using
the Instance.Tags member.
- The Instance.Archivable property now works correctly.
- XML files now save/load metadata correctly.
- Cleaned up the property tokens directory.

I probably missed a few things, but that's a general overview of
everything that changed.
2019-06-30 17:01:19 -05:00

181 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using RobloxFiles.DataTypes;
using RobloxFiles.XmlFormat;
namespace RobloxFiles
{
public class XmlRobloxFile : RobloxFile
{
public readonly XmlDocument XmlDocument = new XmlDocument();
internal Dictionary<string, Instance> Instances = new Dictionary<string, Instance>();
internal HashSet<string> SharedStrings = new HashSet<string>();
private Dictionary<string, string> RawMetadata = new Dictionary<string, string>();
public Dictionary<string, string> Metadata => RawMetadata;
public XmlRobloxFile()
{
Name = "XmlRobloxFile";
ParentLocked = true;
Referent = "null";
}
protected override void ReadFile(byte[] buffer)
{
try
{
string xml = Encoding.UTF8.GetString(buffer);
XmlDocument.LoadXml(xml);
}
catch
{
throw new Exception("XmlRobloxFile: Could not read provided buffer as XML!");
}
XmlNode roblox = XmlDocument.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")
{
Instance item = XmlRobloxFileReader.ReadInstance(child, this);
item.Parent = this;
}
else if (child.Name == "SharedStrings")
{
XmlRobloxFileReader.ReadSharedStrings(child, this);
}
else if (child.Name == "Meta")
{
XmlRobloxFileReader.ReadMetadata(child, this);
}
}
// Query the properties.
var allProps = Instances.Values
.SelectMany(inst => inst.Properties)
.Select(pair => pair.Value);
// Resolve referent properties.
var refProps = allProps.Where(prop => prop.Type == PropertyType.Ref);
foreach (Property refProp in refProps)
{
string refId = refProp.XmlToken;
refProp.XmlToken = "Ref";
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);
refProp.Value = null;
}
}
// Record shared strings.
var sharedProps = allProps.Where(prop => prop.Type == PropertyType.SharedString);
foreach (Property sharedProp in sharedProps)
{
SharedString shared = sharedProp.CastValue<SharedString>();
SharedStrings.Add(shared.MD5_Key);
}
}
else
{
throw new Exception("XmlRobloxFile: No 'roblox' element found!");
}
}
public override void Save(Stream stream)
{
XmlDocument doc = new XmlDocument();
XmlElement roblox = doc.CreateElement("roblox");
roblox.SetAttribute("version", "4");
doc.AppendChild(roblox);
Instances.Clear();
SharedStrings.Clear();
// First, append the metadata
foreach (string key in Metadata.Keys)
{
string value = Metadata[key];
XmlElement meta = doc.CreateElement("Meta");
meta.SetAttribute("name", key);
meta.InnerText = value;
roblox.AppendChild(meta);
}
Instance[] children = GetChildren();
// Record all of the instances.
foreach (Instance inst in children)
XmlRobloxFileWriter.RecordInstances(this, inst);
// Now append them into the document.
foreach (Instance inst in children)
{
if (inst.Archivable)
{
XmlNode instNode = XmlRobloxFileWriter.WriteInstance(inst, doc, this);
roblox.AppendChild(instNode);
}
}
// Append the shared strings.
if (SharedStrings.Count > 0)
{
XmlNode sharedStrings = XmlRobloxFileWriter.WriteSharedStrings(doc, this);
roblox.AppendChild(sharedStrings);
}
// Write the XML file.
using (StringWriter buffer = new StringWriter())
{
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);
stream.SetLength(0);
writer.Write(data);
}
}
}
}
}