From 86ef6510ef1db1543afd2732a8d70ebfc2a08bf0 Mon Sep 17 00:00:00 2001 From: Jeff Campbell Date: Wed, 26 Feb 2020 09:47:56 +0100 Subject: [PATCH] String format fix and simplified assembly skip Fixed missing format arguments for exception * Added missing string format arguments used for exception * Moved duplicate exception message to XNodeRuntimeConstants class * Modified NodeDataCache deserialization exception thrown to be the same format as Node Simplified logic for discovering node types * Simplified the reflection logic used to discover node types by creating a constant array of assembly prefixes to ignore and then checking to see if discovered assembly names begin with those prefixes, and if they do skip checking them for derived node types. Added additional common assembly prefixes that Unity loads into memory that would not contain Node types. --- Scripts/Node.cs | 8 +++++- Scripts/NodeDataCache.cs | 38 +++++++++++++-------------- Scripts/XNodeRuntimeConstants.cs | 33 +++++++++++++++++++++++ Scripts/XNodeRuntimeConstants.cs.meta | 11 ++++++++ 4 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 Scripts/XNodeRuntimeConstants.cs create mode 100644 Scripts/XNodeRuntimeConstants.cs.meta diff --git a/Scripts/Node.cs b/Scripts/Node.cs index beb5f77..30709cf 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -378,7 +378,13 @@ namespace XNode { this.Clear(); if (keys.Count != values.Count) - throw new System.Exception("there are " + keys.Count + " keys and " + values.Count + " values after deserialization. Make sure that both key and value types are serializable."); + { + var msg = string.Format( + XNodeRuntimeConstants.MISMATCHED_KEYS_TO_VALUES_EXCEPTION_MESSAGE, + keys.Count, + values.Count); + throw new Exception(msg); + } for (int i = 0; i < keys.Count; i++) this.Add(keys[i], values[i]); diff --git a/Scripts/NodeDataCache.cs b/Scripts/NodeDataCache.cs index ad55f7b..e1a6c4d 100644 --- a/Scripts/NodeDataCache.cs +++ b/Scripts/NodeDataCache.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using UnityEngine; @@ -121,27 +122,20 @@ namespace XNode { /// Cache node types private static void BuildCache() { portDataCache = new PortDataCache(); - System.Type baseType = typeof(Node); - List nodeTypes = new List(); - System.Reflection.Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies(); + Type baseType = typeof(Node); + List nodeTypes = new List(); + Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); // Loop through assemblies and add node types to list - foreach (Assembly assembly in assemblies) { + foreach (Assembly assembly in assemblies) + { // Skip certain dlls to improve performance string assemblyName = assembly.GetName().Name; - int index = assemblyName.IndexOf('.'); - if (index != -1) assemblyName = assemblyName.Substring(0, index); - switch (assemblyName) { - // The following assemblies, and sub-assemblies (eg. UnityEngine.UI) are skipped - case "UnityEditor": - case "UnityEngine": - case "System": - case "mscorlib": - case "Microsoft": - continue; - default: - nodeTypes.AddRange(assembly.GetTypes().Where(t => !t.IsAbstract && baseType.IsAssignableFrom(t)).ToArray()); - break; + if (!XNodeRuntimeConstants.IGNORE_ASSEMBLY_PREFIXES.Any(x => assemblyName.StartsWith(x))) + { + IEnumerable foundNodeTypes = assembly.GetTypes() + .Where(t => !t.IsAbstract && baseType.IsAssignableFrom(t)); + nodeTypes.AddRange(foundNodeTypes); } } @@ -201,7 +195,13 @@ namespace XNode { this.Clear(); if (keys.Count != values.Count) - throw new System.Exception(string.Format("there are {0} keys and {1} values after deserialization. Make sure that both key and value types are serializable.")); + { + var msg = string.Format( + XNodeRuntimeConstants.MISMATCHED_KEYS_TO_VALUES_EXCEPTION_MESSAGE, + keys.Count, + values.Count); + throw new Exception(msg); + } for (int i = 0; i < keys.Count; i++) this.Add(keys[i], values[i]); diff --git a/Scripts/XNodeRuntimeConstants.cs b/Scripts/XNodeRuntimeConstants.cs new file mode 100644 index 0000000..027f979 --- /dev/null +++ b/Scripts/XNodeRuntimeConstants.cs @@ -0,0 +1,33 @@ +namespace XNode +{ + /// + /// A helper class containing shared constants. + /// + public static class XNodeRuntimeConstants + { + // Exceptions + public const string MISMATCHED_KEYS_TO_VALUES_EXCEPTION_MESSAGE = + "There are {0} keys and {1} values after deserialization. " + + "Make sure that both key and value types are serializable."; + + // Reflection + + /// + /// A collection of assembly prefixes that should not be reflected for derived node types. + /// + public static string[] IGNORE_ASSEMBLY_PREFIXES = + { + "ExCSS", + "Microsoft", + "Mono", + "netstandard", + "mscorlib", + "nunit", + "SyntaxTree", + "System", + "Unity", + "UnityEditor", + "UnityEngine" + }; + } +} diff --git a/Scripts/XNodeRuntimeConstants.cs.meta b/Scripts/XNodeRuntimeConstants.cs.meta new file mode 100644 index 0000000..c4040a8 --- /dev/null +++ b/Scripts/XNodeRuntimeConstants.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cf01fc218f8f12d4aa557c9799289063 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: