From 0a5bb6948662c23a8222d3967514e5669eaaa782 Mon Sep 17 00:00:00 2001 From: CloneTrooper1019 Date: Sun, 6 Oct 2019 02:18:42 -0500 Subject: [PATCH] Improvements and bug fixes - Added some more methods to the Instance class to make instance queries more powerful. - Fixed a bug where comment nodes were being processed by the XmlFileReader --- Tree/Instance.cs | 50 ++++++++++++++++++++++++++++++++--- XmlFormat/IO/XmlFileReader.cs | 3 +++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/Tree/Instance.cs b/Tree/Instance.cs index 99389df..2f297f3 100644 --- a/Tree/Instance.cs +++ b/Tree/Instance.cs @@ -131,6 +131,22 @@ namespace RobloxFiles return classType.IsAssignableFrom(myType); } + /// + /// Attempts to cast this Instance to an inherited class of type ''. + /// Returns null if the instance cannot be casted to the provided type. + /// + /// The type of Instance to cast to. + /// The instance as the type '' if it can be converted, or null. + public T Cast() where T : Instance + { + T result = null; + + if (IsA()) + result = this as T; + + return result; + } + /// /// The parent of this Instance, or null if the instance is the root of a tree. /// Setting the value of this property will throw an exception if: @@ -163,7 +179,7 @@ namespace RobloxFiles } /// - /// Returns a snapshot of the Instances currently parented to this Instance, as an array. + /// Returns an array containing all the children of this Instance. /// public Instance[] GetChildren() { @@ -171,7 +187,20 @@ namespace RobloxFiles } /// - /// Returns a snapshot of the Instances that are descendants of this Instance, as an array. + /// Returns an array containing all the children of this Instance, whose type is ''. + /// + public T[] GetChildrenOfType() where T : Instance + { + T[] ofType = GetChildren() + .Where(child => child.IsA()) + .Cast() + .ToArray(); + + return ofType; + } + + /// + /// Returns an array containing all the descendants of this Instance. /// public Instance[] GetDescendants() { @@ -190,6 +219,19 @@ namespace RobloxFiles return results.ToArray(); } + /// + /// Returns an array containing all the descendants of this Instance, whose type is ''. + /// + public T[] GetDescendantsOfType() where T : Instance + { + T[] ofType = GetDescendants() + .Where(desc => desc.IsA()) + .Cast() + .ToArray(); + + return ofType; + } + /// /// Returns the first child of this Instance whose Name is the provided string name. /// If the instance is not found, this returns null. @@ -291,7 +333,7 @@ namespace RobloxFiles } /// - /// Returns the first ancestor of this Instance which derives from the provided type T. + /// Returns the first ancestor of this Instance which derives from the provided type . /// If the instance is not found, this returns null. /// /// The Name of the Instance to find. @@ -349,7 +391,7 @@ namespace RobloxFiles } /// - /// Returns the first child of this Instance which derives from the provided type T. + /// Returns the first child of this Instance which derives from the provided type . /// If the instance is not found, this returns null. /// /// Whether this should search descendants as well. diff --git a/XmlFormat/IO/XmlFileReader.cs b/XmlFormat/IO/XmlFileReader.cs index 377dc4c..aa1e908 100644 --- a/XmlFormat/IO/XmlFileReader.cs +++ b/XmlFormat/IO/XmlFileReader.cs @@ -75,6 +75,9 @@ namespace RobloxFiles.XmlFormat foreach (XmlNode propNode in propsNode.ChildNodes) { + if (propNode.NodeType == XmlNodeType.Comment) + continue; + string propType = propNode.Name; XmlNode propName = propNode.Attributes.GetNamedItem("name");