-
Notifications
You must be signed in to change notification settings - Fork 6
/
ExtensionsManager.cs
259 lines (240 loc) · 10.4 KB
/
ExtensionsManager.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using BlueprintEditorPlugin.Attributes;
using BlueprintEditorPlugin.Editors.BlueprintEditor.Extensions;
using BlueprintEditorPlugin.Editors.BlueprintEditor.LayoutManager;
using BlueprintEditorPlugin.Editors.BlueprintEditor.Nodes;
using BlueprintEditorPlugin.Editors.GraphEditor;
using BlueprintEditorPlugin.Editors.GraphEditor.LayoutManager;
using BlueprintEditorPlugin.Editors.GraphEditor.LayoutManager.Algorithms;
using BlueprintEditorPlugin.Models.Nodes;
using FrostyEditor;
using FrostySdk;
using FrostySdk.Managers;
namespace BlueprintEditorPlugin
{
/// <summary>
/// Central class for management of Blueprint Editor extensions.
/// This handles everything, from GraphEditors to Node mappings.
/// </summary>
public static class ExtensionsManager
{
public static readonly Dictionary<string, Type> EntityNodeExtensions = new();
public static readonly Dictionary<string, Type> TransientNodeExtensions = new();
private static List<Type> _graphEditors = new();
public static IEnumerable<Type> GraphEditorExtensions => _graphEditors;
private static List<Type> _blueprintMenuItems = new();
public static IEnumerable<Type> BlueprintMenuItemExtensions => _blueprintMenuItems;
private static List<Type> _entityLayoutManagers = new();
public static IEnumerable<Type> EntityLayoutManagers => _entityLayoutManagers;
/// <summary>
/// Initiates the ExtensionManager
/// </summary>
public static void Initiate()
{
// Register internal Entity Nodes
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
if (type.IsSubclassOf(typeof(EntityNode)) && !type.IsAbstract && type.Name != "EntityMappingNode")
{
try
{
EntityNode node = (EntityNode)Activator.CreateInstance(type);
if (node.ObjectType == null)
{
App.Logger.LogError("Object Type for node {0} was not specified.", type.Name);
}
if (node.IsValid() && !EntityNodeExtensions.ContainsKey(node.ObjectType))
{
EntityNodeExtensions.Add(node.ObjectType, type);
}
}
catch (Exception e)
{
Console.WriteLine($"{type.Name} failed!");
App.Logger.LogError("Entity node {0} caused an exception when processing! Exception: {1}", type.Name, e.Message);
}
}
if (type.GetInterface("ITransient") != null && !type.IsAbstract)
{
try
{
ITransient trans = (ITransient)Activator.CreateInstance(type);
if (trans.IsValid() && !TransientNodeExtensions.ContainsKey(type.Name))
{
TransientNodeExtensions.Add(type.Name, type);
}
else if (TransientNodeExtensions.ContainsKey(type.Name))
{
App.Logger.LogError("To whomever may read this: Please give type {0} a unique name. Transient nodes must have unique type names god dammit!", type.Name);
}
}
catch (Exception e)
{
App.Logger.LogError("Transient node {0} caused an exception when processing! Exception: {1}", type.Name, e.Message);
}
}
if (type.GetInterface("IEbxGraphEditor") != null && !type.IsAbstract)
{
try
{
Application.Current.Dispatcher.Invoke(() =>
{
IEbxGraphEditor graphEditor = (IEbxGraphEditor)Activator.CreateInstance(type);
if (graphEditor.IsValid())
{
_graphEditors.Add(type);
}
});
}
catch (Exception e)
{
App.Logger.LogError("Graph Editor {0} caused an exception when processing! Exception: {1}", type.Name, e.Message);
}
}
}
foreach (string item in Directory.EnumerateFiles("Plugins", "*.dll", SearchOption.AllDirectories))
{
try
{
FileInfo fileInfo = new(item);
Assembly plugin = Assembly.LoadFile(fileInfo.FullName);
foreach (Attribute attribute in plugin.GetCustomAttributes())
{
if (attribute is RegisterEntityNode entityRegister)
{
RegisterExtension(entityRegister);
}
else if (attribute is RegisterEbxGraphEditor graphRegister)
{
RegisterExtension(graphRegister);
}
else if (attribute is RegisterBlueprintMenuExtension menuExtension)
{
RegisterExtension(menuExtension);
}
else if (attribute is RegisterEntityLayoutExtension layoutExtension)
{
RegisterExtension(layoutExtension);
}
}
}
catch (Exception )
{
// Suppress all errors please!
}
}
}
#region Register methods
public static void RegisterExtension(RegisterEbxGraphEditor graphRegister)
{
try
{
var extension = (IEbxGraphEditor)Activator.CreateInstance(graphRegister.GraphType);
if (extension.IsValid() && extension is Control)
{
// Override our internal extension with external one
_graphEditors.Add(graphRegister.GraphType);
}
else if (extension is not Control)
{
App.Logger.LogError("Graph editor {0} must be a control", graphRegister.GraphType.Name);
}
}
catch (Exception)
{
App.Logger.LogError("Could not load graph extension {0}", graphRegister.GraphType.Name);
}
}
public static void RegisterExtension(RegisterEntityNode entityRegister)
{
try
{
var extension = (EntityNode)Activator.CreateInstance(entityRegister.EntityNodeExtension);
if (extension.IsValid())
{
// Override our internal extension with external one
if (EntityNodeExtensions.ContainsKey(extension.ObjectType))
{
EntityNodeExtensions.Remove(extension.ObjectType);
}
EntityNodeExtensions.Add(extension.ObjectType, entityRegister.EntityNodeExtension);
}
}
catch (Exception)
{
App.Logger.LogError("Could not load node extension {0}", entityRegister.EntityNodeExtension.Name);
}
}
public static void RegisterExtension(RegisterBlueprintMenuExtension menuRegister)
{
try
{
var extension = (BlueprintMenuItemExtension)Activator.CreateInstance(menuRegister.MenuType);
if (extension.IsValid())
{
_blueprintMenuItems.Add(menuRegister.MenuType);
}
}
catch (Exception e)
{
App.Logger.LogError("Blueprint editor menu extension {0} threw the exception {1} at {2}", menuRegister.MenuType.Name, e.Message, e.StackTrace);
}
}
public static void RegisterExtension(RegisterEntityLayoutExtension layoutExtension)
{
try
{
var extension = (EntityLayoutManager)Activator.CreateInstance(layoutExtension.LayoutManagerType, new object[] { null });
if (extension.IsValid())
{
_entityLayoutManagers.Add(layoutExtension.LayoutManagerType);
}
}
catch (Exception e)
{
App.Logger.LogError("Blueprint editor menu extension {0} threw the exception {1} at {2}", layoutExtension.LayoutManagerType.Name, e.Message, e.StackTrace);
}
}
#endregion
/// <summary>
/// Gets a valid <see cref="IEbxGraphEditor"/> for the specified <see cref="EbxAssetEntry"/>
/// </summary>
/// <param name="assetEntry"></param>
/// <returns></returns>
public static IEbxGraphEditor GetValidGraphEditor(EbxAssetEntry assetEntry)
{
foreach (Type graphType in GraphEditorExtensions)
{
IEbxGraphEditor graphEditor = (IEbxGraphEditor)Activator.CreateInstance(graphType);
if (graphEditor.IsValid(assetEntry))
{
return graphEditor;
}
}
return null;
}
/// <summary>
/// Gets a valid <see cref="EntityLayoutManager"/> for the specified <see cref="EbxAssetEntry"/>
/// </summary>
/// <param name="assetEntry"></param>
/// <returns></returns>
public static EntityLayoutManager GetValidLayoutManager(EbxAssetEntry assetEntry)
{
foreach (Type layoutType in _entityLayoutManagers)
{
EntityLayoutManager layoutManager = (EntityLayoutManager)Activator.CreateInstance(layoutType, new object[] { null });
if (layoutManager.IsValid(assetEntry))
{
return layoutManager;
}
}
return null;
}
}
}