de8df15d3f
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.
162 lines
5.5 KiB
C#
162 lines
5.5 KiB
C#
using System;
|
|
using System.Xml;
|
|
|
|
using RobloxFiles.DataTypes;
|
|
|
|
namespace RobloxFiles.XmlFormat
|
|
{
|
|
public static class XmlRobloxFileReader
|
|
{
|
|
private static Func<string, Exception> createErrorHandler(string label)
|
|
{
|
|
var errorHandler = new Func<string, Exception>((message) =>
|
|
{
|
|
string contents = $"XmlDataReader.{label}: {message}";
|
|
return new Exception(contents);
|
|
});
|
|
|
|
return errorHandler;
|
|
}
|
|
|
|
public static void ReadSharedStrings(XmlNode sharedStrings, XmlRobloxFile file)
|
|
{
|
|
var error = createErrorHandler("ReadSharedStrings");
|
|
|
|
if (sharedStrings.Name != "SharedStrings")
|
|
throw error("Provided XmlNode's class should be 'SharedStrings'!");
|
|
|
|
foreach (XmlNode sharedString in sharedStrings)
|
|
{
|
|
if (sharedString.Name == "SharedString")
|
|
{
|
|
XmlNode md5Node = sharedString.Attributes.GetNamedItem("md5");
|
|
|
|
if (md5Node == null)
|
|
throw error("Got a SharedString without an 'md5' attribute!");
|
|
|
|
string key = md5Node.InnerText;
|
|
string value = sharedString.InnerText.Replace("\n", "");
|
|
|
|
byte[] buffer = Convert.FromBase64String(value);
|
|
SharedString record = SharedString.FromBase64(value);
|
|
|
|
if (record.MD5_Key != key)
|
|
throw error("The provided md5 hash did not match with the md5 hash computed for the value!");
|
|
|
|
file.SharedStrings.Add(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void ReadMetadata(XmlNode meta, XmlRobloxFile file)
|
|
{
|
|
var error = createErrorHandler("ReadMetadata");
|
|
|
|
if (meta.Name != "Meta")
|
|
throw error("Provided XmlNode's class should be 'Meta'!");
|
|
|
|
XmlNode propName = meta.Attributes.GetNamedItem("name");
|
|
|
|
if (propName == null)
|
|
throw error("Got a Meta node without a 'name' attribute!");
|
|
|
|
string key = propName.InnerText;
|
|
string value = meta.InnerText;
|
|
|
|
file.Metadata[key] = value;
|
|
}
|
|
|
|
public static void ReadProperties(Instance instance, XmlNode propsNode)
|
|
{
|
|
var error = createErrorHandler("ReadProperties");
|
|
|
|
if (propsNode.Name != "Properties")
|
|
throw error("Provided XmlNode's class should be 'Properties'!");
|
|
|
|
foreach (XmlNode propNode in propsNode.ChildNodes)
|
|
{
|
|
string propType = propNode.Name;
|
|
XmlNode propName = propNode.Attributes.GetNamedItem("name");
|
|
|
|
if (propName == null)
|
|
{
|
|
if (propNode.Name == "Item")
|
|
continue;
|
|
|
|
throw error("Got a property node without a 'name' attribute!");
|
|
}
|
|
|
|
IXmlPropertyToken tokenHandler = XmlPropertyTokens.GetHandler(propType);
|
|
|
|
if (tokenHandler != null)
|
|
{
|
|
Property prop = new Property()
|
|
{
|
|
Name = propName.InnerText,
|
|
Instance = instance,
|
|
XmlToken = propType
|
|
};
|
|
|
|
if (!tokenHandler.ReadProperty(prop, propNode))
|
|
Console.WriteLine("Could not read property: " + prop.GetFullName() + '!');
|
|
|
|
instance.AddProperty(ref prop);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("No IXmlPropertyToken found for property type: " + propType + '!');
|
|
}
|
|
}
|
|
}
|
|
|
|
public static Instance ReadInstance(XmlNode instNode, XmlRobloxFile file)
|
|
{
|
|
var error = createErrorHandler("ReadInstance");
|
|
|
|
// Process the instance itself
|
|
if (instNode.Name != "Item")
|
|
throw error("Provided XmlNode's name should be 'Item'!");
|
|
|
|
XmlNode classToken = instNode.Attributes.GetNamedItem("class");
|
|
if (classToken == null)
|
|
throw error("Got an Item without a defined 'class' attribute!");
|
|
|
|
|
|
string className = classToken.InnerText;
|
|
|
|
Type instType = Type.GetType($"RobloxFiles.{className}") ?? typeof(Instance);
|
|
Instance inst = Activator.CreateInstance(instType) as Instance;
|
|
|
|
// The 'referent' attribute is optional, but should be defined if a Ref property needs to link to this Instance.
|
|
XmlNode refToken = instNode.Attributes.GetNamedItem("referent");
|
|
|
|
if (refToken != null && file != null)
|
|
{
|
|
string referent = refToken.InnerText;
|
|
inst.Referent = referent;
|
|
|
|
if (file.Instances.ContainsKey(referent))
|
|
throw error("Got an Item with a duplicate 'referent' attribute!");
|
|
|
|
file.Instances.Add(referent, inst);
|
|
}
|
|
|
|
// Process the child nodes of this instance.
|
|
foreach (XmlNode childNode in instNode.ChildNodes)
|
|
{
|
|
if (childNode.Name == "Properties")
|
|
{
|
|
ReadProperties(inst, childNode);
|
|
}
|
|
else if (childNode.Name == "Item")
|
|
{
|
|
Instance child = ReadInstance(childNode, file);
|
|
child.Parent = inst;
|
|
}
|
|
}
|
|
|
|
return inst;
|
|
}
|
|
}
|
|
}
|