-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathVFXGraphValidation.cs
More file actions
171 lines (152 loc) · 5.45 KB
/
Copy pathVFXGraphValidation.cs
File metadata and controls
171 lines (152 loc) · 5.45 KB
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
#define _RESTRICT_SOURCE_CURRENT_ATTRIBUTE
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.VFX;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;
using System.Collections.ObjectModel;
using System.Reflection;
namespace UnityEditor.VFX.UI
{
class VFXGraphValidation
{
VFXGraph m_Graph;
public VFXGraphValidation(VFXGraph graph)
{
m_Graph = graph;
}
public void ValidateGraph()
{
foreach (var child in m_Graph.children)
{
if (child is IVFXSlotContainer)
{
ValidateSlotContainer(child as IVFXSlotContainer, m_Graph);
}
}
//Validate links
foreach (var child in m_Graph.children)
{
if (child is IVFXSlotContainer)
{
ValidateSlotContainerLinks(child as IVFXSlotContainer, m_Graph);
}
}
}
void LogError(object error)
{
Debug.LogError(error);
}
string GetVFXModelDesc(VFXModel model)
{
if (model == null)
return "null(VFXModel)";
return string.Format("'{0}'({1})", model.name, model.GetType().Name);
}
string GetVFXModelDesc(IVFXSlotContainer model)
{
return GetVFXModelDesc(model as VFXModel);
}
bool ValidateVFXModel(VFXModel model, VFXModel expectedParent)
{
if (model == null)
{
LogError("Model error : null. in parent:" + GetVFXModelDesc(expectedParent));
return false;
}
if (model.GetParent() != expectedParent)
{
LogError("Model error : wrong parent. expected:" + GetVFXModelDesc(expectedParent) + " actual:" + GetVFXModelDesc(model.GetParent()));
}
if (!(model is VFXSlot) && model.GetGraph() != m_Graph)
{
LogError("Model error : " + GetVFXModelDesc(model) + " wrong graph. expected:" + GetVFXModelDesc(m_Graph) + " actual:" + GetVFXModelDesc(model.GetParent()));
}
return true;
}
Dictionary<VFXSlot, IVFXSlotContainer> m_SlotOwners = new Dictionary<VFXSlot, IVFXSlotContainer>();
void ValidateSlotContainer(IVFXSlotContainer slotContainer, VFXModel expectedParent)
{
if (!ValidateVFXModel(slotContainer as VFXModel, expectedParent))
return;
ValidateSlots(slotContainer.inputSlots, slotContainer);
ValidateSlots(slotContainer.outputSlots, slotContainer);
if (slotContainer.activationSlot != null)
{
ValidateSlot(slotContainer.activationSlot, slotContainer, null);
}
if (slotContainer is VFXContext)
{
VFXContext context = slotContainer as VFXContext;
foreach (var block in context.children)
{
ValidateSlotContainer(block, context);
}
}
}
void ValidateSlotContainerLinks(IVFXSlotContainer slotContainer, VFXModel expectedParent)
{
ValidateSlotsLinks(slotContainer.inputSlots, slotContainer);
ValidateSlotsLinks(slotContainer.outputSlots, slotContainer);
if (slotContainer.activationSlot != null)
{
ValidateSlotLinks(slotContainer.activationSlot);
}
if (slotContainer is VFXContext)
{
VFXContext context = slotContainer as VFXContext;
foreach (var block in context.children)
{
ValidateSlotContainerLinks(block, context);
}
}
}
void ValidateSlots(IEnumerable<VFXSlot> slots, IVFXSlotContainer expectedOwner)
{
foreach (var slot in slots)
{
ValidateSlot(slot, expectedOwner, null);
}
}
void ValidateSlotsLinks(IEnumerable<VFXSlot> slots, IVFXSlotContainer expectedOwner)
{
foreach (var slot in slots)
{
ValidateSlotLinks(slot);
}
}
void ValidateSlot(VFXSlot slot, IVFXSlotContainer expectedOwner, VFXSlot expectedParent)
{
if (!ValidateVFXModel(slot, expectedParent))
{
return;
}
m_SlotOwners[slot] = expectedOwner;
if (slot.owner != expectedOwner)
{
LogError("Slot error : wrong owner. expected:" + GetVFXModelDesc(expectedOwner) + " actual:" + GetVFXModelDesc(slot.owner));
}
foreach (var subSlot in slot.children)
{
ValidateSlot(subSlot, expectedOwner, slot);
}
}
void ValidateSlotLinks(VFXSlot slot)
{
foreach (var link in slot.LinkedSlots)
{
if (!m_SlotOwners.ContainsKey(link))
{
LogError("Slot :" + GetVFXModelDesc(slot) + "of owner " + GetVFXModelDesc(slot.owner) + " has invalid link :" + GetVFXModelDesc(link));
}
}
foreach (var subSlot in slot.children)
{
ValidateSlotLinks(subSlot);
}
}
}
}