Files
tbd-station-14/Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs
Flipp Syder 9ace52a6c1 Device link visualizer (#11054)
* shuffles devicelist to shared, adds an overlay for devicelist

* adds space property to overlay

* moves networkconfigurator to shared, makes devicelistsystem clientside check activedevicelist

* dirties components upon change, adds networkedcomponent to sharednetworkconfigurator

* state handlers for networked components

* whoops

* lots of shuffling, renaming, and access changes

* randomizes color for every new entity added to the overlay

* adds a client-side action to clear all network overlays if they're active

* clones action (oops)

* localization, adds a command for clearing network link overlays (in case the action disappears)

* moves the entity manager up into the bui fields

* makes that a dependency

* attempts to just directly get the color from the dict when drawing, now

* fixes up a few comments

* adds dirty on init to devicelistcomponent

* hacky solution related to mapping with a networkconfigurator

* more stricter bound on that hacky solution

* just checks if the life stage is initialized instead of if the entity was initialized

* moves getalldevices to shared

* readds linq import

* tries to ensure that the show button is toggled on if the device we're trying to configure is currently being tracked by the overlay

* some reorganization
2022-09-05 19:55:44 -05:00

72 lines
2.4 KiB
C#

using System.Linq;
using Content.Server.DeviceNetwork.Components;
using Content.Shared.DeviceNetwork;
using Content.Shared.Interaction;
using JetBrains.Annotations;
namespace Content.Server.DeviceNetwork.Systems;
[UsedImplicitly]
public sealed class DeviceListSystem : SharedDeviceListSystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DeviceListComponent, BeforeBroadcastAttemptEvent>(OnBeforeBroadcast);
SubscribeLocalEvent<DeviceListComponent, BeforePacketSentEvent>(OnBeforePacketSent);
}
/// <summary>
/// Gets the given device list as a dictionary
/// </summary>
public Dictionary<string, EntityUid> GetDeviceList(EntityUid uid, DeviceListComponent? deviceList = null)
{
if (!Resolve(uid, ref deviceList))
return new Dictionary<string, EntityUid>();
var devices = new Dictionary<string, EntityUid>(deviceList.Devices.Count);
foreach (var deviceUid in deviceList.Devices)
{
if (!TryComp(deviceUid, out DeviceNetworkComponent? deviceNet))
continue;
devices.Add(deviceNet.Address, deviceUid);
}
return devices;
}
/// <summary>
/// Filters the broadcasts recipient list against the device list as either an allow or deny list depending on the components IsAllowList field
/// </summary>
private void OnBeforeBroadcast(EntityUid uid, DeviceListComponent component, BeforeBroadcastAttemptEvent args)
{
//Don't filter anything if the device list is empty
if (component.Devices.Count == 0)
{
if (component.IsAllowList) args.Cancel();
return;
}
HashSet<DeviceNetworkComponent> filteredRecipients = new(args.Recipients.Count);
foreach (var recipient in args.Recipients)
{
if (component.Devices.Contains(recipient.Owner) == component.IsAllowList) filteredRecipients.Add(recipient);
}
args.ModifiedRecipients = filteredRecipients;
}
/// <summary>
/// Filters incoming packets if that is enabled <see cref="OnBeforeBroadcast"/>
/// </summary>
private void OnBeforePacketSent(EntityUid uid, DeviceListComponent component, BeforePacketSentEvent args)
{
if (component.HandleIncomingPackets && component.Devices.Contains(args.Sender) != component.IsAllowList)
args.Cancel();
}
}