Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor EntitySystem proxy into its own class, give proxy to BUIs #5561

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ namespace Robust.Shared.GameObjects
/// <summary>
/// An abstract class to override to implement bound user interfaces.
/// </summary>
public abstract class BoundUserInterface : IDisposable
public abstract class BoundUserInterface : EntManProxy, IDisposable
{
[Dependency] protected readonly IEntityManager EntMan = default!;
[Dependency] protected readonly ISharedPlayerManager PlayerManager = default!;
protected IEntityManager EntMan => EntityManager;
protected readonly SharedUserInterfaceSystem UiSystem;

public readonly Enum UiKey;
Expand Down
97 changes: 97 additions & 0 deletions Robust.Shared/GameObjects/EntManProxy.Events.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Runtime.CompilerServices;
using Robust.Shared.Network;
using Robust.Shared.Player;

namespace Robust.Shared.GameObjects;

public abstract partial class EntManProxy
{
protected void RaiseLocalEvent<T>(T message) where T : notnull
{
EntityManager.EventBus.RaiseEvent(EventSource.Local, message);
}

protected void RaiseLocalEvent<T>(ref T message) where T : notnull
{
EntityManager.EventBus.RaiseEvent(EventSource.Local, ref 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, ICommonSession session)
{
EntityManager.EntityNetManager?.SendSystemNetworkMessage(message, session.Channel);
}

/// <summary>
/// Raises a networked event with some filter.
/// </summary>
/// <param name="message">The event to send</param>
/// <param name="filter">The filter that specifies recipients</param>
/// <param name="recordReplay">Optional bool specifying whether or not to save this event to replays.</param>
protected void RaiseNetworkEvent(EntityEventArgs message, Filter filter, bool recordReplay = true)
{
if (recordReplay)
ReplayMan.RecordServerMessage(message);

foreach (var session in filter.Recipients)
{
EntityManager.EntityNetManager?.SendSystemNetworkMessage(message, session.Channel);
}
}

protected void RaiseNetworkEvent(EntityEventArgs message, EntityUid recipient)
{
if (PlayerManager.TryGetSessionByEntity(recipient, out var session))
EntityManager.EntityNetManager?.SendSystemNetworkMessage(message, session.Channel);
}

protected void RaiseLocalEvent<TEvent>(EntityUid uid, TEvent args, bool broadcast = false)
where TEvent : notnull
{
EntityManager.EventBus.RaiseLocalEvent(uid, args, broadcast);
}

protected void RaiseLocalEvent(EntityUid uid, object args, bool broadcast = false)
{
EntityManager.EventBus.RaiseLocalEvent(uid, args, broadcast);
}

protected void RaiseLocalEvent<TEvent>(EntityUid uid, ref TEvent args, bool broadcast = false)
where TEvent : notnull
{
EntityManager.EventBus.RaiseLocalEvent(uid, ref args, broadcast);
}

protected void RaiseLocalEvent(EntityUid uid, ref object args, bool broadcast = false)
{
EntityManager.EventBus.RaiseLocalEvent(uid, ref args, broadcast);
}

/// <summary>
/// Sends a networked message to the server, while also repeatedly raising it locally for every time this tick gets re-predicted.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void RaisePredictiveEvent<T>(T msg) where T : EntityEventArgs
{
EntityManager.RaisePredictiveEvent(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,43 @@
using Robust.Shared.Containers;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using Robust.Shared.Replays;

namespace Robust.Shared.GameObjects;

public partial class EntitySystem
/// <summary>
/// Base abstract class for classes that have an <see cref="EntityManager"/> that can have methods proxied for convenience.
/// </summary>
/// <example>
/// In an <see cref="EntitySystem"/> instead of using the verbose <c>EntityManager.HasComponent<T>(uid)</c>, you can use the proxy <c>HasComp(uid)</c>.
/// </example>
public abstract partial class EntManProxy
{
#region Managers

/// <summary>
/// The <see cref="EntityManager"/> reference to use for proxy methods.
/// </summary>
/// <remarks>
/// This cannot use <see cref="IEntityManager"/> as some non-interfaced methods are used.
/// </remarks>
[IoC.Dependency] protected readonly EntityManager EntityManager = default!;

/// <summary>
/// The <see cref="ISharedPlayerManager"/> reference to use for raising network events.
/// </summary>
[IoC.Dependency] protected readonly ISharedPlayerManager PlayerManager = default!;

/// <summary>
/// The <see cref="IReplayRecordingManager"/> reference to use for raising network events.
/// </summary>
[IoC.Dependency] protected readonly IReplayRecordingManager ReplayMan = default!;

#endregion

#region Entity LifeStage

/// <inheritdoc cref="IEntityManager.EntityExists(EntityUid)" />
Expand Down Expand Up @@ -928,19 +958,6 @@ protected IEnumerable<TComp1> EntityQuery<TComp1>(bool includePaused = false) wh

#endregion

#region Networked Events

/// <summary>
/// Sends a networked message to the server, while also repeatedly raising it locally for every time this tick gets re-predicted.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void RaisePredictiveEvent<T>(T msg) where T : EntityEventArgs
{
EntityManager.RaisePredictiveEvent(msg);
}

#endregion

#region NetEntities

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
92 changes: 1 addition & 91 deletions Robust.Shared/GameObjects/EntitySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Reflection;
using Robust.Shared.Replays;

namespace Robust.Shared.GameObjects
{
Expand All @@ -21,12 +18,9 @@ namespace Robust.Shared.GameObjects
/// This class is instantiated by the <c>EntitySystemManager</c>, and any IoC Dependencies will be resolved.
/// </remarks>
[Reflect(false), PublicAPI]
public abstract partial class EntitySystem : IEntitySystem, IPostInjectInit
public abstract partial class EntitySystem : EntManProxy, IEntitySystem, IPostInjectInit
{
[Dependency] protected readonly EntityManager EntityManager = default!;
[Dependency] protected readonly ILogManager LogManager = default!;
[Dependency] private readonly ISharedPlayerManager _playerMan = default!;
[Dependency] private readonly IReplayRecordingManager _replayMan = default!;
[Dependency] protected readonly ILocalizationManager Loc = default!;

public ISawmill Log { get; private set; } = default!;
Expand Down Expand Up @@ -93,90 +87,6 @@ public virtual void Shutdown()
ShutdownSubscriptions();
}

#region Event Proxy

protected void RaiseLocalEvent<T>(T message) where T : notnull
{
EntityManager.EventBus.RaiseEvent(EventSource.Local, message);
}

protected void RaiseLocalEvent<T>(ref T message) where T : notnull
{
EntityManager.EventBus.RaiseEvent(EventSource.Local, ref 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, ICommonSession session)
{
EntityManager.EntityNetManager?.SendSystemNetworkMessage(message, session.Channel);
}

/// <summary>
/// Raises a networked event with some filter.
/// </summary>
/// <param name="message">The event to send</param>
/// <param name="filter">The filter that specifies recipients</param>
/// <param name="recordReplay">Optional bool specifying whether or not to save this event to replays.</param>
protected void RaiseNetworkEvent(EntityEventArgs message, Filter filter, bool recordReplay = true)
{
if (recordReplay)
_replayMan.RecordServerMessage(message);

foreach (var session in filter.Recipients)
{
EntityManager.EntityNetManager?.SendSystemNetworkMessage(message, session.Channel);
}
}

protected void RaiseNetworkEvent(EntityEventArgs message, EntityUid recipient)
{
if (_playerMan.TryGetSessionByEntity(recipient, out var session))
EntityManager.EntityNetManager?.SendSystemNetworkMessage(message, session.Channel);
}

protected void RaiseLocalEvent<TEvent>(EntityUid uid, TEvent args, bool broadcast = false)
where TEvent : notnull
{
EntityManager.EventBus.RaiseLocalEvent(uid, args, broadcast);
}

protected void RaiseLocalEvent(EntityUid uid, object args, bool broadcast = false)
{
EntityManager.EventBus.RaiseLocalEvent(uid, args, broadcast);
}

protected void RaiseLocalEvent<TEvent>(EntityUid uid, ref TEvent args, bool broadcast = false)
where TEvent : notnull
{
EntityManager.EventBus.RaiseLocalEvent(uid, ref args, broadcast);
}

protected void RaiseLocalEvent(EntityUid uid, ref object args, bool broadcast = false)
{
EntityManager.EventBus.RaiseLocalEvent(uid, ref args, broadcast);
}

#endregion

#region Static Helpers
/*
NOTE: Static helpers relating to EntitySystems are here rather than in a
Expand Down
Loading