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: