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 WebThingPlugin p = (WebThingPlugin) a.CreateInstance(t.FullName, false, BindingFlags.ExactBinding, null, null, null, null);
32 Plugins[t.FullName] = p;
33 Console.WriteLine("Successfully loaded {0}", t.FullName);
34 PluginOrder.Add(t.FullName);
39 public void WebViewSetup(WebView view) {
40 foreach (string key in PluginOrder) {
41 Plugins[key].InitWebView(view);
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);
54 public bool Call(string funcname, params object[] args) {
56 if (args.Length > 0) {
57 foreach (string key in PluginOrder) {
58 WebThingPlugin plugin = Plugins[key];
59 Type ptype = plugin.GetType();
61 List<Type> types = new List<Type>();
62 foreach (object o in args)
63 types.Add(o.GetType());
65 MethodInfo method = ptype.GetMethod(funcname, types.ToArray());
66 if (method == null) continue;
67 method.Invoke(plugin, args);
71 foreach (string key in PluginOrder) {
72 WebThingPlugin plugin = Plugins[key];
73 Type ptype = plugin.GetType();
75 MethodInfo method = ptype.GetMethod(funcname, new Type[] {});
76 if (method == null) continue;
77 method.Invoke(plugin, null);