mirror of
https://github.com/Impostor/Impostor.git
synced 2026-10-19 15:51:42 +00:00
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:
5 files changed
+53
-4
No files matched your search
@@ -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,11 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Impostor.Api\Impostor.Api.csproj" />
|
||||
<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)
|
||||
@@ -120,4 +126,4 @@ namespace Impostor.Server.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user