feat: modify plugin loading process

This commit is contained in:
Andrew Welker 2025-07-04 13:33:56 -05:00 committed by Neil Dorin
parent 04c4557528
commit aaa5b0532b
14 changed files with 147 additions and 189 deletions

View file

@ -1,27 +0,0 @@
using System;
namespace PepperDash.Essentials.Core
{
/// <summary>
/// Represents a ConfigSnippetAttribute
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class ConfigSnippetAttribute : Attribute
{
/// <summary>
/// Represents a configuration snippet for the device.
/// </summary>
/// <param name="configSnippet"></param>
public ConfigSnippetAttribute(string configSnippet)
{
ConfigSnippet = configSnippet;
}
/// <summary>
/// Gets the configuration snippet for the device.
/// This snippet can be used in the DeviceConfig to instantiate the device.
/// </summary>
public string ConfigSnippet { get; }
}
}

View file

@ -1,26 +0,0 @@
using System;
namespace PepperDash.Essentials.Core
{
/// <summary>
/// Represents a description attribute for a device.
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class DescriptionAttribute : Attribute
{
/// <summary>
/// Represents a description attribute for a device.
/// </summary>
/// <param name="description"></param>
public DescriptionAttribute(string description)
{
Description = description;
}
/// <summary>
/// Gets the description for the device.
/// </summary>
public string Description { get; }
}
}

View file

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using PepperDash.Core; using PepperDash.Core;
using PepperDash.Essentials.Core.Config;
using Serilog.Events; using Serilog.Events;
namespace PepperDash.Essentials.Core namespace PepperDash.Essentials.Core
@ -100,4 +102,88 @@ namespace PepperDash.Essentials.Core
} }
} }
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class DescriptionAttribute : Attribute
{
private string _Description;
public DescriptionAttribute(string description)
{
//Debug.LogMessage(LogEventLevel.Verbose, "Setting Description: {0}", description);
_Description = description;
}
public string Description
{
get { return _Description; }
}
}
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class ConfigSnippetAttribute : Attribute
{
private string _ConfigSnippet;
public ConfigSnippetAttribute(string configSnippet)
{
//Debug.LogMessage(LogEventLevel.Verbose, "Setting Config Snippet {0}", configSnippet);
_ConfigSnippet = configSnippet;
}
public string ConfigSnippet
{
get { return _ConfigSnippet; }
}
}
/// <summary>
/// Represents a factory for creating processor extension devices.
/// </summary>
/// <typeparam name="T">The type of the processor extension device.</typeparam>
public abstract class ProcessorExtensionDeviceFactory<T> : IProcessorExtensionDeviceFactory where T : EssentialsDevice
{
#region IProcessorExtensionDeviceFactory Members
/// <summary>
/// A list of strings that can be used in the type property of a DeviceConfig object to build an instance of this device
/// </summary>
public List<string> TypeNames { get; protected set; }
/// <summary>
/// Loads an item to the ProcessorExtensionDeviceFactory.ProcessorExtensionFactoryMethods dictionary for each entry in the TypeNames list
/// </summary>
public void LoadFactories()
{
foreach (var typeName in TypeNames)
{
//Debug.LogMessage(LogEventLevel.Verbose, "Getting Description Attribute from class: '{0}'", typeof(T).FullName);
var descriptionAttribute = typeof(T).GetCustomAttributes(typeof(DescriptionAttribute), true) as DescriptionAttribute[];
string description = descriptionAttribute[0].Description;
var snippetAttribute = typeof(T).GetCustomAttributes(typeof(ConfigSnippetAttribute), true) as ConfigSnippetAttribute[];
ProcessorExtensionDeviceFactory.AddFactoryForType(typeName.ToLower(), description, typeof(T), BuildDevice);
}
}
/// <summary>
/// The method that will build the device
/// </summary>
/// <param name="dc">The device config</param>
/// <returns>An instance of the device</returns>
public abstract EssentialsDevice BuildDevice(DeviceConfig dc);
#endregion
}
/// <summary>
/// Devices the basic needs for a Device Factory
/// </summary>
public abstract class EssentialsPluginDeviceFactory<T> : EssentialsDeviceFactory<T>, IPluginDeviceFactory where T : EssentialsDevice
{
/// <summary>
/// Specifies the minimum version of Essentials required for a plugin to run. Must use the format Major.Minor.Build (ex. "1.4.33")
/// </summary>
public string MinimumEssentialsFrameworkVersion { get; protected set; }
}
} }

View file

