-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathPluginManager.cs
More file actions
188 lines (155 loc) · 4.96 KB
/
PluginManager.cs
File metadata and controls
188 lines (155 loc) · 4.96 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
/*
* Copyright (c) 2006, Alexandros Frantzis (alf82 [at] freemail [dot] gr)
*
* This file is part of Bless.
*
* Bless is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Bless is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bless; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using System.IO;
using System.Globalization;
using Bless.Util;
namespace Bless.Plugins
{
public class PluginDependencyException : Exception
{
public PluginDependencyException(string msg)
: base(msg)
{ }
}
public class PluginManager
{
Dictionary<string, Plugin> plugins;
Type[] ctorArgTypes;
object[] ctorArgs;
Type pluginType;
private PluginManager(Type pluginType, object[] args)
{
plugins = new Dictionary<string, Plugin>();
this.pluginType = pluginType;
ctorArgTypes = new Type[args.Length];
ctorArgs = args;
for (int i = 0; i < ctorArgs.Length; i++)
ctorArgTypes[i] = ctorArgs[i].GetType();
// find system-wide plugins
string systemPluginDir = FileResourcePath.GetBinPath();
string[] systemPluginFiles = Directory.GetFiles(systemPluginDir);
CompareInfo compare = CultureInfo.InvariantCulture.CompareInfo;
foreach (string file in systemPluginFiles) {
if (compare.IndexOf(file, "plugin", CompareOptions.IgnoreCase) >= 0
&& file.EndsWith(".dll")) {
//Console.WriteLine("Searching File {0}", file);
AddPluginFile(file);
}
}
try {
// find local user plugins
string userPluginDir = FileResourcePath.GetUserPath("plugins");
string[] userPluginFiles = Directory.GetFiles(userPluginDir);
foreach (string file in userPluginFiles) {
if (compare.IndexOf(file, "plugin", CompareOptions.IgnoreCase) >= 0
&& file.EndsWith(".dll")) {
//Console.WriteLine("Searching File {0}", file);
AddPluginFile(file);
}
}
}
catch (IOException e) {
System.Console.WriteLine("Failed to open plugins directory: " + e.Message);
}
catch (System.IO.IOException e) {
System.Console.WriteLine(e.Message);
System.Console.WriteLine("plugins directory may not exist");
}
}
private void AddPluginFile(string file)
{
try {
Assembly asm = Assembly.LoadFile(file);
Type[] types = asm.GetTypes();
foreach(Type t in types) {
if (t.BaseType == pluginType) {
//Console.WriteLine(" Found Type {0}", t.FullName);
ConstructorInfo ctor = t.GetConstructor(ctorArgTypes);
AddToPluginCollection((Plugin)ctor.Invoke(ctorArgs));
}
}
}
catch (Exception e) {
System.Console.WriteLine(e.Message);
}
}
private void AddToPluginCollection(Plugin plugin)
{
plugins.Add(plugin.Name, plugin);
//System.Console.WriteLine("Added plugin {0}", plugin.Name);
}
public bool LoadPlugin(Plugin plugin)
{
StringCollection visited = new StringCollection();
return LoadPluginInternal(plugin, visited);
}
private bool LoadPluginInternal(Plugin plugin, StringCollection visited)
{
visited.Add(plugin.Name);
if (plugin.Loaded)
return true;
foreach(string dep in plugin.Dependencies) {
if (visited.Contains(dep))
throw new PluginDependencyException("Cyclic dependency detected!");
if (!plugins.ContainsKey(dep))
throw new PluginDependencyException(string.Format("Cannot find plugin '{0}' needed by '{1}'", dep, plugin.Name));
if (LoadPluginInternal(plugins[dep], visited) == false)
return false;
}
foreach(string la in plugin.LoadAfter) {
if (visited.Contains(la))
throw new PluginDependencyException("Cyclic LoadAfter association detected!");
if (plugins.ContainsKey(la))
LoadPluginInternal(plugins[la], visited);
}
return plugin.Load();
}
public Plugin[] Plugins {
get {
Plugin[] pa = new Plugin[plugins.Count];
int i = 0;
foreach (Plugin p in plugins.Values)
pa[i++] = p;
return pa;
}
}
static Dictionary<Type, PluginManager> pluginManagers = new Dictionary<Type, PluginManager>();
public static void AddForType(Type pluginType, object[] args)
{
PluginManager pm = new PluginManager(pluginType, args);
pluginManagers[pluginType] = pm;
}
public static PluginManager GetForType(Type pluginType)
{
PluginManager ret = null;
if (pluginManagers.ContainsKey(pluginType)) {
ret = pluginManagers[pluginType];
}
return ret;
}
public static IEnumerable<KeyValuePair<Type, PluginManager>> AllManagers {
get { return pluginManagers; }
}
}
}