2019-01-30 06:36:56 +00:00
|
|
|
|
using System;
|
2019-05-17 12:08:06 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-01-30 06:36:56 +00:00
|
|
|
|
using System.Xml;
|
2019-02-01 17:19:20 +00:00
|
|
|
|
using RobloxFiles.DataTypes;
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
2019-02-01 17:19:20 +00:00
|
|
|
|
namespace RobloxFiles.XmlFormat.PropertyTokens
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
|
|
|
|
public class PhysicalPropertiesToken : IXmlPropertyToken
|
|
|
|
|
{
|
|
|
|
|
public string Token => "PhysicalProperties";
|
|
|
|
|
|
|
|
|
|
private Func<string, T> createReader<T>(Func<string, T> parse, XmlNode token) where T : struct
|
|
|
|
|
{
|
|
|
|
|
return new Func<string, T>(key =>
|
|
|
|
|
{
|
|
|
|
|
XmlElement node = token[key];
|
|
|
|
|
return parse(node.InnerText);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 12:08:06 +00:00
|
|
|
|
public bool ReadProperty(Property prop, XmlNode token)
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
|
|
|
|
var readBool = createReader(bool.Parse, token);
|
2019-05-17 12:08:06 +00:00
|
|
|
|
var readFloat = createReader(Formatting.ParseFloat, token);
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
bool custom = readBool("CustomPhysics");
|
2019-02-03 13:28:54 +00:00
|
|
|
|
prop.Type = PropertyType.PhysicalProperties;
|
|
|
|
|
|
2019-01-30 06:36:56 +00:00
|
|
|
|
if (custom)
|
|
|
|
|
{
|
|
|
|
|
prop.Value = new PhysicalProperties
|
|
|
|
|
(
|
|
|
|
|
readFloat("Density"),
|
|
|
|
|
readFloat("Friction"),
|
|
|
|
|
readFloat("Elasticity"),
|
|
|
|
|
readFloat("FrictionWeight"),
|
|
|
|
|
readFloat("ElasticityWeight")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-17 12:08:06 +00:00
|
|
|
|
|
|
|
|
|
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
|
|
|
|
|
{
|
|
|
|
|
bool hasCustomPhysics = (prop.Value != null);
|
|
|
|
|
|
|
|
|
|
XmlElement customPhysics = doc.CreateElement("CustomPhysics");
|
|
|
|
|
customPhysics.InnerText = hasCustomPhysics
|
|
|
|
|
.ToString()
|
|
|
|
|
.ToLower();
|
|
|
|
|
|
|
|
|
|
node.AppendChild(customPhysics);
|
|
|
|
|
|
|
|
|
|
if (hasCustomPhysics)
|
|
|
|
|
{
|
|
|
|
|
var customProps = prop.Value as PhysicalProperties;
|
|
|
|
|
|
|
|
|
|
var data = new Dictionary<string, float>()
|
|
|
|
|
{
|
|
|
|
|
{ "Density", customProps.Density },
|
|
|
|
|
{ "Friction", customProps.Friction },
|
|
|
|
|
{ "Elasticity", customProps.Elasticity },
|
|
|
|
|
{ "FrictionWeight", customProps.FrictionWeight },
|
|
|
|
|
{ "ElasticityWeight", customProps.ElasticityWeight }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (string elementType in data.Keys)
|
|
|
|
|
{
|
|
|
|
|
float value = data[elementType];
|
|
|
|
|
|
|
|
|
|
XmlElement element = doc.CreateElement(elementType);
|
|
|
|
|
element.InnerText = value.ToInvariantString();
|
|
|
|
|
node.AppendChild(element);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-30 06:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|