mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
Can do ordered session event subscription. Can subscribe to "All" in one call, to reduce shared boilerplate.
66 lines
1.9 KiB
C#
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();
|
|
}
|
|
}
|
|
}
|
|
}
|