-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathNodeTypesBuilder.cs
More file actions
182 lines (153 loc) · 5.61 KB
/
NodeTypesBuilder.cs
File metadata and controls
182 lines (153 loc) · 5.61 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
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ReClassNET.Controls;
using ReClassNET.Nodes;
using ReClassNET.Plugins;
namespace ReClassNET.UI
{
internal static class NodeTypesBuilder
{
private static readonly List<Type[]> defaultNodeTypeGroupList = new List<Type[]>();
private static readonly Dictionary<Plugin, IReadOnlyList<Type>> pluginNodeTypes = new Dictionary<Plugin, IReadOnlyList<Type>>();
static NodeTypesBuilder()
{
defaultNodeTypeGroupList.Add(new[] { typeof(Hex64Node), typeof(Hex32Node), typeof(Hex16Node), typeof(Hex8Node) });
defaultNodeTypeGroupList.Add(new[] { typeof(NIntNode), typeof(Int64Node), typeof(Int32Node), typeof(Int16Node), typeof(Int8Node) });
defaultNodeTypeGroupList.Add(new[] { typeof(NUIntNode), typeof(UInt64Node), typeof(UInt32Node), typeof(UInt16Node), typeof(UInt8Node) });
defaultNodeTypeGroupList.Add(new[] { typeof(BoolNode), typeof(BitFieldNode), typeof(EnumNode) });
defaultNodeTypeGroupList.Add(new[] { typeof(FloatNode), typeof(DoubleNode) });
defaultNodeTypeGroupList.Add(new[] { typeof(Vector4Node), typeof(Vector3Node), typeof(Vector2Node), typeof(Matrix4x4Node), typeof(Matrix3x4Node), typeof(Matrix3x3Node) });
defaultNodeTypeGroupList.Add(new[] { typeof(Utf8TextNode), typeof(Utf8TextPtrNode), typeof(Utf16TextNode), typeof(Utf16TextPtrNode) });
defaultNodeTypeGroupList.Add(new[] { typeof(PointerNode), typeof(ArrayNode), typeof(UnionNode) });
defaultNodeTypeGroupList.Add(new[] { typeof(ClassInstanceNode) });
defaultNodeTypeGroupList.Add(new[] { typeof(VirtualMethodTableNode), typeof(FunctionNode), typeof(FunctionPtrNode) });
}
public static void AddPluginNodeGroup(Plugin plugin, IReadOnlyList<Type> nodeTypes)
{
Contract.Requires(plugin != null);
Contract.Requires(nodeTypes != null);
if (pluginNodeTypes.ContainsKey(plugin))
{
throw new InvalidOperationException(); // TODO
}
pluginNodeTypes.Add(plugin, nodeTypes);
}
public static void RemovePluginNodeGroup(Plugin plugin)
{
Contract.Requires(plugin != null);
pluginNodeTypes.Remove(plugin);
}
public static IEnumerable<ToolStripItem> CreateToolStripButtons(Action<Type> handler)
{
Contract.Requires(handler != null);
var clickHandler = new EventHandler((sender, e) => handler((sender as TypeToolStripButton)?.Value ?? ((TypeToolStripMenuItem)sender).Value));
return CreateToolStripItems(t =>
{
GetNodeInfoFromType(t, out var label, out var icon);
var item = new TypeToolStripButton
{
Value = t,
ToolTipText = label,
DisplayStyle = ToolStripItemDisplayStyle.Image,
Image = icon
};
item.Click += clickHandler;
return item;
}, p => new ToolStripDropDownButton
{
ToolTipText = "",
Image = p.Icon
}, t =>
{
GetNodeInfoFromType(t, out var label, out var icon);
var item = new TypeToolStripMenuItem
{
Value = t,
Text = label,
Image = icon
};
item.Click += clickHandler;
return item;
});
}
public static IEnumerable<ToolStripItem> CreateToolStripMenuItems(Action<Type> handler, bool addNoneType)
{
Contract.Requires(handler != null);
var clickHandler = new EventHandler((sender, e) => handler(((TypeToolStripMenuItem)sender).Value));
var items = CreateToolStripItems(t =>
{
GetNodeInfoFromType(t, out var label, out var icon);
var item = new TypeToolStripMenuItem
{
Value = t,
Text = label,
Image = icon
};
item.Click += clickHandler;
return item;
}, p => new ToolStripMenuItem
{
Text = p.GetType().ToString(),
Image = p.Icon
});
if (addNoneType)
{
ToolStripItem noneItem = new TypeToolStripMenuItem
{
Value = null,
Text = "None"
};
items = items.Prepend(new ToolStripSeparator()).Prepend(noneItem);
}
return items;
}
private static IEnumerable<ToolStripItem> CreateToolStripItems(Func<Type, ToolStripItem> createItem, Func<Plugin, ToolStripDropDownItem> createPluginContainerItem)
{
Contract.Requires(createItem != null);
Contract.Requires(createPluginContainerItem != null);
return CreateToolStripItems(createItem, createPluginContainerItem, createItem);
}
private static IEnumerable<ToolStripItem> CreateToolStripItems(Func<Type, ToolStripItem> createItem, Func<Plugin, ToolStripDropDownItem> createPluginContainerItem, Func<Type, ToolStripItem> createPluginItem)
{
Contract.Requires(createItem != null);
Contract.Requires(createPluginContainerItem != null);
Contract.Requires(createPluginItem != null);
if (!defaultNodeTypeGroupList.Any())
{
return Enumerable.Empty<ToolStripItem>();
}
var items = defaultNodeTypeGroupList
.Select(t => t.Select(createItem))
.Aggregate((l1, l2) => l1.Append(new ToolStripSeparator()).Concat(l2));
if (pluginNodeTypes.Any())
{
foreach (var kv in pluginNodeTypes)
{
var pluginContainerItem = createPluginContainerItem(kv.Key);
pluginContainerItem.Tag = kv.Key;
pluginContainerItem.DropDownItems.AddRange(
kv.Value
.Select(createPluginItem)
.ToArray()
);
items = items.Append(new ToolStripSeparator()).Append(pluginContainerItem);
}
}
return items;
}
private static void GetNodeInfoFromType(Type nodeType, out string label, out Image icon)
{
Contract.Requires(nodeType != null);
var node = BaseNode.CreateInstanceFromType(nodeType, false);
if (node == null)
{
throw new InvalidOperationException($"'{nodeType}' is not a valid node type.");
}
node.GetUserInterfaceInfo(out label, out icon);
}
}
}