1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-03-26 22:49:02 +08:00

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.
This commit is contained in:
Jeff Campbell 2020-02-26 09:47:56 +01:00
parent 1ef3896893
commit 86ef6510ef
4 changed files with 70 additions and 20 deletions

View File

@ -378,7 +378,13 @@ namespace XNode {
this.Clear(); this.Clear();
if (keys.Count != values.Count) 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++) for (int i = 0; i < keys.Count; i++)
this.Add(keys[i], values[i]); this.Add(keys[i], values[i]);

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using UnityEngine; using UnityEngine;
@ -121,27 +122,20 @@ namespace XNode {
/// <summary> Cache node types </summary> /// <summary> Cache node types </summary>
private static void BuildCache() { private static void BuildCache() {
portDataCache = new PortDataCache(); portDataCache = new PortDataCache();
System.Type baseType = typeof(Node); Type baseType = typeof(Node);
List<System.Type> nodeTypes = new List<System.Type>(); List<Type> nodeTypes = new List<Type>();
System.Reflection.Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies(); Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
// Loop through assemblies and add node types to list // 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 // Skip certain dlls to improve performance
string assemblyName = assembly.GetName().Name; string assemblyName = assembly.GetName().Name;
int index = assemblyName.IndexOf('.'); if (!XNodeRuntimeConstants.IGNORE_ASSEMBLY_PREFIXES.Any(x => assemblyName.StartsWith(x)))
if (index != -1) assemblyName = assemblyName.Substring(0, index); {
switch (assemblyName) { IEnumerable<Type> foundNodeTypes = assembly.GetTypes()
// The following assemblies, and sub-assemblies (eg. UnityEngine.UI) are skipped .Where(t => !t.IsAbstract && baseType.IsAssignableFrom(t));
case "UnityEditor": nodeTypes.AddRange(foundNodeTypes);
case "UnityEngine":
case "System":
case "mscorlib":
case "Microsoft":
continue;
default:
nodeTypes.AddRange(assembly.GetTypes().Where(t => !t.IsAbstract && baseType.IsAssignableFrom(t)).ToArray());
break;
} }
} }
@ -201,7 +195,13 @@ namespace XNode {
this.Clear(); this.Clear();
if (keys.Count != values.Count) 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++) for (int i = 0; i < keys.Count; i++)
this.Add(keys[i], values[i]); this.Add(keys[i], values[i]);

View File

@ -0,0 +1,33 @@
namespace XNode
{
/// <summary>
/// A helper class containing shared constants.
/// </summary>
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
/// <summary>
/// A collection of assembly prefixes that should not be reflected for derived node types.
/// </summary>
public static string[] IGNORE_ASSEMBLY_PREFIXES =
{
"ExCSS",
"Microsoft",
"Mono",
"netstandard",
"mscorlib",
"nunit",
"SyntaxTree",
"System",
"Unity",
"UnityEditor",
"UnityEngine"
};
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cf01fc218f8f12d4aa557c9799289063
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: