Roblox-File-Format/Tokens/BrickColor.cs

44 lines
1.6 KiB
C#
Raw Normal View History

2021-02-25 20:09:54 +00:00
using System.Xml;
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
{
2021-02-25 20:09:54 +00:00
public class BrickColorToken : IXmlPropertyToken, IAttributeToken<BrickColor>
{
2021-02-25 20:09:54 +00:00
// This is a lie: The token is actually int, but that would cause a name collision.
// Since BrickColors are written as ints, the IntToken class will try to redirect
// to this handler if it believes that its representing a BrickColor.
public string XmlPropertyToken => "BrickColor";
public AttributeType AttributeType => AttributeType.BrickColor;
2021-02-25 20:09:54 +00:00
public BrickColor ReadAttribute(Attribute attr) => attr.ReadInt();
public void WriteAttribute(Attribute attr, BrickColor value) => attr.WriteInt(value.Number);
public bool ReadProperty(Property prop, XmlNode token)
{
2020-08-17 05:33:59 +00:00
if (XmlPropertyTokens.ReadPropertyGeneric(token, out int value))
{
BrickColor brickColor = BrickColor.FromNumber(value);
prop.XmlToken = "BrickColor";
prop.Value = brickColor;
return true;
}
return false;
}
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
{
BrickColor value = prop.CastValue<BrickColor>();
XmlElement brickColor = doc.CreateElement("int");
brickColor.InnerText = value.Number.ToInvariantString();
brickColor.SetAttribute("name", prop.Name);
brickColor.AppendChild(node);
}
}
}