Add a crew monitoring server (#7542)

This commit is contained in:
Julian Giebel
2023-01-23 02:07:57 +01:00
committed by GitHub
parent ad9c5ae5e9
commit c2b87dfeda
20 changed files with 518 additions and 58 deletions

View File

@@ -0,0 +1,22 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
namespace Content.Server.DeviceNetwork.Components
{
[RegisterComponent]
public sealed class StationLimitedNetworkComponent : Component
{
/// <summary>
/// The station id the device is limited to.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public EntityUid? StationId;
/// <summary>
/// Whether the entity is allowed to receive packets from entities that are not tied to any station
/// </summary>
[DataField("allowNonStationPackets")]
[ViewVariables(VVAccess.ReadWrite)]
public bool AllowNonStationPackets = false;
}
}

View File

@@ -82,7 +82,7 @@ public sealed class DeviceNet
/// </summary>
public bool Remove(DeviceNetworkComponent device)
{
if (device.Address == null || Devices.Remove(device.Address))
if (device.Address == null || !Devices.Remove(device.Address))
return false;
if (device.ReceiveFrequency is not uint freq)

View File

@@ -48,18 +48,22 @@ namespace Content.Server.DeviceNetwork.Systems
/// <param name="address">The address of the entity that the packet gets sent to. If null, the message is broadcast to all devices on that frequency (except the sender)</param>
/// <param name="frequency">The frequency to send on</param>
/// <param name="data">The data to be sent</param>
public void QueuePacket(EntityUid uid, string? address, NetworkPayload data, uint? frequency = null, DeviceNetworkComponent? device = null)
/// <returns>Returns true when the packet was successfully enqueued.</returns>
public bool QueuePacket(EntityUid uid, string? address, NetworkPayload data, uint? frequency = null, DeviceNetworkComponent? device = null)
{
if (!Resolve(uid, ref device, false))
return;
return false;
if (device.Address == string.Empty)
return;
return false;
frequency ??= device.TransmitFrequency;
if (frequency != null)
_packets.Enqueue(new DeviceNetworkPacketEvent(device.DeviceNetId, address, frequency.Value, device.Address, uid, data));
if (frequency == null)
return false;
_packets.Enqueue(new DeviceNetworkPacketEvent(device.DeviceNetId, address, frequency.Value, device.Address, uid, data));
return true;
}
private void OnExamine(EntityUid uid, DeviceNetworkComponent device, ExaminedEvent args)
@@ -137,6 +141,32 @@ namespace Content.Server.DeviceNetwork.Systems
return GetNetwork(device.DeviceNetId).Remove(device);
}
/// <summary>
/// Checks if a device is already connected to its network
/// </summary>
/// <returns>True if the device was found in the network with its corresponding network id</returns>
public bool IsDeviceConnected(EntityUid uid, DeviceNetworkComponent? device)
{
if (!Resolve(uid, ref device, false))
return false;
if (!_networks.TryGetValue(device.DeviceNetId, out var deviceNet))
return false;
return deviceNet.Devices.ContainsValue(device);
}
/// <summary>
/// Checks if an address exists in the network with the given netId
/// </summary>
public bool IsAddressPresent(int netId, string? address)
{
if (address == null || !_networks.TryGetValue(netId, out var network))
return false;
return network.Devices.ContainsKey(address);
}
public void SetReceiveFrequency(EntityUid uid, uint? frequency, DeviceNetworkComponent? device = null)
{
if (!Resolve(uid, ref device, false))

View File

@@ -0,0 +1,68 @@
using Content.Server.DeviceNetwork.Components;
using Content.Server.Station.Systems;
using JetBrains.Annotations;
using Robust.Shared.Map;
namespace Content.Server.DeviceNetwork.Systems
{
/// <summary>
/// This system requires the StationLimitedNetworkComponent to be on the the sending entity as well as the receiving entity
/// </summary>
[UsedImplicitly]
public sealed class StationLimitedNetworkSystem : EntitySystem
{
[Dependency] private readonly StationSystem _stationSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<StationLimitedNetworkComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<StationLimitedNetworkComponent, BeforePacketSentEvent>(OnBeforePacketSent);
}
/// <summary>
/// Sets the station id the device is limited to.
/// </summary>
public void SetStation(EntityUid uid, EntityUid? stationId, StationLimitedNetworkComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
component.StationId = stationId;
}
/// <summary>
/// Set the station id to the one the entity is on when the station limited component is added
/// </summary>
private void OnMapInit(EntityUid uid, StationLimitedNetworkComponent networkComponent, MapInitEvent args)
{
networkComponent.StationId = _stationSystem.GetOwningStation(uid);
}
/// <summary>
/// Checks if both devices are limited to the same station
/// </summary>
private void OnBeforePacketSent(EntityUid uid, StationLimitedNetworkComponent component, BeforePacketSentEvent args)
{
if (!CheckStationId(args.Sender, component.AllowNonStationPackets, component.StationId))
{
args.Cancel();
}
}
/// <summary>
/// Compares the station IDs of the sending and receiving network components.
/// Returns false if either of them doesn't have a station ID or if their station ID isn't equal.
/// Returns true even when the sending entity isn't tied to a station if `allowNonStationPackets` is set to true.
/// </summary>
private bool CheckStationId(EntityUid senderUid, bool allowNonStationPackets, EntityUid? receiverStationId, StationLimitedNetworkComponent? sender = null)
{
if (!receiverStationId.HasValue)
return false;
if (!Resolve(senderUid, ref sender, false))
return allowNonStationPackets;
return sender.StationId == receiverStationId;
}
}
}