2021-02-25 20:09:54 +00:00
|
|
|
|
using System.Xml;
|
2019-02-01 17:19:20 +00:00
|
|
|
|
using RobloxFiles.DataTypes;
|
2021-02-25 20:09:54 +00:00
|
|
|
|
using RobloxFiles.XmlFormat;
|
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 Color3Token : IXmlPropertyToken, IAttributeToken<Color3>
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2021-02-25 20:09:54 +00:00
|
|
|
|
public string XmlPropertyToken => "Color3";
|
|
|
|
|
private readonly string[] XmlFields = new string[3] { "R", "G", "B" };
|
|
|
|
|
|
|
|
|
|
public AttributeType AttributeType => AttributeType.Color3;
|
2021-06-05 22:21:12 +00:00
|
|
|
|
public Color3 ReadAttribute(RbxAttribute attr) => ReadColor3(attr);
|
|
|
|
|
public void WriteAttribute(RbxAttribute attr, Color3 value) => WriteColor3(attr, value);
|
2021-02-25 20:09:54 +00:00
|
|
|
|
|
2021-06-05 22:21:12 +00:00
|
|
|
|
public static Color3 ReadColor3(RbxAttribute attr)
|
2021-02-25 20:09:54 +00:00
|
|
|
|
{
|
|
|
|
|
float r = attr.ReadFloat(),
|
|
|
|
|
g = attr.ReadFloat(),
|
|
|
|
|
b = attr.ReadFloat();
|
|
|
|
|
|
|
|
|
|
return new Color3(r, g, b);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-05 22:21:12 +00:00
|
|
|
|
public static void WriteColor3(RbxAttribute attr, Color3 value)
|
2021-02-25 20:09:54 +00:00
|
|
|
|
{
|
|
|
|
|
attr.WriteFloat(value.R);
|
|
|
|
|
attr.WriteFloat(value.G);
|
|
|
|
|
attr.WriteFloat(value.B);
|
|
|
|
|
}
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
2019-05-17 12:08:06 +00:00
|
|
|
|
public bool ReadProperty(Property prop, XmlNode token)
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2019-05-17 06:14:04 +00:00
|
|
|
|
bool success = true;
|
2021-02-25 20:09:54 +00:00
|
|
|
|
float[] fields = new float[XmlFields.Length];
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
2019-05-17 06:14:04 +00:00
|
|
|
|
for (int i = 0; i < fields.Length; i++)
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2021-02-25 20:09:54 +00:00
|
|
|
|
string key = XmlFields[i];
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
2019-05-17 06:14:04 +00:00
|
|
|
|
try
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2019-05-17 06:14:04 +00:00
|
|
|
|
var coord = token[key];
|
2020-09-21 18:29:31 +00:00
|
|
|
|
string text = coord?.InnerText;
|
|
|
|
|
|
|
|
|
|
if (text == null)
|
|
|
|
|
{
|
|
|
|
|
text = "0";
|
|
|
|
|
success = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fields[i] = Formatting.ParseFloat(text);
|
2019-05-17 06:14:04 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
success = false;
|
|
|
|
|
break;
|
2019-01-30 06:36:56 +00:00
|
|
|
|
}
|
2019-05-17 06:14:04 +00:00
|
|
|
|
}
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
2019-05-17 06:14:04 +00:00
|
|
|
|
if (success)
|
|
|
|
|
{
|
2019-01-30 06:36:56 +00:00
|
|
|
|
float r = fields[0],
|
|
|
|
|
g = fields[1],
|
|
|
|
|
b = fields[2];
|
|
|
|
|
|
|
|
|
|
prop.Type = PropertyType.Color3;
|
|
|
|
|
prop.Value = new Color3(r, g, b);
|
|
|
|
|
}
|
2019-05-17 06:14:04 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Try falling back to the Color3uint8 technique...
|
|
|
|
|
var color3uint8 = XmlPropertyTokens.GetHandler<Color3uint8Token>();
|
2019-05-17 12:08:06 +00:00
|
|
|
|
success = color3uint8.ReadProperty(prop, token);
|
2019-05-17 06:14:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
Color3 color = prop.CastValue<Color3>();
|
|
|
|
|
float[] rgb = new float[3] { color.R, color.G, color.B };
|
2019-05-17 12:08:06 +00:00
|
|
|
|
|
2019-06-30 22:01:19 +00:00
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
|
{
|
2021-02-25 20:09:54 +00:00
|
|
|
|
string field = XmlFields[i];
|
2019-06-30 22:01:19 +00:00
|
|
|
|
float value = rgb[i];
|
2019-05-17 12:08:06 +00:00
|
|
|
|
|
2019-06-30 22:01:19 +00:00
|
|
|
|
XmlElement channel = doc.CreateElement(field);
|
|
|
|
|
channel.InnerText = value.ToInvariantString();
|
2019-05-17 12:08:06 +00:00
|
|
|
|
|
2019-06-30 22:01:19 +00:00
|
|
|
|
node.AppendChild(channel);
|
2019-05-17 12:08:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-30 06:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|