214 lines
7.8 KiB
C#
214 lines
7.8 KiB
C#
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
|
|
{
|
|
public delegate void PluginFeatureEventHandler(PluginFeatureController controller, PluginFeature feature);
|
|
|
|
/// <summary>
|
|
/// Fires when a new <see cref="PluginFeature"/> has been registred.
|
|
/// </summary>
|
|
public static event PluginFeatureEventHandler? OnPluginFeatureReistred;
|
|
|
|
/// <summary>
|
|
/// Fires when a <see cref="PluginFeature"/> has been unregistred.
|
|
/// </summary>
|
|
public static event PluginFeatureEventHandler? OnPluginFeatureUnregistred;
|
|
|
|
/// <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 HashSet<PluginFeature> features = [];
|
|
|
|
/// <summary>
|
|
/// A wrapper of all registred <see cref="PluginFeature"/> instances.
|
|
/// </summary>
|
|
public FeatureController Features { get; init; }
|
|
|
|
/// <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()
|
|
{
|
|
Features = new(this);
|
|
Functions = new(this);
|
|
Modules = new(this);
|
|
}
|
|
|
|
public void RegisterAllFromMyAssembly()
|
|
{
|
|
RegisterAllFromAssembly(Assembly.GetCallingAssembly());
|
|
}
|
|
|
|
public void RegisterAllFromAssembly(Assembly assembly)
|
|
{
|
|
const string nameGetFeatures = $"{nameof(IPluginFeaturesProvider.GetFeatures)}";
|
|
const string nameGetFeaturesExplicit = $"{nameof(IPluginFeaturesProvider)}.{nameof(IPluginFeaturesProvider.GetFeatures)}";
|
|
const string nameInstance = $"get_{nameof(IPluginFeatureProvider.Instance)}";
|
|
const string nameInstnaceExplicit = $"get_{nameof(IPluginFeatureProvider)}.{nameof(IPluginFeatureProvider.Instance)}";
|
|
|
|
foreach (var type in assembly.GetTypes())
|
|
{
|
|
if (type.IsAssignableTo(typeof(IPluginFeaturesProvider)))
|
|
{
|
|
var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
|
var method = methods.FirstOrDefault(n => n.Name == nameGetFeaturesExplicit || n.Name == nameGetFeatures);
|
|
|
|
if (method != null && method.Invoke(null, null) is PluginFeature[] features)
|
|
{
|
|
foreach (var feature in features)
|
|
Register(feature);
|
|
}
|
|
}
|
|
else if (type.IsAssignableTo(typeof(IPluginFeatureProvider)))
|
|
{
|
|
var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
|
var method = methods.FirstOrDefault(n => n.Name == nameInstnaceExplicit || n.Name == nameInstance);
|
|
|
|
if (method != null && method.Invoke(null, null) is PluginFeature feature)
|
|
Register(feature);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Register<TProvider>() where TProvider : IPluginFeatureProvider
|
|
{
|
|
Register(TProvider.Instance);
|
|
}
|
|
|
|
public void RegisterAll<TProvider>() where TProvider : IPluginFeaturesProvider
|
|
{
|
|
foreach (var feature in TProvider.GetFeatures())
|
|
Register(feature);
|
|
}
|
|
|
|
public void Register(PluginFeature module)
|
|
{
|
|
if (!features.Contains(module))
|
|
{
|
|
features.Add(module);
|
|
OnPluginFeatureReistred?.Invoke(this, module);
|
|
}
|
|
}
|
|
|
|
public void Unregister(PluginFeature module)
|
|
{
|
|
features.Remove(module);
|
|
OnPluginFeatureUnregistred?.Invoke(this, module);
|
|
}
|
|
|
|
public class FeatureController(PluginFeatureController controller)
|
|
{
|
|
protected readonly PluginFeatureController controller = controller;
|
|
|
|
public virtual IEnumerable<PluginFeature> GetAll()
|
|
{
|
|
return controller.features.ToArray();
|
|
}
|
|
|
|
public virtual IEnumerable<PluginFeature> Get(string featureType)
|
|
{
|
|
return controller.features.Where(n => n.Type == featureType);
|
|
}
|
|
|
|
public virtual PluginFeature? GetFirst(string featureType)
|
|
{
|
|
return controller.features.FirstOrDefault(n => n.Type == featureType);
|
|
}
|
|
|
|
public virtual PluginFeature? GetByIdentifier(string fullIdentifier)
|
|
{
|
|
return controller.features.FirstOrDefault(n => n.FullIdentifier == fullIdentifier);
|
|
}
|
|
|
|
public virtual PluginFeature? GetByIdentifier(string featureType, string identifier)
|
|
{
|
|
return controller.features.FirstOrDefault(n => n.Type == featureType && n.Identifier == identifier);
|
|
}
|
|
}
|
|
|
|
public class FeatureController<T>(PluginFeatureController controller) : FeatureController(controller) where T : PluginFeature
|
|
{
|
|
public override IEnumerable<T> GetAll()
|
|
{
|
|
return controller.features.OfType<T>();
|
|
}
|
|
|
|
public override IEnumerable<T> Get(string moduleType)
|
|
{
|
|
return GetAll().Where(n => n.Type == moduleType);
|
|
}
|
|
|
|
public override T? GetFirst(string moduleType)
|
|
{
|
|
return base.GetFirst(moduleType) as T;
|
|
}
|
|
|
|
public override T? GetByIdentifier(string fullIdentifier)
|
|
{
|
|
return base.GetByIdentifier(fullIdentifier) as T;
|
|
}
|
|
|
|
public override T? GetByIdentifier(string featureType, string identifier)
|
|
{
|
|
return base.GetByIdentifier(featureType, identifier) as T;
|
|
}
|
|
}
|
|
|
|
public class ModuleController(PluginFeatureController controller) : FeatureController<PluginModuleBase>(controller)
|
|
{
|
|
}
|
|
|
|
public class FunctionController(PluginFeatureController controller) : FeatureController<PluginFunction>(controller)
|
|
{
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
}
|