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

50 lines
1.3 KiB
C#
Raw Normal View History

using System;
2020-08-17 05:33:59 +00:00
using System.Diagnostics.Contracts;
using System.Reflection;
using System.Xml;
namespace RobloxFiles.XmlFormat.PropertyTokens
{
public class EnumToken : IXmlPropertyToken
{
public string Token => "token";
public bool ReadProperty(Property prop, XmlNode token)
{
2020-08-17 05:33:59 +00:00
Contract.Requires(prop != null);
2020-08-17 05:33:59 +00:00
if (XmlPropertyTokens.ReadPropertyGeneric(token, out uint value))
{
Instance inst = prop.Instance;
Type instType = inst?.GetType();
FieldInfo info = instType.GetField(prop.Name, Property.BindingFlags);
if (info != null)
{
Type enumType = info.FieldType;
string item = value.ToInvariantString();
prop.Type = PropertyType.Enum;
prop.Value = Enum.Parse(enumType, item);
return true;
}
}
return false;
}
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
{
2020-08-17 05:33:59 +00:00
Contract.Requires(prop != null && node != null);
object rawValue = prop.Value;
2020-08-17 05:33:59 +00:00
int signed = (int)rawValue;
uint value = (uint)signed;
2020-08-17 05:33:59 +00:00
node.InnerText = value.ToInvariantString();
}
}
}