From da0f291a4467b7ca310045c4784f8918da877e7f Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Sun, 27 Nov 2022 15:59:59 +0100 Subject: [PATCH] converted PortDataCache to a dictionary that holds a dictionary of fieldname and port indexed by type. Removed serialized from PortDataCache as it was not being saved anywhere. --- Scripts/NodeDataCache.cs | 48 +++++++--------------------------------- 1 file changed, 8 insertions(+), 40 deletions(-) diff --git a/Scripts/NodeDataCache.cs b/Scripts/NodeDataCache.cs index 32906a6..4f4937d 100644 --- a/Scripts/NodeDataCache.cs +++ b/Scripts/NodeDataCache.cs @@ -9,7 +9,6 @@ namespace XNode { private static PortDataCache portDataCache; private static Dictionary> formerlySerializedAsCache; private static Dictionary typeQualifiedNameCache; - private static Dictionary staticPorts; private static bool Initialized { get { return portDataCache != null; } } public static string GetTypeQualifiedName(System.Type type) { @@ -35,15 +34,10 @@ namespace XNode { List dynamicListPorts = new List(); - List typePortCache; - if (portDataCache.TryGetValue(nodeType, out typePortCache)) { -#if UNITY_2021_3_OR_NEWER - staticPorts.EnsureCapacity(typePortCache.Count); -#endif - for (int i = 0; i < typePortCache.Count; i++) { - staticPorts.Add(typePortCache[i].fieldName, typePortCache[i]); - } - } + Dictionary staticPorts; + if (!portDataCache.TryGetValue(nodeType, out staticPorts)) { + staticPorts = new Dictionary(); + } // Cleanup port dict - Remove nonexisting static ports - update static port types // AND update dynamic ports (albeit only those in lists) too, in order to enforce proper serialisation. @@ -108,8 +102,6 @@ namespace XNode { listPort.connectionType = backingPort.connectionType; listPort.typeConstraint = backingPort.typeConstraint; } - - staticPorts.Clear(); } /// @@ -150,7 +142,6 @@ namespace XNode { /// Cache node types private static void BuildCache() { portDataCache = new PortDataCache(); - staticPorts = new Dictionary(); System.Type baseType = typeof(Node); List nodeTypes = new List(); System.Reflection.Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies(); @@ -214,8 +205,9 @@ namespace XNode { if (inputAttrib != null && outputAttrib != null) Debug.LogError("Field " + fieldInfo[i].Name + " of type " + nodeType.FullName + " cannot be both input and output."); else { - if (!portDataCache.ContainsKey(nodeType)) portDataCache.Add(nodeType, new List()); - portDataCache[nodeType].Add(new NodePort(fieldInfo[i])); + if (!portDataCache.ContainsKey(nodeType)) portDataCache.Add(nodeType, new Dictionary()); + NodePort port = new NodePort(fieldInfo[i]); + portDataCache[nodeType].Add(port.fieldName, port); } if (formerlySerializedAsAttribute != null) { @@ -229,30 +221,6 @@ namespace XNode { } [System.Serializable] - private class PortDataCache : Dictionary>, ISerializationCallbackReceiver { - [SerializeField] private List keys = new List(); - [SerializeField] private List> values = new List>(); - - // save the dictionary to lists - public void OnBeforeSerialize() { - keys.Clear(); - values.Clear(); - foreach (var pair in this) { - keys.Add(pair.Key); - values.Add(pair.Value); - } - } - - // load dictionary from lists - public void OnAfterDeserialize() { - 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.")); - - for (int i = 0; i < keys.Count; i++) - this.Add(keys[i], values[i]); - } - } + private class PortDataCache : Dictionary> { } } }