1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 09:16:01 +08:00

moved reflection code to cache part in initialization

This commit is contained in:
Simon Rodriguez 2021-05-04 08:29:16 +02:00
parent 2f69c4350d
commit 3287575dcd

View File

@ -7,6 +7,7 @@ namespace XNode {
/// <summary> Precaches reflection data in editor so we won't have to do it runtime </summary> /// <summary> Precaches reflection data in editor so we won't have to do it runtime </summary>
public static class NodeDataCache { public static class NodeDataCache {
private static PortDataCache portDataCache; private static PortDataCache portDataCache;
private static Dictionary<System.Type, Dictionary<string, string>> formerlySerializedAsCache;
private static bool Initialized { get { return portDataCache != null; } } private static bool Initialized { get { return portDataCache != null; } }
/// <summary> Update static ports and dynamic ports managed by DynamicPortLists to reflect class fields. </summary> /// <summary> Update static ports and dynamic ports managed by DynamicPortLists to reflect class fields. </summary>
@ -14,10 +15,12 @@ namespace XNode {
if (!Initialized) BuildCache(); if (!Initialized) BuildCache();
Dictionary<string, NodePort> staticPorts = new Dictionary<string, NodePort>(); Dictionary<string, NodePort> staticPorts = new Dictionary<string, NodePort>();
Dictionary<string, string> formerlySerializedAs = new Dictionary<string, string>();
Dictionary<string, List<NodePort>> removedPorts = new Dictionary<string, List<NodePort>>(); Dictionary<string, List<NodePort>> removedPorts = new Dictionary<string, List<NodePort>>();
System.Type nodeType = node.GetType(); System.Type nodeType = node.GetType();
Dictionary<string, string> formerlySerializedAs = null;
if (formerlySerializedAsCache != null) formerlySerializedAsCache.TryGetValue(nodeType, out formerlySerializedAs);
List<NodePort> dynamicListPorts = new List<NodePort>(); List<NodePort> dynamicListPorts = new List<NodePort>();
List<NodePort> typePortCache; List<NodePort> typePortCache;
@ -25,11 +28,6 @@ namespace XNode {
for (int i = 0; i < typePortCache.Count; i++) { for (int i = 0; i < typePortCache.Count; i++) {
staticPorts.Add(typePortCache[i].fieldName, portDataCache[nodeType][i]); staticPorts.Add(typePortCache[i].fieldName, portDataCache[nodeType][i]);
} }
for (int i = 0; i < typePortCache.Count; i++) {
var fieldInfo = nodeType.GetField(typePortCache[i].fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var attribute = fieldInfo.GetCustomAttributes(true).FirstOrDefault(x => x is UnityEngine.Serialization.FormerlySerializedAsAttribute) as UnityEngine.Serialization.FormerlySerializedAsAttribute;
if (attribute != null) formerlySerializedAs.Add(attribute.oldName, typePortCache[i].fieldName);
}
} }
// Cleanup port dict - Remove nonexisting static ports - update static port types // Cleanup port dict - Remove nonexisting static ports - update static port types
@ -52,7 +50,7 @@ namespace XNode {
//See if the field is tagged with FormerlySerializedAs, if so add the port with its new field name to removedPorts //See if the field is tagged with FormerlySerializedAs, if so add the port with its new field name to removedPorts
// so it can be reconnected in missing ports stage. // so it can be reconnected in missing ports stage.
string newName = null; string newName = null;
if (formerlySerializedAs.TryGetValue(port.fieldName, out newName)) removedPorts.Add(newName, port.GetConnections()); if (formerlySerializedAs != null && formerlySerializedAs.TryGetValue(port.fieldName, out newName)) removedPorts.Add(newName, port.GetConnections());
port.ClearConnections(); port.ClearConnections();
ports.Remove(port.fieldName); ports.Remove(port.fieldName);
@ -188,6 +186,7 @@ namespace XNode {
object[] attribs = fieldInfo[i].GetCustomAttributes(true); object[] attribs = fieldInfo[i].GetCustomAttributes(true);
Node.InputAttribute inputAttrib = attribs.FirstOrDefault(x => x is Node.InputAttribute) as Node.InputAttribute; Node.InputAttribute inputAttrib = attribs.FirstOrDefault(x => x is Node.InputAttribute) as Node.InputAttribute;
Node.OutputAttribute outputAttrib = attribs.FirstOrDefault(x => x is Node.OutputAttribute) as Node.OutputAttribute; Node.OutputAttribute outputAttrib = attribs.FirstOrDefault(x => x is Node.OutputAttribute) as Node.OutputAttribute;
UnityEngine.Serialization.FormerlySerializedAsAttribute formerlySerializedAsAttribute = attribs.FirstOrDefault(x => x is UnityEngine.Serialization.FormerlySerializedAsAttribute) as UnityEngine.Serialization.FormerlySerializedAsAttribute;
if (inputAttrib == null && outputAttrib == null) continue; if (inputAttrib == null && outputAttrib == null) continue;
@ -196,6 +195,12 @@ namespace XNode {
if (!portDataCache.ContainsKey(nodeType)) portDataCache.Add(nodeType, new List<NodePort>()); if (!portDataCache.ContainsKey(nodeType)) portDataCache.Add(nodeType, new List<NodePort>());
portDataCache[nodeType].Add(new NodePort(fieldInfo[i])); portDataCache[nodeType].Add(new NodePort(fieldInfo[i]));
} }
if(formerlySerializedAsAttribute != null) {
if (formerlySerializedAsCache == null) formerlySerializedAsCache = new Dictionary<System.Type, Dictionary<string, string>>();
if (!formerlySerializedAsCache.ContainsKey(nodeType)) formerlySerializedAsCache.Add(nodeType, new Dictionary<string, string>());
formerlySerializedAsCache[nodeType].Add(formerlySerializedAsAttribute.oldName, fieldInfo[i].Name);
}
} }
} }