Device network DeviceLists and the NetworkConfigurator (Makes air alarms usable) (#7697)
* Implement DeviceList Implement NetworkConfigurator I sould really get into the habit of making smaller commits * Remove ApcNetworkComponent from vents, scrubbers anf firelocks * Change BeforeBroadcastAttemptEvent#Recepients to readonly IReadonlySet and add a ModifiedRecepients field * Address revievs in NetworkConfigurationSystem * Fix red and green button styles * Change NetworkConfiguratorSystem#UpdateState to remove saved entites that don't exist anymore * Add AtmosDevices device net id * Add const strings for style classes Fix wrong margin for NetworkConfiguratorConfigurationMenu * Hello? Github? * Add access check before opening the configuration ui * Address reviews * Fix call to access reader * You shall not live again IgnoreComponent * Fix interaction verb check * Fix configuration window not closing when target gets deleted / out of range * Change device is already saved message to say 'network device: ... is already saves' * Apply suggestions from code review Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> * Fix applied suggestion Co-authored-by: wrexbe <81056464+wrexbe@users.noreply.github.com> Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>
This commit is contained in:
93
Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs
Normal file
93
Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using Content.Server.DeviceNetwork.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Content.Server.DeviceNetwork.Systems;
|
||||
|
||||
[UsedImplicitly]
|
||||
public sealed class DeviceListSystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<DeviceListComponent, BeforeBroadcastAttemptEvent>(OnBeforeBroadcast);
|
||||
SubscribeLocalEvent<DeviceListComponent, BeforePacketSentEvent>(OnBeforePacketSent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces or merges the current device list with the given one
|
||||
/// </summary>
|
||||
public void UpdateDeviceList(EntityUid uid, IEnumerable<EntityUid> devices, bool merge = false, DeviceListComponent? deviceList = null)
|
||||
{
|
||||
if (!Resolve(uid, ref deviceList))
|
||||
return;
|
||||
|
||||
if (!merge)
|
||||
deviceList.Devices.Clear();
|
||||
|
||||
deviceList.Devices.UnionWith(devices);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// Toggles the given device lists connection visualisation on and off.
|
||||
/// TODO: Implement an overlay that draws a line between the given entity and the entities in the device list
|
||||
/// </summary>
|
||||
public void ToggleVisualization(EntityUid uid, bool ensureOff = false, DeviceListComponent? deviceList = null)
|
||||
{
|
||||
if (!Resolve(uid, ref deviceList))
|
||||
return;
|
||||
}
|
||||
|
||||
/// <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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user