Roblox-File-Format/Tokens/ProtectedString.cs

48 lines
1.3 KiB
C#
Raw Normal View History

using System.Text;
using System.Xml;
2021-02-25 20:09:54 +00:00
using RobloxFiles.DataTypes;
2021-02-25 20:09:54 +00:00
using RobloxFiles.XmlFormat;
2021-02-25 20:09:54 +00:00
namespace RobloxFiles.Tokens
{
public class ProtectedStringToken : IXmlPropertyToken
{
2021-02-25 20:09:54 +00:00
public string XmlPropertyToken => "ProtectedString";
public bool ReadProperty(Property prop, XmlNode token)
{
ProtectedString contents = token.InnerText;
2021-05-01 22:40:09 +00:00
prop.Type = PropertyType.String;
prop.Value = contents.ToString();
return true;
}
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
{
ProtectedString value = prop.CastValue<ProtectedString>();
if (value.IsCompiled)
{
var binary = XmlPropertyTokens.GetHandler<BinaryStringToken>();
binary.WriteProperty(prop, doc, node);
}
else
{
string contents = Encoding.UTF8.GetString(value.RawBuffer);
if (contents.Contains("\r") || contents.Contains("\n"))
{
XmlCDataSection cdata = doc.CreateCDataSection(contents);
node.AppendChild(cdata);
}
else
{
node.InnerText = contents;
}
}
}
}
}