Skip to content

Commit

Permalink
Sync back library
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWandererProductions committed Jun 5, 2024
1 parent 80fdd09 commit 5387e94
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 14 deletions.
Binary file modified CommonLibraryGuiTests/Image/example Polaris.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CommonLibraryGuiTests/Image/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions Plugin/IAsyncPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: Plugin
* FILE: Plugin/IAsyncPlugin.cs
* PURPOSE: Basic Plugin Support, in this case async
* PROGRAMER: Peter Geinitz (Wayfarer)
* SOURCES: https://docs.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support
*/

// ReSharper disable UnusedParameter.Global, future proofing, it is up to the person how to use this ids
// ReSharper disable UnusedMember.Global


using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Plugin
{
/// <summary>
/// Async version of the Plugin Interface
/// </summary>
public interface IAsyncPlugin
{
/// <summary>
/// Gets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
string Name { get; }

/// <summary>
/// Gets the type.
/// </summary>
/// <value>
/// The type.
/// </value>
string Type { get; }

/// <summary>
/// Gets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
string Description { get; }

/// <summary>
/// Gets the version.
/// </summary>
/// <value>
/// The version.
/// </value>
Version Version { get; }

/// <summary>
/// Gets the commands.
/// </summary>
/// <value>
/// The commands.
/// </value>
List<Command> Commands { get; }

/// <summary>
/// Executes the asynchronous.
/// </summary>
/// <returns>Status Code asnc</returns>
Task<int> ExecuteAsync();

/// <summary>
/// Executes the command asynchronous.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>Result object, async.</returns>
Task<object> ExecuteCommandAsync(int id);

/// <summary>
/// Gets the plugin type asynchronous.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>Status Code asnc</returns>
Task<int> GetPluginTypeAsync(int id);

/// <summary>
/// Gets the information.
/// </summary>
/// <returns>
/// Info about the plugin
/// </returns>
string GetInfo();

/// <summary>
/// Closes asynchronous.
/// </summary>
/// <returns>Status Code</returns>
Task<int> CloseAsync();
}
}
2 changes: 1 addition & 1 deletion Plugin/IPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public interface IPlugin
/// This method is optional.
/// </summary>
/// <param name="id">The identifier of the command.</param>
/// <returns>Status Code</returns>
/// <returns>Result object</returns>
object ExecuteCommand(int id);

/// <summary>
Expand Down
36 changes: 23 additions & 13 deletions PluginLoader/PluginLoad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public static class PluginLoad
/// </value>
public static List<IPlugin> PluginContainer { get; private set; }

/// <summary>
/// Gets the asynchronous plugin container.
/// </summary>
/// <value>
/// The asynchronous plugin container.
/// </value>
public static List<IAsyncPlugin> AsyncPluginContainer { get; private set; }

/// <summary>
/// Loads all.
/// </summary>
Expand All @@ -56,14 +64,18 @@ public static bool LoadAll(string path, string extension = PluginLoaderResources
}

PluginContainer = new List<IPlugin>();
AsyncPluginContainer = new List<IAsyncPlugin>();

foreach (var pluginPath in pluginPaths)
{
try
{
var pluginAssembly = LoadPlugin(pluginPath);
var lst = CreateCommands(pluginAssembly).ToList();
PluginContainer.AddRange(lst);
var syncPlugins = CreateCommands<IPlugin>(pluginAssembly).ToList();
var asyncPlugins = CreateCommands<IAsyncPlugin>(pluginAssembly).ToList();

PluginContainer.AddRange(syncPlugins);
AsyncPluginContainer.AddRange(asyncPlugins);
}
catch (Exception ex) when (ex is ArgumentException or FileLoadException or ApplicationException
or ReflectionTypeLoadException or BadImageFormatException
Expand All @@ -74,7 +86,7 @@ public static bool LoadAll(string path, string extension = PluginLoaderResources
}
}

return PluginContainer.Count != 0;
return PluginContainer.Count != 0 || AsyncPluginContainer.Count != 0;
}

/// <summary>
Expand Down Expand Up @@ -138,20 +150,21 @@ private static Assembly LoadPlugin(string pluginLocation)
/// <summary>
/// Creates the commands.
/// </summary>
/// <typeparam name="T">Type of Plugin</typeparam>
/// <param name="assembly">The assembly.</param>
/// <returns>Adds References to the Commands</returns>
/// <exception cref="ApplicationException">
/// <returns>
/// Adds References to the Commands
/// Can't find any type which implements IPlugin in {assembly} from {assembly.Location}.\n" +
/// $"Available types: {availableTypes}
/// </exception>
/// $"Available types: {availableTypes}</exception>
/// <exception cref="ArgumentException">Could not find the Plugin</exception>
private static IEnumerable<IPlugin> CreateCommands(Assembly assembly)
private static IEnumerable<T> CreateCommands<T>(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;
}
Expand All @@ -165,12 +178,9 @@ private static IEnumerable<IPlugin> 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);
}
}
Expand Down

0 comments on commit 5387e94

Please sign in to comment.