@ -1,14 +1,16 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using PepperDash.Essentials.Core.Config; using PepperDash.Essentials.Core.Config;
namespace PepperDash.Essentials.Core namespace PepperDash.Essentials.Core
{ {
/// <summary> /// <summary>
/// Provides the basic needs for a Device Factory /// Devices the basic needs for a Device Factory
/// </summary> /// </summary>
public abstract class EssentialsDeviceFactory<T> : IDeviceFactory where T:EssentialsDevice public abstract class EssentialsDeviceFactory<T> : IDeviceFactory where T:EssentialsDevice
{ {
#region IDeviceFactory Members
/// <inheritdoc /> /// <inheritdoc />
public Type FactoryType => typeof(T); public Type FactoryType => typeof(T);
@ -18,10 +20,13 @@ namespace PepperDash.Essentials.Core
public List<string> TypeNames { get; protected set; } public List<string> TypeNames { get; protected set; }
/// <summary> /// <summary>
/// Build the device using the configuration /// The method that will build the device
/// </summary> /// </summary>
/// <param name="dc">The device config</param> /// <param name="dc">The device config</param>
/// <returns>An instance of the device</returns> /// <returns>An instance of the device</returns>
public abstract EssentialsDevice BuildDevice(DeviceConfig dc); public abstract EssentialsDevice BuildDevice(DeviceConfig dc);
#endregion
} }
} }

View file

@ -1,22 +0,0 @@
using System.Collections.Generic;
namespace PepperDash.Essentials.Core
{
/// <summary>
/// EssentialsPluginDevelopmentDeviceFactory class
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class EssentialsPluginDevelopmentDeviceFactory<T> : EssentialsDeviceFactory<T>, IPluginDevelopmentDeviceFactory where T : EssentialsDevice
{
/// <summary>
/// Specifies the minimum version of Essentials required for a plugin to run. Must use the format Major.Minor.Build (ex. "1.4.33")
/// </summary>
public string MinimumEssentialsFrameworkVersion { get; protected set; }
/// <summary>
/// Gets or sets the DevelopmentEssentialsFrameworkVersions
/// </summary>
public List<string> DevelopmentEssentialsFrameworkVersions { get; protected set; }
}
}

View file

@ -1,14 +0,0 @@
namespace PepperDash.Essentials.Core
{
/// <summary>
/// Devices the basic needs for a Device Factory
/// </summary>
public abstract class EssentialsPluginDeviceFactory<T> : EssentialsDeviceFactory<T>, IPluginDeviceFactory where T : EssentialsDevice
{
/// <summary>
/// Specifies the minimum version of Essentials required for a plugin to run. Must use the format Major.Minor.Build (ex. "1.4.33")
/// </summary>
public string MinimumEssentialsFrameworkVersion { get; protected set; }
}
}

View file

@ -1,47 +0,0 @@
using System;
using System.Collections.Generic;
using PepperDash.Essentials.Core.Config;
namespace PepperDash.Essentials.Core
{
/// <summary>
/// Represents a factory for creating processor extension devices.
/// </summary>
/// <typeparam name="T">The type of the processor extension device.</typeparam>
[Obsolete("will be removed in a future version")]
public abstract class ProcessorExtensionDeviceFactory<T> : IProcessorExtensionDeviceFactory where T : EssentialsDevice
{
#region IProcessorExtensionDeviceFactory Members
/// <summary>
/// Gets or sets the TypeNames
/// </summary>
public List<string> TypeNames { get; protected set; }
/// <summary>
/// LoadFactories method
/// </summary>
public void LoadFactories()
{
foreach (var typeName in TypeNames)
{
string description = typeof(T).GetCustomAttributes(typeof(DescriptionAttribute), true) is DescriptionAttribute[] descriptionAttribute && descriptionAttribute.Length > 0
? descriptionAttribute[0].Description
: "No description available";
ProcessorExtensionDeviceFactory.AddFactoryForType(typeName.ToLower(), description, typeof(T), BuildDevice);
}
}
/// <summary>
/// The method that will build the device
/// </summary>
/// <param name="dc">The device config</param>
/// <returns>An instance of the device</returns>
public abstract EssentialsDevice BuildDevice(DeviceConfig dc);
#endregion
}
}

View file

