Fix scrolling functionality
[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 = FindAssembly(assemblyname);
27             if (a == null) return;
28             Type[] types = a.GetTypes();
29
30             foreach (Type t in types) {
31                 if (t.IsSubclassOf(typeof(WebThingPlugin))) {
32                     if (Plugins.ContainsKey(t.FullName))
33                         continue;
34                     WebThingPlugin p = (WebThingPlugin) a.CreateInstance(t.FullName, false, BindingFlags.ExactBinding, null, null, null, null);
35                     p.Init(wt);
36                     Plugins[t.FullName] = p;
37                     Console.WriteLine("Successfully loaded {0}", t.FullName);
38                     PluginOrder.Add(t.FullName);
39                 }
40             }
41         }
42
43         private Assembly FindAssembly(string assemblyname) {
44             foreach (string path in Config.PluginPath) {
45                 try {
46                     return Assembly.LoadFile(path + "/" + assemblyname + ".dll");
47                 } catch {
48                 }
49             }
50             Console.WriteLine("Could not find {0}", assemblyname);
51             return null;
52         }
53
54         public void WebViewSetup(WebView view) {
55             foreach (string key in PluginOrder) {
56                 Plugins[key].InitWebView(view);
57             }
58         }
59
60         public void Deinit() {
61             // This reverses the plugin order on the assumption that it
62             // will only be called once on program shutdown
63             PluginOrder.Reverse();
64             foreach (string key in PluginOrder) {
65                 Plugins[key].Deinit(wt);
66             }
67         }
68
69         public bool Call(string funcname, params object[] args) {
70             bool found = false;
71             if (args.Length > 0) {
72                 foreach (string key in PluginOrder) {
73                     WebThingPlugin plugin = Plugins[key];
74                     Type ptype = plugin.GetType();
75
76                     List<Type> types = new List<Type>();
77                     foreach (object o in args)
78                         types.Add(o.GetType());
79
80                     MethodInfo method = ptype.GetMethod(funcname, types.ToArray());
81                     if (method == null) continue;
82                     method.Invoke(plugin, args);
83                     found = true;
84                 }
85             } else {
86                 foreach (string key in PluginOrder) {
87                     WebThingPlugin plugin = Plugins[key];
88                     Type ptype = plugin.GetType();
89
90                     MethodInfo method = ptype.GetMethod(funcname, new Type[] {});
91                     if (method == null) continue;
92                     method.Invoke(plugin, null);
93                     found = true;
94                 }
95             }
96             return found;
97         }
98     }
99 }