-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathConfigGraph.cs
318 lines (268 loc) · 9.67 KB
/
ConfigGraph.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using UnityEditor;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace UnityEngine.AssetGraph.DataModel.Version2
{
/*
* Save data which holds all AssetBundleGraph settings and configurations.
*/
[CreateAssetMenu(fileName = "New AssetGraph", menuName = "Asset Graph", order = 650)]
public class ConfigGraph : ScriptableObject
{
/*
* Important:
* ABG_FILE_VERSION must be increased by one when any structure change(s) happen
*/
public const int ABG_FILE_VERSION = 2;
[SerializeField] private List<NodeData> m_allNodes;
[SerializeField] private List<ConnectionData> m_allConnections;
[SerializeField] private string m_lastModified;
[SerializeField] private int m_version;
[SerializeField] private string m_graphDescription;
[SerializeField] private bool m_useAsAssetPostprocessor;
[SerializeField] private int m_graphExecOrderPriority;
void OnEnable()
{
Initialize();
Validate();
}
private string GetFileTimeUtcString()
{
return DateTime.UtcNow.ToFileTimeUtc().ToString();
}
private void Initialize()
{
if (string.IsNullOrEmpty(m_lastModified))
{
m_lastModified = GetFileTimeUtcString();
m_allNodes = new List<NodeData>();
m_allConnections = new List<ConnectionData>();
m_version = ABG_FILE_VERSION;
m_graphDescription = String.Empty;
m_graphExecOrderPriority = Settings.GRAPHEXECPRIORITY_DEFAULT;
EditorUtility.SetDirty(this);
}
}
private void Import(AssetBundleGraph.SaveData v1)
{
m_lastModified = GetFileTimeUtcString();
m_version = ABG_FILE_VERSION;
foreach (var n in v1.Nodes)
{
m_allNodes.Add(new NodeData(n));
}
foreach (var c in v1.Connections)
{
m_allConnections.Add(new ConnectionData(c));
}
EditorUtility.SetDirty(this);
}
public bool UseAsAssetPostprocessor
{
get { return m_useAsAssetPostprocessor; }
set
{
if (m_useAsAssetPostprocessor == value)
return;
m_useAsAssetPostprocessor = value;
SetGraphDirty();
}
}
public DateTime LastModified
{
get
{
long utcFileTime = long.Parse(m_lastModified);
DateTime d = DateTime.FromFileTimeUtc(utcFileTime);
return d;
}
}
public string Descrption
{
get { return m_graphDescription; }
set
{
if (m_graphDescription == value)
return;
m_graphDescription = value;
SetGraphDirty();
}
}
public int Version
{
get { return m_version; }
}
public int ExecuteOrderPriority
{
get { return m_graphExecOrderPriority; }
set { m_graphExecOrderPriority = value; }
}
public List<NodeData> Nodes
{
get { return m_allNodes; }
}
public List<ConnectionData> Connections
{
get { return m_allConnections; }
}
public List<NodeData> CollectAllLeafNodes()
{
var nodesWithChild = new List<NodeData>();
foreach (var c in m_allConnections)
{
NodeData n = m_allNodes.Find(v => v.Id == c.FromNodeId);
if (n != null)
{
nodesWithChild.Add(n);
}
}
return m_allNodes.Except(nodesWithChild).ToList();
}
public void Save()
{
m_allNodes.ForEach(n => n.Operation.Save());
SetGraphDirty();
}
public void SetGraphDirty()
{
EditorUtility.SetDirty(this);
}
public string GetGraphName()
{
var path = AssetDatabase.GetAssetOrScenePath(this);
return Path.GetFileNameWithoutExtension(path);
}
public string GetGraphGuid()
{
return AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(this));
}
//
// Save/Load to disk
//
public void ApplyGraph(List<NodeGUI> nodes, List<ConnectionGUI> connections)
{
List<NodeData> n = nodes.Select(v => v.Data).ToList();
List<ConnectionData> c = connections.Select(v => v.Data).ToList();
if (!Enumerable.SequenceEqual(n.OrderBy(v => v.Id), m_allNodes.OrderBy(v => v.Id)) ||
!Enumerable.SequenceEqual(c.OrderBy(v => v.Id), m_allConnections.OrderBy(v => v.Id)))
{
LogUtility.Logger.Log("[ApplyGraph] SaveData updated.");
m_version = ABG_FILE_VERSION;
m_lastModified = GetFileTimeUtcString();
m_allNodes = n;
m_allConnections = c;
Save();
}
else
{
LogUtility.Logger.Log("[ApplyGraph] SaveData update skipped. graph is equivarent.");
}
}
public NodeData AddNode(Type nodeType)
{
return AddNode(string.Empty, nodeType, 100, 100);
}
public NodeData AddNode(string name, Type nodeType, int x, int y)
{
var nodeInstance = NodeUtility.CreateNodeInstance(nodeType.AssemblyQualifiedName);
var node = new NodeData(name, nodeInstance, x, y);
m_allNodes.Add(node);
return node;
}
public ConnectionData Connect(string label, ConnectionPointData outputPort, ConnectionPointData inputPort)
{
if (!outputPort.IsOutput || !inputPort.IsInput)
{
return null;
}
var outputNode = m_allNodes.FirstOrDefault(n => n.Id == outputPort.NodeId);
var inputNode = m_allNodes.FirstOrDefault(n => n.Id == inputPort.NodeId);
if (outputNode == null || inputNode == null)
{
return null;
}
var existingConnections = m_allConnections
.Where (con => con.FromNodeConnectionPointId == outputPort.Id)
.Where(con => con.FromNodeConnectionPointId != inputPort.Id).ToList();
foreach (var c in existingConnections)
{
m_allConnections.Remove(c);
}
var newConnection = new ConnectionData(label, outputPort, inputPort);
m_allConnections.Add(newConnection);
return newConnection;
}
public static ConfigGraph CreateNewGraph()
{
return CreateInstance<ConfigGraph>();
}
public static ConfigGraph CreateNewGraph(string pathToSave)
{
var data = CreateInstance<ConfigGraph>();
AssetDatabase.CreateAsset(data, pathToSave);
return data;
}
public static ConfigGraph CreateNewGraphFromImport(string pathToLoad)
{
// try load from v1.
try
{
var loadedData = AssetBundleGraph.SaveData.LoadSaveData(pathToLoad);
var newGraph = CreateNewGraph(Settings.Path.ASSETS_PATH + "importedGraph.asset");
newGraph.Import(loadedData);
return newGraph;
}
catch (Exception e)
{
LogUtility.Logger.LogError(LogUtility.kTag, "Failed to import graph from previous version." + e);
}
return null;
}
/*
* Checks deserialized SaveData, and make some changes if necessary
* return false if any changes are perfomed.
*/
private bool Validate()
{
var changed = false;
if (m_allNodes != null)
{
List<NodeData> removingNodes = new List<NodeData>();
foreach (var n in m_allNodes)
{
if (!n.Validate())
{
removingNodes.Add(n);
changed = true;
LogUtility.Logger.LogFormat(LogType.Error,
"Validation failed for node \"{0}\". Class \"{1}\" not found or failed to instantiate.", n.Name,
n.Operation.ClassName);
}
}
m_allNodes.RemoveAll(n => removingNodes.Contains(n));
}
if (m_allConnections != null)
{
List<ConnectionData> removingConnections = new List<ConnectionData>();
foreach (var c in m_allConnections)
{
if (!c.Validate(m_allNodes, m_allConnections))
{
removingConnections.Add(c);
changed = true;
LogUtility.Logger.LogFormat(LogType.Error,
"Validation failed for connection \"{0}\". One of connecting node not found.", c.Label);
}
}
m_allConnections.RemoveAll(c => removingConnections.Contains(c));
}
if (changed)
{
m_lastModified = GetFileTimeUtcString();
}
return !changed;
}
}
}