Skip to content

Commit 7ecd7f4

Browse files
JKamskerWeirdoAeonLucid
authored
Giving plugin more control over when it should be executed (Impostor#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 <[email protected]> Co-authored-by: AeonLucid <[email protected]>
1 parent 055366f commit 7ecd7f4

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/src/Impostor.Plugins.Debugger/Properties/launchSettings.json
55
/tools
66

7+
.local/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Threading.Tasks;
2+
3+
namespace Impostor.Api.Events
4+
{
5+
public interface IManualEventListener : IEventListener
6+
{
7+
public bool CanExecute<T>();
8+
9+
public ValueTask Execute(IEvent @event);
10+
11+
EventPriority Priority { get; set; }
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<ProjectReference Include="..\Impostor.Api\Impostor.Api.csproj" />
8+
<ProjectReference Include="..\Impostor.Api\Impostor.Api.csproj"/>
99
</ItemGroup>
10-
1110
</Project>

src/Impostor.Server/Events/EventManager.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ private IEnumerable<EventHandler> GetHandlers<TEvent>(IServiceProvider services)
9898
{
9999
foreach (var handler in services.GetServices<IEventListener>())
100100
{
101+
if (handler is IManualEventListener manualEventListener && manualEventListener.CanExecute<TEvent>())
102+
{
103+
yield return new EventHandler(handler, new ManualRegisteredEventListener(manualEventListener));
104+
continue;
105+
}
106+
101107
var events = RegisteredEventListener.FromType(handler.GetType());
102108

103109
foreach (var eventHandler in events)
@@ -120,4 +126,4 @@ private IEnumerable<EventHandler> GetHandlers<TEvent>(IServiceProvider services)
120126
}
121127
}
122128
}
123-
}
129+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Impostor.Api.Events;
4+
5+
namespace Impostor.Server.Events.Register
6+
{
7+
internal class ManualRegisteredEventListener : IRegisteredEventListener
8+
{
9+
public Type EventType { get; } = typeof(object);
10+
11+
private readonly IManualEventListener _manualEventListener;
12+
13+
public ManualRegisteredEventListener(IManualEventListener manualEventListener)
14+
{
15+
_manualEventListener = manualEventListener;
16+
}
17+
18+
public EventPriority Priority => _manualEventListener.Priority;
19+
20+
public ValueTask InvokeAsync(object eventHandler, object @event, IServiceProvider provider)
21+
{
22+
if (@event is IEvent typedEvent)
23+
{
24+
return _manualEventListener.Execute(typedEvent);
25+
}
26+
27+
return ValueTask.CompletedTask;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)