using System; using System.Collections.Generic; namespace Robust.Shared.GameObjects { public abstract partial class EntitySystem { private List? _subscriptions; protected void SubscribeNetworkEvent(EntityEventHandler handler) where T : notnull { EntityManager.EventBus.SubscribeEvent(EventSource.Network, this, handler); _subscriptions ??= new(); _subscriptions.Add(new SubBroadcast(EventSource.Network)); } protected void SubscribeNetworkEvent(EntitySessionEventHandler handler) where T : notnull { EntityManager.EventBus.SubscribeSessionEvent(EventSource.Network, this, handler); _subscriptions ??= new(); _subscriptions.Add(new SubBroadcast>(EventSource.Network)); } protected void SubscribeLocalEvent(EntityEventHandler handler) where T : notnull { EntityManager.EventBus.SubscribeEvent(EventSource.Local, this, handler); _subscriptions ??= new(); _subscriptions.Add(new SubBroadcast(EventSource.Local)); } protected void SubscribeLocalEvent(EntitySessionEventHandler handler) where T : notnull { EntityManager.EventBus.SubscribeSessionEvent(EventSource.Local, this, handler); _subscriptions ??= new(); _subscriptions.Add(new SubBroadcast>(EventSource.Local)); } [Obsolete("Unsubscribing of entity system events is now automatic")] protected void UnsubscribeNetworkEvent() where T : notnull { EntityManager.EventBus.UnsubscribeEvent(EventSource.Network, this); } [Obsolete("Unsubscribing of entity system events is now automatic")] protected void UnsubscribeLocalEvent() where T : notnull { EntityManager.EventBus.UnsubscribeEvent(EventSource.Local, this); } protected void SubscribeLocalEvent(ComponentEventHandler handler) where TComp : IComponent where TEvent : EntityEventArgs { EntityManager.EventBus.SubscribeLocalEvent(handler); _subscriptions ??= new(); _subscriptions.Add(new SubLocal()); } [Obsolete("Unsubscribing of entity system events is now automatic")] protected void UnsubscribeLocalEvent(ComponentEventHandler handler) where TComp : IComponent where TEvent : EntityEventArgs { EntityManager.EventBus.UnsubscribeLocalEvent(); } [Obsolete("Unsubscribing of entity system events is now automatic")] protected void UnsubscribeLocalEvent() where TComp : IComponent where TEvent : EntityEventArgs { EntityManager.EventBus.UnsubscribeLocalEvent(); } private void ShutdownSubscriptions() { if (_subscriptions == null) return; foreach (var sub in _subscriptions) { sub.Unsubscribe(this, EntityManager.EventBus); } _subscriptions = null; } private abstract class SubBase { public abstract void Unsubscribe(EntitySystem sys, IEventBus bus); } private sealed class SubBroadcast : SubBase where T : notnull { private readonly EventSource _source; public SubBroadcast(EventSource source) { _source = source; } public override void Unsubscribe(EntitySystem sys, IEventBus bus) { bus.UnsubscribeEvent(_source, sys); } } private sealed class SubLocal : SubBase where TComp : IComponent where TBase : EntityEventArgs { public override void Unsubscribe(EntitySystem sys, IEventBus bus) { bus.UnsubscribeLocalEvent(); } } } }