-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGLDataControl.cs
More file actions
340 lines (306 loc) · 13.6 KB
/
GLDataControl.cs
File metadata and controls
340 lines (306 loc) · 13.6 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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace GLGUI.Advanced
{
public class GLDataControl : GLScrollableControl
{
public GLSkin.GLLabelSkin LabelSkin { get { return labelSkin; } set { labelSkin = value; if(history.Count > 0) SetData(history.First()); } }
public GLSkin.GLLabelSkin LinkLabelSkin { get { return linkLabelSkin; } set { linkLabelSkin = value; if (history.Count > 0) SetData(history.First()); } }
public Dictionary<Type, string[]> Hidden = new Dictionary<Type, string[]>();
public Dictionary<Type, Tuple<string, Func<object>>[]> Links = new Dictionary<Type, Tuple<string, Func<object>>[]>();
public Dictionary<Type, Func<object, object>> Shortcuts = new Dictionary<Type, Func<object, object>>();
private int row;
private Stack<Func<object>> history = new Stack<Func<object>>();
private List<Tuple<Func<object>, GLLabel>> updateData = new List<Tuple<Func<object>, GLLabel>>(256);
private GLFlowLayout horizontal, left, right;
private GLSkin.GLLabelSkin labelSkin, linkLabelSkin;
public GLDataControl(GLGui gui) : base(gui)
{
Render += (s, d) => UpdateData();
horizontal = Add(new GLFlowLayout(gui) { FlowDirection = GLFlowDirection.LeftToRight, AutoSize = true });
left = horizontal.Add(new GLFlowLayout(gui) { FlowDirection = GLFlowDirection.TopDown, AutoSize = true });
var skin = left.Skin;
skin.Space = 0;
left.Skin = skin;
right = horizontal.Add(new GLFlowLayout(gui) { FlowDirection = GLFlowDirection.TopDown, AutoSize = true });
right.Skin = skin;
labelSkin = gui.Skin.LabelEnabled;
linkLabelSkin = gui.Skin.LinkLabelEnabled;
// defaults
Hidden.Add(typeof(IEnumerable), new string[] {
"_items", "_size", "_version", "_syncRoot",
"m_buckets", "m_slots", "m_count", "m_lastIndex", "m_freeList", "m_comparer",
"m_version", "m_siInfo", "m_collection", "m_boundedCapacity", "m_freeNodes",
"m_occupiedNodes", "m_isDisposed", "m_ConsumersCancellationTokenSource",
"m_ProducersCancellationTokenSource", "m_currentAdders",
"buckets", "entries", "count", "version", "freeList", "freeCount", "comparer", "keys", "values",
"IsFixedSize", "IsReadOnly", "IsSynchronized", "SyncRoot" });
Hidden.Add(typeof(Array), new string[] { "LongLength", "Rank", "Count" });
Hidden.Add(typeof(KeyValuePair<,>), new string[] { "key", "value" });
Hidden.Add(typeof(Dictionary<,>), new string[] { "Keys", "Values" });
}
private void AddRow(string name, EventHandler click, Func<object> value)
{
var link = left.Add(new GLLinkLabel(Gui) { AutoSize = true, Text = name, SkinEnabled = linkLabelSkin });
if (value() != null)
link.Click += click;
var label = right.Add(new GLLabel(Gui) { AutoSize = true, Text = (value() ?? "null").ToString(), SkinEnabled = labelSkin });
updateData.Add(new Tuple<Func<object>, GLLabel>(value, label));
row++;
}
public void SetData(object obj)
{
setData(() => obj, true);
}
private List<T> getRelated<T>(Dictionary<Type, T[]> dict, Type type)
{
List<T> list = new List<T>();
while (type != null)
{
if (dict.ContainsKey(type))
list.AddRange(dict[type]);
if (type.IsGenericType)
{
Type genType = type.GetGenericTypeDefinition();
if (dict.ContainsKey(genType))
list.AddRange(dict[genType]);
}
var interfaces = type.GetInterfaces();
foreach(var iface in interfaces)
{
if (dict.ContainsKey(iface))
list.AddRange(dict[iface]);
if (iface.IsGenericType)
{
Type ifaceGenType = iface.GetGenericTypeDefinition();
if (dict.ContainsKey(ifaceGenType))
list.AddRange(dict[ifaceGenType]);
}
}
type = type.BaseType;
}
return list;
}
private void considerShortcutsAndAdd(string name, Func<object> obj)
{
Func<object> value = obj;
var o = obj();
if (o != null)
{
Type type = o.GetType();
Func<object, object> shortcut;
while (type != null)
{
if (Shortcuts.TryGetValue(type, out shortcut))
{
value = () => shortcut(obj());
break;
}
if (type.IsGenericType)
{
Type genType = type.GetGenericTypeDefinition();
if (Shortcuts.TryGetValue(genType, out shortcut))
{
value = () => shortcut(obj());
break;
}
}
var interfaces = type.GetInterfaces();
foreach (var iface in interfaces)
{
if (Shortcuts.TryGetValue(iface, out shortcut))
{
value = () => shortcut(obj());
goto shortcutFound;
}
if (iface.IsGenericType)
{
Type ifaceGenType = iface.GetGenericTypeDefinition();
if (Shortcuts.TryGetValue(ifaceGenType, out shortcut))
{
value = () => shortcut(obj());
goto shortcutFound;
}
}
}
type = type.BaseType;
}
}
shortcutFound:
AddRow(name, (s, e) => setData(value, false), value);
}
private void setData(Func<object> obj, bool resetHistory)
{
left.Clear();
right.Clear();
Horizontal.Value = 0.0f;
Vertical.Value = 0.0f;
updateData.Clear();
if (resetHistory)
history.Clear();
row = 0;
if (history.Count > 0)
{
AddRow("[..]", (s, e) =>
{
if (history.Count < 2)
{
setData(null, true);
return;
}
history.Pop();
setData(history.Pop(), false);
}, history.Peek());
}
if (obj == null || obj() == null)
return;
try
{
Type type = obj().GetType();
Type rtype = type.BaseType;
var hidden = getRelated<string>(Hidden, type);
var links = getRelated<Tuple<string, Func<object>>>(Links, type);
// fields
rtype = type.BaseType;
var fields = new Dictionary<string, FieldInfo>();
foreach (var f in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
string name = f.Name.Split('.').Last();
if (!fields.ContainsKey(name))
fields.Add(name, f);
}
while (rtype != null)
{
var addFields = rtype.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var af in addFields)
{
string name = af.Name.Split('.').Last();
if (!fields.ContainsKey(name))
fields.Add(name, af);
}
rtype = rtype.BaseType;
}
foreach (var field in fields)
{
if (field.Key[0] == '<') // skip property backing fields
continue;
if (hidden.Contains(field.Key))
continue;
var f = field.Value;
considerShortcutsAndAdd(field.Key, () => { try { return f.GetValue(obj()); } catch (Exception) { return null; } });
}
// properties
rtype = type.BaseType;
var properties = new Dictionary<string, PropertyInfo>();
foreach (var p in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
string name = p.Name.Split('.').Last();
if (!properties.ContainsKey(name))
properties.Add(name, p);
}
while (rtype != null)
{
var addProperties = rtype.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var ap in addProperties)
{
string name = ap.Name.Split('.').Last();
if (!properties.ContainsKey(name))
properties.Add(name, ap);
}
rtype = rtype.BaseType;
}
foreach (var property in properties)
{
if (hidden.Contains(property.Key))
continue;
var p = property.Value;
try { p.GetValue(obj(), null); }
catch (Exception) { continue; }
considerShortcutsAndAdd(property.Key, () => { try { return p.GetValue(obj(), null); } catch (Exception) { return "Exception caught in get method"; } });
}
// links
foreach (var link in links)
{
Func<object> linkObj = link.Item2;
considerShortcutsAndAdd(link.Item1, () => { try { return linkObj; } catch (Exception) { return "Exception caught in link method"; } });
}
// arrays and collections
if (type.IsArray)
{
Array array = (Array)obj();
for (int i = 0; i < array.Length; i++)
{
int j = i;
considerShortcutsAndAdd("[" + i + "]", () => { try { return ((Array)obj()).GetValue(j); } catch (Exception) { return null; } });
}
}
else if (type.GetInterfaces().Contains(typeof(ICollection)))
{
int i = 0;
try
{
foreach (object item in ((IEnumerable)obj()))
{
int j = i++;
considerShortcutsAndAdd("[" + i + "]", () => { try { return ((IEnumerable)obj()).Cast<object>().ElementAt(j); } catch(Exception) { return null; } });
}
}
catch (InvalidOperationException)
{
AddRow("retry", (s, e) => { setData(history.Pop(), false); }, () => "InvalidOperationException");
}
catch (IndexOutOfRangeException)
{
AddRow("retry", (s, e) => { setData(history.Pop(), false); }, () => "IndexOutOfRangeException");
}
}
}
catch (Exception e)
{
Console.WriteLine("[GLDataControl] Unknown error occured:\n{0}", e.ToString());
SetData(null);
}
history.Push(obj);
}
MethodInfo updateLayout = typeof(GLLabel).GetMethod("UpdateLayout", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.NonPublic);
object[] updateParams = new object[0];
public void UpdateData()
{
Gui.SuspendLayout();
bool changed = false;
foreach(var u in updateData)
{
try
{
string text = (u.Item1() ?? "null").ToString();
if (u.Item2.Text != text)
{
u.Item2.Text = text;
updateLayout.Invoke(u.Item2, updateParams);
changed = true;
}
}
catch(Exception)
{
if (history.Count > 1)
{
history.Pop();
setData(history.Pop(), false);
}
else
{
setData(null, true);
}
Gui.ResumeLayout();
return;
}
}
Gui.ResumeLayout();
if(changed)
right.Invalidate();
}
}
}