From 2f69c4350def2043f87cf94b2db9fb9388b516c1 Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Sat, 1 May 2021 18:47:39 +0200 Subject: [PATCH] Added support for unitys FormerlySerializedAsAttribute. It reconnects ports that has the old name to its new name. --- Scripts/NodeDataCache.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Scripts/NodeDataCache.cs b/Scripts/NodeDataCache.cs index ba52e1b..e05d033 100644 --- a/Scripts/NodeDataCache.cs +++ b/Scripts/NodeDataCache.cs @@ -14,6 +14,7 @@ namespace XNode { if (!Initialized) BuildCache(); Dictionary staticPorts = new Dictionary(); + Dictionary formerlySerializedAs = new Dictionary(); Dictionary> removedPorts = new Dictionary>(); System.Type nodeType = node.GetType(); @@ -24,6 +25,11 @@ namespace XNode { for (int i = 0; i < typePortCache.Count; 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 @@ -43,6 +49,11 @@ namespace XNode { } // If port doesn't exist anymore, remove it else if (port.IsStatic) { + //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. + string newName = null; + if (formerlySerializedAs.TryGetValue(port.fieldName, out newName)) removedPorts.Add(newName, port.GetConnections()); + port.ClearConnections(); ports.Remove(port.fieldName); }