PluginFeature

This commit is contained in:
2023-12-05 13:52:35 +01:00
parent 066d5a1a81
commit 0ecbde3fd7
7 changed files with 164 additions and 126 deletions

View File

@@ -9,7 +9,7 @@
<PropertyGroup> <PropertyGroup>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>2.0.0</Version> <Version>2.1.0</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.WinControls;
namespace Pilz.Plugins.Advanced
{
public abstract class PluginFeature
{
public string Type { get; init; }
public virtual string? Name { get; init; }
public virtual RadSvgImage? Icon { get; set; }
public virtual bool Enabled { get; set; } = true;
protected PluginFeature(string functionType)
{
Type = functionType;
}
protected PluginFeature(string functionType, string? functionName) : this(functionType)
{
Name = functionName;
}
}
}

View File

@@ -0,0 +1,123 @@
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
{
/// <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();
private readonly List<PluginFeature> features = new();
/// <summary>
/// Returns a collection of all registred <see cref="PluginFeature"/> instances.
/// </summary>
public IReadOnlyList<PluginFeature> Features => features.AsReadOnly();
/// <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()
{
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<PluginModule> GetAll()
{
return controller.features.OfType<PluginModule>();
}
public IEnumerable<PluginModule> 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<PluginFunction> GetAll()
{
return controller.features.OfType<PluginFunction>();
}
public IEnumerable<PluginFunction> 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<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));
}
}
}
}

View File

@@ -7,59 +7,41 @@ using Telerik.WinControls;
namespace Pilz.Plugins.Advanced namespace Pilz.Plugins.Advanced
{ {
public abstract class PluginFunction public abstract class PluginFunction : PluginFeature
{ {
public string Type { get; init; } protected PluginFunction(string functionType) : base(functionType)
public string? Name { get; init; }
public RadSvgImage? Icon { get; set; }
public bool Enabled { get; set; } = true;
protected PluginFunction(string functionType)
{ {
Type = functionType;
} }
protected PluginFunction(string functionType, string functionName) : this(functionType) protected PluginFunction(string functionType, string? functionName) : base(functionType, functionName)
{ {
Name = functionName;
} }
public object? Execute() public virtual object? Execute()
{ {
return Execute((PluginFunctionParameter?)null); return Execute((PluginFunctionParameter?)null);
} }
public T? Execute<T>(params object?[]? @params) public virtual T? Execute<T>(params object?[]? @params)
{ {
return Execute<T>(new PluginFunctionSimpleParamter(@params)); return Execute<T>(new PluginFunctionSimpleParamter(@params));
} }
public object? Execute(params object?[]? @params) public virtual object? Execute(params object?[]? @params)
{ {
return Execute(new PluginFunctionSimpleParamter(@params)); return Execute(new PluginFunctionSimpleParamter(@params));
} }
public T? Execute<T>(PluginFunctionSimpleParamter? @params) public virtual T? Execute<T>(PluginFunctionSimpleParamter? @params)
{ {
if (Execute(@params) is T result) if (Execute(@params) is T result)
return result; return result;
return default; return default;
} }
public object? Execute(PluginFunctionParameter? @params) public virtual object? Execute(PluginFunctionParameter? @params)
{ {
#if !DEBUG
try
{
#endif
return ExecuteFunction(@params); return ExecuteFunction(@params);
#if !DEBUG
}
catch (Exception)
{
return null;
}
#endif
} }
protected abstract object? ExecuteFunction(PluginFunctionParameter? @params); protected abstract object? ExecuteFunction(PluginFunctionParameter? @params);

View File

@@ -1,60 +0,0 @@
namespace Pilz.Plugins.Advanced
{
public sealed class PluginFunctionController
{
public static PluginFunctionController Instance { get; private set; } = new();
private readonly List<PluginFunction> functions = new();
public IReadOnlyCollection<PluginFunction> Functions => functions.AsReadOnly();
public void RegisterFunction(PluginFunction function)
{
if (!functions.Contains(function))
functions.Add(function);
}
public void UnregisterFunction(PluginFunction function)
{
functions.Remove(function);
}
public IEnumerable<PluginFunction> GetFunctions(string functionType)
{
return functions.Where(n => n.Type == functionType);
}
public void ExecuteAll(string functionType)
{
foreach (var function in GetFunctions(functionType))
function.Execute();
}
public void ExecuteAll(string functionType, params object?[]? @params)
{
foreach (var function in GetFunctions(functionType))
function.Execute(@params);
}
public void ExecuteAll(string functionType, PluginFunctionParameter @params)
{
foreach (var function in GetFunctions(functionType))
function.Execute(@params);
}
public IEnumerable<object?> ExcuteAndGetResults(string functionType)
{
return GetFunctions(functionType).Select(n => n.Execute());
}
public IEnumerable<object?> ExcuteAndGetResults(string functionType, params object?[]? @params)
{
return GetFunctions(functionType).Select(n => n.Execute(@params));
}
public IEnumerable<object?> ExcuteAndGetResults(string functionType, PluginFunctionParameter @params)
{
return GetFunctions(functionType).Select(n => n.Execute(@params));
}
}
}

View File

@@ -6,19 +6,13 @@ using Telerik.WinControls;
namespace Pilz.Plugins.Advanced namespace Pilz.Plugins.Advanced
{ {
public abstract class PluginModule public abstract class PluginModule : PluginFeature
{ {
public string Type { get; init; }
public string Name { get; init; }
public RadSvgImage? Icon { get; set; }
public bool Visible { get; set; } = true; public bool Visible { get; set; } = true;
public bool RequiresRomManager { get; set; } = true;
public bool AllowEmbedding { get; set; } = true; public bool AllowEmbedding { get; set; } = true;
protected PluginModule(string moduleType, string moduleName) protected PluginModule(string moduleType, string moduleName) : base(moduleType, moduleName)
{ {
Type = moduleType;
Name = moduleName;
} }
public virtual void ShowUI() public virtual void ShowUI()
@@ -26,11 +20,11 @@ namespace Pilz.Plugins.Advanced
if (CreateNewUI() is PluginModuleUI ui) if (CreateNewUI() is PluginModuleUI ui)
{ {
ui.BackColor = Color.Transparent; ui.BackColor = Color.Transparent;
DialogBaseForm.Show(ui, Name, Icon!.ToImage().ToIcon()!); DialogBaseForm.Show(ui, Name!, Icon!.ToImage().ToIcon()!);
} }
} }
public PluginModuleUI CreateUI() public virtual PluginModuleUI CreateUI()
{ {
return CreateNewUI(); return CreateNewUI();
} }

View File

@@ -1,28 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pilz.Plugins.Advanced
{
public sealed class PluginModuleController
{
public static PluginModuleController Instance { get; private set; } = new();
private readonly List<PluginModule> modules = new();
public IReadOnlyList<PluginModule> Modules => modules.AsReadOnly();
public void RegisterModule(PluginModule module)
{
if (!modules.Contains(module))
modules.Add(module);
}
public void UnregisterModule(PluginModule module)
{
modules.Remove(module);
}
}
}