Convert datatypes to classes instead of structs.

This commit is contained in:
CloneTrooper1019 2019-02-01 12:40:39 -06:00
parent 795018e243
commit f7184eb8f8
35 changed files with 182 additions and 169 deletions

View File

@ -415,7 +415,7 @@ namespace RobloxFiles.BinaryFormat.Chunks
g = color3uint8_G[i],
b = color3uint8_B[i];
return Color3.fromRGB(r, g, b);
return Color3.FromRGB(r, g, b);
});
break;

View File

@ -1,5 +1,5 @@
// This is an auto-generated list of all available enums on Roblox!
// Updated as of 0.369.1.273919
// Updated as of 0.370.0.274702
namespace RobloxFiles.Enums
{
@ -806,7 +806,8 @@ namespace RobloxFiles.Enums
Players = 7,
Chat = 15,
Avatar,
Analytics = 22
Analytics = 22,
Localization = 24
}
public enum HumanoidDisplayDistanceType

View File

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using RobloxFiles.DataTypes.Utility;
using RobloxFiles.Utility;
namespace RobloxFiles.DataTypes
{
@ -35,7 +35,7 @@ namespace RobloxFiles.DataTypes
Name = name;
Number = number;
Color = Color3.fromRGB(r, g, b);
Color = Color3.FromRGB(r, g, b);
}
static BrickColor()
@ -43,7 +43,7 @@ namespace RobloxFiles.DataTypes
ByNumber = BrickColors.ColorMap.ToDictionary(brickColor => brickColor.Number);
ByPalette = BrickColors.PaletteMap.Select(number => ByNumber[number]).ToList();
}
public static BrickColor FromName(string name)
{
BrickColor result = null;

View File

@ -20,6 +20,13 @@ namespace RobloxFiles.DataTypes
public Vector3 RightVector => new Vector3( m11, m21, m31);
public Vector3 UpVector => new Vector3( m12, m22, m32);
public CFrame()
{
m14 = 0;
m24 = 0;
m34 = 0;
}
public CFrame(Vector3 pos)
{
m14 = pos.X;
@ -27,6 +34,14 @@ namespace RobloxFiles.DataTypes
m34 = pos.Z;
}
public CFrame(float nx = 0, float ny = 0, float nz = 0)
{
m14 = nx;
m24 = ny;
m34 = nz;
}
public CFrame(Vector3 eye, Vector3 look)
{
Vector3 zAxis = (eye - look).Unit;
@ -37,9 +52,9 @@ namespace RobloxFiles.DataTypes
{
if (zAxis.Y < 0)
{
xAxis = new Vector3(0, 0, -1);
yAxis = new Vector3(1, 0, 0);
zAxis = new Vector3(0, -1, 0);
xAxis = new Vector3(0, 0, -1);
yAxis = new Vector3(1, 0, 0);
zAxis = new Vector3(0, -1, 0);
}
else
{
@ -54,13 +69,6 @@ namespace RobloxFiles.DataTypes
m31 = xAxis.Z; m32 = yAxis.Z; m33 = zAxis.Z; m34 = eye.Z;
}
public CFrame(float nx = 0, float ny = 0, float nz = 0)
{
m14 = nx;
m24 = ny;
m34 = nz;
}
public CFrame(float nx, float ny, float nz, float i, float j, float k, float w)
{
float ii = i * i;

View File

@ -2,7 +2,7 @@
namespace RobloxFiles.DataTypes
{
public struct Color3
public class Color3
{
public readonly float R, G, B;
@ -18,12 +18,12 @@ namespace RobloxFiles.DataTypes
return string.Join(", ", R, G, B);
}
public static Color3 fromRGB(uint r = 0, uint g = 0, uint b = 0)
public static Color3 FromRGB(uint r = 0, uint g = 0, uint b = 0)
{
return new Color3(r / 255f, g / 255f, b / 255f);
}
public static Color3 fromHSV(float h = 0, float s = 0, float v = 0)
public static Color3 FromHSV(float h = 0, float s = 0, float v = 0)
{
int i = (int)Math.Min(5, Math.Floor(6.0 * h));
float f = 6.0f * h - i;
@ -51,7 +51,7 @@ namespace RobloxFiles.DataTypes
}
}
public static float[] toHSV(Color3 color)
public static float[] ToHSV(Color3 color)
{
float val = Math.Max(Math.Max(color.R, color.G), color.B);

View File

@ -2,7 +2,7 @@
namespace RobloxFiles.DataTypes
{
public struct ColorSequence
public class ColorSequence
{
public readonly ColorSequenceKeypoint[] Keypoints;
@ -46,7 +46,7 @@ namespace RobloxFiles.DataTypes
public override string ToString()
{
return string.Join(" ", Keypoints);
return string.Join<ColorSequenceKeypoint>(" ", Keypoints);
}
}
}

View File

@ -1,6 +1,6 @@
namespace RobloxFiles.DataTypes
{
public struct ColorSequenceKeypoint
public class ColorSequenceKeypoint
{
public readonly float Time;
public readonly Color3 Value;

View File

@ -2,12 +2,12 @@
namespace RobloxFiles.DataTypes
{
public struct NumberRange
public class NumberRange
{
public readonly float Min;
public readonly float Max;
public NumberRange(float min, float max)
public NumberRange(float min = 0, float max = 0)
{
if (max - min < 0)
throw new Exception("NumberRange: invalid range");

View File

@ -2,7 +2,7 @@
namespace RobloxFiles.DataTypes
{
public struct NumberSequence
public class NumberSequence
{
public readonly NumberSequenceKeypoint[] Keypoints;
@ -46,7 +46,7 @@ namespace RobloxFiles.DataTypes
public override string ToString()
{
return string.Join(" ", Keypoints);
return string.Join<NumberSequenceKeypoint>(" ", Keypoints);
}
}
}

View File

@ -1,6 +1,6 @@
namespace RobloxFiles.DataTypes
{
public struct NumberSequenceKeypoint
public class NumberSequenceKeypoint
{
public readonly float Time;
public readonly float Value;

View File

@ -1,38 +1,28 @@
using RobloxFiles.Enums;
using RobloxFiles.DataTypes.Utility;
using RobloxFiles.Utility;
namespace RobloxFiles.DataTypes
{
public struct PhysicalProperties
public class PhysicalProperties
{
public readonly float Density;
public readonly float Friction;
public readonly float Elasticity;
public readonly float Density = 1.0f;
public readonly float Friction = 1.0f;
public readonly float Elasticity = 0.5f;
public readonly float FrictionWeight;
public readonly float ElasticityWeight;
public readonly float FrictionWeight = 1.0f;
public readonly float ElasticityWeight = 1.0f;
public PhysicalProperties(Material material)
{
if (MaterialInfo.FrictionWeightMap.ContainsKey(material))
FrictionWeight = MaterialInfo.FrictionWeightMap[material];
Density = MaterialInfo.DensityMap[material];
Friction = MaterialInfo.FrictionMap[material];
Elasticity = MaterialInfo.ElasticityMap[material];
FrictionWeight = 1;
ElasticityWeight = 1;
}
public PhysicalProperties(float density, float friction, float elasticity)
{
Density = density;
Friction = friction;
Elasticity = elasticity;
FrictionWeight = 1;
ElasticityWeight = 1;
}
public PhysicalProperties(float density, float friction, float elasticity, float frictionWeight, float elasticityWeight)
public PhysicalProperties(float density, float friction, float elasticity, float frictionWeight = 1f, float elasticityWeight = 1f)
{
Density = density;
Friction = friction;

View File

@ -1,6 +1,6 @@
namespace RobloxFiles.DataTypes
{
public struct Ray
public class Ray
{
public readonly Vector3 Origin;
public readonly Vector3 Direction;
@ -33,15 +33,19 @@
public Vector3 ClosestPoint(Vector3 point)
{
Vector3 offset = point - Origin;
float diff = offset.Dot(Direction) / Direction.Dot(Direction);
return Origin + (diff * Direction);
Vector3 result = Origin;
float t = Direction.Dot(point - result);
if (t >= 0)
result += (Direction * t);
return result;
}
public float Distance(Vector3 point)
{
Vector3 projected = ClosestPoint(point);
return (point - projected).Magnitude;
Vector3 closestPoint = ClosestPoint(point);
return (closestPoint - point).Magnitude;
}
}
}

View File

@ -1,6 +1,6 @@
namespace RobloxFiles.DataTypes
{
public struct Rect
public class Rect
{
public readonly Vector2 Min;
public readonly Vector2 Max;
@ -8,7 +8,7 @@
public float Width => (Max - Min).X;
public float Height => (Max - Min).Y;
public Rect(Vector2? min, Vector2? max)
public Rect(Vector2 min = null, Vector2 max = null)
{
Min = min ?? Vector2.Zero;
Max = max ?? Vector2.Zero;

View File

@ -2,7 +2,7 @@
namespace RobloxFiles.DataTypes
{
public struct Region3
public class Region3
{
public readonly CFrame CFrame;
public readonly Vector3 Size;

View File

@ -10,7 +10,7 @@ namespace RobloxFiles.DataTypes
{
public readonly Vector3int16 Min, Max;
public Region3int16(Vector3int16? min, Vector3int16? max)
public Region3int16(Vector3int16 min = null, Vector3int16 max = null)
{
Min = min ?? new Vector3int16();
Max = max ?? new Vector3int16();

View File

@ -1,6 +1,6 @@
namespace RobloxFiles.DataTypes
{
public struct UDim
public class UDim
{
public readonly float Scale;
public readonly int Offset;

View File

@ -1,13 +1,13 @@
namespace RobloxFiles.DataTypes
{
public struct UDim2
public class UDim2
{
public readonly UDim X, Y;
public UDim Width => X;
public UDim Height => Y;
public UDim2(float scaleX, int offsetX, float scaleY, int offsetY)
public UDim2(float scaleX = 0, int offsetX = 0, float scaleY = 0, int offsetY = 0)
{
X = new UDim(scaleX, offsetX);
Y = new UDim(scaleY, offsetY);

View File

@ -2,7 +2,7 @@
namespace RobloxFiles.DataTypes
{
public struct Vector2
public class Vector2
{
public readonly float X, Y;

View File

@ -3,7 +3,7 @@ using RobloxFiles.Enums;
namespace RobloxFiles.DataTypes
{
public struct Vector3
public class Vector3
{
public readonly float X, Y, Z;

View File

@ -2,10 +2,17 @@
namespace RobloxFiles.DataTypes
{
public struct Vector3int16
public class Vector3int16
{
public readonly short X, Y, Z;
public Vector3int16()
{
X = 0;
Y = 0;
Z = 0;
}
public Vector3int16(short x = 0, short y = 0, short z = 0)
{
X = x;

View File

@ -5,12 +5,11 @@ using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("RobloxFiles")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTitle("Roblox File Format")]
[assembly: AssemblyDescription("Implementation of Roblox's File Format in C# for .NET 4.0")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RobloxFiles")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyCompany("Written by CloneTrooper1019")]
[assembly: AssemblyProduct("Roblox File Format")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

View File

@ -42,7 +42,7 @@
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<WarningLevel>1</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
@ -86,7 +86,7 @@
<Compile Include="DataTypes\PhysicalProperties.cs" />
<Compile Include="DataTypes\Ray.cs" />
<Compile Include="DataTypes\Region3int16.cs" />
<Compile Include="DataTypes\Utility\BrickColors.cs" />
<Compile Include="Utility\BrickColors.cs" />
<Compile Include="DataTypes\Vector3int16.cs" />
<Compile Include="DataTypes\Rect.cs" />
<Compile Include="DataTypes\Region3.cs" />
@ -94,9 +94,8 @@
<Compile Include="DataTypes\UDim2.cs" />
<Compile Include="DataTypes\Vector2.cs" />
<Compile Include="DataTypes\Vector3.cs" />
<Compile Include="DataTypes\Utility\MaterialInfo.cs" />
<Compile Include="DataTypes\Utility\Quaternion.cs" />
<Compile Include="UnitTest.cs" />
<Compile Include="Utility\MaterialInfo.cs" />
<Compile Include="Utility\Quaternion.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="XmlFormat\XmlPropertyTokens.cs" />
<Compile Include="XmlFormat\XmlDataReader.cs" />
@ -128,9 +127,6 @@
<Compile Include="XmlFormat\PropertyTokens\Vector2.cs" />
<Compile Include="XmlFormat\PropertyTokens\Vector3.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Packages.config" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.5.2">
<Visible>False</Visible>

View File

@ -1,19 +0,0 @@
#if DEBUG
using System.Diagnostics;
namespace RobloxFiles
{
// This is a placeholder.
internal class UnitTest
{
public static void Main(string[] args)
{
if (args.Length > 0)
{
RobloxFile file = new RobloxFile(args[0]);
Debugger.Break();
}
}
}
}
#endif

View File

@ -1,8 +1,13 @@
using System.Collections.Generic;
using RobloxFiles.DataTypes;
namespace RobloxFiles.DataTypes.Utility
namespace RobloxFiles.Utility
{
/// <summary>
/// This static class defines all of Roblox's built-in BrickColor data.
/// It is used primarily by the BrickColor DataType.
/// </summary>
public static class BrickColors
{
/// <summary>
@ -33,10 +38,10 @@ namespace RobloxFiles.DataTypes.Utility
};
/// <summary>
/// This contains a list of all available BrickColors on Roblox.
/// This contains a list of all defined BrickColors on Roblox.
/// </summary>
public static IReadOnlyCollection<BrickColor> ColorMap = new List<BrickColor>()
public static IReadOnlyList<BrickColor> ColorMap = new List<BrickColor>()
{
new BrickColor( 1, 0xF2F3F3, "White"),
new BrickColor( 2, 0xA1A5A2, "Grey"),

View File

@ -1,10 +1,17 @@
using System.Collections.Generic;
using RobloxFiles.Enums;
namespace RobloxFiles.DataTypes.Utility
namespace RobloxFiles.Utility
{
/// <summary>
/// This class defines several dictionaries of metadata for Roblox materials.
/// It is primarily used for the PhysicalProperties DataType.
/// </summary>
public static class MaterialInfo
{
/// <summary>
/// A dictionary mapping materials to their default Density.
/// </summary>
public static IReadOnlyDictionary<Material, float> DensityMap = new Dictionary<Material, float>()
{
{Material.Air, 0.01f},
@ -45,6 +52,9 @@ namespace RobloxFiles.DataTypes.Utility
{Material.WoodPlanks, 0.35f},
};
/// <summary>
/// A dictionary mapping materials to their default Elasticity.
/// </summary>
public static IReadOnlyDictionary<Material, float> ElasticityMap = new Dictionary<Material, float>()
{
{Material.Air, 0.01f},
@ -85,6 +95,9 @@ namespace RobloxFiles.DataTypes.Utility
{Material.WoodPlanks, 0.20f},
};
/// <summary>
/// A dictionary mapping materials to their default Friction.
/// </summary>
public static IReadOnlyDictionary<Material, float> FrictionMap = new Dictionary<Material, float>()
{
{Material.Air, 0.01f},
@ -124,5 +137,20 @@ namespace RobloxFiles.DataTypes.Utility
{Material.Wood, 0.48f},
{Material.WoodPlanks, 0.48f},
};
/// <summary>
/// A dictionary mapping materials to their default Friction.<para/>
/// NOTE: This only maps materials that have different FrictionWeights. If it isn't in here, assume their FrictionWeight is 1.
/// </summary>
public static IReadOnlyDictionary<Material, float> FrictionWeightMap = new Dictionary<Material, float>()
{
{Material.Asphalt, 0.30f},
{Material.Basalt, 0.30f},
{Material.Brick, 0.30f},
{Material.Concrete, 0.30f},
{Material.Ice, 3.00f},
{Material.Sand, 5.00f},
{Material.Sandstone, 5.00f},
};
}
}

View File

@ -2,12 +2,13 @@
namespace RobloxFiles.DataTypes.Utility
{
/// <summary>
/// Quaternion is a utility used by the CFrame DataType to handle rotation interpolation.
/// It can be used as an independent Quaternion implementation if you so please!
/// </summary>
public class Quaternion
{
public readonly float X;
public readonly float Y;
public readonly float Z;
public readonly float W;
public readonly float X, Y, Z, W;
public float Magnitude
{
@ -41,8 +42,7 @@ namespace RobloxFiles.DataTypes.Utility
CFrame matrix = (cf - cf.Position);
float[] ac = cf.GetComponents();
float m41 = ac[0], m42 = ac[1], m43 = ac[2],
m11 = ac[3], m12 = ac[4], m13 = ac[5],
float m11 = ac[3], m12 = ac[4], m13 = ac[5],
m21 = ac[6], m22 = ac[7], m23 = ac[8],
m31 = ac[9], m32 = ac[10], m33 = ac[11];

View File

@ -20,7 +20,7 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
uint g = (value >> 8) & 0xFF;
uint b = value & 0xFF;
prop.Value = Color3.fromRGB(r, g, b);
prop.Value = Color3.FromRGB(r, g, b);
}
return success;

View File

@ -10,31 +10,30 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public bool ReadToken(Property prop, XmlNode token)
{
Vector3[] read = new Vector3[Fields.Length];
for (int i = 0; i < read.Length; i++)
try
{
string field = Fields[i];
try
Vector3[] read = new Vector3[Fields.Length];
for (int i = 0; i < read.Length; i++)
{
string field = Fields[i];
var fieldToken = token[field];
Vector3? vector3 = Vector3Token.ReadVector3(fieldToken);
read[i] = vector3.Value;
}
catch
{
return false;
read[i] = Vector3Token.ReadVector3(fieldToken);
}
Vector3 origin = read[0],
direction = read[1];
Ray ray = new Ray(origin, direction);
prop.Type = PropertyType.Ray;
prop.Value = ray;
return true;
}
catch
{
return false;
}
Vector3 origin = read[0],
direction = read[1];
Ray ray = new Ray(origin, direction);
prop.Type = PropertyType.Ray;
prop.Value = ray;
return true;
}
}
}

View File

@ -10,31 +10,30 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public bool ReadToken(Property prop, XmlNode token)
{
Vector2[] read = new Vector2[Fields.Length];
for (int i = 0; i < read.Length; i++)
try
{
string field = Fields[i];
try
Vector2[] read = new Vector2[Fields.Length];
for (int i = 0; i < read.Length; i++)
{
string field = Fields[i];
var fieldToken = token[field];
Vector2? vector2 = Vector2Token.ReadVector2(fieldToken);
read[i] = vector2.Value;
}
catch
{
return false;
read[i] = Vector2Token.ReadVector2(fieldToken);
}
Vector2 min = read[0],
max = read[1];
Rect rect = new Rect(min, max);
prop.Type = PropertyType.Rect;
prop.Value = rect;
return true;
}
catch
{
return false;
}
Vector2 min = read[0],
max = read[1];
Rect rect = new Rect(min, max);
prop.Type = PropertyType.Rect;
prop.Value = rect;
return true;
}
}
}

View File

@ -7,7 +7,7 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
{
public string Token => "UDim";
public static UDim? ReadUDim(XmlNode token, string prefix = "")
public static UDim ReadUDim(XmlNode token, string prefix = "")
{
try
{
@ -27,13 +27,13 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public bool ReadToken(Property property, XmlNode token)
{
UDim? result = ReadUDim(token);
bool success = result.HasValue;
UDim result = ReadUDim(token);
bool success = (result != null);
if (success)
{
property.Type = PropertyType.UDim;
property.Value = result.Value;
property.Value = result;
}
return success;

View File

@ -9,13 +9,13 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public bool ReadToken(Property property, XmlNode token)
{
UDim? xDim = UDimToken.ReadUDim(token, "X");
UDim? yDim = UDimToken.ReadUDim(token, "Y");
UDim xUDim = UDimToken.ReadUDim(token, "X");
UDim yUDim = UDimToken.ReadUDim(token, "Y");
if (xDim != null && yDim != null)
if (xUDim != null && yUDim != null)
{
property.Type = PropertyType.UDim2;
property.Value = new UDim2(xDim.Value, yDim.Value);
property.Value = new UDim2(xUDim, yUDim);
return true;
}

View File

@ -8,7 +8,7 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public string Token => "Vector2";
private static string[] Coords = new string[2] { "X", "Y" };
public static Vector2? ReadVector2(XmlNode token)
public static Vector2 ReadVector2(XmlNode token)
{
float[] xy = new float[2];
@ -33,13 +33,13 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public bool ReadToken(Property property, XmlNode token)
{
Vector2? result = ReadVector2(token);
bool success = result.HasValue;
Vector2 result = ReadVector2(token);
bool success = (result != null);
if (success)
{
property.Type = PropertyType.Vector2;
property.Value = result.Value;
property.Value = result;
}
return success;

View File

@ -8,7 +8,7 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public string Token => "Vector3";
private static string[] Coords = new string[3] { "X", "Y", "Z" };
public static Vector3? ReadVector3(XmlNode token)
public static Vector3 ReadVector3(XmlNode token)
{
float[] xyz = new float[3];
@ -32,13 +32,13 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public bool ReadToken(Property property, XmlNode token)
{
Vector3? result = ReadVector3(token);
bool success = result.HasValue;
Vector3 result = ReadVector3(token);
bool success = (result != null);
if (success)
{
property.Type = PropertyType.Vector3;
property.Value = result.Value;
property.Value = result;
}
return success;

View File

@ -26,7 +26,7 @@ namespace RobloxFiles.XmlFormat
}
catch
{
throw new Exception("XmlRobloxFile: Could not read XML!");
throw new Exception("XmlRobloxFile: Could not read provided buffer as XML!");
}
XmlNode roblox = Root.FirstChild;

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="lz4net" version="1.0.15.93" targetFramework="net452" />
</packages>