Optimizations and memory leak fixes

This commit is contained in:
CloneTrooper1019 2020-08-17 00:33:59 -05:00
parent 0ca6738cb9
commit 297426bdb5
20 changed files with 134 additions and 125 deletions

View File

@ -26,12 +26,6 @@ namespace RobloxFiles.BinaryFormat
public bool HasWriteBuffer { get; private set; } public bool HasWriteBuffer { get; private set; }
public byte[] WriteBuffer { get; private set; } public byte[] WriteBuffer { get; private set; }
public BinaryRobloxFileReader GetDataReader(BinaryRobloxFile file)
{
MemoryStream buffer = new MemoryStream(Data);
return new BinaryRobloxFileReader(file, buffer);
}
public override string ToString() public override string ToString()
{ {
string chunkType = ChunkType.Replace('\0', ' '); string chunkType = ChunkType.Replace('\0', ' ');
@ -81,7 +75,7 @@ namespace RobloxFiles.BinaryFormat
if (!compress || CompressedSize > Size) if (!compress || CompressedSize > Size)
{ {
CompressedSize = 0; CompressedSize = 0;
CompressedData = new byte[0]; CompressedData = Array.Empty<byte>();
} }
ChunkType = writer.ChunkType; ChunkType = writer.ChunkType;

View File

@ -28,9 +28,9 @@ namespace RobloxFiles
public Instance[] Instances { get; internal set; } public Instance[] Instances { get; internal set; }
public INST[] Classes { get; internal set; } public INST[] Classes { get; internal set; }
internal META META = null; internal META META;
internal SSTR SSTR = null; internal SSTR SSTR;
internal SIGN SIGN = null; internal SIGN SIGN;
public bool HasMetadata => (META != null); public bool HasMetadata => (META != null);
public Dictionary<string, string> Metadata => META?.Data; public Dictionary<string, string> Metadata => META?.Data;
@ -104,17 +104,21 @@ namespace RobloxFiles
reading = false; reading = false;
break; break;
case string unhandled: case string unhandled:
Console.WriteLine("BinaryRobloxFile - Unhandled chunk-type: {0}!", unhandled); Console.Error.WriteLine("BinaryRobloxFile - Unhandled chunk-type: {0}!", unhandled);
break; break;
default: break; default: break;
} }
if (handler != null) if (handler != null)
{
using (var readBuffer = new MemoryStream(chunk.Data))
{
using (var dataReader = new BinaryRobloxFileReader(this, readBuffer))
{ {
chunk.Handler = handler; chunk.Handler = handler;
using (var dataReader = chunk.GetDataReader(this))
handler.Load(dataReader); handler.Load(dataReader);
}
}
ChunksImpl.Add(chunk); ChunksImpl.Add(chunk);
} }
@ -133,7 +137,8 @@ namespace RobloxFiles
// Generate the chunk data. // Generate the chunk data.
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
using (var writer = new BinaryRobloxFileWriter(this)) using (var workBuffer = new MemoryStream())
using (var writer = new BinaryRobloxFileWriter(this, workBuffer))
{ {
// Clear the existing data. // Clear the existing data.
Referent = "-1"; Referent = "-1";

View File

@ -10,7 +10,7 @@ namespace RobloxFiles.BinaryFormat
public class BinaryRobloxFileReader : BinaryReader public class BinaryRobloxFileReader : BinaryReader
{ {
public readonly BinaryRobloxFile File; public readonly BinaryRobloxFile File;
private byte[] lastStringBuffer = new byte[0] { }; private byte[] lastStringBuffer = Array.Empty<byte>();
public BinaryRobloxFileReader(BinaryRobloxFile file, Stream stream) : base(stream) public BinaryRobloxFileReader(BinaryRobloxFile file, Stream stream) : base(stream)
{ {

View File

@ -28,7 +28,7 @@ namespace RobloxFiles.BinaryFormat
// Instances in child->parent order // Instances in child->parent order
internal List<Instance> PostInstances { get; private set; } internal List<Instance> PostInstances { get; private set; }
public BinaryRobloxFileWriter(BinaryRobloxFile file, Stream workBuffer = null) : base(workBuffer ?? new MemoryStream()) public BinaryRobloxFileWriter(BinaryRobloxFile file, Stream workBuffer) : base(workBuffer)
{ {
File = file; File = file;
@ -167,14 +167,13 @@ namespace RobloxFiles.BinaryFormat
if (!instance.Archivable) if (!instance.Archivable)
continue; continue;
int instId = (int)(File.NumInstances++); int instId = (int)File.NumInstances++;
instance.Referent = instId.ToString(); string className = instance.ClassName;
instance.Referent = instId.ToInvariantString();
Instances.Add(instance); Instances.Add(instance);
string className = instance.ClassName; if (!ClassMap.TryGetValue(className, out INST inst))
INST inst;
if (!ClassMap.TryGetValue(className, out inst))
{ {
inst = new INST() inst = new INST()
{ {

View File

@ -53,7 +53,7 @@ namespace RobloxFiles.DataTypes
BrickColor result = null; BrickColor result = null;
var query = BrickColors.ColorMap.Where((bc) => bc.Name == name); var query = BrickColors.ColorMap.Where((bc) => bc.Name == name);
if (query.Count() > 0) if (query.Any())
result = query.First(); result = query.First();
else else
result = FromName(DefaultName); result = FromName(DefaultName);

View File

@ -1,13 +1,15 @@
using System; using System;
using System.Diagnostics.Contracts;
using RobloxFiles.Enums; using RobloxFiles.Enums;
namespace RobloxFiles.DataTypes namespace RobloxFiles.DataTypes
{ {
public class CFrame public class CFrame
{ {
private float m11 = 1, m12 = 0, m13 = 0, m14 = 0; private float m11 = 1, m12, m13, m14;
private float m21 = 0, m22 = 1, m23 = 0, m24 = 0; private float m21, m22 = 1, m23, m24;
private float m31 = 0, m32 = 0, m33 = 1, m34 = 0; private float m31, m32, m33 = 1, m34;
private const float m41 = 0, m42 = 0, m43 = 0, m44 = 1; private const float m41 = 0, m42 = 0, m43 = 0, m44 = 1;
public float X => m14; public float X => m14;
@ -16,21 +18,21 @@ namespace RobloxFiles.DataTypes
public Vector3 Position public Vector3 Position
{ {
get get => new Vector3(X, Y, Z);
{
return new Vector3(X, Y, Z);
}
set set
{ {
Contract.Requires(value != null);
m14 = value.X; m14 = value.X;
m24 = value.Y; m24 = value.Y;
m34 = value.Z; m34 = value.Z;
} }
} }
public Vector3 RightVector => new Vector3( m11, m12, m13);
public Vector3 UpVector => new Vector3( m21, m22, m23); public Vector3 UpVector => new Vector3( m21, m22, m23);
public Vector3 LookVector => new Vector3(-m31, -m32, -m33); public Vector3 LookVector => new Vector3(-m31, -m32, -m33);
public Vector3 RightVector => new Vector3(m11, m12, m13);
public CFrame() public CFrame()
{ {
@ -41,6 +43,8 @@ namespace RobloxFiles.DataTypes
public CFrame(Vector3 pos) public CFrame(Vector3 pos)
{ {
Contract.Requires(pos != null);
m14 = pos.X; m14 = pos.X;
m24 = pos.Y; m24 = pos.Y;
m34 = pos.Z; m34 = pos.Z;
@ -113,8 +117,7 @@ namespace RobloxFiles.DataTypes
public CFrame(params float[] comp) public CFrame(params float[] comp)
{ {
if (comp.Length < 12) Contract.Requires(comp.Length >= 12, "There should be 12 floats provided to construct a CFrame with an array of floats");
throw new Exception("There should be 12 floats provided to construct CFrame with an array of floats");
m14 = comp[0]; m24 = comp[1]; m34 = comp[2]; m14 = comp[0]; m24 = comp[1]; m34 = comp[2];
m11 = comp[3]; m12 = comp[4]; m13 = comp[5]; m11 = comp[3]; m12 = comp[4]; m13 = comp[5];
@ -135,6 +138,7 @@ namespace RobloxFiles.DataTypes
public CFrame(Vector3 pos, Vector3 vX, Vector3 vY, Vector3 vZ = null) public CFrame(Vector3 pos, Vector3 vX, Vector3 vY, Vector3 vZ = null)
{ {
Contract.Requires(pos != null && vX != null && vY != null);
initFromMatrix(pos, vX, vY, vZ); initFromMatrix(pos, vX, vY, vZ);
} }
@ -192,16 +196,22 @@ namespace RobloxFiles.DataTypes
public static Vector3 operator *(CFrame a, Vector3 b) public static Vector3 operator *(CFrame a, Vector3 b)
{ {
if (a == null)
throw new ArgumentNullException(nameof(a));
else if (b == null)
throw new ArgumentNullException(nameof(b));
float[] ac = a.GetComponents(); float[] ac = a.GetComponents();
float x = ac[0], y = ac[1], z = ac[2], float m11 = ac[3], m12 = ac[4], m13 = ac[5],
m11 = ac[3], m12 = ac[4], m13 = ac[5],
m21 = ac[6], m22 = ac[7], m23 = ac[8], m21 = ac[6], m22 = ac[7], m23 = ac[8],
m31 = ac[9], m32 = ac[10], m33 = ac[11]; m31 = ac[9], m32 = ac[10], m33 = ac[11];
Vector3 right = new Vector3(m11, m21, m31);
Vector3 up = new Vector3(m12, m22, m32); var up = new Vector3(m12, m22, m32);
Vector3 back = new Vector3(m13, m23, m33); var back = new Vector3(m13, m23, m33);
var right = new Vector3(m11, m21, m31);
return a.Position + b.X * right + b.Y * up + b.Z * back; return a.Position + b.X * right + b.Y * up + b.Z * back;
} }
@ -235,11 +245,6 @@ namespace RobloxFiles.DataTypes
float n33 = a31 * b13 + a32 * b23 + a33 * b33 + a34 * m43; float n33 = a31 * b13 + a32 * b23 + a33 * b33 + a34 * m43;
float n34 = a31 * b14 + a32 * b24 + a33 * b34 + a34 * m44; float n34 = a31 * b14 + a32 * b24 + a33 * b34 + a34 * m44;
float n41 = m41 * b11 + m42 * b21 + m43 * b31 + m44 * m41;
float n42 = m41 * b12 + m42 * b22 + m43 * b32 + m44 * m42;
float n43 = m41 * b13 + m42 * b23 + m43 * b33 + m44 * m43;
float n44 = m41 * b14 + m42 * b24 + m43 * b34 + m44 * m44;
return new CFrame(n14, n24, n34, n11, n12, n13, n21, n22, n23, n31, n32, n33); return new CFrame(n14, n24, n34, n11, n12, n13, n21, n22, n23, n31, n32, n33);
} }
@ -295,11 +300,6 @@ namespace RobloxFiles.DataTypes
float b33 = (a11 * a22 * m44 + a12 * a24 * m41 + a14 * a21 * m42 - a11 * a24 * m42 - a12 * a21 * m44 - a14 * a22 * m41) / det; float b33 = (a11 * a22 * m44 + a12 * a24 * m41 + a14 * a21 * m42 - a11 * a24 * m42 - a12 * a21 * m44 - a14 * a22 * m41) / det;
float b34 = (a11 * a24 * a32 + a12 * a21 * a34 + a14 * a22 * a31 - a11 * a22 * a34 - a12 * a24 * a31 - a14 * a21 * a32) / det; float b34 = (a11 * a24 * a32 + a12 * a21 * a34 + a14 * a22 * a31 - a11 * a22 * a34 - a12 * a24 * a31 - a14 * a21 * a32) / det;
float b41 = (a21 * a33 * m42 + a22 * a31 * m43 + a23 * a32 * m41 - a21 * a32 * m43 - a22 * a33 * m41 - a23 * a31 * m42) / det;
float b42 = (a11 * a32 * m43 + a12 * a33 * m41 + a13 * a31 * m42 - a11 * a33 * m42 - a12 * a31 * m43 - a13 * a32 * m41) / det;
float b43 = (a11 * a23 * m42 + a12 * a21 * m43 + a13 * a22 * m41 - a11 * a22 * m43 - a12 * a23 * m41 - a13 * a21 * m42) / det;
float b44 = (a11 * a22 * a33 + a12 * a23 * a31 + a13 * a21 * a32 - a11 * a23 * a32 - a12 * a21 * a33 - a13 * a22 * a31) / det;
return new CFrame(b14, b24, b34, b11, b12, b13, b21, b22, b23, b31, b32, b33); return new CFrame(b14, b24, b34, b11, b12, b13, b21, b22, b23, b31, b32, b33);
} }

View File

@ -1,10 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\RCT-Source\packages\Microsoft.CodeAnalysis.FxCopAnalyzers.3.3.0\build\Microsoft.CodeAnalysis.FxCopAnalyzers.props" Condition="Exists('..\RCT-Source\packages\Microsoft.CodeAnalysis.FxCopAnalyzers.3.3.0\build\Microsoft.CodeAnalysis.FxCopAnalyzers.props')" />
<Import Project="..\RCT-Source\packages\Microsoft.NetFramework.Analyzers.3.3.0\build\Microsoft.NetFramework.Analyzers.props" Condition="Exists('..\RCT-Source\packages\Microsoft.NetFramework.Analyzers.3.3.0\build\Microsoft.NetFramework.Analyzers.props')" />
<Import Project="..\RCT-Source\packages\Microsoft.NetCore.Analyzers.3.3.0\build\Microsoft.NetCore.Analyzers.props" Condition="Exists('..\RCT-Source\packages\Microsoft.NetCore.Analyzers.3.3.0\build\Microsoft.NetCore.Analyzers.props')" />
<Import Project="..\RCT-Source\packages\Microsoft.CodeQuality.Analyzers.3.3.0\build\Microsoft.CodeQuality.Analyzers.props" Condition="Exists('..\RCT-Source\packages\Microsoft.CodeQuality.Analyzers.3.3.0\build\Microsoft.CodeQuality.Analyzers.props')" />
<Import Project="..\RCT-Source\packages\Microsoft.CodeAnalysis.VersionCheckAnalyzer.3.3.0\build\Microsoft.CodeAnalysis.VersionCheckAnalyzer.props" Condition="Exists('..\RCT-Source\packages\Microsoft.CodeAnalysis.VersionCheckAnalyzer.3.3.0\build\Microsoft.CodeAnalysis.VersionCheckAnalyzer.props')" />
<Import Project="packages\Costura.Fody.4.1.0\build\Costura.Fody.props" Condition="Exists('packages\Costura.Fody.4.1.0\build\Costura.Fody.props')" /> <Import Project="packages\Costura.Fody.4.1.0\build\Costura.Fody.props" Condition="Exists('packages\Costura.Fody.4.1.0\build\Costura.Fody.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
@ -181,17 +176,6 @@
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Analyzer Include="..\RCT-Source\packages\Microsoft.CodeAnalysis.VersionCheckAnalyzer.3.3.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.VersionCheckAnalyzer.resources.dll" />
<Analyzer Include="..\RCT-Source\packages\Microsoft.CodeAnalysis.VersionCheckAnalyzer.3.3.0\analyzers\dotnet\Microsoft.CodeAnalysis.VersionCheckAnalyzer.dll" />
<Analyzer Include="..\RCT-Source\packages\Microsoft.CodeQuality.Analyzers.3.3.0\analyzers\dotnet\cs\Humanizer.dll" />
<Analyzer Include="..\RCT-Source\packages\Microsoft.CodeQuality.Analyzers.3.3.0\analyzers\dotnet\cs\Microsoft.CodeQuality.Analyzers.dll" />
<Analyzer Include="..\RCT-Source\packages\Microsoft.CodeQuality.Analyzers.3.3.0\analyzers\dotnet\cs\Microsoft.CodeQuality.CSharp.Analyzers.dll" />
<Analyzer Include="..\RCT-Source\packages\Microsoft.NetCore.Analyzers.3.3.0\analyzers\dotnet\cs\Microsoft.NetCore.Analyzers.dll" />
<Analyzer Include="..\RCT-Source\packages\Microsoft.NetCore.Analyzers.3.3.0\analyzers\dotnet\cs\Microsoft.NetCore.CSharp.Analyzers.dll" />
<Analyzer Include="..\RCT-Source\packages\Microsoft.NetFramework.Analyzers.3.3.0\analyzers\dotnet\cs\Microsoft.NetFramework.Analyzers.dll" />
<Analyzer Include="..\RCT-Source\packages\Microsoft.NetFramework.Analyzers.3.3.0\analyzers\dotnet\cs\Microsoft.NetFramework.CSharp.Analyzers.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup> <PropertyGroup>
<PostBuildEvent>copy /y $(TargetPath) $(ProjectDir)$(TargetFileName)</PostBuildEvent> <PostBuildEvent>copy /y $(TargetPath) $(ProjectDir)$(TargetFileName)</PostBuildEvent>
@ -202,11 +186,6 @@
</PropertyGroup> </PropertyGroup>
<Error Condition="!Exists('packages\Costura.Fody.4.1.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.4.1.0\build\Costura.Fody.props'))" /> <Error Condition="!Exists('packages\Costura.Fody.4.1.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.4.1.0\build\Costura.Fody.props'))" />
<Error Condition="!Exists('packages\Fody.6.2.0\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Fody.6.2.0\build\Fody.targets'))" /> <Error Condition="!Exists('packages\Fody.6.2.0\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Fody.6.2.0\build\Fody.targets'))" />
<Error Condition="!Exists('..\RCT-Source\packages\Microsoft.CodeAnalysis.VersionCheckAnalyzer.3.3.0\build\Microsoft.CodeAnalysis.VersionCheckAnalyzer.props')" Text="$([System.String]::Format('$(ErrorText)', '..\RCT-Source\packages\Microsoft.CodeAnalysis.VersionCheckAnalyzer.3.3.0\build\Microsoft.CodeAnalysis.VersionCheckAnalyzer.props'))" />
<Error Condition="!Exists('..\RCT-Source\packages\Microsoft.CodeQuality.Analyzers.3.3.0\build\Microsoft.CodeQuality.Analyzers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\RCT-Source\packages\Microsoft.CodeQuality.Analyzers.3.3.0\build\Microsoft.CodeQuality.Analyzers.props'))" />
<Error Condition="!Exists('..\RCT-Source\packages\Microsoft.NetCore.Analyzers.3.3.0\build\Microsoft.NetCore.Analyzers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\RCT-Source\packages\Microsoft.NetCore.Analyzers.3.3.0\build\Microsoft.NetCore.Analyzers.props'))" />
<Error Condition="!Exists('..\RCT-Source\packages\Microsoft.NetFramework.Analyzers.3.3.0\build\Microsoft.NetFramework.Analyzers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\RCT-Source\packages\Microsoft.NetFramework.Analyzers.3.3.0\build\Microsoft.NetFramework.Analyzers.props'))" />
<Error Condition="!Exists('..\RCT-Source\packages\Microsoft.CodeAnalysis.FxCopAnalyzers.3.3.0\build\Microsoft.CodeAnalysis.FxCopAnalyzers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\RCT-Source\packages\Microsoft.CodeAnalysis.FxCopAnalyzers.3.3.0\build\Microsoft.CodeAnalysis.FxCopAnalyzers.props'))" />
</Target> </Target>
<Import Project="packages\Fody.6.2.0\build\Fody.targets" Condition="Exists('packages\Fody.6.2.0\build\Fody.targets')" /> <Import Project="packages\Fody.6.2.0\build\Fody.targets" Condition="Exists('packages\Fody.6.2.0\build\Fody.targets')" />
</Project> </Project>

Binary file not shown.

View File

@ -41,7 +41,7 @@ namespace RobloxFiles
Region3int16, Region3int16,
} }
public class Attribute public class Attribute : IDisposable
{ {
public AttributeType DataType { get; private set; } public AttributeType DataType { get; private set; }
public object Value { get; private set; } public object Value { get; private set; }
@ -203,6 +203,11 @@ namespace RobloxFiles
reader = null; reader = null;
} }
public void Dispose()
{
reader.Dispose();
}
internal Attribute(BinaryReader reader) internal Attribute(BinaryReader reader)
{ {
this.reader = reader; this.reader = reader;
@ -252,7 +257,7 @@ namespace RobloxFiles
internal byte[] Serialize() internal byte[] Serialize()
{ {
// TODO // TODO
return new byte[0]; return Array.Empty<byte>();
} }
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@ -66,7 +67,7 @@ namespace RobloxFiles
{ {
get get
{ {
return Attributes?.Serialize() ?? new byte[0]; return Attributes?.Serialize() ?? Array.Empty<byte>();
} }
set set
{ {
@ -136,6 +137,7 @@ namespace RobloxFiles
/// <param name="ancestor">The instance whose ancestry will be tested against this Instance.</param> /// <param name="ancestor">The instance whose ancestry will be tested against this Instance.</param>
public bool IsDescendantOf(Instance ancestor) public bool IsDescendantOf(Instance ancestor)
{ {
Contract.Requires(ancestor != null);
return ancestor.IsAncestorOf(this); return ancestor.IsAncestorOf(this);
} }
@ -265,7 +267,7 @@ namespace RobloxFiles
.Where(child => name == child.Name) .Where(child => name == child.Name)
.Cast<T>(); .Cast<T>();
if (query.Count() > 0) if (query.Any())
{ {
result = query.First(); result = query.First();
} }
@ -309,7 +311,7 @@ namespace RobloxFiles
while (ancestor != null) while (ancestor != null)
{ {
if (ancestor is T && ancestor.Name == name) if (ancestor is T && ancestor.Name == name)
return (T)ancestor; return ancestor as T;
ancestor = ancestor.Parent; ancestor = ancestor.Parent;
} }
@ -334,15 +336,12 @@ namespace RobloxFiles
/// <param name="name">The Name of the Instance to find.</param> /// <param name="name">The Name of the Instance to find.</param>
public T FindFirstAncestorOfClass<T>() where T : Instance public T FindFirstAncestorOfClass<T>() where T : Instance
{ {
Type classType = typeof(T);
string className = classType.Name;
Instance ancestor = Parent; Instance ancestor = Parent;
while (ancestor != null) while (ancestor != null)
{ {
if (ancestor is T) if (ancestor is T)
return (T)ancestor; return ancestor as T;
ancestor = ancestor.Parent; ancestor = ancestor.Parent;
} }
@ -387,7 +386,7 @@ namespace RobloxFiles
T result = null; T result = null;
if (query.Count() > 0) if (query.Any())
{ {
result = query.First(); result = query.First();
} }
@ -421,7 +420,7 @@ namespace RobloxFiles
T result = null; T result = null;
if (query.Count() > 0) if (query.Any())
{ {
result = query.First(); result = query.First();
} }

View File

@ -152,7 +152,7 @@ namespace RobloxFiles
{ {
FieldInfo directField = instType FieldInfo directField = instType
.GetFields() .GetFields()
.Where(field => field.Name.StartsWith(Name)) .Where(field => field.Name.StartsWith(Name, StringComparison.InvariantCulture))
.Where(field => field.DeclaringType == instType) .Where(field => field.DeclaringType == instType)
.FirstOrDefault(); .FirstOrDefault();

View File

@ -55,6 +55,26 @@ internal static class Formatting
} }
} }
public static string ToLowerInvariant(this string str)
{
return str.ToLower(Invariant);
}
public static string ToUpperInvariant(this string str)
{
return str.ToUpper(Invariant);
}
public static bool StartsWithInvariant(this string str, string other)
{
return str.StartsWith(other, StringComparison.InvariantCulture);
}
public static bool EndsWithInvariant(this string str, string other)
{
return str.EndsWith(other, StringComparison.InvariantCulture);
}
public static float ParseFloat(string value) public static float ParseFloat(string value)
{ {
switch (value) switch (value)

View File

@ -1,4 +1,6 @@
using System; using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Xml; using System.Xml;
@ -21,13 +23,15 @@ namespace RobloxFiles.XmlFormat
public static string CreateReferent() public static string CreateReferent()
{ {
Guid referentGuid = Guid.NewGuid(); var referentGuid = Guid
.NewGuid()
.ToString();
string referent = "RBX" + referentGuid string referent = "RBX" + referentGuid
.ToString() .ToUpper(CultureInfo.InvariantCulture)
.ToUpper(); .Replace("-", "");
return referent.Replace("-", ""); return referent;
} }
private static string GetEnumName<T>(T item) where T : struct private static string GetEnumName<T>(T item) where T : struct
@ -77,7 +81,7 @@ namespace RobloxFiles.XmlFormat
case PropertyType.Float: case PropertyType.Float:
case PropertyType.Int64: case PropertyType.Int64:
case PropertyType.Double: case PropertyType.Double:
propType = propType.ToLower(); propType = propType.ToLower(CultureInfo.InvariantCulture);
break; break;
case PropertyType.String: case PropertyType.String:
propType = (prop.HasRawBuffer ? "BinaryString" : "string"); propType = (prop.HasRawBuffer ? "BinaryString" : "string");
@ -166,6 +170,7 @@ namespace RobloxFiles.XmlFormat
public static XmlNode WriteSharedStrings(XmlDocument doc, XmlRobloxFile file) public static XmlNode WriteSharedStrings(XmlDocument doc, XmlRobloxFile file)
{ {
Contract.Requires(doc != null && file != null);
XmlElement sharedStrings = doc.CreateElement("SharedStrings"); XmlElement sharedStrings = doc.CreateElement("SharedStrings");
var binaryWriter = XmlPropertyTokens.GetHandler<BinaryStringToken>(); var binaryWriter = XmlPropertyTokens.GetHandler<BinaryStringToken>();

View File

@ -9,9 +9,7 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public bool ReadProperty(Property prop, XmlNode token) public bool ReadProperty(Property prop, XmlNode token)
{ {
uint value; if (XmlPropertyTokens.ReadPropertyGeneric(token, out uint value))
if (XmlPropertyTokens.ReadPropertyGeneric(token, out value))
{ {
Axes axes = (Axes)value; Axes axes = (Axes)value;
prop.Value = axes; prop.Value = axes;

View File

@ -13,9 +13,7 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public bool ReadProperty(Property prop, XmlNode token) public bool ReadProperty(Property prop, XmlNode token)
{ {
int value; if (XmlPropertyTokens.ReadPropertyGeneric(token, out int value))
if (XmlPropertyTokens.ReadPropertyGeneric(token, out value))
{ {
BrickColor brickColor = BrickColor.FromNumber(value); BrickColor brickColor = BrickColor.FromNumber(value);
prop.XmlToken = "BrickColor"; prop.XmlToken = "BrickColor";

View File

@ -1,5 +1,7 @@
using System.Xml; using System.Xml;
using RobloxFiles.DataTypes; using RobloxFiles.DataTypes;
using System.Diagnostics.Contracts;
using System;
namespace RobloxFiles.XmlFormat.PropertyTokens namespace RobloxFiles.XmlFormat.PropertyTokens
{ {
@ -9,9 +11,7 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public bool ReadProperty(Property prop, XmlNode token) public bool ReadProperty(Property prop, XmlNode token)
{ {
uint value; if (XmlPropertyTokens.ReadPropertyGeneric(token, out uint value))
if (XmlPropertyTokens.ReadPropertyGeneric(token, out value))
{ {
uint r = (value >> 16) & 0xFF; uint r = (value >> 16) & 0xFF;
uint g = (value >> 8) & 0xFF; uint g = (value >> 8) & 0xFF;
@ -28,14 +28,16 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node) public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
{ {
Color3uint8 color = prop.CastValue<Color3uint8>(); Color3uint8 color = prop?.CastValue<Color3uint8>();
Contract.Requires(node != null);
uint r = color.R, uint r = color.R,
g = color.G, g = color.G,
b = color.B; b = color.B;
uint rgb = (255u << 24) | (r << 16) | (g << 8) | b; uint rgb = (255u << 24) | (r << 16) | (g << 8) | b;
node.InnerText = rgb.ToString(); node.InnerText = rgb.ToInvariantString();
} }
} }
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics.Contracts;
using System.Reflection; using System.Reflection;
using System.Xml; using System.Xml;
@ -10,9 +11,9 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public bool ReadProperty(Property prop, XmlNode token) public bool ReadProperty(Property prop, XmlNode token)
{ {
uint value; Contract.Requires(prop != null);
if (XmlPropertyTokens.ReadPropertyGeneric(token, out value)) if (XmlPropertyTokens.ReadPropertyGeneric(token, out uint value))
{ {
Instance inst = prop.Instance; Instance inst = prop.Instance;
Type instType = inst?.GetType(); Type instType = inst?.GetType();
@ -36,13 +37,13 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node) public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
{ {
Contract.Requires(prop != null && node != null);
object rawValue = prop.Value; object rawValue = prop.Value;
Type valueType = rawValue.GetType();
int signed = (int)rawValue; int signed = (int)rawValue;
uint value = (uint)signed; uint value = (uint)signed;
node.InnerText = value.ToString(); node.InnerText = value.ToInvariantString();
} }
} }
} }

View File

@ -1,4 +1,5 @@
using System.Xml; using System.Diagnostics.Contracts;
using System.Xml;
using RobloxFiles.DataTypes; using RobloxFiles.DataTypes;
namespace RobloxFiles.XmlFormat.PropertyTokens namespace RobloxFiles.XmlFormat.PropertyTokens
@ -9,9 +10,9 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public bool ReadProperty(Property prop, XmlNode token) public bool ReadProperty(Property prop, XmlNode token)
{ {
uint value; Contract.Requires(prop != null);
if (XmlPropertyTokens.ReadPropertyGeneric(token, out value)) if (XmlPropertyTokens.ReadPropertyGeneric(token, out uint value))
{ {
Faces faces = (Faces)value; Faces faces = (Faces)value;
prop.Value = faces; prop.Value = faces;
@ -24,6 +25,8 @@ namespace RobloxFiles.XmlFormat.PropertyTokens
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node) public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
{ {
Contract.Requires(prop != null && doc != null && node != null);
XmlElement faces = doc.CreateElement("faces"); XmlElement faces = doc.CreateElement("faces");
node.AppendChild(faces); node.AppendChild(faces);

View File

@ -33,7 +33,14 @@ namespace RobloxFiles
try try
{ {
string xml = Encoding.UTF8.GetString(buffer); string xml = Encoding.UTF8.GetString(buffer);
XmlDocument.LoadXml(xml); var settings = new XmlReaderSettings() { XmlResolver = null };
using (StringReader reader = new StringReader(xml))
{
XmlReader xmlReader = XmlReader.Create(reader, settings);
XmlDocument.Load(xmlReader);
xmlReader.Dispose();
}
} }
catch catch
{ {
@ -46,9 +53,8 @@ namespace RobloxFiles
{ {
// Verify the version we are using. // Verify the version we are using.
XmlNode version = roblox.Attributes.GetNamedItem("version"); XmlNode version = roblox.Attributes.GetNamedItem("version");
int schemaVersion;
if (version == null || !int.TryParse(version.Value, out schemaVersion)) if (version == null || !int.TryParse(version.Value, out int schemaVersion))
throw new Exception("XmlRobloxFile: No version number defined!"); throw new Exception("XmlRobloxFile: No version number defined!");
else if (schemaVersion < 4) else if (schemaVersion < 4)
throw new Exception("XmlRobloxFile: Provided version must be at least 4!"); throw new Exception("XmlRobloxFile: Provided version must be at least 4!");

View File

@ -4,10 +4,5 @@
<package id="Fody" version="6.2.0" targetFramework="net472" developmentDependency="true" /> <package id="Fody" version="6.2.0" targetFramework="net472" developmentDependency="true" />
<package id="Konscious.Security.Cryptography.Blake2" version="1.0.9" targetFramework="net472" /> <package id="Konscious.Security.Cryptography.Blake2" version="1.0.9" targetFramework="net472" />
<package id="lz4net" version="1.0.15.93" targetFramework="net452" /> <package id="lz4net" version="1.0.15.93" targetFramework="net452" />
<package id="Microsoft.CodeAnalysis.FxCopAnalyzers" version="3.3.0" targetFramework="net472" developmentDependency="true" />
<package id="Microsoft.CodeAnalysis.VersionCheckAnalyzer" version="3.3.0" targetFramework="net472" developmentDependency="true" />
<package id="Microsoft.CodeQuality.Analyzers" version="3.3.0" targetFramework="net472" developmentDependency="true" />
<package id="Microsoft.NetCore.Analyzers" version="3.3.0" targetFramework="net472" developmentDependency="true" />
<package id="Microsoft.NetFramework.Analyzers" version="3.3.0" targetFramework="net472" developmentDependency="true" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" /> <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
</packages> </packages>