2 using System.Reflection;
3 using System.Collections.Generic;
6 namespace bytex64.WebThing {
7 public class PluginManager {
8 public Dictionary<string,WebThingPlugin> Plugins;
9 public List<string> PluginOrder;
13 public PluginManager(WebThing wt) {
14 Plugins = new Dictionary<string,WebThingPlugin>();
15 PluginOrder = new List<string>();
20 foreach (string plugin in Config.Plugins) {
25 public void LoadPlugin(string assemblyname) {
26 Assembly a = Assembly.LoadFile(Environment.GetEnvironmentVariable("WEBTHING_HOME") + "/plugins/" + assemblyname + ".dll");
27 Type[] types = a.GetTypes();
28 foreach (Type t in types) {
29 if (t.IsSubclassOf(typeof(WebThingPlugin))) {
30 if (Plugins.ContainsKey(t.FullName))
32 WebThingPlugin p = (WebThingPlugin) a.CreateInstance(t.FullName, false, BindingFlags.ExactBinding, null, null, null, null);
34 Plugins[t.FullName] = p;
35 Console.WriteLine("Successfully loaded {0}", t.FullName);
36 PluginOrder.Add(t.FullName);
41 public void WebViewSetup(WebView view) {
42 foreach (string key in PluginOrder) {
43 Plugins[key].InitWebView(view);
47 public void Deinit() {
48 // This reverses the plugin order on the assumption that it
49 // will only be called once on program shutdown
50 PluginOrder.Reverse();
51 foreach (string key in PluginOrder) {
52 Plugins[key].Deinit(wt);
56 public bool Call(string funcname, params object[] args) {
58 if (args.Length > 0) {
59 foreach (string key in PluginOrder) {
60 WebThingPlugin plugin = Plugins[key];
61 Type ptype = plugin.GetType();
63 List<Type> types = new List<Type>();
64 foreach (object o in args)
65 types.Add(o.GetType());
67 MethodInfo method = ptype.GetMethod(funcname, types.ToArray());
68 if (method == null) continue;
69 method.Invoke(plugin, args);
73 foreach (string key in PluginOrder) {
74 WebThingPlugin plugin = Plugins[key];
75 Type ptype = plugin.GetType();
77 MethodInfo method = ptype.GetMethod(funcname, new Type[] {});
78 if (method == null) continue;
79 method.Invoke(plugin, null);