2019-06-30 22:01:19 +00:00
|
|
|
|
using System;
|
2020-08-17 05:33:59 +00:00
|
|
|
|
using System.Diagnostics.Contracts;
|
2019-06-30 22:01:19 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
2021-02-25 20:09:54 +00:00
|
|
|
|
using RobloxFiles.XmlFormat;
|
|
|
|
|
|
|
|
|
|
namespace RobloxFiles.Tokens
|
2019-06-30 22:01:19 +00:00
|
|
|
|
{
|
|
|
|
|
public class EnumToken : IXmlPropertyToken
|
|
|
|
|
{
|
2021-02-25 20:09:54 +00:00
|
|
|
|
public string XmlPropertyToken => "token";
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
|
|
|
|
public bool ReadProperty(Property prop, XmlNode token)
|
|
|
|
|
{
|
2020-08-17 05:33:59 +00:00
|
|
|
|
Contract.Requires(prop != null);
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
2020-08-17 05:33:59 +00:00
|
|
|
|
if (XmlPropertyTokens.ReadPropertyGeneric(token, out uint value))
|
2019-06-30 22:01:19 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
2019-06-30 22:01:19 +00:00
|
|
|
|
object rawValue = prop.Value;
|
2020-08-17 05:33:59 +00:00
|
|
|
|
|
2022-02-18 21:45:42 +00:00
|
|
|
|
if (!(rawValue is uint value))
|
|
|
|
|
{
|
|
|
|
|
Type type = rawValue.GetType();
|
|
|
|
|
|
|
|
|
|
if (type.IsEnum)
|
|
|
|
|
{
|
|
|
|
|
var signed = (int)rawValue;
|
|
|
|
|
value = (uint)signed;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
value = 0;
|
|
|
|
|
RobloxFile.LogError($"Raw value for enum property {prop} could not be casted to uint!");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-30 22:01:19 +00:00
|
|
|
|
|
2020-08-17 05:33:59 +00:00
|
|
|
|
node.InnerText = value.ToInvariantString();
|
2019-06-30 22:01:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|