mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 17:26:02 +08:00
Merge pull request #317 from nostek/feature/formerlyserializedas
Added support for unitys FormerlySerializedAsAttribute.
This commit is contained in:
commit
6cba54f3d4
@ -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>
|
||||||
@ -17,6 +18,9 @@ namespace XNode {
|
|||||||
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;
|
||||||
@ -43,6 +47,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 != null && formerlySerializedAs.TryGetValue(port.fieldName, out newName)) removedPorts.Add(newName, port.GetConnections());
|
||||||
|
|
||||||
port.ClearConnections();
|
port.ClearConnections();
|
||||||
ports.Remove(port.fieldName);
|
ports.Remove(port.fieldName);
|
||||||
}
|
}
|
||||||
@ -177,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;
|
||||||
|
|
||||||
@ -185,6 +195,14 @@ 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>());
|
||||||
|
|
||||||
|
if (formerlySerializedAsCache[nodeType].ContainsKey(formerlySerializedAsAttribute.oldName)) Debug.LogError("Another FormerlySerializedAs with value '" + formerlySerializedAsAttribute.oldName + "' already exist on this node.");
|
||||||
|
else formerlySerializedAsCache[nodeType].Add(formerlySerializedAsAttribute.oldName, fieldInfo[i].Name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user