Files
RobustToolbox/Robust.Shared/GameObjects/EventBusExt.cs
Pieter-Jan Briers 72314a102d EventBus improvements
Can do ordered session event subscription.

Can subscribe to "All" in one call, to reduce shared boilerplate.
2021-07-21 00:56:42 +02:00

66 lines
1.9 KiB
C#

using System;
namespace Robust.Shared.GameObjects
{
public static class EventBusExt
{
public static void SubscribeSessionEvent<T>(
this IEventBus eventBus,
EventSource source,
IEntityEventSubscriber subscriber,
EntitySessionEventHandler<T> handler)
{
var wrapper = new HandlerWrapper<T>(handler);
eventBus.SubscribeEvent<EntitySessionMessage<T>>(source, subscriber, wrapper.Invoke);
}
public static void SubscribeSessionEvent<T>(
this IEventBus eventBus,
EventSource source,
IEntityEventSubscriber subscriber,
EntitySessionEventHandler<T> handler,
Type orderType,
Type[]? before=null,
Type[]? after=null)
{
var wrapper = new HandlerWrapper<T>(handler);
eventBus.SubscribeEvent<EntitySessionMessage<T>>(
source,
subscriber,
wrapper.Invoke,
orderType,
before, after);
}
private sealed class HandlerWrapper<T>
{
public HandlerWrapper(EntitySessionEventHandler<T> handler)
{
Handler = handler;
}
public EntitySessionEventHandler<T> Handler { get; }
public void Invoke(EntitySessionMessage<T> msg)
{
Handler(msg.Message, msg.EventArgs);
}
private bool Equals(HandlerWrapper<T> other)
{
return Handler.Equals(other.Handler);
}
public override bool Equals(object? obj)
{
return ReferenceEquals(this, obj) || obj is HandlerWrapper<T> other && Equals(other);
}
public override int GetHashCode()
{
return Handler.GetHashCode();
}
}
}
}