2022-12-27 12:36:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.Remoting.Messaging;
|
|
|
|
|
using System.Xml;
|
2019-02-01 17:19:20 +00:00
|
|
|
|
using RobloxFiles.DataTypes;
|
2022-12-27 12:36:50 +00:00
|
|
|
|
using RobloxFiles.Enums;
|
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
|
|
|
|
{
|
2022-12-27 12:36:50 +00:00
|
|
|
|
public class CFrameToken : IXmlPropertyToken, IAttributeToken<CFrame>
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2021-02-25 20:09:54 +00:00
|
|
|
|
public string XmlPropertyToken => "CoordinateFrame; CFrame";
|
2022-12-27 12:36:50 +00:00
|
|
|
|
public AttributeType AttributeType => AttributeType.CFrame;
|
|
|
|
|
|
|
|
|
|
private static readonly string[] Coords = new string[12]
|
|
|
|
|
{
|
|
|
|
|
"X", "Y", "Z",
|
|
|
|
|
"R00", "R01", "R02",
|
|
|
|
|
"R10", "R11", "R12",
|
|
|
|
|
"R20", "R21", "R22"
|
|
|
|
|
};
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
|
|
|
|
public static CFrame ReadCFrame(XmlNode token)
|
|
|
|
|
{
|
|
|
|
|
float[] components = new float[12];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 12; i++)
|
|
|
|
|
{
|
|
|
|
|
string key = Coords[i];
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var coord = token[key];
|
2019-05-17 12:08:06 +00:00
|
|
|
|
components[i] = Formatting.ParseFloat(coord.InnerText);
|
2019-01-30 06:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new CFrame(components);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 18:39:01 +00:00
|
|
|
|
public static void WriteCFrame(CFrame cf, XmlDocument doc, XmlNode node)
|
2021-05-01 22:40:09 +00:00
|
|
|
|
{
|
|
|
|
|
float[] components = cf.GetComponents();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 12; i++)
|
|
|
|
|
{
|
|
|
|
|
string coordName = Coords[i];
|
|
|
|
|
float coordValue = components[i];
|
|
|
|
|
|
|
|
|
|
XmlElement coord = doc.CreateElement(coordName);
|
|
|
|
|
coord.InnerText = coordValue.ToInvariantString();
|
|
|
|
|
|
|
|
|
|
node.AppendChild(coord);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 12:08:06 +00:00
|
|
|
|
public bool ReadProperty(Property prop, XmlNode token)
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
|
|
|
|
CFrame result = ReadCFrame(token);
|
|
|
|
|
bool success = (result != null);
|
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
2019-05-17 12:08:06 +00:00
|
|
|
|
prop.Type = PropertyType.CFrame;
|
|
|
|
|
prop.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)
|
|
|
|
|
{
|
2021-05-05 18:39:01 +00:00
|
|
|
|
CFrame value = prop.Value as CFrame;
|
|
|
|
|
WriteCFrame(value, doc, node);
|
2019-05-17 12:08:06 +00:00
|
|
|
|
}
|
2022-12-27 12:36:50 +00:00
|
|
|
|
|
|
|
|
|
public CFrame ReadAttribute(RbxAttribute attribute)
|
|
|
|
|
{
|
|
|
|
|
float x = attribute.ReadFloat(),
|
|
|
|
|
y = attribute.ReadFloat(),
|
|
|
|
|
z = attribute.ReadFloat();
|
|
|
|
|
|
|
|
|
|
var rawOrientId = attribute.ReadByte();
|
|
|
|
|
var pos = new Vector3(x, y, z);
|
|
|
|
|
|
|
|
|
|
if (rawOrientId > 0)
|
|
|
|
|
{
|
|
|
|
|
int orientId = (rawOrientId - 1) % 36;
|
|
|
|
|
return CFrame.FromOrientId(orientId) + pos;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
float[] matrix = new float[12];
|
|
|
|
|
|
|
|
|
|
for (int i = 3; i < 12; i++)
|
|
|
|
|
matrix[i] = attribute.ReadFloat();
|
|
|
|
|
|
|
|
|
|
return new CFrame(matrix) + pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteAttribute(RbxAttribute attribute, CFrame value)
|
|
|
|
|
{
|
|
|
|
|
Vector3 pos = value.Position;
|
2022-12-31 21:10:56 +00:00
|
|
|
|
Vector3Token.WriteVector3(attribute, pos);
|
2022-12-27 12:36:50 +00:00
|
|
|
|
|
|
|
|
|
int orientId = value.GetOrientId();
|
|
|
|
|
attribute.WriteByte((byte)(orientId + 1));
|
|
|
|
|
|
|
|
|
|
if (orientId == -1)
|
|
|
|
|
{
|
|
|
|
|
float[] components = value.GetComponents();
|
|
|
|
|
|
|
|
|
|
for (int i = 3; i < 12; i++)
|
|
|
|
|
{
|
|
|
|
|
float component = components[i];
|
|
|
|
|
attribute.WriteFloat(component);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-30 06:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|