Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Update DeviceListSystem.cs #99

Closed
wants to merge 37 commits into from
Closed
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
0365000
Update DeviceListSystem.cs
Vonsant May 1, 2024
e2c2b9a
Update DeviceListSystem.cs
Vonsant May 1, 2024
9262564
Update DeviceListSystem.cs
Vonsant May 1, 2024
e16527e
Update DeviceListSystem.cs
Vonsant May 1, 2024
4a42271
Update DeviceListSystem.cs
Vonsant May 1, 2024
597a07e
Update DeviceListSystem.cs
Vonsant May 1, 2024
e9ead14
Update DeviceListSystem.cs
Vonsant May 1, 2024
f1c69cd
Update DeviceListSystem.cs
Vonsant May 1, 2024
130f9a2
Update DeviceListSystem.cs
Vonsant May 1, 2024
1b68e58
Update DeviceListSystem.cs
Vonsant May 1, 2024
dbe5258
Update DeviceListSystem.cs
Vonsant May 1, 2024
9ac12d3
Update DeviceListSystem.cs
Vonsant May 1, 2024
78c0a3d
Update DeviceListSystem.cs
Vonsant May 1, 2024
9313d7e
Update DeviceListSystem.cs
Vonsant May 1, 2024
68b9edc
Update DeviceListSystem.cs
Vonsant May 1, 2024
a363c62
Update DeviceListSystem.cs
Vonsant May 1, 2024
908d5ba
Update DeviceListSystem.cs
Vonsant May 1, 2024
7db7d05
Update DeviceListSystem.cs
Vonsant May 1, 2024
bf1f676
Update DeviceListSystem.cs
Vonsant May 1, 2024
eccbfdc
Update DeviceListSystem.cs
Vonsant May 1, 2024
37ca9c1
Update DeviceListSystem.cs
Vonsant May 1, 2024
c0be248
Update DeviceListSystem.cs
Vonsant May 1, 2024
6ecb675
Update DeviceListSystem.cs
Vonsant May 1, 2024
f81c51c
Update DeviceListSystem.cs
Vonsant May 1, 2024
690be75
Update DeviceListSystem.cs
Vonsant May 1, 2024
83f28f5
Update DeviceListSystem.cs
Vonsant May 1, 2024
6e334d9
Update DeviceListSystem.cs
Vonsant May 1, 2024
abff62d
Update DeviceListSystem.cs
Vonsant May 1, 2024
244e842
Update DeviceListSystem.cs
Vonsant May 1, 2024
f859f91
Update DeviceListSystem.cs
Vonsant May 1, 2024
794c485
Update DeviceListSystem.cs
Vonsant May 1, 2024
8e1a72a
Update DeviceListSystem.cs
Vonsant May 1, 2024
8cebe87
Update DeviceListSystem.cs
Vonsant May 1, 2024
764c6b9
Update DeviceListSystem.cs
Vonsant May 1, 2024
71282f3
Update DeviceListSystem.cs
Vonsant May 1, 2024
a3f63d3
Update DeviceListSystem.cs
Vonsant May 1, 2024
331dbad
Update DeviceListSystem.cs
Vonsant May 1, 2024
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
144 changes: 38 additions & 106 deletions Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
using System.Linq;
using System.Collections.Generic;
using Content.Server.DeviceNetwork.Components;
using Content.Shared.DeviceNetwork;
using Content.Shared.DeviceNetwork.Components;
using Content.Shared.DeviceNetwork.Systems;
using Content.Shared.Interaction;
using JetBrains.Annotations;
using Robust.Shared.Map.Events;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Timing;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.IoC;

namespace Content.Server.DeviceNetwork.Systems;

[UsedImplicitly]
public sealed class DeviceListSystem : SharedDeviceListSystem
{
[Dependency] private readonly NetworkConfiguratorSystem _configurator = default!;
private readonly List<DeviceListProcessingData> _processingData = new();

public override void Initialize()
{
Expand All @@ -21,6 +28,8 @@
SubscribeLocalEvent<DeviceListComponent, BeforeBroadcastAttemptEvent>(OnBeforeBroadcast);
SubscribeLocalEvent<DeviceListComponent, BeforePacketSentEvent>(OnBeforePacketSent);
SubscribeLocalEvent<BeforeSaveEvent>(OnMapSave);
SubscribeLocalEvent<PostUpdateEvent>(OnPostUpdate);
SubscribeLocalEvent<GenericEvent>(OnDeviceListProcessed);
}

private void OnShutdown(EntityUid uid, DeviceListComponent component, ComponentShutdown args)
Expand All @@ -39,54 +48,8 @@
component.Devices.Clear();
}

/// <summary>
/// Gets the given device list as a dictionary
/// </summary>
/// <remarks>
/// If any entity in the device list is pre-map init, it will show the entity UID of the device instead.
/// </remarks>
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;

var address = MetaData(deviceUid).EntityLifeStage == EntityLifeStage.MapInitialized
? deviceNet.Address
: $"UID: {deviceUid.ToString()}";

devices.Add(address, deviceUid);

}

return devices;
}

/// <summary>
/// Checks if the given address is present in a device list
/// </summary>
/// <param name="uid">The entity uid that has the device list that should be checked for the address</param>
/// <param name="address">The address to check for</param>
/// <param name="deviceList">The device list component</param>
/// <returns>True if the address is present. False if not</returns>
public bool ExistsInDeviceList(EntityUid uid, string address, DeviceListComponent? deviceList = null)
{
var addresses = GetDeviceList(uid).Keys;
return addresses.Contains(address);
}

