code optimization
This commit is contained in:
@@ -1,15 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
namespace Pilz.Plugins.Advanced
|
||||
public enum FeaturePrioritization
|
||||
{
|
||||
public enum FeaturePrioritization
|
||||
{
|
||||
Low = -1,
|
||||
Default = 0,
|
||||
High = 1,
|
||||
}
|
||||
Low = -1,
|
||||
Default = 0,
|
||||
High = 1,
|
||||
}
|
||||
|
||||
@@ -1,21 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
namespace Pilz.Plugins.Advanced
|
||||
public interface IPluginFeatureProvider
|
||||
{
|
||||
public interface IPluginFeatureProvider
|
||||
{
|
||||
static abstract PluginFeature Instance { get; }
|
||||
}
|
||||
|
||||
public interface IPluginFeatureProvider<T> : IPluginFeatureProvider where T : PluginFeature, IPluginFeatureProvider<T>
|
||||
{
|
||||
static new abstract T Instance { get; }
|
||||
static PluginFeature IPluginFeatureProvider.Instance => T.Instance;
|
||||
}
|
||||
static abstract PluginFeature Instance { get; }
|
||||
}
|
||||
|
||||
public interface IPluginFeatureProvider<T> : IPluginFeatureProvider where T : PluginFeature, IPluginFeatureProvider<T>
|
||||
{
|
||||
static new abstract T Instance { get; }
|
||||
static PluginFeature IPluginFeatureProvider.Instance => T.Instance;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
namespace Pilz.Plugins.Advanced
|
||||
public interface IPluginFeaturesProvider
|
||||
{
|
||||
public interface IPluginFeaturesProvider
|
||||
{
|
||||
static abstract PluginFeature[] GetFeatures();
|
||||
}
|
||||
static abstract PluginFeature[] GetFeatures();
|
||||
}
|
||||
|
||||
@@ -1,60 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
namespace Pilz.Plugins.Advanced
|
||||
public abstract class PluginFeature
|
||||
{
|
||||
public abstract class PluginFeature
|
||||
/// <summary>
|
||||
/// The type of the feature defines where the feature get integrated.
|
||||
/// </summary>
|
||||
public string Type { get; init; }
|
||||
/// <summary>
|
||||
/// The identifier of the feature should be uniquie for the current <see cref="Type"/>.
|
||||
/// It defines a feature within a type.
|
||||
/// </summary>
|
||||
public string Identifier { get; init; }
|
||||
/// <summary>
|
||||
/// The full identifier of the feature should be uniquie and is the combination of <see cref="Type"/> and <see cref="Identifier"/>.
|
||||
/// It defines a feature across all types.
|
||||
/// </summary>
|
||||
public string FullIdentifier => GetFullIdentifier(Type, Identifier);
|
||||
/// <summary>
|
||||
/// The (display) name of the feature.
|
||||
/// </summary>
|
||||
public virtual string? Name { get; init; }
|
||||
/// <summary>
|
||||
/// The symbol for the feature.
|
||||
/// </summary>
|
||||
public virtual object? Icon { get; set; }
|
||||
/// <summary>
|
||||
/// Sets the prioritization of the feature.
|
||||
/// This will be respected on abfragen features and on inserting as items using the extension methods"/>.
|
||||
/// Some applications might implement a way to regonize feature prioritization via its own way.
|
||||
/// </summary>
|
||||
public virtual FeaturePrioritization Prioritization { get; set; }
|
||||
/// <summary>
|
||||
/// Defines if the feature is enabled/visible.
|
||||
/// </summary>
|
||||
public virtual bool Enabled { get; set; } = true;
|
||||
|
||||
protected PluginFeature(string featureType, string identifier)
|
||||
{
|
||||
/// <summary>
|
||||
/// The type of the feature defines where the feature get integrated.
|
||||
/// </summary>
|
||||
public string Type { get; init; }
|
||||
/// <summary>
|
||||
/// The identifier of the feature should be uniquie for the current <see cref="Type"/>.
|
||||
/// It defines a feature within a type.
|
||||
/// </summary>
|
||||
public string Identifier { get; init; }
|
||||
/// <summary>
|
||||
/// The full identifier of the feature should be uniquie and is the combination of <see cref="Type"/> and <see cref="Identifier"/>.
|
||||
/// It defines a feature across all types.
|
||||
/// </summary>
|
||||
public string FullIdentifier => GetFullIdentifier(Type, Identifier);
|
||||
/// <summary>
|
||||
/// The (display) name of the feature.
|
||||
/// </summary>
|
||||
public virtual string? Name { get; init; }
|
||||
/// <summary>
|
||||
/// The symbol for the feature.
|
||||
/// </summary>
|
||||
public virtual object? Icon { get; set; }
|
||||
/// <summary>
|
||||
/// Sets the prioritization of the feature.
|
||||
/// This will be respected on abfragen features and on inserting as items using the extension methods"/>.
|
||||
/// Some applications might implement a way to regonize feature prioritization via its own way.
|
||||
/// </summary>
|
||||
public virtual FeaturePrioritization Prioritization { get; set; }
|
||||
/// <summary>
|
||||
/// Defines if the feature is enabled/visible.
|
||||
/// </summary>
|
||||
public virtual bool Enabled { get; set; } = true;
|
||||
Identifier = identifier;
|
||||
Type = featureType;
|
||||
}
|
||||
|
||||
protected PluginFeature(string featureType, string identifier)
|
||||
{
|
||||
Identifier = identifier;
|
||||
Type = featureType;
|
||||
}
|
||||
protected PluginFeature(string featureType, string featureIdentifier, string? featureName) : this(featureType, featureIdentifier)
|
||||
{
|
||||
Name = featureName;
|
||||
}
|
||||
|
||||
protected PluginFeature(string featureType, string featureIdentifier, string? featureName) : this(featureType, featureIdentifier)
|
||||
{
|
||||
Name = featureName;
|
||||
}
|
||||
|
||||
public static string GetFullIdentifier(string featureType, string identifier)
|
||||
{
|
||||
return $"{featureType}:{identifier}";
|
||||
}
|
||||
public static string GetFullIdentifier(string featureType, string identifier)
|
||||
{
|
||||
return $"{featureType}:{identifier}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,273 +1,267 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Pilz.Plugins.Advanced
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
public class PluginFeatureController
|
||||
{
|
||||
public class PluginFeatureController
|
||||
// D e l e g a t e s
|
||||
|
||||
public delegate void PluginFeatureEventHandler(PluginFeatureController controller, PluginFeature feature);
|
||||
|
||||
// S t a t i c E v e n t s
|
||||
|
||||
/// <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;
|
||||
|
||||
// S t a t i c M e m b e r s
|
||||
|
||||
protected static readonly string nameGetFeatures = $"{nameof(IPluginFeaturesProvider.GetFeatures)}";
|
||||
protected static readonly string nameGetFeaturesExplicit = $"{typeof(IPluginFeaturesProvider).FullName}.{nameof(IPluginFeaturesProvider.GetFeatures)}";
|
||||
protected static readonly string nameInstance = $"get_{nameof(IPluginFeatureProvider.Instance)}";
|
||||
protected static readonly string nameInstnaceExplicit = $"{typeof(IPluginFeaturesProvider).FullName}.get_{nameof(IPluginFeatureProvider.Instance)}";
|
||||
|
||||
/// <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();
|
||||
|
||||
// I n s t a n c e M e m e b e r s
|
||||
|
||||
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()
|
||||
{
|
||||
// D e l e g a t e s
|
||||
Features = new(this);
|
||||
Functions = new(this);
|
||||
Modules = new(this);
|
||||
}
|
||||
|
||||
public delegate void PluginFeatureEventHandler(PluginFeatureController controller, PluginFeature feature);
|
||||
/// <summary>
|
||||
/// Registers all features found in the currently executing Assembly via <see cref="IPluginFeatureProvider"/>, <see cref="IPluginFeatureProvider{T}"/> and <see cref="IPluginFeaturesProvider"/>.
|
||||
/// <para><b>Note:</b><br/>Explicit implementations of <see cref="IPluginFeatureProvider{T}.Instance"/> can not be detected. For this case just implement <see cref="IPluginFeatureProvider.Instance"/> instead.</para>
|
||||
/// </summary>
|
||||
public void RegisterAllOwn()
|
||||
{
|
||||
RegisterAll(Assembly.GetCallingAssembly());
|
||||
}
|
||||
|
||||
// S t a t i c E v e n t s
|
||||
/// <summary>
|
||||
/// Registers all features found in the given <see cref="Assembly[]"/> via <see cref="IPluginFeatureProvider"/>, <see cref="IPluginFeatureProvider{T}"/> and <see cref="IPluginFeaturesProvider"/>.
|
||||
/// <para><b>Note:</b><br/>Explicit implementations of <see cref="IPluginFeatureProvider{T}.Instance"/> can not be detected. For this case just implement <see cref="IPluginFeatureProvider.Instance"/> instead.</para>
|
||||
/// </summary>
|
||||
/// <param name="assemblies"></param>
|
||||
public void RegisterAll(Assembly[] assemblies)
|
||||
{
|
||||
foreach (var assembly in assemblies)
|
||||
RegisterAll(assembly);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires when a new <see cref="PluginFeature"/> has been registred.
|
||||
/// </summary>
|
||||
public static event PluginFeatureEventHandler? OnPluginFeatureReistred;
|
||||
/// <summary>
|
||||
/// Registers all features found in the given <see cref="Assembly"/> via <see cref="IPluginFeatureProvider"/>, <see cref="IPluginFeatureProvider{T}"/> and <see cref="IPluginFeaturesProvider"/>.
|
||||
/// <para><b>Note:</b><br/>Explicit implementations of <see cref="IPluginFeatureProvider{T}.Instance"/> can not be detected. For this case just implement <see cref="IPluginFeatureProvider.Instance"/> instead.</para>
|
||||
/// </summary>
|
||||
/// <param name="assembly"></param>
|
||||
public void RegisterAll(Assembly assembly)
|
||||
{
|
||||
RegisterAll(assembly.GetTypes());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires when a <see cref="PluginFeature"/> has been unregistred.
|
||||
/// </summary>
|
||||
public static event PluginFeatureEventHandler? OnPluginFeatureUnregistred;
|
||||
/// <summary>
|
||||
/// Registers all features found from the given <see cref="Type[]"/> via <see cref="IPluginFeatureProvider"/>, <see cref="IPluginFeatureProvider{T}"/> and <see cref="IPluginFeaturesProvider"/>.
|
||||
/// <para><b>Note:</b><br/>Explicit implementations of <see cref="IPluginFeatureProvider{T}.Instance"/> can not be detected. For this case just implement <see cref="IPluginFeatureProvider.Instance"/> instead.</para>
|
||||
/// </summary>
|
||||
/// <param name="types"></param>
|
||||
public void RegisterAll(Type[] types)
|
||||
{
|
||||
foreach (var type in types)
|
||||
RegisterAll(type);
|
||||
}
|
||||
|
||||
// S t a t i c M e m b e r s
|
||||
|
||||
protected static readonly string nameGetFeatures = $"{nameof(IPluginFeaturesProvider.GetFeatures)}";
|
||||
protected static readonly string nameGetFeaturesExplicit = $"{typeof(IPluginFeaturesProvider).FullName}.{nameof(IPluginFeaturesProvider.GetFeatures)}";
|
||||
protected static readonly string nameInstance = $"get_{nameof(IPluginFeatureProvider.Instance)}";
|
||||
protected static readonly string nameInstnaceExplicit = $"{typeof(IPluginFeaturesProvider).FullName}.get_{nameof(IPluginFeatureProvider.Instance)}";
|
||||
|
||||
/// <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();
|
||||
|
||||
// I n s t a n c e M e m e b e r s
|
||||
|
||||
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()
|
||||
/// <summary>
|
||||
/// Registers all features found from the given <see cref="Type"/> via <see cref="IPluginFeatureProvider"/>, <see cref="IPluginFeatureProvider{T}"/> and <see cref="IPluginFeaturesProvider"/>.
|
||||
/// <para><b>Note:</b><br/>Explicit implementations of <see cref="IPluginFeatureProvider{T}.Instance"/> can not be detected. For this case just implement <see cref="IPluginFeatureProvider.Instance"/> instead.</para>
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
public void RegisterAll(Type type)
|
||||
{
|
||||
if (type.IsAssignableTo(typeof(IPluginFeaturesProvider)))
|
||||
{
|
||||
Features = new(this);
|
||||
Functions = new(this);
|
||||
Modules = new(this);
|
||||
}
|
||||
var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
var method = methods.FirstOrDefault(n => n.Name == nameGetFeaturesExplicit || n.Name == nameGetFeatures);
|
||||
|
||||
/// <summary>
|
||||
/// Registers all features found in the currently executing Assembly via <see cref="IPluginFeatureProvider"/>, <see cref="IPluginFeatureProvider{T}"/> and <see cref="IPluginFeaturesProvider"/>.
|
||||
/// <para><b>Note:</b><br/>Explicit implementations of <see cref="IPluginFeatureProvider{T}.Instance"/> can not be detected. For this case just implement <see cref="IPluginFeatureProvider.Instance"/> instead.</para>
|
||||
/// </summary>
|
||||
public void RegisterAllOwn()
|
||||
{
|
||||
RegisterAll(Assembly.GetCallingAssembly());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers all features found in the given <see cref="Assembly[]"/> via <see cref="IPluginFeatureProvider"/>, <see cref="IPluginFeatureProvider{T}"/> and <see cref="IPluginFeaturesProvider"/>.
|
||||
/// <para><b>Note:</b><br/>Explicit implementations of <see cref="IPluginFeatureProvider{T}.Instance"/> can not be detected. For this case just implement <see cref="IPluginFeatureProvider.Instance"/> instead.</para>
|
||||
/// </summary>
|
||||
/// <param name="assemblies"></param>
|
||||
public void RegisterAll(Assembly[] assemblies)
|
||||
{
|
||||
foreach (var assembly in assemblies)
|
||||
RegisterAll(assembly);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers all features found in the given <see cref="Assembly"/> via <see cref="IPluginFeatureProvider"/>, <see cref="IPluginFeatureProvider{T}"/> and <see cref="IPluginFeaturesProvider"/>.
|
||||
/// <para><b>Note:</b><br/>Explicit implementations of <see cref="IPluginFeatureProvider{T}.Instance"/> can not be detected. For this case just implement <see cref="IPluginFeatureProvider.Instance"/> instead.</para>
|
||||
/// </summary>
|
||||
/// <param name="assembly"></param>
|
||||
public void RegisterAll(Assembly assembly)
|
||||
{
|
||||
RegisterAll(assembly.GetTypes());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers all features found from the given <see cref="Type[]"/> via <see cref="IPluginFeatureProvider"/>, <see cref="IPluginFeatureProvider{T}"/> and <see cref="IPluginFeaturesProvider"/>.
|
||||
/// <para><b>Note:</b><br/>Explicit implementations of <see cref="IPluginFeatureProvider{T}.Instance"/> can not be detected. For this case just implement <see cref="IPluginFeatureProvider.Instance"/> instead.</para>
|
||||
/// </summary>
|
||||
/// <param name="types"></param>
|
||||
public void RegisterAll(Type[] types)
|
||||
{
|
||||
foreach (var type in types)
|
||||
RegisterAll(type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers all features found from the given <see cref="Type"/> via <see cref="IPluginFeatureProvider"/>, <see cref="IPluginFeatureProvider{T}"/> and <see cref="IPluginFeaturesProvider"/>.
|
||||
/// <para><b>Note:</b><br/>Explicit implementations of <see cref="IPluginFeatureProvider{T}.Instance"/> can not be detected. For this case just implement <see cref="IPluginFeatureProvider.Instance"/> instead.</para>
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
public void RegisterAll(Type type)
|
||||
{
|
||||
if (type.IsAssignableTo(typeof(IPluginFeaturesProvider)))
|
||||
if (method != null && method.Invoke(null, null) is PluginFeature[] features)
|
||||
{
|
||||
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)
|
||||
foreach (var feature in features)
|
||||
Register(feature);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a feature via the given <see cref="IPluginFeatureProvider"/> or <see cref="IPluginFeatureProvider{T}"/>.
|
||||
/// </summary>
|
||||
public void Register<TProvider>() where TProvider : IPluginFeatureProvider
|
||||
else if (type.IsAssignableTo(typeof(IPluginFeatureProvider)))
|
||||
{
|
||||
Register(TProvider.Instance);
|
||||
}
|
||||
var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
var method = methods.FirstOrDefault(n => n.Name == nameInstnaceExplicit || n.Name == nameInstance);
|
||||
|
||||
/// <summary>
|
||||
/// Registers all features via the given <see cref="IPluginFeaturesProvider"/>.
|
||||
/// </summary>
|
||||
public void RegisterAll<TProvider>() where TProvider : IPluginFeaturesProvider
|
||||
{
|
||||
foreach (var feature in TProvider.GetFeatures())
|
||||
if (method != null && method.Invoke(null, null) is PluginFeature feature)
|
||||
Register(feature);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers the given feature.
|
||||
/// </summary>
|
||||
/// <param name="module"></param>
|
||||
public void Register(PluginFeature module)
|
||||
/// <summary>
|
||||
/// Registers a feature via the given <see cref="IPluginFeatureProvider"/> or <see cref="IPluginFeatureProvider{T}"/>.
|
||||
/// </summary>
|
||||
public void Register<TProvider>() where TProvider : IPluginFeatureProvider
|
||||
{
|
||||
Register(TProvider.Instance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers all features via the given <see cref="IPluginFeaturesProvider"/>.
|
||||
/// </summary>
|
||||
public void RegisterAll<TProvider>() where TProvider : IPluginFeaturesProvider
|
||||
{
|
||||
foreach (var feature in TProvider.GetFeatures())
|
||||
Register(feature);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers the given feature.
|
||||
/// </summary>
|
||||
/// <param name="module"></param>
|
||||
public void Register(PluginFeature module)
|
||||
{
|
||||
if (!features.Contains(module))
|
||||
{
|
||||
if (!features.Contains(module))
|
||||
{
|
||||
features.Add(module);
|
||||
OnPluginFeatureReistred?.Invoke(this, module);
|
||||
}
|
||||
features.Add(module);
|
||||
OnPluginFeatureReistred?.Invoke(this, module);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters the given feature.
|
||||
/// </summary>
|
||||
/// <param name="module"></param>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters the given feature.
|
||||
/// </summary>
|
||||
/// <param name="module"></param>
|
||||
public void Unregister(PluginFeature module)
|
||||
public virtual IEnumerable<PluginFeature> Get(string featureType)
|
||||
{
|
||||
features.Remove(module);
|
||||
OnPluginFeatureUnregistred?.Invoke(this, module);
|
||||
return controller.features.Where(n => n.Type == featureType);
|
||||
}
|
||||
|
||||
public class FeatureController(PluginFeatureController controller)
|
||||
public virtual PluginFeature? GetFirst(string featureType)
|
||||
{
|
||||
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);
|
||||
}
|
||||
return controller.features.FirstOrDefault(n => n.Type == featureType);
|
||||
}
|
||||
|
||||
public class FeatureController<T>(PluginFeatureController controller) : FeatureController(controller) where T : PluginFeature
|
||||
public virtual PluginFeature? GetByIdentifier(string fullIdentifier)
|
||||
{
|
||||
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;
|
||||
}
|
||||
return controller.features.FirstOrDefault(n => n.FullIdentifier == fullIdentifier);
|
||||
}
|
||||
|
||||
public class ModuleController(PluginFeatureController controller) : FeatureController<PluginModuleBase>(controller)
|
||||
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 class FunctionController(PluginFeatureController controller) : FeatureController<PluginFunction>(controller)
|
||||
public override IEnumerable<T> Get(string moduleType)
|
||||
{
|
||||
public void ExecuteAll(string functionType)
|
||||
{
|
||||
foreach (var function in Get(functionType))
|
||||
function.Execute();
|
||||
}
|
||||
return GetAll().Where(n => n.Type == moduleType);
|
||||
}
|
||||
|
||||
public void ExecuteAll(string functionType, params object?[]? @params)
|
||||
{
|
||||
foreach (var function in Get(functionType))
|
||||
function.Execute(@params);
|
||||
}
|
||||
public override T? GetFirst(string moduleType)
|
||||
{
|
||||
return base.GetFirst(moduleType) as T;
|
||||
}
|
||||
|
||||
public void ExecuteAll(string functionType, PluginFunctionParameter @params)
|
||||
{
|
||||
foreach (var function in Get(functionType))
|
||||
function.Execute(@params);
|
||||
}
|
||||
public override T? GetByIdentifier(string fullIdentifier)
|
||||
{
|
||||
return base.GetByIdentifier(fullIdentifier) as T;
|
||||
}
|
||||
|
||||
public IEnumerable<object?> ExcuteAndGetResults(string functionType)
|
||||
{
|
||||
return Get(functionType).Select(n => n.Execute());
|
||||
}
|
||||
public override T? GetByIdentifier(string featureType, string identifier)
|
||||
{
|
||||
return base.GetByIdentifier(featureType, identifier) as T;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<object?> ExcuteAndGetResults(string functionType, params object?[]? @params)
|
||||
{
|
||||
return Get(functionType).Select(n => n.Execute(@params));
|
||||
}
|
||||
public class ModuleController(PluginFeatureController controller) : FeatureController<PluginModuleBase>(controller)
|
||||
{
|
||||
}
|
||||
|
||||
public IEnumerable<object?> ExcuteAndGetResults(string functionType, PluginFunctionParameter @params)
|
||||
{
|
||||
return Get(functionType).Select(n => n.Execute(@params));
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
namespace Pilz.Plugins.Advanced
|
||||
public abstract class PluginFunction : PluginFeature
|
||||
{
|
||||
public abstract class PluginFunction : PluginFeature
|
||||
protected PluginFunction(string functionType, string functionIdentifier) : base(functionType, functionIdentifier)
|
||||
{
|
||||
protected PluginFunction(string functionType, string functionIdentifier) : base(functionType, functionIdentifier)
|
||||
{
|
||||
}
|
||||
|
||||
protected PluginFunction(string functionType, string functionIdentifier, string? functionName) : base(functionType, functionIdentifier, functionName)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual object? Execute()
|
||||
{
|
||||
return Execute((PluginFunctionParameter?)null);
|
||||
}
|
||||
|
||||
public virtual T? Execute<T>(params object?[]? @params)
|
||||
{
|
||||
return Execute<T>(new PluginFunctionSimpleParamter(@params));
|
||||
}
|
||||
|
||||
public virtual object? Execute(params object?[]? @params)
|
||||
{
|
||||
return Execute(new PluginFunctionSimpleParamter(@params));
|
||||
}
|
||||
|
||||
public virtual T? Execute<T>(PluginFunctionSimpleParamter? @params)
|
||||
{
|
||||
if (Execute(@params) is T result)
|
||||
return result;
|
||||
return default;
|
||||
}
|
||||
|
||||
public virtual object? Execute(PluginFunctionParameter? @params)
|
||||
{
|
||||
return ExecuteFunction(@params);
|
||||
}
|
||||
|
||||
protected abstract object? ExecuteFunction(PluginFunctionParameter? @params);
|
||||
}
|
||||
|
||||
protected PluginFunction(string functionType, string functionIdentifier, string? functionName) : base(functionType, functionIdentifier, functionName)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual object? Execute()
|
||||
{
|
||||
return Execute((PluginFunctionParameter?)null);
|
||||
}
|
||||
|
||||
public virtual T? Execute<T>(params object?[]? @params)
|
||||
{
|
||||
return Execute<T>(new PluginFunctionSimpleParamter(@params));
|
||||
}
|
||||
|
||||
public virtual object? Execute(params object?[]? @params)
|
||||
{
|
||||
return Execute(new PluginFunctionSimpleParamter(@params));
|
||||
}
|
||||
|
||||
public virtual T? Execute<T>(PluginFunctionSimpleParamter? @params)
|
||||
{
|
||||
if (Execute(@params) is T result)
|
||||
return result;
|
||||
return default;
|
||||
}
|
||||
|
||||
public virtual object? Execute(PluginFunctionParameter? @params)
|
||||
{
|
||||
return ExecuteFunction(@params);
|
||||
}
|
||||
|
||||
protected abstract object? ExecuteFunction(PluginFunctionParameter? @params);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
namespace Pilz.Plugins.Advanced
|
||||
public class PluginFunctionParameter
|
||||
{
|
||||
public class PluginFunctionParameter
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
namespace Pilz.Plugins.Advanced
|
||||
public sealed class PluginFunctionSimpleParamter : PluginFunctionParameter
|
||||
{
|
||||
public sealed class PluginFunctionSimpleParamter : PluginFunctionParameter
|
||||
{
|
||||
public object?[]? Params { get; init; }
|
||||
public object?[]? Params { get; init; }
|
||||
|
||||
public PluginFunctionSimpleParamter(params object?[]? @params)
|
||||
{
|
||||
Params = @params;
|
||||
}
|
||||
public PluginFunctionSimpleParamter(params object?[]? @params)
|
||||
{
|
||||
Params = @params;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Pilz.Plugins.Advanced
|
||||
{
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public abstract class PluginModuleBase : PluginFeature
|
||||
{
|
||||
protected PluginModuleBase(string moduleType, string moduleIdentifier) : base(moduleType, moduleIdentifier)
|
||||
{
|
||||
}
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
protected PluginModuleBase(string moduleType, string moduleIdentifier, string moduleName) : base(moduleType, moduleIdentifier, moduleName)
|
||||
{
|
||||
}
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public abstract class PluginModuleBase : PluginFeature
|
||||
{
|
||||
protected PluginModuleBase(string moduleType, string moduleIdentifier) : base(moduleType, moduleIdentifier)
|
||||
{
|
||||
}
|
||||
|
||||
protected PluginModuleBase(string moduleType, string moduleIdentifier, string moduleName) : base(moduleType, moduleIdentifier, moduleName)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user