From a231bb75a6fc3753c5718458e913cff0c3154500 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Wed, 1 Apr 2020 14:42:26 -0600 Subject: [PATCH] Adds back the correct PluginLoader version that supports loading with the new plugin methodology --- .../Config/Comm and IR/GenericComm.cs | 15 +- .../Devices/EssentialsDevice.cs | 2 +- .../Plugins/PluginLoader.cs | 137 ++++++++++++------ 3 files changed, 100 insertions(+), 54 deletions(-) diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Config/Comm and IR/GenericComm.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Config/Comm and IR/GenericComm.cs index dd3a835a..45657e3e 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Config/Comm and IR/GenericComm.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Config/Comm and IR/GenericComm.cs @@ -18,7 +18,6 @@ namespace PepperDash.Essentials.Core /// public class GenericComm : ReconfigurableDevice { - EssentialsControlPropertiesConfig PropertiesConfig; public IBasicCommunication CommPort { get; private set; } @@ -30,7 +29,6 @@ namespace PepperDash.Essentials.Core CommPort = CommFactory.CreateCommForDevice(config); - } public static IKeyed BuildDevice(DeviceConfig dc) @@ -59,20 +57,19 @@ namespace PepperDash.Essentials.Core ConfigWriter.UpdateDeviceConfig(config); } - - } + } public class GenericCommFactory : Essentials.Core.EssentialsDeviceFactory { + public GenericCommFactory() + { + TypeNames = new List() { "genericComm" }; + } + public override EssentialsDevice BuildDevice(DeviceConfig dc) { Debug.Console(1, "Factory Attempting to create new Generic Comm Device"); return new GenericComm(dc); } - - public GenericCommFactory() - { - TypeNames = new List() { "genericComm" }; - } } } \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/EssentialsDevice.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/EssentialsDevice.cs index c3fd2460..7bca06b9 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/EssentialsDevice.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Devices/EssentialsDevice.cs @@ -36,7 +36,7 @@ namespace PepperDash.Essentials.Core public List TypeNames { get; protected set; } - public virtual void LoadTypeFactories() + public void LoadTypeFactories() { foreach (var typeName in TypeNames) { diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Plugins/PluginLoader.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Plugins/PluginLoader.cs index 3870159d..ca9c7b2f 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Plugins/PluginLoader.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Plugins/PluginLoader.cs @@ -8,6 +8,7 @@ using Crestron.SimplSharp.Reflection; using PepperDash.Core; using PepperDash.Essentials.Core; +using PepperDash.Essentials.Core.Plugins; namespace PepperDash.Essentials { @@ -69,6 +70,11 @@ namespace PepperDash.Essentials { version = Global.AssemblyVersion; assembly = Assembly.GetExecutingAssembly(); + break; + } + case ("PepperDashEssentialsBase.dll"): + { + break; } case ("PepperDash_Core.dll"): @@ -338,47 +344,19 @@ namespace PepperDash.Essentials { try { - var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static); - var loadPlugin = methods.FirstOrDefault(m => m.Name.Equals("LoadPlugin")); - if (loadPlugin != null) + if (typeof(IPluginDeviceFactory).IsAssignableFrom(type)) { - Debug.Console(2, "LoadPlugin method found in {0}", type.Name); - - var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static); - - var minimumVersion = fields.FirstOrDefault(p => p.Name.Equals("MinimumEssentialsFrameworkVersion")); - if (minimumVersion != null) + var plugin = (IPluginDeviceFactory)Crestron.SimplSharp.Reflection.Activator.CreateInstance(type); + LoadCustomPlugin(plugin, loadedAssembly); + } + else + { + var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static); + var loadPlugin = methods.FirstOrDefault(m => m.Name.Equals("LoadPlugin")); + if (loadPlugin != null) { - Debug.Console(2, "MinimumEssentialsFrameworkVersion found"); - - var minimumVersionString = minimumVersion.GetValue(null) as string; - - if (!string.IsNullOrEmpty(minimumVersionString)) - { - var passed = Global.IsRunningMinimumVersionOrHigher(minimumVersionString); - - if (!passed) - { - Debug.Console(0, Debug.ErrorLogLevel.Error, "Plugin indicates minimum Essentials version {0}. Dependency check failed. Skipping Plugin", minimumVersionString); - continue; - } - else - { - Debug.Console(0, Debug.ErrorLogLevel.Notice, "Passed plugin passed dependency check (required version {0})", minimumVersionString); - } - } - else - { - Debug.Console(0, Debug.ErrorLogLevel.Warning, "MinimumEssentialsFrameworkVersion found but not set. Loading plugin, but your mileage may vary."); - } + LoadCustomLegacyPlugin(type, loadPlugin, loadedAssembly); } - else - { - Debug.Console(0, Debug.ErrorLogLevel.Warning, "MinimumEssentialsFrameworkVersion not found. Loading plugin, but your mileage may vary."); - } - - Debug.Console(0, Debug.ErrorLogLevel.Notice, "Adding plugin: {0}", loadedAssembly.Name); - loadPlugin.Invoke(null, null); } } catch (Exception e) @@ -400,6 +378,75 @@ namespace PepperDash.Essentials Debug.Console(0, "Done Loading Custom Plugin Types."); } + /// + /// Loads a + /// + /// + static void LoadCustomPlugin(IPluginDeviceFactory plugin, LoadedAssembly loadedAssembly) + { + var passed = Global.IsRunningMinimumVersionOrHigher(plugin.MinimumEssentialsFrameworkVersion); + + if (!passed) + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "Plugin indicates minimum Essentials version {0}. Dependency check failed. Skipping Plugin", plugin.MinimumEssentialsFrameworkVersion); + return; + } + else + { + Debug.Console(0, Debug.ErrorLogLevel.Notice, "Passed plugin passed dependency check (required version {0})", plugin.MinimumEssentialsFrameworkVersion); + } + + Debug.Console(0, Debug.ErrorLogLevel.Notice, "Loading plugin: {0}", loadedAssembly.Name); + plugin.LoadTypeFactories(); + } + + /// + /// Loads a a custom plugin via the legacy method + /// + /// + /// + static void LoadCustomLegacyPlugin(CType type, MethodInfo loadPlugin, LoadedAssembly loadedAssembly) + { + Debug.Console(2, "LoadPlugin method found in {0}", type.Name); + + var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static); + + var minimumVersion = fields.FirstOrDefault(p => p.Name.Equals("MinimumEssentialsFrameworkVersion")); + if (minimumVersion != null) + { + Debug.Console(2, "MinimumEssentialsFrameworkVersion found"); + + var minimumVersionString = minimumVersion.GetValue(null) as string; + + if (!string.IsNullOrEmpty(minimumVersionString)) + { + var passed = Global.IsRunningMinimumVersionOrHigher(minimumVersionString); + + if (!passed) + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "Plugin indicates minimum Essentials version {0}. Dependency check failed. Skipping Plugin", minimumVersionString); + return; + } + else + { + Debug.Console(0, Debug.ErrorLogLevel.Notice, "Passed plugin passed dependency check (required version {0})", minimumVersionString); + } + } + else + { + Debug.Console(0, Debug.ErrorLogLevel.Warning, "MinimumEssentialsFrameworkVersion found but not set. Loading plugin, but your mileage may vary."); + } + } + else + { + Debug.Console(0, Debug.ErrorLogLevel.Warning, "MinimumEssentialsFrameworkVersion not found. Loading plugin, but your mileage may vary."); + } + + Debug.Console(0, Debug.ErrorLogLevel.Notice, "Loading legacy plugin: {0}", loadedAssembly.Name); + loadPlugin.Invoke(null, null); + + } + /// /// Loads plugins /// @@ -415,15 +462,17 @@ namespace PepperDash.Essentials // Deal with any .cplz files UnzipAndMoveCplzArchives(); - if(Directory.Exists(_loadedPluginsDirectoryPath)) { - // Load the assemblies from the loadedPlugins folder into the AppDomain - LoadPluginAssemblies(); + if (Directory.Exists(_loadedPluginsDirectoryPath)) + { + // Load the assemblies from the loadedPlugins folder into the AppDomain + LoadPluginAssemblies(); - // Load the types from any custom plugin assemblies - LoadCustomPluginTypes(); - } + // Load the types from any custom plugin assemblies + LoadCustomPluginTypes(); + } } } + } ///