-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSystemVisualizer.cs
266 lines (237 loc) · 9.08 KB
/
SystemVisualizer.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
/// 5argon - Exceed7 Experiments
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using System.Reflection;
using System;
using System.Linq;
using UnityEngine.Experimental.LowLevel;
using UnityEngine.Experimental.PlayerLoop;
using UnityEditor;
public class SystemVisualizer : EditorWindow
{
[MenuItem("Window/System Visualizer")]
static void OpenWindow()
{
SystemVisualizer window = (SystemVisualizer)EditorWindow.GetWindow(typeof(SystemVisualizer));
window.titleContent = new GUIContent("Systems");
window.Init();
window.Show();
}
Dictionary<string, SystemVisualizerNode> linkDict;
List<SystemVisualizerNode> rootNodes;
public void Init()
{
MakeLinkDictRootNodes(out linkDict, out rootNodes);
//rects = new Rect[dependencies.Count];
style = new GUIStyle(EditorStyles.toolbarTextField);
style.alignment = TextAnchor.MiddleCenter;
SystemVisualizerNode.depthMaxWidth.Clear();
SystemVisualizerNode.depthCurrentOrder.Clear();
foreach(var n in rootNodes)
{
n.Traverse(linkDict);
}
foreach(var n in linkDict.Values)
{
n.UpdateRect();
}
}
public void OnEnable()
{
Init();
}
GUIStyle style;
float scrollValue;
void OnGUI()
{
scrollValue = GUI.HorizontalScrollbar(new Rect(0, position.height-20, position.width, 20), scrollValue, 500, 0, 2000);
BeginWindows();
foreach(var nodes in linkDict.Values)
{
GUI.color = Color.yellow;
GUI.Window(nodes.id, new Rect( nodes.rect.x - scrollValue, nodes.rect.y, nodes.rect.width, nodes.rect.height), DrawNodeWindow, nodes.text, style);
foreach (var n in rootNodes)
{
n.TraverseDrawLine(linkDict,scrollValue);
}
}
EndWindows();
}
void DrawNodeWindow(int id)
{
GUI.DragWindow();
}
public static void DrawNodeCurve(Rect start, Rect end, float scroll)
{
Vector3 startPos = new Vector3(start.x + start.width - scroll, start.y + start.height / 2, 0);
Vector3 endPos = new Vector3(end.x- scroll, end.y + end.height / 2, 0);
Vector3 startTan = startPos + Vector3.right * 50;
Vector3 endTan = endPos + Vector3.left * 50;
Color shadowCol = new Color(0, 0, 0, 0.06f);
Handles.DrawBezier(startPos, endPos, startTan, endTan, Color.black, null, 0.5f);
}
private class SystemVisualizerNode
{
public ScriptBehaviourUpdateOrder.DependantBehavior dependant;
public Rect rect;
public int depth;
public int orderInDepth;
public int textWidth => WidthOf(dependant.Manager.ToString());
public string text => this.dependant.Manager.ToString();
public bool root;
public int id;
private static int runningId;
public SystemVisualizerNode(ScriptBehaviourUpdateOrder.DependantBehavior dependant)
{
this.dependant = dependant;
this.root = dependant.UpdateAfter.Count == 0;
this.id = runningId;
this.depth = 0;
runningId++;
}
public void UpdateRect()
{
int xNow = 0;
for (int i = 0; i < this.depth; i++)
{
xNow += depthMaxWidth[i];
}
this.rect = new Rect(xNow, orderInDepth * 20, textWidth, 20);
}
public static Dictionary<int, int> depthMaxWidth = new Dictionary<int, int>();
public static Dictionary<int, int> depthCurrentOrder= new Dictionary<int, int>();
public void TraverseDrawLine(Dictionary<string, SystemVisualizerNode> linkDict, float scroll)
{
foreach (var next in this.dependant.UpdateBefore)
{
var nextNode = linkDict[next.FullName];
SystemVisualizer.DrawNodeCurve(this.rect, nextNode.rect,scroll);
nextNode.TraverseDrawLine(linkDict,scroll);
}
}
public void Traverse(Dictionary<string, SystemVisualizerNode> linkDict)
{
int maxDepth;
if (depthMaxWidth.TryGetValue(this.depth, out maxDepth))
{
if (textWidth > maxDepth)
{
depthMaxWidth.Remove(this.depth);
depthMaxWidth.Add(this.depth, textWidth);
}
}
else
{
depthMaxWidth.Add(this.depth, textWidth);
}
int currentOrder;
if (depthCurrentOrder.TryGetValue(this.depth, out currentOrder))
{
this.orderInDepth = currentOrder + 1;
depthCurrentOrder.Remove(this.depth);
depthCurrentOrder.Add(this.depth, this.orderInDepth);
}
else
{
depthCurrentOrder.Add(this.depth, 0);
this.orderInDepth = 0;
}
foreach (var next in this.dependant.UpdateBefore)
{
var nextNode = linkDict[next.FullName];
nextNode.depth = this.depth + 1;
nextNode.Traverse(linkDict);
}
}
private int WidthOf(string text)
{
int maxX = 0;
EditorStyles.toolbarTextField.font.RequestCharactersInTexture(text, EditorStyles.toolbarTextField.fontSize, FontStyle.Normal);
foreach (char c in text)
{
CharacterInfo ci;
if (EditorStyles.toolbarTextField.font.GetCharacterInfo(c, out ci))
{
maxX += ci.maxX;
}
}
return maxX;
}
}
private static void MakeLinkDictRootNodes(out Dictionary<string, SystemVisualizerNode> linkDict, out List<SystemVisualizerNode> rootNodes)
{
var deps = GetAllDepsList();
rootNodes = new List<SystemVisualizerNode>();
linkDict = new Dictionary<string, SystemVisualizerNode>();
foreach (var d in deps)
{
var newNode = new SystemVisualizerNode(d);
if (newNode.root)
{
rootNodes.Add(newNode);
}
linkDict.Add(d.Manager.ToString(), newNode);
}
}
static List<ScriptBehaviourUpdateOrder.DependantBehavior> GetAllDepsList()
{
var world = new World("Visualizer");
IEnumerable<Type> allTypes;
foreach (var ass in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
allTypes = ass.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
allTypes = e.Types.Where(t => t != null);
Debug.LogWarning("DefaultWorldInitialization failed loading assembly: " + ass.Location);
}
// Create all ComponentSystem
CreateBehaviourManagersForMatchingTypes(false, allTypes, world);
}
var method = typeof(ScriptBehaviourUpdateOrder).GetMethod("CreateSystemDependencyList", BindingFlags.NonPublic | BindingFlags.Static);
Type insertionBucketType = typeof(ScriptBehaviourUpdateOrder).GetNestedType("InsertionBucket", BindingFlags.NonPublic);
var systems = insertionBucketType.GetField("Systems", BindingFlags.Public | BindingFlags.Instance);
var insertPos = insertionBucketType.GetField("InsertPos", BindingFlags.Public | BindingFlags.Instance);
var insertPos2 = insertionBucketType.GetField("InsertSubPos", BindingFlags.Public | BindingFlags.Instance);
var bucket = (IEnumerable<object>)method.Invoke(null, new object[] { world.BehaviourManagers, PlayerLoop.GetDefaultPlayerLoop() });
List<ScriptBehaviourUpdateOrder.DependantBehavior> all = new List<ScriptBehaviourUpdateOrder.DependantBehavior>();
foreach (object obj in bucket)
{
var returnSystems = (List<ScriptBehaviourUpdateOrder.DependantBehavior>)systems.GetValue(obj);
var returnInsertPos = (int)insertPos.GetValue(obj);
var returnInsertSubPos = (int)insertPos2.GetValue(obj);
all.AddRange(returnSystems);
}
return all;
}
static void CreateBehaviourManagersForMatchingTypes(bool editorWorld, IEnumerable<Type> allTypes, World world)
{
var systemTypes = allTypes.Where(t =>
t.IsSubclassOf(typeof(ComponentSystemBase)) &&
!t.IsAbstract &&
!t.ContainsGenericParameters &&
t.GetCustomAttributes(typeof(DisableAutoCreationAttribute), true).Length == 0);
foreach (var type in systemTypes)
{
if (editorWorld && type.GetCustomAttributes(typeof(ExecuteInEditMode), true).Length == 0)
continue;
GetBehaviourManagerAndLogException(world, type);
}
}
static void GetBehaviourManagerAndLogException(World world, Type type)
{
try
{
world.GetOrCreateManager(type);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
}