From 80865e6b884a4bb289cb4a835a6fa66b3529c872 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Wed, 3 Jun 2009 04:10:26 -0500 Subject: [PATCH] Formalized plugin interface --- Makefile | 4 ++-- WebThing.cs | 15 +++++++++++---- plugins/DefaultPage.cs | 4 ++-- plugins/FFNav.cs | 4 ++-- plugins/LoadProgress.cs | 4 ++-- plugins/Vimish.cs | 4 ++-- 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 48a29e5..afeea07 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,8 @@ all: main.exe plugins main.exe: main.cs WebThing.dll SoupSettings.so gmcs $(CSFLAGS) -r:WebThing.dll main.cs -WebThing.dll: WebThing.cs - gmcs $(CSFLAGS) $(references) -target:library -out:$@ $< +WebThing.dll: WebThing.cs WebThingPlugin.cs + gmcs $(CSFLAGS) $(references) -target:library -out:$@ $^ SoupSettings.so: SoupSettings.c $(CC) $(CFLAGS) -shared $(LDFLAGS) $< -o $@ diff --git a/WebThing.cs b/WebThing.cs index 678194b..523a160 100644 --- a/WebThing.cs +++ b/WebThing.cs @@ -44,7 +44,7 @@ namespace bytex64.WebThing { private Gtk.Table WidgetGrid; private Gtk.Alignment InteriorOverlay; - public Dictionary Plugins; + public Dictionary Plugins; public Dictionary Options; public string[] Arguments; @@ -80,7 +80,7 @@ namespace bytex64.WebThing { _Window.ShowAll(); - Plugins = new Dictionary(); + Plugins = new Dictionary(); // TODO: Conf.Get("plugins") instead of hard-coded path? using (TextReader f = new StreamReader("plugins.conf")) { string line; @@ -155,8 +155,15 @@ namespace bytex64.WebThing { public void LoadPlugin(string assemblyname) { Assembly a = Assembly.LoadFile("plugins/" + assemblyname + ".dll"); - object obj = a.CreateInstance("Plugin", false, BindingFlags.ExactBinding, null, new object[] { this }, null, null); - Plugins[assemblyname] = obj; + Type[] types = a.GetTypes(); + foreach (Type t in types) { + if (t.GetInterface("WebThingPlugin") != null) { + WebThingPlugin p = (WebThingPlugin) a.CreateInstance(t.FullName, false, BindingFlags.ExactBinding, null, null, null, null); + p.Init(this); + Plugins[t.FullName] = p; + Console.WriteLine("Successfully loaded {0}", t.FullName); + } + } } public void AttachWidget(Gtk.Widget widget, CompassDirection direction, AttachOptions xoptions, AttachOptions yoptions) { diff --git a/plugins/DefaultPage.cs b/plugins/DefaultPage.cs index 3de2410..38ed672 100644 --- a/plugins/DefaultPage.cs +++ b/plugins/DefaultPage.cs @@ -1,8 +1,8 @@ using System; using bytex64.WebThing; -public class Plugin { - public Plugin(WebThing wt) { +public class DefaultPage : WebThingPlugin { + public void Init(WebThing wt) { if (wt.Arguments.Length > 0) { wt.OpenUri(wt.Arguments[0]); } else { diff --git a/plugins/FFNav.cs b/plugins/FFNav.cs index 4f22b63..f8ba39c 100644 --- a/plugins/FFNav.cs +++ b/plugins/FFNav.cs @@ -2,10 +2,10 @@ using System; using Gtk; using bytex64.WebThing; -public class Plugin { +public class FFNav : WebThingPlugin { WebThing wt; - public Plugin(WebThing wt) { + public void Init(WebThing wt) { this.wt = wt; wt.WebView.KeyPressEvent += WebView_KeyPress; } diff --git a/plugins/LoadProgress.cs b/plugins/LoadProgress.cs index 9dc6838..a4be542 100644 --- a/plugins/LoadProgress.cs +++ b/plugins/LoadProgress.cs @@ -79,8 +79,8 @@ public class LoadThrobber : Gtk.DrawingArea { } } -public class Plugin { - public Plugin(WebThing wt) { +public class LoadThrobberPlugin : WebThingPlugin { + public void Init(WebThing wt) { LoadThrobber lt = new LoadThrobber(wt); wt.AttachWidget(lt, CompassDirection.Interior); } diff --git a/plugins/Vimish.cs b/plugins/Vimish.cs index 3b4089d..7acdad2 100644 --- a/plugins/Vimish.cs +++ b/plugins/Vimish.cs @@ -3,11 +3,11 @@ using System.Text.RegularExpressions; using Gtk; using bytex64.WebThing; -public class Plugin { +public class Vimish : WebThingPlugin { WebThing wt; Gtk.Entry commandline; - public Plugin(WebThing wt) { + public void Init(WebThing wt) { this.wt = wt; wt.WebView.KeyPressEvent += WebView_KeyPress; -- 2.25.1