From 9ffed42e1a4f47bc1d7e9e7933f2304540937ae7 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Thu, 2 May 2024 14:11:57 +0200 Subject: [PATCH] more load plugins methods --- Pilz.Plugins/PluginManagerT.cs | 101 +++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 29 deletions(-) diff --git a/Pilz.Plugins/PluginManagerT.cs b/Pilz.Plugins/PluginManagerT.cs index 5f17a58..d6d4577 100644 --- a/Pilz.Plugins/PluginManagerT.cs +++ b/Pilz.Plugins/PluginManagerT.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Net.Http.Headers; using System.Reflection; using System.Reflection.Metadata.Ecma335; using System.Text; @@ -13,6 +14,11 @@ namespace Pilz.Plugins { private readonly List loadedPlugins = []; + /// + /// The default initialization parameters used when no parameters passed. Commonly used for dynamically loading assemblies. + /// + public object?[] DefaultParameters { get; set; } = []; + /// /// Returns a list of all currently loaded plugins. /// @@ -20,7 +26,7 @@ namespace Pilz.Plugins private void CurrentDomain_AssemblyLoad(object? sender, AssemblyLoadEventArgs args) { - LoadPlugin(args.LoadedAssembly); + LoadPlugins(args.LoadedAssembly); } /// @@ -50,11 +56,20 @@ namespace Pilz.Plugins var results = new List>(); foreach (var assembly in assemblies) - results.Add(LoadPlugin(assembly, parameters)); + results.Add(LoadPlugins(assembly, parameters)); return results; } + /// + /// Loads plugins from already loaded assemblies for the current . + /// + /// + public IEnumerable> LoadOwnPlugins(params object?[]? parameters) + { + return LoadOwnPlugins(false, parameters); + } + /// /// Loads plugins from already loaded assemblies for the current . /// @@ -68,13 +83,13 @@ namespace Pilz.Plugins AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad; foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) - results.Add(LoadPlugin(assembly, parameters)); + results.Add(LoadPlugins(assembly, parameters)); return results; } /// - /// Loads an assembly from the given file path and then loads the plugin from the assembly. + /// Loads an assembly from the given file path and then loads plugins from the assembly. /// /// /// @@ -105,12 +120,12 @@ namespace Pilz.Plugins } /// - /// Loads the plugin from a given assembly. + /// Load plugins from a given assembly. /// /// /// /// - public PluginLoadInfo LoadPlugin(Assembly assembly, params object?[]? parameters) + public PluginLoadInfo LoadPlugins(Assembly assembly, params object?[]? parameters) { var result = new PluginLoadInfo { @@ -122,9 +137,27 @@ namespace Pilz.Plugins return result; } + /// + /// Loads the plugin from a given type. + ///
Commonly used to ensure loading core plugins. + ///
+ /// + /// + /// + public PluginLoadInfo LoadPlugin(params object?[]? parameters) where TPlugin : TPluginInterface + { + var result = new PluginLoadInfo + { + Assembly = typeof(TPlugin).Assembly + }; + + LoadPlugin(result, typeof(TPlugin), parameters); + + return result; + } + private void LoadPlugin(PluginLoadInfo result, params object?[]? parameters) { - var irmplugin = typeof(TPluginInterface); if (result.Assembly == null) result.Status = PluginLoadStatus.NoValidPlugin; @@ -133,32 +166,42 @@ namespace Pilz.Plugins else { foreach (var type in result.Assembly.GetTypes()) - { - if (irmplugin.IsAssignableFrom(type)) - { - var info = Activator.CreateInstance(); - info.Assembly = result.Assembly; - result.PluginsInternal.Add(info); - - try - { - if (Activator.CreateInstance(type, parameters) is TPluginInterface plugin) - { - info.Plugin = plugin; - info.Status = PluginStatus.Success; - loadedPlugins.Add(info); - } - } - catch - { - info.Status = PluginStatus.ErrorAtInitializing; - } - } - } + LoadPlugin(result, type, parameters); } if (result.PluginsInternal.Count == 0) result.Status = PluginLoadStatus.NoValidPlugin; } + + private void LoadPlugin(PluginLoadInfo result, Type type, params object?[]? parameters) + { + if (parameters == null || parameters.Length == 0) + parameters = DefaultParameters; + + if (loadedPlugins.Any(n => n.Plugin != null && n.Plugin.GetType() == type)) + result.Status = PluginLoadStatus.AlreadyLoaded; + else if (type.IsAssignableTo(typeof(TPluginInterface))) + { + var info = Activator.CreateInstance(); + info.Assembly = result.Assembly; + result.PluginsInternal.Add(info); + + try + { + if (Activator.CreateInstance(type, parameters) is TPluginInterface plugin) + { + info.Plugin = plugin; + info.Status = PluginStatus.Success; + loadedPlugins.Add(info); + } + } + catch + { + info.Status = PluginStatus.ErrorAtInitializing; + } + } + else + result.Status = PluginLoadStatus.NoValidPlugin; + } } }