@ -1,6 +1,6 @@
using System; using PepperDash.Essentials.Core.Config;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using PepperDash.Essentials.Core.Config;
namespace PepperDash.Essentials.Core namespace PepperDash.Essentials.Core
{ {

View file

@ -20,7 +20,7 @@ namespace PepperDash.Essentials.Core
/// </summary> /// </summary>
public ProcessorExtensionDeviceFactory() { public ProcessorExtensionDeviceFactory() {
var assy = Assembly.GetExecutingAssembly(); var assy = Assembly.GetExecutingAssembly();
PluginLoader.SetEssentialsAssembly(assy.GetName().Name, assy); PluginLoader.AddLoadedAssembly(assy.GetName().Name, assy);
var extensions = assy.GetTypes().Where(ct => typeof(IProcessorExtensionDeviceFactory) var extensions = assy.GetTypes().Where(ct => typeof(IProcessorExtensionDeviceFactory)
.IsAssignableFrom(ct) && !ct.IsInterface && !ct.IsAbstract); .IsAssignableFrom(ct) && !ct.IsInterface && !ct.IsAbstract);

View file

@ -1,4 +1,4 @@
using System; using PepperDash.Essentials.Core.Config;
using System.Collections.Generic; using System.Collections.Generic;
@ -13,20 +13,6 @@ namespace PepperDash.Essentials.Core
/// Required to define the minimum version for Essentials in the format xx.yy.zz /// Required to define the minimum version for Essentials in the format xx.yy.zz
/// </summary> /// </summary>
string MinimumEssentialsFrameworkVersion { get; } string MinimumEssentialsFrameworkVersion { get; }
}
/// <summary>
/// Defines a factory for creating plugin development devices, including support for specific framework versions.
/// </summary>
/// <remarks>This interface extends <see cref="IPluginDeviceFactory"/> to provide additional functionality
/// specific to plugin development environments.</remarks>
public interface IPluginDevelopmentDeviceFactory : IPluginDeviceFactory
{
/// <summary>
/// Gets a list of Essentials versions that this device is compatible with.
/// </summary>
List<string> DevelopmentEssentialsFrameworkVersions { get; }
} }
} }

View file

