Repository navigation
Expand file tree
/
Copy pathPluginServices.cs
More file actions
145 lines (134 loc) · 4.63 KB
/
Copy pathPluginServices.cs
File metadata and controls
145 lines (134 loc) · 4.63 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using PluginCore;
using PluginCore.Helpers;
using PluginCore.Localization;
using PluginCore.Managers;
namespace FlashDevelop.Utilities
{
class PluginServices
{
public static List<String> KnownDLLs;
public static List<AvailablePlugin> AvailablePlugins;
public static Int32 REQUIRED_API_LEVEL = 1;
static PluginServices()
{
KnownDLLs = new List<String>();
AvailablePlugins = new List<AvailablePlugin>();
}
/// <summary>
/// Finds plugins from the specified folder
/// </summary>
public static void FindPlugins(String path)
{
EnsureUpdatedPlugins(path);
foreach (String fileOn in Directory.GetFiles(path, "*.dll"))
{
String name = Path.GetFileNameWithoutExtension(fileOn);
if (name != "PluginCore" && !KnownDLLs.Contains(name))
{
KnownDLLs.Add(name);
AddPlugin(fileOn);
}
}
}
/// <summary>
/// Ensures that the plugins are updated before init
/// </summary>
public static void EnsureUpdatedPlugins(String path)
{
foreach (String file in Directory.GetFiles(path))
{
FileHelper.EnsureUpdatedFile(file);
}
}
/// <summary>
/// Finds a plugin from the plugin collection
/// </summary>
public static AvailablePlugin Find(String guid)
{
foreach (AvailablePlugin plugin in AvailablePlugins)
{
if (plugin.Instance.Guid == guid)
{
return plugin;
}
}
return null;
}
/// <summary>
/// Disposes all available plugins that are active
/// </summary>
public static void DisposePlugins()
{
foreach (AvailablePlugin pluginOn in AvailablePlugins)
{
try
{
if (pluginOn.IsActive)
{
pluginOn.Instance.Dispose();
}
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
}
AvailablePlugins.Clear();
}
/// <summary>
/// Adds a plugin to the plugin collection
/// </summary>
private static void AddPlugin(String fileName)
{
Assembly pluginAssembly = Assembly.LoadFrom(fileName);
try
{
foreach (Type pluginType in pluginAssembly.GetTypes())
{
if (pluginType.IsPublic && !pluginType.IsAbstract)
{
Type typeInterface = pluginType.GetInterface("PluginCore.IPlugin", true);
if (typeInterface != null)
{
AvailablePlugin newPlugin = new AvailablePlugin(fileName);
newPlugin.Instance = (IPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
if (newPlugin.Instance.Api != REQUIRED_API_LEVEL)
{
// Invalid plugin, ignore...
throw new Exception("Required API level does not match.");
}
if (!Globals.Settings.DisabledPlugins.Contains(newPlugin.Instance.Guid))
{
newPlugin.Instance.Initialize();
newPlugin.IsActive = true;
}
if (!AvailablePlugins.Contains(newPlugin))
{
AvailablePlugins.Add(newPlugin);
}
}
}
}
}
catch (Exception ex)
{
String message = TextHelper.GetString("Info.UnableToLoadPlugin");
ErrorManager.ShowWarning(message + " \n" + fileName, ex);
}
}
}
public class AvailablePlugin
{
public Boolean IsActive = false;
public String Assembly = String.Empty;
public IPlugin Instance = null;
public AvailablePlugin(String assembly)
{
this.Assembly = assembly;
}
}
}