From 95c0f570008be87d594267d71a664b7f03733951 Mon Sep 17 00:00:00 2001 From: Heath Volmer Date: Tue, 21 May 2019 13:58:57 -0600 Subject: [PATCH] Plugin loading! --- PepperDashEssentials/ControlSystem.cs | 63 +++++++++++-------- .../Properties/AssemblyInfo.cs | 2 +- essentials-framework | 2 +- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/PepperDashEssentials/ControlSystem.cs b/PepperDashEssentials/ControlSystem.cs index ae2bdcd7..ebfd5f4a 100644 --- a/PepperDashEssentials/ControlSystem.cs +++ b/PepperDashEssentials/ControlSystem.cs @@ -11,7 +11,6 @@ using Crestron.SimplSharp.Reflection; using PepperDash.Core; using PepperDash.Essentials.Core; using PepperDash.Essentials.Core.Config; -using PepperDash.Essentials.Core.Factory; using PepperDash.Essentials.Devices.Common; using PepperDash.Essentials.DM; using PepperDash.Essentials.Fusion; @@ -194,26 +193,38 @@ namespace PepperDash.Essentials var dir = Global.FilePathPrefix + "plugins"; if (Directory.Exists(dir)) { + // TODO Clear out or create localPlugins folder (maybe in program slot folder) + Debug.Console(0, Debug.ErrorLogLevel.Notice, "Plugins directory found, checking for factory plugins"); var di = new DirectoryInfo(dir); var files = di.GetFiles("*.dll"); foreach (FileInfo fi in files) { - Debug.Console(0, "Checking file '{0}' for factory", fi.Name); + // TODO COPY plugin to loadedPlugins folder + + // TODO LOAD that loadedPlugins dll file + var assy = Assembly.LoadFrom(fi.FullName); - var type = assy.GetType("PepperDash.Essentials.Plugin.Factory"); - var factory = Crestron.SimplSharp.Reflection.Activator.CreateInstance(type); - if (factory is PepperDash.Essentials.Core.Factory.IGetCrestronDevice) + var ver = assy.GetName().Version; + var verStr = string.Format("{0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision); + Debug.Console(0, Debug.ErrorLogLevel.Notice, "Loaded plugin file '{0}', version {1}", fi.FullName, verStr); + + // iteratate this assembly's classes, looking for "LoadPlugin()" methods + var types = assy.GetTypes(); + foreach (var type in types) { - Debug.Console(0, Debug.ErrorLogLevel.Notice, "Loaded '{0}' for Crestron device(s)", fi.Name); - FactoryObjects.Add(factory); - } - else - { - Debug.Console(0, Debug.ErrorLogLevel.Warning, - "Plugin '{0}' does not conform to any loadable types. DLL should be removed from plugins folder. Ignoring", - fi.Name); + var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static); + var loadPlugin = methods.FirstOrDefault(m => m.Name.Equals("LoadPlugin")); + if (loadPlugin != null) + { + Debug.Console(0, Debug.ErrorLogLevel.Notice, "Adding type {0}", fi.FullName, type.FullName); + loadPlugin.Invoke(null, null); + } + } + + // plugin dll will be loaded. Any classes in plugin should have a static constructor + // that registers that class with the Core.DeviceFactory } } } @@ -318,7 +329,7 @@ namespace PepperDash.Essentials try { - Debug.Console(0, Debug.ErrorLogLevel.Notice, "Creating device '{0}'", devConf.Key); + Debug.Console(0, Debug.ErrorLogLevel.Notice, "Creating device '{0}', type '{1}'", devConf.Key, devConf.Type); // Skip this to prevent unnecessary warnings if (devConf.Key == "processor") { @@ -348,18 +359,18 @@ namespace PepperDash.Essentials if (newDev == null) newDev = PepperDash.Essentials.BridgeFactory.GetDevice(devConf); - if (newDev == null) // might want to consider the ability to override an essentials "type" - { - // iterate plugin factories - foreach (var f in FactoryObjects) - { - var cresFactory = f as IGetCrestronDevice; - if (cresFactory != null) - { - newDev = cresFactory.GetDevice(devConf, this); - } - } - } + //if (newDev == null) // might want to consider the ability to override an essentials "type" + //{ + // // iterate plugin factories + // foreach (var f in FactoryObjects) + // { + // var cresFactory = f as IGetCrestronDevice; + // if (cresFactory != null) + // { + // newDev = cresFactory.GetDevice(devConf, this); + // } + // } + //} if (newDev != null) DeviceManager.AddDevice(newDev); diff --git a/PepperDashEssentials/Properties/AssemblyInfo.cs b/PepperDashEssentials/Properties/AssemblyInfo.cs index 97d14f56..83beada4 100644 --- a/PepperDashEssentials/Properties/AssemblyInfo.cs +++ b/PepperDashEssentials/Properties/AssemblyInfo.cs @@ -4,5 +4,5 @@ [assembly: AssemblyCompany("PepperDash Technology Corp")] [assembly: AssemblyProduct("PepperDashEssentials")] [assembly: AssemblyCopyright("Copyright © PepperDash Technology Corp 2018")] -[assembly: AssemblyVersion("1.3.2.*")] +[assembly: AssemblyVersion("1.4.0.*")] diff --git a/essentials-framework b/essentials-framework index adc687ef..15205a78 160000 --- a/essentials-framework +++ b/essentials-framework @@ -1 +1 @@ -Subproject commit adc687ef860c0f1d4a3e44c02cb76de12faaa5d3 +Subproject commit 15205a78cb051c9b18f03e816915e81d4c18b450