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

Added support for unitys FormerlySerializedAsAttribute. It reconnects ports that has the old name to its new name.

This commit is contained in:
Simon Rodriguez 2021-05-01 18:47:39 +02:00
parent 93615e7722
commit 2f69c4350d

View File

@ -14,6 +14,7 @@ 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();
@ -24,6 +25,11 @@ 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
@ -43,6 +49,11 @@ namespace XNode {
} }
// If port doesn't exist anymore, remove it // If port doesn't exist anymore, remove it
else if (port.IsStatic) { 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(); port.ClearConnections();
ports.Remove(port.fieldName); ports.Remove(port.fieldName);
} }