Cleaning up some things.
This commit is contained in:
12
XmlFormat/PropertyTokens/IXmlPropertyToken.cs
Normal file
12
XmlFormat/PropertyTokens/IXmlPropertyToken.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System.Xml;
|
||||
|
||||
namespace RobloxFiles.XmlFormat
|
||||
{
|
||||
public interface IXmlPropertyToken
|
||||
{
|
||||
string Token { get; }
|
||||
|
||||
bool ReadProperty(Property prop, XmlNode token);
|
||||
void WriteProperty(Property prop, XmlDocument doc, XmlNode node);
|
||||
}
|
||||
}
|
@ -15,14 +15,14 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
|
||||
prop.Value = base64;
|
||||
|
||||
byte[] buffer = Convert.FromBase64String(base64);
|
||||
prop.SetRawBuffer(buffer);
|
||||
prop.RawBuffer = buffer;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
|
||||
{
|
||||
byte[] data = prop.GetRawBuffer();
|
||||
byte[] data = prop.RawBuffer;
|
||||
string value = Convert.ToBase64String(data);
|
||||
|
||||
if (value.Length > 72)
|
@ -22,7 +22,7 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
|
||||
{
|
||||
// Roblox technically doesn't support this anymore, but load it anyway :P
|
||||
byte[] buffer = Convert.FromBase64String(content);
|
||||
prop.SetRawBuffer(buffer);
|
||||
prop.RawBuffer = buffer;
|
||||
}
|
||||
}
|
||||
|
@ -20,8 +20,6 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
|
||||
{
|
||||
return XmlPropertyTokens.ReadPropertyGeneric<int>(prop, PropertyType.Int, token);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
|
@ -78,6 +78,7 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
|
||||
|
||||
XmlElement element = doc.CreateElement(elementType);
|
||||
element.InnerText = value.ToInvariantString();
|
||||
|
||||
node.AppendChild(element);
|
||||
}
|
||||
}
|
@ -22,7 +22,7 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
|
||||
read[i] = Vector3Token.ReadVector3(fieldToken);
|
||||
}
|
||||
|
||||
Vector3 origin = read[0],
|
||||
Vector3 origin = read[0],
|
||||
direction = read[1];
|
||||
|
||||
Ray ray = new Ray(origin, direction);
|
||||
@ -42,11 +42,12 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
|
||||
Ray ray = prop.Value as Ray;
|
||||
|
||||
XmlElement origin = doc.CreateElement("origin");
|
||||
Vector3Token.WriteVector3(doc, origin, ray.Origin);
|
||||
node.AppendChild(origin);
|
||||
|
||||
XmlElement direction = doc.CreateElement("direction");
|
||||
|
||||
Vector3Token.WriteVector3(doc, origin, ray.Origin);
|
||||
Vector3Token.WriteVector3(doc, direction, ray.Direction);
|
||||
|
||||
node.AppendChild(origin);
|
||||
node.AppendChild(direction);
|
||||
}
|
||||
}
|
88
XmlFormat/PropertyTokens/XmlPropertyTokens.cs
Normal file
88
XmlFormat/PropertyTokens/XmlPropertyTokens.cs
Normal file
@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Xml;
|
||||
|
||||
namespace RobloxFiles.XmlFormat
|
||||
{
|
||||
public static class XmlPropertyTokens
|
||||
{
|
||||
public static IReadOnlyDictionary<string, IXmlPropertyToken> Handlers;
|
||||
|
||||
static XmlPropertyTokens()
|
||||
{
|
||||
// Initialize the PropertyToken handler singletons.
|
||||
Type IXmlPropertyToken = typeof(IXmlPropertyToken);
|
||||
|
||||
var handlerTypes = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(assembly => assembly.GetTypes())
|
||||
.Where(type => type != IXmlPropertyToken)
|
||||
.Where(type => IXmlPropertyToken.IsAssignableFrom(type));
|
||||
|
||||
var propTokens = handlerTypes.Select(handlerType => Activator.CreateInstance(handlerType) as IXmlPropertyToken);
|
||||
var tokenHandlers = new Dictionary<string, IXmlPropertyToken>();
|
||||
|
||||
foreach (IXmlPropertyToken propToken in propTokens)
|
||||
{
|
||||
var tokens = propToken.Token.Split(';')
|
||||
.Select(token => token.Trim())
|
||||
.ToList();
|
||||
|
||||
tokens.ForEach(token => tokenHandlers.Add(token, propToken));
|
||||
}
|
||||
|
||||
Handlers = tokenHandlers;
|
||||
}
|
||||
|
||||
public static bool ReadPropertyGeneric<T>(Property prop, PropertyType propType, XmlNode token) where T : struct
|
||||
{
|
||||
try
|
||||
{
|
||||
string value = token.InnerText;
|
||||
|
||||
if (typeof(T) == typeof(int))
|
||||
prop.Value = Formatting.ParseInt(value);
|
||||
else if (typeof(T) == typeof(float))
|
||||
prop.Value = Formatting.ParseFloat(value);
|
||||
else if (typeof(T) == typeof(double))
|
||||
prop.Value = Formatting.ParseDouble(value);
|
||||
|
||||
if (prop.Value == null)
|
||||
{
|
||||
Type resultType = typeof(T);
|
||||
TypeConverter converter = TypeDescriptor.GetConverter(resultType);
|
||||
|
||||
object result = converter.ConvertFromString(token.InnerText);
|
||||
prop.Value = result;
|
||||
}
|
||||
|
||||
prop.Type = propType;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static IXmlPropertyToken GetHandler(string tokenName)
|
||||
{
|
||||
IXmlPropertyToken result = null;
|
||||
|
||||
if (Handlers.ContainsKey(tokenName))
|
||||
result = Handlers[tokenName];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static T GetHandler<T>() where T : IXmlPropertyToken
|
||||
{
|
||||
IXmlPropertyToken result = Handlers.Values
|
||||
.Where(token => token is T)
|
||||
.First();
|
||||
|
||||
return (T)result;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user