PluginFeature

This commit is contained in:
2023-12-05 13:52:35 +01:00
parent 066d5a1a81
commit 0ecbde3fd7
7 changed files with 164 additions and 126 deletions

View File

@@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Pilz.Plugins.Advanced
{
public sealed class PluginFeatureController
{
/// <summary>
/// The default public instance that can be used by plugins and the interface providing software.
/// </summary>
public static PluginFeatureController Instance { get; private set; } = new();
private readonly List<PluginFeature> features = new();
/// <summary>
/// Returns a collection of all registred <see cref="PluginFeature"/> instances.
/// </summary>
public IReadOnlyList<PluginFeature> Features => features.AsReadOnly();
/// <summary>
/// A wrapper for all registred <see cref="PluginModule"/> instances.
/// </summary>
public ModuleController Modules { get; init; }
/// <summary>
/// A wrapper for all registred <see cref="PluginFunction"/> instances.
/// </summary>
public FunctionController Functions { get; init; }
public PluginFeatureController()
{
Functions = new(this);
Modules = new(this);
}
public void Register(PluginFeature module)
{
if (!features.Contains(module))
features.Add(module);
}
public void Unregister(PluginFeature module)
{
features.Remove(module);
}
public class ModuleController
{
private readonly PluginFeatureController controller;
public ModuleController(PluginFeatureController controller)
{
this.controller = controller;
}
public IEnumerable<PluginModule> GetAll()
{
return controller.features.OfType<PluginModule>();
}
public IEnumerable<PluginModule> Get(string moduleType)
{
return GetAll().Where(n => n.Type == moduleType);
}
}
public class FunctionController
{
private readonly PluginFeatureController controller;
public FunctionController(PluginFeatureController controller)
{
this.controller = controller;
}
public IEnumerable<PluginFunction> GetAll()
{
return controller.features.OfType<PluginFunction>();
}
public IEnumerable<PluginFunction> Get(string functionType)
{
return GetAll().Where(n => n.Type == functionType);
}
public void ExecuteAll(string functionType)
{
foreach (var function in Get(functionType))
function.Execute();
}
public void ExecuteAll(string functionType, params object?[]? @params)
{
foreach (var function in Get(functionType))
function.Execute(@params);
}
public void ExecuteAll(string functionType, PluginFunctionParameter @params)
{
foreach (var function in Get(functionType))
function.Execute(@params);
}
public IEnumerable<object?> ExcuteAndGetResults(string functionType)
{
return Get(functionType).Select(n => n.Execute());
}
public IEnumerable<object?> ExcuteAndGetResults(string functionType, params object?[]? @params)
{
return Get(functionType).Select(n => n.Execute(@params));
}
public IEnumerable<object?> ExcuteAndGetResults(string functionType, PluginFunctionParameter @params)
{
return Get(functionType).Select(n => n.Execute(@params));
}
}
}
}