StationLimitedNetworkSystem.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Content.Server.DeviceNetwork.Components;
  2. using Content.Server.Station.Systems;
  3. using JetBrains.Annotations;
  4. using Robust.Shared.Map;
  5. namespace Content.Server.DeviceNetwork.Systems
  6. {
  7. /// <summary>
  8. /// This system requires the StationLimitedNetworkComponent to be on the the sending entity as well as the receiving entity
  9. /// </summary>
  10. [UsedImplicitly]
  11. public sealed class StationLimitedNetworkSystem : EntitySystem
  12. {
  13. [Dependency] private readonly StationSystem _stationSystem = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<StationLimitedNetworkComponent, MapInitEvent>(OnMapInit);
  18. SubscribeLocalEvent<StationLimitedNetworkComponent, BeforePacketSentEvent>(OnBeforePacketSent);
  19. }
  20. /// <summary>
  21. /// Sets the station id the device is limited to.
  22. /// </summary>
  23. public void SetStation(EntityUid uid, EntityUid? stationId, StationLimitedNetworkComponent? component = null)
  24. {
  25. if (!Resolve(uid, ref component))
  26. return;
  27. component.StationId = stationId;
  28. }
  29. /// <summary>
  30. /// Tries to set the station id to the current station if the device is currently on a station
  31. /// </summary>
  32. public bool TrySetStationId(EntityUid uid, StationLimitedNetworkComponent? component = null)
  33. {
  34. if (!Resolve(uid, ref component) || !Transform(uid).GridUid.HasValue)
  35. return false;
  36. component.StationId = _stationSystem.GetOwningStation(uid);
  37. return component.StationId.HasValue;
  38. }
  39. /// <summary>
  40. /// Set the station id to the one the entity is on when the station limited component is added
  41. /// </summary>
  42. private void OnMapInit(EntityUid uid, StationLimitedNetworkComponent networkComponent, MapInitEvent args)
  43. {
  44. networkComponent.StationId = _stationSystem.GetOwningStation(uid);
  45. }
  46. /// <summary>
  47. /// Checks if both devices are limited to the same station
  48. /// </summary>
  49. private void OnBeforePacketSent(EntityUid uid, StationLimitedNetworkComponent component, BeforePacketSentEvent args)
  50. {
  51. if (!component.StationId.HasValue)
  52. TrySetStationId(uid, component);
  53. if (!CheckStationId(args.Sender, component.AllowNonStationPackets, component.StationId))
  54. {
  55. args.Cancel();
  56. }
  57. }
  58. /// <summary>
  59. /// Compares the station IDs of the sending and receiving network components.
  60. /// Returns false if either of them doesn't have a station ID or if their station ID isn't equal.
  61. /// Returns true even when the sending entity isn't tied to a station if `allowNonStationPackets` is set to true.
  62. /// </summary>
  63. private bool CheckStationId(EntityUid senderUid, bool allowNonStationPackets, EntityUid? receiverStationId, StationLimitedNetworkComponent? sender = null)
  64. {
  65. if (!receiverStationId.HasValue)
  66. return false;
  67. if (!Resolve(senderUid, ref sender, false))
  68. return allowNonStationPackets;
  69. if (!sender.StationId.HasValue)
  70. TrySetStationId(senderUid, sender);
  71. return sender.StationId == receiverStationId;
  72. }
  73. }
  74. }