Roblox-File-Format/XmlFormat/IO/XmlFileWriter.cs

231 lines
7.1 KiB
C#
Raw Normal View History

using System;
2020-08-17 05:33:59 +00:00
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml;
using RobloxFiles.DataTypes;
2020-08-18 01:12:24 +00:00
using RobloxFiles.Utility;
using RobloxFiles.XmlFormat.PropertyTokens;
namespace RobloxFiles.XmlFormat
{
2019-05-19 04:44:51 +00:00
public static class XmlRobloxFileWriter
{
2019-05-19 04:44:51 +00:00
public static readonly XmlWriterSettings Settings = new XmlWriterSettings()
{
Indent = true,
IndentChars = "\t",
NewLineChars = "\r\n",
Encoding = Encoding.UTF8,
2019-05-19 04:44:51 +00:00
OmitXmlDeclaration = true
};
2019-05-19 04:44:51 +00:00
public static string CreateReferent()
{
2020-08-17 05:33:59 +00:00
var referentGuid = Guid
.NewGuid()
.ToString();
string referent = "RBX" + referentGuid
2020-08-17 05:33:59 +00:00
.ToUpper(CultureInfo.InvariantCulture)
.Replace("-", "");
2020-08-17 05:33:59 +00:00
return referent;
}
private static string GetEnumName<T>(T item) where T : struct
{
return Enum.GetName(typeof(T), item);
}
internal static void RecordInstances(XmlRobloxFile file, Instance inst)
{
2020-08-18 01:12:24 +00:00
inst.Referent = "RBX" + file.RefCounter++;
foreach (Instance child in inst.GetChildren())
RecordInstances(file, child);
file.Instances.Add(inst.Referent, inst);
}
public static XmlNode WriteProperty(Property prop, XmlDocument doc, XmlRobloxFile file)
{
if (prop.Name == "Archivable")
return null;
string propType = prop.XmlToken;
2019-11-04 21:25:22 +00:00
if (propType == null)
propType = "";
if (propType.Length == 0)
{
propType = GetEnumName(prop.Type);
switch (prop.Type)
{
case PropertyType.CFrame:
case PropertyType.Quaternion:
propType = "CoordinateFrame";
break;
case PropertyType.Enum:
propType = "token";
break;
case PropertyType.Rect:
propType = "Rect2D";
break;
case PropertyType.Int:
case PropertyType.Bool:
case PropertyType.Float:
case PropertyType.Int64:
case PropertyType.Double:
2020-08-17 05:33:59 +00:00
propType = propType.ToLower(CultureInfo.InvariantCulture);
break;
case PropertyType.String:
propType = (prop.HasRawBuffer ? "BinaryString" : "string");
break;
default: break;
}
}
IXmlPropertyToken handler = XmlPropertyTokens.GetHandler(propType);
if (handler == null)
{
Console.WriteLine("XmlDataWriter.WriteProperty: No token handler found for property type: {0}", propType);
return null;
}
if (prop.Type == PropertyType.SharedString)
{
2020-08-18 01:12:24 +00:00
SharedString str = prop.CastValue<SharedString>();
if (str == null)
{
byte[] value = prop.CastValue<byte[]>();
str = SharedString.FromBuffer(value);
}
2020-08-18 01:12:24 +00:00
if (str.ComputedKey == null)
{
2020-08-18 01:12:24 +00:00
var newShared = SharedString.FromBuffer(str.SharedValue);
str.Key = newShared.ComputedKey;
}
2020-08-18 01:12:24 +00:00
file.SharedStrings.Add(str.Key);
}
XmlElement propElement = doc.CreateElement(propType);
propElement.SetAttribute("name", prop.Name);
XmlNode propNode = propElement;
handler.WriteProperty(prop, doc, propNode);
if (propNode.ParentNode != null)
{
XmlNode newNode = propNode.ParentNode;
newNode.RemoveChild(propNode);
propNode = newNode;
}
return propNode;
}
public static XmlNode WriteInstance(Instance instance, XmlDocument doc, XmlRobloxFile file)
{
if (!instance.Archivable)
return null;
XmlElement instNode = doc.CreateElement("Item");
instNode.SetAttribute("class", instance.ClassName);
instNode.SetAttribute("referent", instance.Referent);
XmlElement propsNode = doc.CreateElement("Properties");
instNode.AppendChild(propsNode);
var props = instance.RefreshProperties();
var orderedKeys = props
.OrderBy(pair => pair.Key)
.Select(pair => pair.Key);
2019-05-19 04:44:51 +00:00
foreach (string propName in orderedKeys)
{
Property prop = props[propName];
2020-08-18 01:12:24 +00:00
bool isDefault = false;
object a = DefaultProperty.Get(instance, prop);
object b = prop.Value;
if (a is float)
{
float f0 = (float)a,
f1 = (float)b;
isDefault = f0.FuzzyEquals(f1);
}
else if (a is double)
{
double d0 = (double)a,
d1 = (double)b;
isDefault = d0.FuzzyEquals(d1);
}
else if (b != null)
{
isDefault = b.Equals(a);
}
else if (a == b)
{
isDefault = true;
}
2020-08-18 01:12:24 +00:00
if (!isDefault)
{
XmlNode propNode = WriteProperty(prop, doc, file);
2020-08-18 01:12:24 +00:00
if (propNode == null)
continue;
propsNode.AppendChild(propNode);
}
}
foreach (Instance child in instance.GetChildren())
{
if (child.Archivable)
{
XmlNode childNode = WriteInstance(child, doc, file);
instNode.AppendChild(childNode);
}
}
return instNode;
}
public static XmlNode WriteSharedStrings(XmlDocument doc, XmlRobloxFile file)
{
2020-08-17 05:33:59 +00:00
Contract.Requires(doc != null && file != null);
XmlElement sharedStrings = doc.CreateElement("SharedStrings");
var binaryWriter = XmlPropertyTokens.GetHandler<BinaryStringToken>();
var binaryBuffer = new Property("SharedString", PropertyType.String);
foreach (string key in file.SharedStrings)
{
XmlElement sharedString = doc.CreateElement("SharedString");
sharedString.SetAttribute("md5", key);
binaryBuffer.RawBuffer = SharedString.Find(key);
binaryWriter.WriteProperty(binaryBuffer, doc, sharedString);
sharedStrings.AppendChild(sharedString);
}
return sharedStrings;
}
}
}