|
|
|
|
|
|
public class SerializableGraph : IGraph, ISerializationCallbackReceiver |
|
|
|
{ |
|
|
|
[NonSerialized] |
|
|
|
private List<IEdge> m_Edges = new List<IEdge>(); |
|
|
|
List<IEdge> m_Edges = new List<IEdge>(); |
|
|
|
private List<INode> m_Nodes = new List<INode>(); |
|
|
|
Dictionary<Guid, INode> m_Nodes = new Dictionary<Guid, INode>(); |
|
|
|
|
|
|
|
[SerializeField] |
|
|
|
List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>(); |
|
|
|
|
|
|
|
|
|
|
public IEnumerable<T> GetNodes<T>() where T : INode |
|
|
|
{ |
|
|
|
return m_Nodes.OfType<T>(); |
|
|
|
return m_Nodes.Values.OfType<T>(); |
|
|
|
} |
|
|
|
|
|
|
|
public IEnumerable<IEdge> edges |
|
|
|
|
|
|
|
|
|
|
public virtual void AddNode(INode node) |
|
|
|
{ |
|
|
|
m_Nodes.Add(node); |
|
|
|
m_Nodes.Add(node.guid, node); |
|
|
|
node.owner = this; |
|
|
|
ValidateGraph(); |
|
|
|
} |
|
|
|
|
|
|
if (!node.canDeleteNode) |
|
|
|
return; |
|
|
|
|
|
|
|
m_Nodes.Remove(node); |
|
|
|
m_Nodes.Remove(node.guid); |
|
|
|
private void RemoveNodeNoValidate(INode node) |
|
|
|
void RemoveNodeNoValidate(INode node) |
|
|
|
m_Nodes.Remove(node); |
|
|
|
m_Nodes.Remove(node.guid); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual Dictionary<SerializationHelper.TypeSerializationInfo, SerializationHelper.TypeSerializationInfo> GetLegacyTypeRemapping() |
|
|
|
|
|
|
ValidateGraph(); |
|
|
|
} |
|
|
|
|
|
|
|
private void RemoveEdgeNoValidate(IEdge e) |
|
|
|
void RemoveEdgeNoValidate(IEdge e) |
|
|
|
{ |
|
|
|
m_Edges.Remove(e); |
|
|
|
} |
|
|
|
|
|
|
return m_Nodes.FirstOrDefault(x => x.guid == guid); |
|
|
|
INode node; |
|
|
|
m_Nodes.TryGetValue(guid, out node); |
|
|
|
return node; |
|
|
|
} |
|
|
|
|
|
|
|
public bool ContainsNodeGuid(Guid guid) |
|
|
|
{ |
|
|
|
return m_Nodes.ContainsKey(guid); |
|
|
|
return m_Nodes.Where(x => x.guid == guid).OfType<T>().FirstOrDefault(); |
|
|
|
var node = GetNodeFromGuid(guid); |
|
|
|
if (node is T) |
|
|
|
return (T)node; |
|
|
|
return default(T); |
|
|
|
} |
|
|
|
|
|
|
|
public IEnumerable<IEdge> GetEdges(SlotReference s) |
|
|
|
|
|
|
|
|
|
|
public virtual void OnBeforeSerialize() |
|
|
|
{ |
|
|
|
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes); |
|
|
|
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes.Values); |
|
|
|
m_Nodes = SerializationHelper.Deserialize<INode>(m_SerializableNodes, GetLegacyTypeRemapping()); |
|
|
|
foreach (var node in m_Nodes) |
|
|
|
var nodes = SerializationHelper.Deserialize<INode>(m_SerializableNodes, GetLegacyTypeRemapping()); |
|
|
|
m_Nodes = new Dictionary<Guid, INode>(nodes.Count); |
|
|
|
foreach (var node in nodes) |
|
|
|
m_Nodes.Add(node.guid, node); |
|
|
|
} |
|
|
|
|
|
|
|
m_SerializableNodes = null; |
|
|
|