From 23c222338ad3f17ab33ed1450cc93b36e0af03fd Mon Sep 17 00:00:00 2001 From: CloneTrooper1019 Date: Sun, 3 Feb 2019 07:28:54 -0600 Subject: [PATCH] Added `this[]` overloads to Instance & RobloxFile. --- Core/Instance.cs | 28 +++++++++++++++++++ Core/RobloxFile.cs | 9 ++++++ DataTypes/CFrame.cs | 2 +- .../PropertyTokens/PhysicalProperties.cs | 4 +-- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Core/Instance.cs b/Core/Instance.cs index f03cdc2..3210d60 100644 --- a/Core/Instance.cs +++ b/Core/Instance.cs @@ -8,6 +8,7 @@ namespace RobloxFiles /// Describes an object in Roblox's DataModel hierarchy. /// Instances can have sets of properties loaded from *.rbxl/*.rbxm files. /// + public class Instance { /// The ClassName of this Instance. @@ -239,5 +240,32 @@ namespace RobloxFiles { Properties.Add(prop); } + + /// + /// Treats the provided string as if you were indexing a specific child or descendant of this Instance. + /// The provided string can either be: + /// - The name of a child that is parented to this Instance. ( Example: game["Workspace"] ) + /// - A period-separated path to a descendant of this Instance. ( Example: game["Workspace.Terrain"] ) + /// This will throw an exception if any instance in the traversal is not found. + /// + public Instance this[string accessor] + { + get + { + Instance result = this; + + foreach (string name in accessor.Split('.')) + { + Instance next = result.FindFirstChild(name); + + if (next == null) + throw new Exception(name + " is not a valid member of " + result.Name); + + result = next; + } + + return result; + } + } } } \ No newline at end of file diff --git a/Core/RobloxFile.cs b/Core/RobloxFile.cs index 0b866bc..589b2f0 100644 --- a/Core/RobloxFile.cs +++ b/Core/RobloxFile.cs @@ -78,5 +78,14 @@ namespace RobloxFiles byte[] buffer = File.ReadAllBytes(filePath); ReadFile(buffer); } + + /// + /// Treats the provided string as if you were indexing a specific child or descendant of the `RobloxFile.Contents` folder. + /// The provided string can either be: + /// - The name of a child that is parented to RobloxFile.Contents ( Example: RobloxFile["Workspace"] ) + /// - A period (.) separated path to a descendant of RobloxFile.Contents ( Example: RobloxFile["Workspace.Terrain"] ) + /// This will throw an exception if any instance in the traversal is not found. + /// + public Instance this[string accessor] => Contents[accessor]; } } diff --git a/DataTypes/CFrame.cs b/DataTypes/CFrame.cs index 16cdfca..499a5e3 100644 --- a/DataTypes/CFrame.cs +++ b/DataTypes/CFrame.cs @@ -327,7 +327,7 @@ namespace RobloxFiles.DataTypes return new float[] { m14, m24, m34, m11, m12, m13, m21, m22, m23, m31, m32, m33 }; } - public float[] toEulerAnglesXYZ() + public float[] ToEulerAnglesXYZ() { float x = (float)Math.Atan2(-m23, m33); float y = (float)Math.Asin(m13); diff --git a/XmlFormat/PropertyTokens/PhysicalProperties.cs b/XmlFormat/PropertyTokens/PhysicalProperties.cs index d992886..05fac4f 100644 --- a/XmlFormat/PropertyTokens/PhysicalProperties.cs +++ b/XmlFormat/PropertyTokens/PhysicalProperties.cs @@ -25,6 +25,8 @@ namespace RobloxFiles.XmlFormat.PropertyTokens try { bool custom = readBool("CustomPhysics"); + prop.Type = PropertyType.PhysicalProperties; + if (custom) { prop.Value = new PhysicalProperties @@ -35,8 +37,6 @@ namespace RobloxFiles.XmlFormat.PropertyTokens readFloat("FrictionWeight"), readFloat("ElasticityWeight") ); - - prop.Type = PropertyType.PhysicalProperties; } return true;