From 5387e94b4c2de06fc2005288a27e325819e094e9 Mon Sep 17 00:00:00 2001 From: LoneWandererProductions Date: Wed, 5 Jun 2024 11:52:04 +0200 Subject: [PATCH] Sync back library --- .../Image/example Polaris.png | Bin 1812 -> 1812 bytes CommonLibraryGuiTests/Image/example.png | Bin 938 -> 938 bytes Plugin/IAsyncPlugin.cs | 99 ++++++++++++++++++ Plugin/IPlugin.cs | 2 +- PluginLoader/PluginLoad.cs | 36 ++++--- 5 files changed, 123 insertions(+), 14 deletions(-) create mode 100644 Plugin/IAsyncPlugin.cs diff --git a/CommonLibraryGuiTests/Image/example Polaris.png b/CommonLibraryGuiTests/Image/example Polaris.png index fbaf0d61449a4e03572982a2da72ddc798364198..b3927c533e282263760f8a87f283bbd547fb7afa 100644 GIT binary patch delta 22 dcmbQjH-&G42e(iO1A|Zr + /// Async version of the Plugin Interface + /// + public interface IAsyncPlugin + { + /// + /// Gets the name. + /// + /// + /// The name. + /// + string Name { get; } + + /// + /// Gets the type. + /// + /// + /// The type. + /// + string Type { get; } + + /// + /// Gets the description. + /// + /// + /// The description. + /// + string Description { get; } + + /// + /// Gets the version. + /// + /// + /// The version. + /// + Version Version { get; } + + /// + /// Gets the commands. + /// + /// + /// The commands. + /// + List Commands { get; } + + /// + /// Executes the asynchronous. + /// + /// Status Code asnc + Task ExecuteAsync(); + + /// + /// Executes the command asynchronous. + /// + /// The identifier. + /// Result object, async. + Task ExecuteCommandAsync(int id); + + /// + /// Gets the plugin type asynchronous. + /// + /// The identifier. + /// Status Code asnc + Task GetPluginTypeAsync(int id); + + /// + /// Gets the information. + /// + /// + /// Info about the plugin + /// + string GetInfo(); + + /// + /// Closes asynchronous. + /// + /// Status Code + Task CloseAsync(); + } +} diff --git a/Plugin/IPlugin.cs b/Plugin/IPlugin.cs index c98a09e..c2554ec 100644 --- a/Plugin/IPlugin.cs +++ b/Plugin/IPlugin.cs @@ -84,7 +84,7 @@ public interface IPlugin /// This method is optional. /// /// The identifier of the command. - /// Status Code + /// Result object object ExecuteCommand(int id); /// diff --git a/PluginLoader/PluginLoad.cs b/PluginLoader/PluginLoad.cs index 86ad065..85171e6 100644 --- a/PluginLoader/PluginLoad.cs +++ b/PluginLoader/PluginLoad.cs @@ -38,6 +38,14 @@ public static class PluginLoad /// public static List PluginContainer { get; private set; } + /// + /// Gets the asynchronous plugin container. + /// + /// + /// The asynchronous plugin container. + /// + public static List AsyncPluginContainer { get; private set; } + /// /// Loads all. /// @@ -56,14 +64,18 @@ public static bool LoadAll(string path, string extension = PluginLoaderResources } PluginContainer = new List(); + AsyncPluginContainer = new List(); foreach (var pluginPath in pluginPaths) { try { var pluginAssembly = LoadPlugin(pluginPath); - var lst = CreateCommands(pluginAssembly).ToList(); - PluginContainer.AddRange(lst); + var syncPlugins = CreateCommands(pluginAssembly).ToList(); + var asyncPlugins = CreateCommands(pluginAssembly).ToList(); + + PluginContainer.AddRange(syncPlugins); + AsyncPluginContainer.AddRange(asyncPlugins); } catch (Exception ex) when (ex is ArgumentException or FileLoadException or ApplicationException or ReflectionTypeLoadException or BadImageFormatException @@ -74,7 +86,7 @@ public static bool LoadAll(string path, string extension = PluginLoaderResources } } - return PluginContainer.Count != 0; + return PluginContainer.Count != 0 || AsyncPluginContainer.Count != 0; } /// @@ -138,20 +150,21 @@ private static Assembly LoadPlugin(string pluginLocation) /// /// Creates the commands. /// + /// Type of Plugin /// The assembly. - /// Adds References to the Commands - /// + /// + /// Adds References to the Commands /// Can't find any type which implements IPlugin in {assembly} from {assembly.Location}.\n" + /// $"Available types: {availableTypes} - /// + /// $"Available types: {availableTypes} /// Could not find the Plugin - private static IEnumerable CreateCommands(Assembly assembly) + private static IEnumerable CreateCommands(Assembly assembly) where T : class { var count = 0; - foreach (var type in assembly.GetTypes().Where(type => typeof(IPlugin).IsAssignableFrom(type))) + foreach (var type in assembly.GetTypes().Where(type => typeof(T).IsAssignableFrom(type))) { - if (Activator.CreateInstance(type) is not IPlugin result) + if (Activator.CreateInstance(type) is not T result) { continue; } @@ -165,12 +178,9 @@ private static IEnumerable CreateCommands(Assembly assembly) yield break; } - var availableTypes = - string.Join(PluginLoaderResources.Separator, assembly.GetTypes().Select(t => t.FullName)); - + var availableTypes = string.Join(PluginLoaderResources.Separator, assembly.GetTypes().Select(t => t.FullName)); var message = string.Concat(PluginLoaderResources.ErrorCouldNotFindPlugin, PluginLoaderResources.Information(assembly, availableTypes)); - throw new ArgumentException(message); } }