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 = FindAssembly(assemblyname);
27 if (a == null) return;
28 Type[] types = a.GetTypes();
30 foreach (Type t in types) {
31 if (t.IsSubclassOf(typeof(WebThingPlugin))) {
32 if (Plugins.ContainsKey(t.FullName))
34 WebThingPlugin p = (WebThingPlugin) a.CreateInstance(t.FullName, false, BindingFlags.ExactBinding, null, null, null, null);
36 Plugins[t.FullName] = p;
37 Console.WriteLine("Successfully loaded {0}", t.FullName);
38 PluginOrder.Add(t.FullName);
43 private Assembly FindAssembly(string assemblyname) {
44 foreach (string path in Config.PluginPath) {
46 return Assembly.LoadFile(path + "/" + assemblyname + ".dll");
50 Console.WriteLine("Could not find {0}", assemblyname);
54 public void WebViewSetup(WebView view) {
55 foreach (string key in PluginOrder) {
56 Plugins[key].InitWebView(view);
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);
69 public bool Call(string funcname, params object[] args) {
71 if (args.Length > 0) {
72 foreach (string key in PluginOrder) {
73 WebThingPlugin plugin = Plugins[key];
74 Type ptype = plugin.GetType();
76 List<Type> types = new List<Type>();
77 foreach (object o in args)
78 types.Add(o.GetType());
80 MethodInfo method = ptype.GetMethod(funcname, types.ToArray());
81 if (method == null) continue;
82 method.Invoke(plugin, args);
86 foreach (string key in PluginOrder) {
87 WebThingPlugin plugin = Plugins[key];
88 Type ptype = plugin.GetType();
90 MethodInfo method = ptype.GetMethod(funcname, new Type[] {});
91 if (method == null) continue;
92 method.Invoke(plugin, null);