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);
///
/// Fires when a new has been registred.
///
public static event PluginFeatureEventHandler? OnPluginFeatureReistred;
///
/// Fires when a has been unregistred.
///
public static event PluginFeatureEventHandler? OnPluginFeatureUnregistred;
///
/// The default public instance that can be used by plugins and the interface providing software.
///
public static PluginFeatureController Instance { get; private set; } = new();
private readonly HashSet features = [];
///
/// A wrapper of all registred instances.
///
public FeatureController Features { get; init; }
///
/// A wrapper for all registred instances.
///
public ModuleController Modules { get; init; }
///
/// A wrapper for all registred instances.
///
public FunctionController Functions { get; init; }
public PluginFeatureController()
{
Features = new(this);
Functions = new(this);
Modules = new(this);
}
//public void RegisterAll()
//{
// var assembly = Assembly.GetCallingAssembly();
// foreach (var type in assembly.GetTypes())
// {
// if (type.IsAssignableFrom(typeof(IPluginFeaturesProvider)))
// {
// var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
// var method = methods.FirstOrDefault(n => n.Name == nameof(IPluginFeaturesProvider.GetFeatures) || n.Name.EndsWith(nameof(IPluginFeaturesProvider.GetFeatures)));
// if (method != null && method.Invoke(null, null) is PluginFeature[] features)
// {
// foreach (var feature in features)
// Register(feature);
// }
// }
// else if (type.IsAssignableFrom(typeof(IPluginFeatureProvider<>)))
// {
// var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
// var method = methods.FirstOrDefault(n => n.Name == nameof(IPluginFeatureProvider.Instance) || n.Name.EndsWith(nameof(IPluginFeatureProvider.Instance)));
// if (method != null && method.Invoke(null, null) is PluginFeature[] features)
// {
// foreach (var feature in features)
// Register(feature);
// }
// }
// }
//}
public void Register() where TProvider : IPluginFeatureProvider
{
Register(TProvider.Instance);
}
public void RegisterAll() 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 GetAll()
{
return controller.features.ToArray();
}
public virtual IEnumerable 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(PluginFeatureController controller) : FeatureController(controller) where T : PluginFeature
{
public override IEnumerable GetAll()
{
return controller.features.OfType();
}
public override IEnumerable 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(controller)
{
}
public class FunctionController(PluginFeatureController controller) : FeatureController(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