-
Notifications
You must be signed in to change notification settings - Fork 364
/
NodeProvider.cs
348 lines (285 loc) · 10.6 KB
/
NodeProvider.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;
using System.IO;
using System.Reflection;
using UnityEditor.Experimental.GraphView;
namespace GraphProcessor
{
public static class NodeProvider
{
public struct PortDescription
{
public Type nodeType;
public Type portType;
public bool isInput;
public string portFieldName;
public string portIdentifier;
public string portDisplayName;
}
static Dictionary< Type, MonoScript > nodeViewScripts = new Dictionary< Type, MonoScript >();
static Dictionary< Type, MonoScript > nodeScripts = new Dictionary< Type, MonoScript >();
static Dictionary< Type, Type > nodeViewPerType = new Dictionary< Type, Type >();
public class NodeDescriptions
{
public Dictionary< string, Type > nodePerMenuTitle = new Dictionary< string, Type >();
public List< Type > slotTypes = new List< Type >();
public List< PortDescription > nodeCreatePortDescription = new List<PortDescription>();
}
public struct NodeSpecificToGraph
{
public Type nodeType;
public List<MethodInfo> isCompatibleWithGraph;
public Type compatibleWithGraphType;
}
static Dictionary<BaseGraph, NodeDescriptions> specificNodeDescriptions = new Dictionary<BaseGraph, NodeDescriptions>();
static List<NodeSpecificToGraph> specificNodes = new List<NodeSpecificToGraph>();
static NodeDescriptions genericNodes = new NodeDescriptions();
static NodeProvider()
{
BuildScriptCache();
BuildGenericNodeCache();
}
public static void LoadGraph(BaseGraph graph)
{
// Clear old graph data in case there was some
specificNodeDescriptions.Remove(graph);
var descriptions = new NodeDescriptions();
specificNodeDescriptions.Add(graph, descriptions);
var graphType = graph.GetType();
foreach (var nodeInfo in specificNodes)
{
bool compatible = nodeInfo.compatibleWithGraphType == null || nodeInfo.compatibleWithGraphType == graphType;
if (nodeInfo.isCompatibleWithGraph != null)
{
foreach (var method in nodeInfo.isCompatibleWithGraph)
compatible &= (bool)method?.Invoke(null, new object[]{ graph });
}
if (compatible)
BuildCacheForNode(nodeInfo.nodeType, descriptions, graph);
}
}
public static void UnloadGraph(BaseGraph graph)
{
specificNodeDescriptions.Remove(graph);
}
static void BuildGenericNodeCache()
{
foreach (var nodeType in TypeCache.GetTypesDerivedFrom<BaseNode>())
{
if (!IsNodeAccessibleFromMenu(nodeType))
continue;
if (IsNodeSpecificToGraph(nodeType))
continue;
BuildCacheForNode(nodeType, genericNodes);
}
}
static void BuildCacheForNode(Type nodeType, NodeDescriptions targetDescription, BaseGraph graph = null)
{
var attrs = nodeType.GetCustomAttributes(typeof(NodeMenuItemAttribute), false) as NodeMenuItemAttribute[];
if (attrs != null && attrs.Length > 0)
{
foreach (var attr in attrs)
targetDescription.nodePerMenuTitle[attr.menuTitle] = nodeType;
}
foreach (var field in nodeType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
if (field.GetCustomAttribute<HideInInspector>() == null && field.GetCustomAttributes().Any(c => c is InputAttribute || c is OutputAttribute))
targetDescription.slotTypes.Add(field.FieldType);
}
ProvideNodePortCreationDescription(nodeType, targetDescription, graph);
}
static bool IsNodeAccessibleFromMenu(Type nodeType)
{
if (nodeType.IsAbstract)
return false;
return nodeType.GetCustomAttributes<NodeMenuItemAttribute>().Count() > 0;
}
// Check if node has anything that depends on the graph type or settings
static bool IsNodeSpecificToGraph(Type nodeType)
{
var isCompatibleWithGraphMethods = nodeType.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).Where(m => m.GetCustomAttribute<IsCompatibleWithGraph>() != null);
var nodeMenuAttributes = nodeType.GetCustomAttributes<NodeMenuItemAttribute>();
List<Type> compatibleGraphTypes = nodeMenuAttributes.Where(n => n.onlyCompatibleWithGraph != null).Select(a => a.onlyCompatibleWithGraph).ToList();
List<MethodInfo> compatibleMethods = new List<MethodInfo>();
foreach (var method in isCompatibleWithGraphMethods)
{
// Check if the method is static and have the correct prototype
var p = method.GetParameters();
if (method.ReturnType != typeof(bool) || p.Count() != 1 || p[0].ParameterType != typeof(BaseGraph))
Debug.LogError($"The function '{method.Name}' marked with the IsCompatibleWithGraph attribute either doesn't return a boolean or doesn't take one parameter of BaseGraph type.");
else
compatibleMethods.Add(method);
}
if (compatibleMethods.Count > 0 || compatibleGraphTypes.Count > 0)
{
// We still need to add the element in specificNode even without specific graph
if (compatibleGraphTypes.Count == 0)
compatibleGraphTypes.Add(null);
foreach (var graphType in compatibleGraphTypes)
{
specificNodes.Add(new NodeSpecificToGraph{
nodeType = nodeType,
isCompatibleWithGraph = compatibleMethods,
compatibleWithGraphType = graphType
});
}
return true;
}
return false;
}
static void BuildScriptCache()
{
foreach (var nodeType in TypeCache.GetTypesDerivedFrom<BaseNode>())
{
if (!IsNodeAccessibleFromMenu(nodeType))
continue;
AddNodeScriptAsset(nodeType);
}
foreach (var nodeViewType in TypeCache.GetTypesDerivedFrom<BaseNodeView>())
{
if (!nodeViewType.IsAbstract)
AddNodeViewScriptAsset(nodeViewType);
}
}
static FieldInfo SetGraph = typeof(BaseNode).GetField("graph", BindingFlags.NonPublic | BindingFlags.Instance);
static void ProvideNodePortCreationDescription(Type nodeType, NodeDescriptions targetDescription, BaseGraph graph = null)
{
var node = Activator.CreateInstance(nodeType) as BaseNode;
try {
SetGraph.SetValue(node, graph);
node.InitializePorts();
node.UpdateAllPorts();
} catch (Exception) { }
foreach (var p in node.inputPorts)
AddPort(p, true);
foreach (var p in node.outputPorts)
AddPort(p, false);
void AddPort(NodePort p, bool input)
{
targetDescription.nodeCreatePortDescription.Add(new PortDescription{
nodeType = nodeType,
portType = p.portData.displayType ?? p.fieldInfo.FieldType,
isInput = input,
portFieldName = p.fieldName,
portDisplayName = p.portData.displayName ?? p.fieldName,
portIdentifier = p.portData.identifier,
});
}
}
static void AddNodeScriptAsset(Type type)
{
var nodeScriptAsset = FindScriptFromClassName(type.Name);
// Try find the class name with Node name at the end
if (nodeScriptAsset == null)
nodeScriptAsset = FindScriptFromClassName(type.Name + "Node");
if (nodeScriptAsset != null)
nodeScripts[type] = nodeScriptAsset;
}
static void AddNodeViewScriptAsset(Type type)
{
var attrs = type.GetCustomAttributes(typeof(NodeCustomEditor), false) as NodeCustomEditor[];
if (attrs != null && attrs.Length > 0)
{
Type nodeType = attrs.First().nodeType;
nodeViewPerType[nodeType] = type;
var nodeViewScriptAsset = FindScriptFromClassName(type.Name);
if (nodeViewScriptAsset == null)
nodeViewScriptAsset = FindScriptFromClassName(type.Name + "View");
if (nodeViewScriptAsset == null)
nodeViewScriptAsset = FindScriptFromClassName(type.Name + "NodeView");
if (nodeViewScriptAsset != null)
nodeViewScripts[type] = nodeViewScriptAsset;
}
}
static MonoScript FindScriptFromClassName(string className)
{
var scriptGUIDs = AssetDatabase.FindAssets($"t:script {className}");
if (scriptGUIDs.Length == 0)
return null;
foreach (var scriptGUID in scriptGUIDs)
{
var assetPath = AssetDatabase.GUIDToAssetPath(scriptGUID);
var script = AssetDatabase.LoadAssetAtPath<MonoScript>(assetPath);
if (script != null && String.Equals(className, Path.GetFileNameWithoutExtension(assetPath), StringComparison.OrdinalIgnoreCase))
return script;
}
return null;
}
public static Type GetNodeViewTypeFromType(Type nodeType)
{
Type view;
if (nodeViewPerType.TryGetValue(nodeType, out view))
return view;
Type baseType = null;
// Allow for inheritance in node views: multiple C# node using the same view
foreach (var type in nodeViewPerType)
{
// Find a view (not first fitted view) of nodeType
if (nodeType.IsSubclassOf(type.Key) && (baseType == null || type.Value.IsSubclassOf(baseType)))
baseType = type.Value;
}
if (baseType != null)
return baseType;
return view;
}
public static IEnumerable<(string path, Type type)> GetNodeMenuEntries(BaseGraph graph = null)
{
foreach (var node in genericNodes.nodePerMenuTitle)
yield return (node.Key, node.Value);
if (graph != null && specificNodeDescriptions.TryGetValue(graph, out var specificNodes))
{
foreach (var node in specificNodes.nodePerMenuTitle)
yield return (node.Key, node.Value);
}
}
public static MonoScript GetNodeViewScript(Type type)
{
nodeViewScripts.TryGetValue(type, out var script);
return script;
}
public static MonoScript GetNodeScript(Type type)
{
nodeScripts.TryGetValue(type, out var script);
return script;
}
public static IEnumerable<Type> GetSlotTypes(BaseGraph graph = null)
{
foreach (var type in genericNodes.slotTypes)
yield return type;
if (graph != null && specificNodeDescriptions.TryGetValue(graph, out var specificNodes))
{
foreach (var type in specificNodes.slotTypes)
yield return type;
}
}
public static IEnumerable<PortDescription> GetEdgeCreationNodeMenuEntry(PortView portView, BaseGraph graph = null)
{
foreach (var description in genericNodes.nodeCreatePortDescription)
{
if (!IsPortCompatible(description))
continue;
yield return description;
}
if (graph != null && specificNodeDescriptions.TryGetValue(graph, out var specificNodes))
{
foreach (var description in specificNodes.nodeCreatePortDescription)
{
if (!IsPortCompatible(description))
continue;
yield return description;
}
}
bool IsPortCompatible(PortDescription description)
{
if ((portView.direction == Direction.Input && description.isInput) || (portView.direction == Direction.Output && !description.isInput))
return false;
if (!BaseGraph.TypesAreConnectable(description.portType, portView.portType))
return false;
return true;
}
}
}
}