add a uniquie event for plugin function execution

This commit is contained in:
2024-07-02 11:32:16 +02:00
parent d13fe67b37
commit 251b55dba5
6 changed files with 95 additions and 18 deletions

View File

@@ -1,7 +1,17 @@
namespace Pilz.Plugins.Advanced;
using System.Reflection.Metadata;
namespace Pilz.Plugins.Advanced;
public abstract class PluginFeature
{
/// <summary>
/// Fires when the plugin feature gets used.
/// <br/>- For <see cref="PluginFunction"/> this fires on <see cref="PluginFunction.ExecuteFunction(PluginFunctionParameter?)"/>.
/// <br/>- For <see cref="T:PluginModule"/> this fires on <see cref="T:PluginModule.CreateNewUI(PluginFunctionParameter?)"/>.
/// <br/>- For any other custom feature implementation this may variate.
/// </summary>
public static event PluginFeatureExecuteEventHandler? OnExecute;
/// <summary>
/// The type of the feature defines where the feature get integrated.
/// </summary>
@@ -50,4 +60,49 @@ public abstract class PluginFeature
{
return $"{featureType}:{identifier}";
}
/// <summary>
/// Fires the <see cref="OnExecute"/> event with the status <see cref="PluginFeatureExecuteEventType.PostEvent"/>.
/// </summary>
/// <param name="params">The present <see cref="PluginFunctionParameter"/>.</param>
/// <param name="result">The resulting object that will be returned.</param>
/// <returns>Returns true if the original function should be executed.</returns>
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;
}
/// <summary>
/// Fires the <see cref="OnExecute"/> event with the status <see cref="PluginFeatureExecuteEventType.PostEvent"/>.
/// </summary>
/// <param name="params">The present <see cref="PluginFunctionParameter"/>.</param>
/// <param name="result">The resulting object that will be returned.</param>
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;
}
}
}