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 { /// /// 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 List features = new(); /// /// Returns a collection of all registred instances. /// public IReadOnlyList Features => features.AsReadOnly(); /// /// A wrapper for all registred instances. /// public ModuleController Modules { get; init; } /// /// A wrapper for all registred instances. /// 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 GetAll() { return controller.features.OfType(); } public IEnumerable 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 GetAll() { return controller.features.OfType(); } public IEnumerable 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 ExcuteAndGetResults(string functionType) { return Get(functionType).Select(n => n.Execute()); } public IEnumerable ExcuteAndGetResults(string functionType, params object?[]? @params) { return Get(functionType).Select(n => n.Execute(@params)); } public IEnumerable ExcuteAndGetResults(string functionType, PluginFunctionParameter @params) { return Get(functionType).Select(n => n.Execute(@params)); } } } }