Giving plugin more control over when it should be executed (#74)

This pr should give the plugin dev the option to receive all events to his liking without constraining it to a specific type at compile time

Co-authored-by: Weirdo <git.jonas@kamsker.at>
Co-authored-by: AeonLucid <aeonlucid@outlook.com>
This commit is contained in:
authored and GitHub committed 2020-10-24 23:06:12 +02:00
1 parent 055366fed8
commit 7ecd7f4ec8
5 files changed
+51 -2

No files matched your search

+1
View File
@@ -4,3 +4,4 @@
/src/Impostor.Plugins.Debugger/Properties/launchSettings.json
/tools
.local/
@@ -0,0 +1,13 @@
using System.Threading.Tasks;
namespace Impostor.Api.Events
{
public interface IManualEventListener : IEventListener
{
public bool CanExecute<T>();
public ValueTask Execute(IEvent @event);
EventPriority Priority { get; set; }
}
}
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
@@ -7,5 +7,4 @@
<ItemGroup>
<ProjectReference Include="..\Impostor.Api\Impostor.Api.csproj"/>
</ItemGroup>
</Project>
@@ -98,6 +98,12 @@ namespace Impostor.Server.Events
{
foreach (var handler in services.GetServices<IEventListener>())
{
if (handler is IManualEventListener manualEventListener && manualEventListener.CanExecute<TEvent>())
{
yield return new EventHandler(handler, new ManualRegisteredEventListener(manualEventListener));
continue;
}
var events = RegisteredEventListener.FromType(handler.GetType());
foreach (var eventHandler in events)
@@ -0,0 +1,30 @@
using System;
using System.Threading.Tasks;
using Impostor.Api.Events;
namespace Impostor.Server.Events.Register
{
internal class ManualRegisteredEventListener : IRegisteredEventListener
{
public Type EventType { get; } = typeof(object);
private readonly IManualEventListener _manualEventListener;
public ManualRegisteredEventListener(IManualEventListener manualEventListener)
{
_manualEventListener = manualEventListener;
}
public EventPriority Priority => _manualEventListener.Priority;
public ValueTask InvokeAsync(object eventHandler, object @event, IServiceProvider provider)
{
if (@event is IEvent typedEvent)
{
return _manualEventListener.Execute(typedEvent);
}
return ValueTask.CompletedTask;
}
}
}