forked from silahian/VisualHFT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginManager.cs
190 lines (178 loc) · 7.49 KB
/
PluginManager.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
using log4net.Plugin;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using VisualHFT.Commons.Helpers;
using VisualHFT.Commons.Studies;
using VisualHFT.DataRetriever;
namespace VisualHFT.PluginManager
{
public static class PluginManager
{
private static List<IPlugin> ALL_PLUGINS = new List<IPlugin>();
private static object _locker = new object();
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static async Task LoadPlugins()
{
// 1. By default load all dll's in current Folder.
var pluginsDirectory = AppDomain.CurrentDomain.BaseDirectory; // This gets the directory where your WPF app is running
await Task.Run(() =>
{
lock (_locker)
{
LoadPluginsByDirectory(pluginsDirectory);
}
});
}
public static List<IPlugin> AllPlugins { get { lock (_locker) return ALL_PLUGINS; } }
public static bool AllPluginsReloaded { get; internal set; }
public static async Task StartPlugins()
{
List<Task> startTasks = new List<Task>();
lock (_locker)
{
if (ALL_PLUGINS.Count == 0) { return; }
foreach (var plugin in ALL_PLUGINS)
{
// Add a task for starting each plugin and handle exceptions within the task
startTasks.Add(Task.Run(() =>
{
try
{
StartPlugin(plugin);
}
catch (Exception ex)
{
plugin.Status = ePluginStatus.STOPPED_FAILED;
//SYSTEM ERROR LOGS
//---------------------
string msg = $"Plugin failed to Start.";
HelperNotificationManager.Instance.AddNotification(plugin.Name, msg, HelprNorificationManagerTypes.ERROR, HelprNorificationManagerCategories.PLUGINS, ex);
}
}));
}
}
// Await all the tasks
await Task.WhenAll(startTasks);
}
public static void StartPlugin(IPlugin plugin)
{
if (plugin != null)
{
if (plugin.Status == ePluginStatus.MALFUNCTIONING || plugin.Status == ePluginStatus.STARTING || plugin.Status == ePluginStatus.STARTED)
return;
if (plugin is IDataRetriever dataRetriever)
dataRetriever.StartAsync();
else if (plugin is IStudy study)
study.StartAsync();
else if (plugin is IMultiStudy mstudy)
mstudy.StartAsync();
}
}
public static void StopPlugin(IPlugin plugin)
{
try
{
if (plugin != null)
{
if (plugin.Status == ePluginStatus.STOPPED || plugin.Status == ePluginStatus.STOPPED_FAILED || plugin.Status == ePluginStatus.STARTING) { return; }
if (plugin is IDataRetriever dataRetriever)
dataRetriever.StopAsync();
else if (plugin is IStudy study)
study.StopAsync();
else if (plugin is IMultiStudy mstudy)
mstudy.StopAsync();
plugin.Status = ePluginStatus.STOPPED;
}
}
catch (Exception ex)
{
plugin.Status = ePluginStatus.STOPPED_FAILED;
//SYSTEM ERROR LOGS
//---------------------
string msg = $"Plugin failed to Stop.";
HelperNotificationManager.Instance.AddNotification(plugin.Name, msg, HelprNorificationManagerTypes.ERROR, HelprNorificationManagerCategories.PLUGINS, ex);
}
}
public static void SettingPlugin(IPlugin plugin)
{
UserControl _ucSettings = null;
try
{
if (plugin != null)
{
var formSettings = new View.PluginSettings();
plugin.CloseSettingWindow = () => {
formSettings.Close();
};
_ucSettings = plugin.GetUISettings() as UserControl;
if (_ucSettings == null)
{
plugin.CloseSettingWindow = null;
formSettings = null;
return;
}
formSettings.MainGrid.Children.Add(_ucSettings);
formSettings.Title = $"{plugin.Name} Settings";
formSettings.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
formSettings.Topmost = true;
formSettings.ShowInTaskbar = false;
formSettings.ShowDialog();
}
}
catch (Exception ex)
{
plugin.Status = ePluginStatus.STOPPED_FAILED;
//SYSTEM ERROR LOGS
//---------------------
string msg = $"Plugin failed to load settings.";
HelperNotificationManager.Instance.AddNotification(plugin.Name, msg, HelprNorificationManagerTypes.ERROR, HelprNorificationManagerCategories.PLUGINS, ex);
}
}
public static void UnloadPlugins()
{
lock (_locker)
{
if (ALL_PLUGINS.Count == 0) { return; }
foreach (var plugin in ALL_PLUGINS.OfType<IDisposable>())
{
plugin.Dispose();
}
}
}
private static void LoadPluginsByDirectory(string pluginsDirectory)
{
foreach (var file in Directory.GetFiles(pluginsDirectory, "*.dll"))
{
var assembly = Assembly.LoadFrom(file);
foreach (var type in assembly.GetExportedTypes())
{
if (!type.IsAbstract && type.GetInterfaces().Contains(typeof(IPlugin)))
{
try
{
var plugin = Activator.CreateInstance(type) as IPlugin;
if (string.IsNullOrEmpty(plugin.Name))
continue;
plugin.Status = ePluginStatus.LOADING;
ALL_PLUGINS.Add(plugin);
log.Info("Plugin assemblies for: " + plugin.Name + " have loaded OK.");
}
catch (Exception ex)
{
//SYSTEM ERROR LOGS
//---------------------
string msg = $"Plugin's assemblies located in {file} have failed to load.";
HelperNotificationManager.Instance.AddNotification("Loading Plugins", msg, HelprNorificationManagerTypes.ERROR, HelprNorificationManagerCategories.PLUGINS, ex);
}
}
}
}
}
}
}