2019-05-17 12:08:06 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Xml;
|
2019-02-01 17:19:20 +00:00
|
|
|
|
using RobloxFiles.DataTypes;
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
2021-02-25 20:09:54 +00:00
|
|
|
|
namespace RobloxFiles.Tokens
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2021-02-25 20:09:54 +00:00
|
|
|
|
public class UDimToken : IXmlPropertyToken, IAttributeToken<UDim>
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2021-02-25 20:09:54 +00:00
|
|
|
|
public string XmlPropertyToken => "UDim";
|
|
|
|
|
public AttributeType AttributeType => AttributeType.UDim;
|
|
|
|
|
|
2021-06-05 22:21:12 +00:00
|
|
|
|
public UDim ReadAttribute(RbxAttribute attr) => ReadUDim(attr);
|
|
|
|
|
public void WriteAttribute(RbxAttribute attr, UDim value) => WriteUDim(attr, value);
|
2021-02-25 20:09:54 +00:00
|
|
|
|
|
2019-02-01 18:40:39 +00:00
|
|
|
|
public static UDim ReadUDim(XmlNode token, string prefix = "")
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
XmlElement scaleToken = token[prefix + 'S'];
|
2019-05-17 12:08:06 +00:00
|
|
|
|
float scale = Formatting.ParseFloat(scaleToken.InnerText);
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
|
|
|
|
XmlElement offsetToken = token[prefix + 'O'];
|
|
|
|
|
int offset = int.Parse(offsetToken.InnerText);
|
|
|
|
|
|
|
|
|
|
return new UDim(scale, offset);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 12:08:06 +00:00
|
|
|
|
public static void WriteUDim(XmlDocument doc, XmlNode node, UDim value, string prefix = "")
|
|
|
|
|
{
|
|
|
|
|
XmlElement scale = doc.CreateElement(prefix + 'S');
|
|
|
|
|
scale.InnerText = value.Scale.ToInvariantString();
|
|
|
|
|
node.AppendChild(scale);
|
|
|
|
|
|
|
|
|
|
XmlElement offset = doc.CreateElement(prefix + 'O');
|
|
|
|
|
offset.InnerText = value.Offset.ToInvariantString();
|
|
|
|
|
node.AppendChild(offset);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-05 22:21:12 +00:00
|
|
|
|
public static UDim ReadUDim(RbxAttribute attr)
|
2021-02-25 20:09:54 +00:00
|
|
|
|
{
|
|
|
|
|
float scale = attr.ReadFloat();
|
|
|
|
|
int offset = attr.ReadInt();
|
|
|
|
|
|
|
|
|
|
return new UDim(scale, offset);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-05 22:21:12 +00:00
|
|
|
|
public static void WriteUDim(RbxAttribute attr, UDim value)
|
2021-02-25 20:09:54 +00:00
|
|
|
|
{
|
|
|
|
|
float scale = value.Scale;
|
|
|
|
|
attr.WriteFloat(scale);
|
|
|
|
|
|
|
|
|
|
int offset = value.Offset;
|
|
|
|
|
attr.WriteInt(offset);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 12:08:06 +00:00
|
|
|
|
public bool ReadProperty(Property property, XmlNode token)
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2019-02-01 18:40:39 +00:00
|
|
|
|
UDim result = ReadUDim(token);
|
|
|
|
|
bool success = (result != null);
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
property.Type = PropertyType.UDim;
|
2019-02-01 18:40:39 +00:00
|
|
|
|
property.Value = result;
|
2019-01-30 06:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
2019-05-17 12:08:06 +00:00
|
|
|
|
|
|
|
|
|
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
|
|
|
|
|
{
|
2019-06-30 22:01:19 +00:00
|
|
|
|
UDim value = prop.CastValue<UDim>();
|
2019-05-17 12:08:06 +00:00
|
|
|
|
WriteUDim(doc, node, value);
|
|
|
|
|
}
|
2019-01-30 06:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|