Get rid of Vimish configuration autosave. It was a silly idea.
[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                     WebThingPlugin p = (WebThingPlugin) a.CreateInstance(t.FullName, false, BindingFlags.ExactBinding, null, null, null, null);
31                     p.Init(wt);
32                     Plugins[t.FullName] = p;
33                     Console.WriteLine("Successfully loaded {0}", t.FullName);
34                     PluginOrder.Add(t.FullName);
35                 }
36             }
37         }
38
39         public void WebViewSetup(WebView view) {
40             foreach (string key in PluginOrder) {
41                 Plugins[key].InitWebView(view);
42             }
43         }
44
45         public void Deinit() {
46             // This reverses the plugin order on the assumption that it
47             // will only be called once on program shutdown
48             PluginOrder.Reverse();
49             foreach (string key in PluginOrder) {
50                 Plugins[key].Deinit(wt);
51             }
52         }
53
54         public bool Call(string funcname, params object[] args) {
55             bool found = false;
56             if (args.Length > 0) {
57                 foreach (string key in PluginOrder) {
58                     WebThingPlugin plugin = Plugins[key];
59                     Type ptype = plugin.GetType();
60
61                     List<Type> types = new List<Type>();
62                     foreach (object o in args)
63                         types.Add(o.GetType());
64
65                     MethodInfo method = ptype.GetMethod(funcname, types.ToArray());
66                     if (method == null) continue;
67                     method.Invoke(plugin, args);
68                     found = true;
69                 }
70             } else {
71                 foreach (string key in PluginOrder) {
72                     WebThingPlugin plugin = Plugins[key];
73                     Type ptype = plugin.GetType();
74
75                     MethodInfo method = ptype.GetMethod(funcname, new Type[] {});
76                     if (method == null) continue;
77                     method.Invoke(plugin, null);
78                     found = true;
79                 }
80             }
81             return found;
82         }
83     }
84 }