Roblox-File-Format/XmlFormat/Tokens/BinaryString.cs

47 lines
1.3 KiB
C#
Raw Normal View History

using System;
using System.Xml;
namespace RobloxFiles.XmlFormat.PropertyTokens
{
public class BinaryStringToken : IXmlPropertyToken
{
public string Token => "BinaryString";
public bool ReadProperty(Property prop, XmlNode token)
{
// BinaryStrings are encoded in base64
string base64 = token.InnerText.Replace("\n", "");
byte[] buffer = Convert.FromBase64String(base64);
2019-07-04 23:26:53 +00:00
prop.Value = buffer;
2019-05-19 04:44:51 +00:00
prop.RawBuffer = buffer;
2019-07-04 23:26:53 +00:00
prop.Type = PropertyType.String;
return true;
}
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
{
2019-05-19 04:44:51 +00:00
byte[] data = prop.RawBuffer;
string value = Convert.ToBase64String(data);
if (value.Length > 72)
{
string buffer = "";
while (value.Length > 72)
{
string chunk = value.Substring(0, 72);
value = value.Substring(72);
buffer += chunk + '\n';
}
value = buffer + value;
}
XmlCDataSection cdata = doc.CreateCDataSection(value);
node.AppendChild(cdata);
}
}
}