56bfe55bd5228dd6b913e6f9f888af40c88a5479
[WebThing.git] / PluginManager.cs
1 using System;
2 using System.Reflection;
3 using System.Collections.Generic;
4 using WebKit;
5
6 namespace bytex64.WebThing {
7     public class PluginManager {
8         public Dictionary<string,WebThingPlugin> Plugins;
9         public List<string> PluginOrder;
10         
11         private WebThing wt;
12
13         public PluginManager(WebThing wt) {
14             Plugins = new Dictionary<string,WebThingPlugin>();
15             PluginOrder = new List<string>();
16             this.wt = wt;
17         }
18
19         public void Load() {
20             foreach (string plugin in Config.Plugins) {
21                 LoadPlugin(plugin);
22             }
23         }
24
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))
31                         continue;
32                     WebThingPlugin p = (WebThingPlugin) a.CreateInstance(t.FullName, false, BindingFlags.ExactBinding, null, null, null, null);
33                     p.Init(wt);
34                     Plugins[t.FullName] = p;
35                     Console.WriteLine("Successfully loaded {0}", t.FullName);
36                     PluginOrder.Add(t.FullName);
37                 }
38             }
39         }
40
41         public void WebViewSetup(WebView view) {
42             foreach (string key in PluginOrder) {
43                 Plugins[key].InitWebView(view);
44             }
45         }
46
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);
53             }
54         }
55
56         public bool Call(string funcname, params object[] args) {
57             bool found = false;
58             if (args.Length > 0) {
59                 foreach (string key in PluginOrder) {
60                     WebThingPlugin plugin = Plugins[key];
61                     Type ptype = plugin.GetType();
62
63                     List<Type> types = new List<Type>();
64                     foreach (object o in args)
65                         types.Add(o.GetType());
66
67                     MethodInfo method = ptype.GetMethod(funcname, types.ToArray());
68                     if (method == null) continue;
69                     method.Invoke(plugin, args);
70                     found = true;
71                 }
72             } else {
73                 foreach (string key in PluginOrder) {
74                     WebThingPlugin plugin = Plugins[key];
75                     Type ptype = plugin.GetType();
76
77                     MethodInfo method = ptype.GetMethod(funcname, new Type[] {});
78                     if (method == null) continue;
79                     method.Invoke(plugin, null);
80                     found = true;
81                 }
82             }
83             return found;
84         }
85     }
86 }