Roblox-File-Format/Tokens/NumberRange.cs

58 lines
1.6 KiB
C#
Raw Normal View History

using System;
using System.Xml;
using RobloxFiles.DataTypes;
2021-02-25 20:09:54 +00:00
namespace RobloxFiles.Tokens
{
2021-02-25 20:09:54 +00:00
public class NumberRangeToken : IXmlPropertyToken, IAttributeToken<NumberRange>
{
2021-02-25 20:09:54 +00:00
public string XmlPropertyToken => "NumberRange";
public AttributeType AttributeType => AttributeType.NumberRange;
public bool ReadProperty(Property prop, XmlNode token)
{
string contents = token.InnerText.Trim();
string[] buffer = contents.Split(' ');
bool valid = (buffer.Length == 2);
if (valid)
{
try
{
float min = Formatting.ParseFloat(buffer[0]);
float max = Formatting.ParseFloat(buffer[1]);
prop.Type = PropertyType.NumberRange;
prop.Value = new NumberRange(min, max);
}
catch
{
valid = false;
}
}
return valid;
}
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
{
NumberRange value = prop.CastValue<NumberRange>();
node.InnerText = value.ToString() + ' ';
}
2021-02-25 20:09:54 +00:00
2021-06-05 22:21:12 +00:00
public NumberRange ReadAttribute(RbxAttribute attr)
2021-02-25 20:09:54 +00:00
{
float min = attr.ReadFloat();
float max = attr.ReadFloat();
return new NumberRange(min, max);
}
2021-06-05 22:21:12 +00:00
public void WriteAttribute(RbxAttribute attr, NumberRange value)
2021-02-25 20:09:54 +00:00
{
attr.WriteFloat(value.Min);
attr.WriteFloat(value.Max);
}
}
}