/// <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)
Expand All @@ -95,7 +58,6 @@
}

HashSet<DeviceNetworkComponent> filteredRecipients = new(args.Recipients.Count);

foreach (var recipient in args.Recipients)
{
if (component.Devices.Contains(recipient.Owner) == component.IsAllowList)
Expand All @@ -105,25 +67,12 @@
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();
}

public void OnDeviceShutdown(Entity<DeviceListComponent?> list, Entity<DeviceNetworkComponent> device)
{
device.Comp.DeviceLists.Remove(list.Owner);
if (!Resolve(list.Owner, ref list.Comp))
return;

list.Comp.Devices.Remove(device);
Dirty(list);
}

private void OnMapSave(BeforeSaveEvent ev)
{
List<EntityUid> toRemove = new();
Expand All @@ -138,8 +87,6 @@
{
if (!query.TryGetComponent(ent, out var linkedXform))
{
// Entity was deleted.
// TODO remove these on deletion instead of on-save.
toRemove.Add(ent);
continue;
}
Expand All @@ -148,9 +95,6 @@
continue;

toRemove.Add(ent);
// TODO full game saves.
// when full saves are supported, this should instead add data to the BeforeSaveEvent informing the
// saving system that this map (or null-space entity) also needs to be included in the save.
Log.Error(
$"Saving a device list ({ToPrettyString(uid)}) that has a reference to an entity on another map ({ToPrettyString(ent)}). Removing entity from list.");
}
Expand All @@ -166,56 +110,44 @@
}
}

/// <summary>
/// Updates the device list stored on this entity.
/// </summary>
/// <param name="uid">The entity to update.</param>
/// <param name="devices">The devices to store.</param>
/// <param name="merge">Whether to merge or replace the devices stored.</param>
/// <param name="deviceList">Device list component</param>
public DeviceListUpdateResult UpdateDeviceList(EntityUid uid, IEnumerable<EntityUid> devices, bool merge = false, DeviceListComponent? deviceList = null)
private void OnPostUpdate(PostUpdateEvent ev)

Check failure on line 113 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / Test Packaging

The type or namespace name 'PostUpdateEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 113 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / Test Packaging

The type or namespace name 'PostUpdateEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 113 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'PostUpdateEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 113 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'PostUpdateEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 113 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / YAML Linter

The type or namespace name 'PostUpdateEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 113 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / YAML Linter

The type or namespace name 'PostUpdateEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 113 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'PostUpdateEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 113 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'PostUpdateEvent' could not be found (are you missing a using directive or an assembly reference?)
{
if (!Resolve(uid, ref deviceList))
return DeviceListUpdateResult.NoComponent;

var list = devices.ToList();
var newDevices = new HashSet<EntityUid>(list);
RaiseLocalEvent(new GenericEvent(DeviceListProcessedEvent));
}

if (merge)
newDevices.UnionWith(deviceList.Devices);
private void OnDeviceListProcessed(GenericEvent ev)

Check failure on line 118 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / Test Packaging

The type or namespace name 'GenericEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 118 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / Test Packaging

The type or namespace name 'GenericEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 118 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'GenericEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 118 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'GenericEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 118 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / YAML Linter

The type or namespace name 'GenericEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 118 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / YAML Linter

The type or namespace name 'GenericEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 118 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'GenericEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 118 in Content.Server/DeviceNetwork/Systems/DeviceListSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'GenericEvent' could not be found (are you missing a using directive or an assembly reference?)
{
if (ev.Event != DeviceListProcessedEvent)
return;

if (newDevices.Count > deviceList.DeviceLimit)
foreach (var data in _processingData)
{
return DeviceListUpdateResult.TooManyDevices;
}

var query = GetEntityQuery<DeviceNetworkComponent>();
var oldDevices = deviceList.Devices.ToList();
foreach (var device in oldDevices)
{
if (newDevices.Contains(device))
continue;
var component = data.Component;
var args = data.Args;
var entities = new HashSet<EntityUid>(component.Devices.Count);
foreach (var device in component.Devices)
{
if (EntityManager.TryGetComponent(device, out MetaDataComponent? meta))
entities.Add(device);
}

deviceList.Devices.Remove(device);
if (query.TryGetComponent(device, out var comp))
comp.DeviceLists.Remove(uid);
args.State = new DeviceListComponentState(entities, component.IsAllowList, component.HandleIncomingPackets);
}

foreach (var device in newDevices)
{
if (!query.TryGetComponent(device, out var comp))
continue;
_processingData.Clear();
}

if (!deviceList.Devices.Add(device))
continue;
public sealed class DeviceListProcessingData
{
public EntityUid Uid { get; }
public DeviceListComponent Component { get; }
public ComponentGetState Args { get; }

comp.DeviceLists.Add(uid);
public DeviceListProcessingData(EntityUid uid, DeviceListComponent component, ComponentGetState args)
{
Uid = uid;
Component = component;
Args = args;
}

RaiseLocalEvent(uid, new DeviceListUpdateEvent(oldDevices, list));

Dirty(uid, deviceList);

return DeviceListUpdateResult.UpdateOk;
}
}
Loading