mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-15 12:44:58 +00:00
feat: modify plugin loading process
This commit is contained in:
@@ -121,43 +121,6 @@ namespace PepperDash.Essentials.Core
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Devices the basic needs for a Device Factory
|
|
||||||
/// </summary>
|
|
||||||
public abstract class EssentialsDeviceFactory<T> : IDeviceFactory where T:EssentialsDevice
|
|
||||||
{
|
|
||||||
#region IDeviceFactory 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 DeviceFactory.FactoryMethods dictionary for each entry in the TypeNames list
|
|
||||||
/// </summary>
|
|
||||||
public void LoadTypeFactories()
|
|
||||||
{
|
|
||||||
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[];
|
|
||||||
DeviceFactory.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
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract class ProcessorExtensionDeviceFactory<T> : IProcessorExtensionDeviceFactory where T: EssentialsDevice
|
public abstract class ProcessorExtensionDeviceFactory<T> : IProcessorExtensionDeviceFactory where T: EssentialsDevice
|
||||||
{
|
{
|
||||||
#region IProcessorExtensionDeviceFactory Members
|
#region IProcessorExtensionDeviceFactory Members
|
||||||
@@ -203,15 +166,4 @@ namespace PepperDash.Essentials.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string MinimumEssentialsFrameworkVersion { get; protected set; }
|
public string MinimumEssentialsFrameworkVersion { get; protected set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
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; }
|
|
||||||
|
|
||||||
public List<string> DevelopmentEssentialsFrameworkVersions { get; protected set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using PepperDash.Essentials.Core.Config;
|
||||||
|
|
||||||
|
namespace PepperDash.Essentials.Core
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Devices the basic needs for a Device Factory
|
||||||
|
/// </summary>
|
||||||
|
public abstract class EssentialsDeviceFactory<T> : IDeviceFactory where T:EssentialsDevice
|
||||||
|
{
|
||||||
|
#region IDeviceFactory Members
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Type FactoryType => typeof(T);
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
namespace PepperDash.Essentials.Core
|
using PepperDash.Essentials.Core.Config;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace PepperDash.Essentials.Core
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines a class that is capable of loading device types
|
/// Defines a class that is capable of loading device types
|
||||||
@@ -6,8 +10,21 @@
|
|||||||
public interface IDeviceFactory
|
public interface IDeviceFactory
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads all the types to the DeviceFactory
|
/// Gets the type of the factory associated with the current instance.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void LoadTypeFactories();
|
Type FactoryType { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a list of type names associated with the current plugin.
|
||||||
|
/// </summary>
|
||||||
|
List<string> TypeNames { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Builds and returns an <see cref="EssentialsDevice"/> instance based on the provided configuration.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="deviceConfig">The configuration settings used to initialize the device. This parameter cannot be null.</param>
|
||||||
|
/// <returns>An <see cref="EssentialsDevice"/> instance configured according to the specified <paramref
|
||||||
|
/// name="deviceConfig"/>.</returns>
|
||||||
|
EssentialsDevice BuildDevice(DeviceConfig deviceConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using PepperDash.Essentials.Core.Config;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
|
||||||
@@ -12,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; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -808,35 +808,53 @@ 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);
|
||||||
plugin.LoadTypeFactories();
|
|
||||||
|
LoadDeviceFactories(deviceFactory);
|
||||||
|
|
||||||
if(!EssentialsPluginAssemblies.Contains(loadedAssembly))
|
if(!EssentialsPluginAssemblies.Contains(loadedAssembly))
|
||||||
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>
|
||||||
|
|||||||
Reference in New Issue
Block a user