2019-01-30 06:36:56 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
2019-02-01 17:19:20 +00:00
|
|
|
|
namespace RobloxFiles.XmlFormat
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2019-02-04 19:30:33 +00:00
|
|
|
|
public static class XmlDataReader
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2019-05-17 06:14:04 +00:00
|
|
|
|
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", "");
|
|
|
|
|
|
|
|
|
|
file.SharedStrings.Add(key, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 06:36:56 +00:00
|
|
|
|
public static void ReadProperties(Instance instance, XmlNode propsNode)
|
|
|
|
|
{
|
2019-05-17 06:14:04 +00:00
|
|
|
|
var error = createErrorHandler("ReadProperties");
|
|
|
|
|
|
2019-01-30 06:36:56 +00:00
|
|
|
|
if (propsNode.Name != "Properties")
|
2019-05-17 06:14:04 +00:00
|
|
|
|
throw error("Provided XmlNode's class should be 'Properties'!");
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
|
|
|
|
foreach (XmlNode propNode in propsNode.ChildNodes)
|
|
|
|
|
{
|
|
|
|
|
string propType = propNode.Name;
|
|
|
|
|
XmlNode propName = propNode.Attributes.GetNamedItem("name");
|
|
|
|
|
|
|
|
|
|
if (propName == null)
|
2019-05-17 06:14:04 +00:00
|
|
|
|
throw error("Got a property node without a 'name' attribute!");
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
|
|
|
|
IXmlPropertyToken tokenHandler = XmlPropertyTokens.GetHandler(propType);
|
|
|
|
|
|
|
|
|
|
if (tokenHandler != null)
|
|
|
|
|
{
|
|
|
|
|
Property prop = new Property();
|
|
|
|
|
prop.Name = propName.InnerText;
|
|
|
|
|
prop.Instance = instance;
|
2019-05-17 12:08:06 +00:00
|
|
|
|
prop.XmlToken = propType;
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
2019-05-17 12:08:06 +00:00
|
|
|
|
if (!tokenHandler.ReadProperty(prop, propNode))
|
2019-05-17 06:14:04 +00:00
|
|
|
|
Console.WriteLine("Could not read property: " + prop.GetFullName() + '!');
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
|
|
|
|
instance.AddProperty(ref prop);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-05-17 06:14:04 +00:00
|
|
|
|
Console.WriteLine("No IXmlPropertyToken found for property type: " + propType + '!');
|
2019-01-30 06:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 12:08:06 +00:00
|
|
|
|
public static Instance ReadInstance(XmlNode instNode, XmlRobloxFile file)
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2019-05-17 06:14:04 +00:00
|
|
|
|
var error = createErrorHandler("ReadInstance");
|
|
|
|
|
|
2019-01-30 06:36:56 +00:00
|
|
|
|
// Process the instance itself
|
|
|
|
|
if (instNode.Name != "Item")
|
2019-05-17 06:14:04 +00:00
|
|
|
|
throw error("Provided XmlNode's name should be 'Item'!");
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
|
|
|
|
XmlNode classToken = instNode.Attributes.GetNamedItem("class");
|
|
|
|
|
if (classToken == null)
|
2019-05-17 06:14:04 +00:00
|
|
|
|
throw error("Got an Item without a defined 'class' attribute!");
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
|
|
|
|
Instance inst = new Instance(classToken.InnerText);
|
|
|
|
|
|
2019-02-01 17:19:20 +00:00
|
|
|
|
// The 'referent' attribute is optional, but should be defined if a Ref property needs to link to this Instance.
|
2019-01-30 06:36:56 +00:00
|
|
|
|
XmlNode refToken = instNode.Attributes.GetNamedItem("referent");
|
|
|
|
|
|
2019-02-04 19:30:33 +00:00
|
|
|
|
if (refToken != null && file != null)
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
|
|
|
|
string refId = refToken.InnerText;
|
2019-02-01 17:19:20 +00:00
|
|
|
|
|
2019-02-04 19:30:33 +00:00
|
|
|
|
if (file.Instances.ContainsKey(refId))
|
2019-05-17 06:14:04 +00:00
|
|
|
|
throw error("Got an Item with a duplicate 'referent' attribute!");
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
2019-02-04 19:30:33 +00:00
|
|
|
|
file.Instances.Add(refId, inst);
|
2019-01-30 06:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
|
{
|
2019-02-04 19:30:33 +00:00
|
|
|
|
Instance child = ReadInstance(childNode, file);
|
2019-01-30 06:36:56 +00:00
|
|
|
|
child.Parent = inst;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return inst;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|