@ -198,7 +198,7 @@ public static class PluginLoader
/// this method updates its associated assembly. If no matching name is found, the method does nothing.</remarks> /// this method updates its associated assembly. If no matching name is found, the method does nothing.</remarks>
/// <param name="name">The name used to identify the assembly. This value is case-sensitive and must not be null or empty.</param> /// <param name="name">The name used to identify the assembly. This value is case-sensitive and must not be null or empty.</param>
/// <param name="assembly">The assembly to associate with the specified name. This value must not be null.</param> /// <param name="assembly">The assembly to associate with the specified name. This value must not be null.</param>
public static void SetEssentialsAssembly(string name, Assembly assembly) public static void AddLoadedAssembly(string name, Assembly assembly)
{ {
var loadedAssembly = LoadedAssemblies.FirstOrDefault(la => la.Name.Equals(name)); var loadedAssembly = LoadedAssemblies.FirstOrDefault(la => la.Name.Equals(name));
@ -824,29 +824,27 @@ public static class PluginLoader
/// <remarks>This method verifies that the plugin meets the minimum required Essentials framework version /// <remarks>This method verifies that the plugin meets the minimum required Essentials framework version
/// before loading it. If the plugin fails the dependency check, it is skipped, and a log message is generated. If /// before loading it. If the plugin fails the dependency check, it is skipped, and a log message is generated. If
/// the plugin passes the check, it is loaded, and its type factories are initialized.</remarks> /// the plugin passes the check, it is loaded, and its type factories are initialized.</remarks>
/// <param name="plugin">The plugin to be loaded, implementing the <see cref="IPluginDeviceFactory"/> interface. If the plugin also /// <param name="deviceFactory">The plugin to be loaded, implementing the <see cref="IPluginDeviceFactory"/> interface. If the plugin also
/// implements <see cref="IPluginDevelopmentDeviceFactory"/>, additional checks for development versions are /// implements <see cref="IPluginDevelopmentDeviceFactory"/>, additional checks for development versions are
/// performed.</param> /// performed.</param>
/// <param name="loadedAssembly">The assembly associated with the plugin being loaded. This is used for logging and tracking purposes.</param> /// <param name="loadedAssembly">The assembly associated with the plugin being loaded. This is used for logging and tracking purposes.</param>
private static void LoadCustomPlugin(IPluginDeviceFactory plugin, LoadedAssembly loadedAssembly) private static void LoadCustomPlugin(IPluginDeviceFactory deviceFactory, LoadedAssembly loadedAssembly)
{ {
var passed = plugin is IPluginDevelopmentDeviceFactory developmentPlugin ? Global.IsRunningDevelopmentVersion var passed = Global.IsRunningMinimumVersionOrHigher(deviceFactory.MinimumEssentialsFrameworkVersion);
(developmentPlugin.DevelopmentEssentialsFrameworkVersions, developmentPlugin.MinimumEssentialsFrameworkVersion)
: Global.IsRunningMinimumVersionOrHigher(plugin.MinimumEssentialsFrameworkVersion);
if (!passed) if (!passed)
{ {
Debug.LogMessage(LogEventLevel.Information, Debug.LogInformation(
"\r\n********************\r\n\tPlugin indicates minimum Essentials version {0}. Dependency check failed. Skipping Plugin {1}\r\n********************", "\r\n********************\r\n\tPlugin indicates minimum Essentials version {minimumEssentialsVersion}. Dependency check failed. Skipping Plugin {pluginName}\r\n********************",
plugin.MinimumEssentialsFrameworkVersion, loadedAssembly.Name); deviceFactory.MinimumEssentialsFrameworkVersion, loadedAssembly.Name);
return; return;
} }
else else
{ {
Debug.LogMessage(LogEventLevel.Information, "Passed plugin passed dependency check (required version {0})", plugin.MinimumEssentialsFrameworkVersion); Debug.LogInformation("Passed plugin passed dependency check (required version {essentialsMinimumVersion})", deviceFactory.MinimumEssentialsFrameworkVersion);
} }
Debug.LogMessage(LogEventLevel.Information, "Loading plugin: {0}", loadedAssembly.Name); Debug.LogInformation("Loading plugin: {pluginName}", loadedAssembly.Name);
LoadDeviceFactories(deviceFactory); LoadDeviceFactories(deviceFactory);
@ -854,6 +852,25 @@ public static class PluginLoader
EssentialsPluginAssemblies.Add(loadedAssembly); EssentialsPluginAssemblies.Add(loadedAssembly);
} }
/// <summary>
/// Loads device factories from the specified plugin device factory and registers them for use.
/// </summary>
/// <remarks>This method retrieves metadata from the provided <paramref name="deviceFactory"/>, including
/// type names, descriptions, and configuration snippets, and registers the factory for each device type. The type
/// names are converted to lowercase for registration.</remarks>
/// <param name="deviceFactory">The plugin device factory that provides the device types, descriptions, and factory methods to be registered.</param>
private static void LoadDeviceFactories(IPluginDeviceFactory deviceFactory)
{
foreach (var typeName in deviceFactory.TypeNames)
{
//Debug.LogMessage(LogEventLevel.Verbose, "Getting Description Attribute from class: '{0}'", typeof(T).FullName);
var descriptionAttribute = deviceFactory.FactoryType.GetCustomAttributes(typeof(DescriptionAttribute), true) as DescriptionAttribute[];
string description = descriptionAttribute[0].Description;
var snippetAttribute = deviceFactory.FactoryType.GetCustomAttributes(typeof(ConfigSnippetAttribute), true) as ConfigSnippetAttribute[];
DeviceFactory.AddFactoryForType(typeName.ToLower(), description, deviceFactory.FactoryType, deviceFactory.BuildDevice);
}
}
/// <summary> /// <summary>
/// Loads plugins from the designated plugin directory, processes them, and integrates them into the application. /// Loads plugins from the designated plugin directory, processes them, and integrates them into the application.
/// </summary> /// </summary>

View file

@ -20,7 +20,7 @@ namespace PepperDash.Essentials.Devices.Common
public DeviceFactory() public DeviceFactory()
{ {
var assy = Assembly.GetExecutingAssembly(); var assy = Assembly.GetExecutingAssembly();
PluginLoader.SetEssentialsAssembly(assy.GetName().Name, assy); PluginLoader.AddLoadedAssembly(assy.GetName().Name, assy);
var types = assy.GetTypes().Where(ct => typeof(IDeviceFactory).IsAssignableFrom(ct) && !ct.IsInterface && !ct.IsAbstract); var types = assy.GetTypes().Where(ct => typeof(IDeviceFactory).IsAssignableFrom(ct) && !ct.IsInterface && !ct.IsAbstract);

View file

@ -18,7 +18,7 @@ namespace PepperDash.Essentials
{ {
var assembly = Assembly.GetExecutingAssembly(); var assembly = Assembly.GetExecutingAssembly();
PluginLoader.SetEssentialsAssembly(assembly.GetName().Name, assembly); PluginLoader.AddLoadedAssembly(assembly.GetName().Name, assembly);
var types = assembly.GetTypes().Where(t => typeof(IDeviceFactory).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract); var types = assembly.GetTypes().Where(t => typeof(IDeviceFactory).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);

View file

@ -21,7 +21,7 @@ namespace PepperDash.Essentials
public DeviceFactory() public DeviceFactory()
{ {
var assy = Assembly.GetExecutingAssembly(); var assy = Assembly.GetExecutingAssembly();
PluginLoader.SetEssentialsAssembly(assy.GetName().Name, assy); PluginLoader.AddLoadedAssembly(assy.GetName().Name, assy);
var types = assy.GetTypes().Where(ct => typeof(IDeviceFactory).IsAssignableFrom(ct) && !ct.IsInterface && !ct.IsAbstract); var types = assy.GetTypes().Where(ct => typeof(IDeviceFactory).IsAssignableFrom(ct) && !ct.IsInterface && !ct.IsAbstract);