using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Robust.Shared.IoC;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Reflection;
namespace Robust.Shared.GameObjects
{
///
/// A subsystem that acts on all components of a type at once.
///
///
/// This class is instantiated by the EntitySystemManager, and any IoC Dependencies will be resolved.
///
[Reflect(false), PublicAPI]
public abstract class EntitySystem : IEntitySystem
{
[Dependency] protected readonly IEntityManager EntityManager = default!;
[Dependency] protected readonly IComponentManager ComponentManager = default!;
[Dependency] protected readonly IEntitySystemManager EntitySystemManager = default!;
[Obsolete("You need to create and store the query yourself in a field.")]
protected IEntityQuery? EntityQuery;
[Obsolete("You need to use `EntityManager.GetEntities(EntityQuery)`, or store a query yourself.")]
protected IEnumerable RelevantEntities => EntityQuery != null ? EntityManager.GetEntities(EntityQuery) : EntityManager.GetEntities();
protected internal List UpdatesAfter { get; } = new();
protected internal List UpdatesBefore { get; } = new();
IEnumerable IEntitySystem.UpdatesAfter => UpdatesAfter;
IEnumerable IEntitySystem.UpdatesBefore => UpdatesBefore;
///
public virtual void Initialize() { }
///
public virtual void Update(float frameTime) { }
///
public virtual void FrameUpdate(float frameTime) { }
///
public virtual void Shutdown() { }
#region Event Proxy
protected void SubscribeNetworkEvent(EntityEventHandler handler)
where T : notnull
{
EntityManager.EventBus.SubscribeEvent(EventSource.Network, this, handler);
}
protected void SubscribeNetworkEvent(EntitySessionEventHandler handler)
where T : notnull
{
EntityManager.EventBus.SubscribeSessionEvent(EventSource.Network, this, handler);
}
protected void SubscribeLocalEvent(EntityEventHandler handler)
where T : notnull
{
EntityManager.EventBus.SubscribeEvent(EventSource.Local, this, handler);
}
protected void SubscribeLocalEvent(EntitySessionEventHandler handler)
where T : notnull
{
EntityManager.EventBus.SubscribeSessionEvent(EventSource.Local, this, handler);
}
protected void UnsubscribeNetworkEvent()
where T : notnull
{
EntityManager.EventBus.UnsubscribeEvent(EventSource.Network, this);
}
protected void UnsubscribeLocalEvent()
where T : notnull
{
EntityManager.EventBus.UnsubscribeEvent(EventSource.Local, this);
}
protected void RaiseLocalEvent(T message) where T : notnull
{
EntityManager.EventBus.RaiseEvent(EventSource.Local, message);
}
protected void RaiseLocalEvent(object message)
{
EntityManager.EventBus.RaiseEvent(EventSource.Local, message);
}
protected void QueueLocalEvent(EntityEventArgs message)
{
EntityManager.EventBus.QueueEvent(EventSource.Local, message);
}
protected void RaiseNetworkEvent(EntityEventArgs message)
{
EntityManager.EntityNetManager?.SendSystemNetworkMessage(message);
}
protected void RaiseNetworkEvent(EntityEventArgs message, INetChannel channel)
{
EntityManager.EntityNetManager?.SendSystemNetworkMessage(message, channel);
}
protected void RaiseNetworkEvent(EntityEventArgs message, Filter filter)
{
foreach (var session in filter.Recipients)
{
EntityManager.EntityNetManager?.SendSystemNetworkMessage(message, session.ConnectedClient);
}
}
protected Task AwaitNetworkEvent(CancellationToken cancellationToken)
where T : EntityEventArgs
{
return EntityManager.EventBus.AwaitEvent(EventSource.Network, cancellationToken);
}
protected void SubscribeLocalEvent(ComponentEventHandler handler)
where TComp : IComponent
where TEvent : EntityEventArgs
{
EntityManager.EventBus.SubscribeLocalEvent(handler);
}
[Obsolete("Use the overload without the handler argument.")]
protected void UnsubscribeLocalEvent(ComponentEventHandler handler)
where TComp : IComponent
where TEvent : EntityEventArgs
{
EntityManager.EventBus.UnsubscribeLocalEvent();
}
protected void UnsubscribeLocalEvent()
where TComp : IComponent
where TEvent : EntityEventArgs
{
EntityManager.EventBus.UnsubscribeLocalEvent();
}
protected void RaiseLocalEvent(EntityUid uid, TEvent args, bool broadcast = true)
where TEvent : EntityEventArgs
{
EntityManager.EventBus.RaiseLocalEvent(uid, args, broadcast);
}
#endregion
#region Static Helpers
/*
NOTE: Static helpers relating to EntitySystems are here rather than in a
static helper class for conciseness / usability. If we had an "EntitySystems" static class
it would conflict with any imported namespace called "EntitySystems" and require using alias directive, and
if we called it something longer like "EntitySystemUtility", writing out "EntitySystemUtility.Get" seems
pretty tedious for a potentially commonly-used method. Putting it here allows writing "EntitySystem.Get"
which is nice and concise.
*/
///
/// Gets the indicated entity system.
///
/// entity system to get
///
public static T Get() where T : IEntitySystem
{
return IoCManager.Resolve().GetEntitySystem();
}
///
/// Tries to get an entity system of the specified type.
///
/// Type of entity system to find.
/// instance matching the specified type (if exists).
/// If an instance of the specified entity system type exists.
public static bool TryGet([NotNullWhen(true)] out T? entitySystem) where T : IEntitySystem
{
return IoCManager.Resolve().TryGetEntitySystem(out entitySystem);
}
#endregion
}
}