using System.Reflection.Metadata;
namespace Pilz.Plugins.Advanced;
public abstract class PluginFeature
{
///
/// Fires when the plugin feature gets used.
///
- For this fires on .
///
- For this fires on .
///
- For any other custom feature implementation this may variate.
///
public static event PluginFeatureExecuteEventHandler? OnExecute;
///
/// The type of the feature defines where the feature get integrated.
///
public string Type { get; init; }
///
/// The identifier of the feature should be uniquie for the current .
/// It defines a feature within a type.
///
public string Identifier { get; init; }
///
/// The full identifier of the feature should be uniquie and is the combination of and .
/// It defines a feature across all types.
///
public string FullIdentifier => GetFullIdentifier(Type, Identifier);
///
/// The (display) name of the feature.
///
public virtual string? Name { get; init; }
///
/// The (display) description of the feature.
///
public virtual string? Description { get; init; }
///
/// The symbol for the feature.
///
public virtual object? Icon { get; set; }
///
/// 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.
///
public virtual FeaturePrioritization Prioritization { get; set; }
///
/// Defines if the feature is enabled/visible.
///
public virtual bool Enabled { get; set; } = true;
protected PluginFeature(string type, string identifier)
{
Identifier = identifier;
Type = type;
}
protected PluginFeature(string type, string identifier, string? name) : this(type, identifier)
{
Name = name;
}
public static string GetFullIdentifier(string featureType, string identifier)
{
return $"{featureType}:{identifier}";
}
///
/// Fires the event with the status .
///
/// The present .
/// The resulting object that will be returned.
/// Returns true if the original function should be executed.
protected bool OnPreExecute(PluginFunctionParameter? @params, ref object? result)
{
if (OnExecute != null)
{
var args = new PluginFeatureExecuteEventArgs(PluginFeatureExecuteEventType.PreEvent, @params);
OnExecute.Invoke(this, args);
if (args.Handled)
{
result = args.Result;
return false;
}
}
return true;
}
///
/// Fires the event with the status .
///
/// The present .
/// The resulting object that will be returned.
protected void OnPostExecute(PluginFunctionParameter? @params, ref object? result)
{
if (OnExecute != null)
{
var args = new PluginFeatureExecuteEventArgs(PluginFeatureExecuteEventType.PostEvent, @params)
{
Result = result
};
OnExecute.Invoke(this, args);
if (args.Handled)
result = args.Result;
}
}
}