Remove devices from device lists when they get deleted (#16783)

* Implement device network device shutdown subscribtion
Implement removing devices from device lists when they get deleted

* Improve name and doc comment for DeviceShutDownEvent

* Change ShutdownSubscriber data field tag

* Change UpdateRemovalSubscription name to UpdateShutdownSubscription
This commit is contained in:
Julian Giebel
2023-05-26 00:08:22 +02:00
committed by GitHub
parent a02753545d
commit 2bb1fde4dc
8 changed files with 157 additions and 67 deletions

View File

@@ -111,6 +111,17 @@ namespace Content.Server.DeviceNetwork.Systems
/// </summary>
private void OnNetworkShutdown(EntityUid uid, DeviceNetworkComponent component, ComponentShutdown args)
{
var eventArgs = new DeviceShutDownEvent(uid);
foreach (var shutdownSubscriberId in component.ShutdownSubscribers)
{
RaiseLocalEvent(shutdownSubscriberId, ref eventArgs);
DeviceNetworkComponent? device = null!;
if (Resolve(shutdownSubscriberId, ref device))
device.ShutdownSubscribers.Remove(uid);
}
GetNetwork(component.DeviceNetId).Remove(component);
}
@@ -224,6 +235,36 @@ namespace Content.Server.DeviceNetwork.Systems
deviceNet.Add(device);
}
public void SubscribeToDeviceShutdown(
EntityUid subscriberId, EntityUid targetId,
DeviceNetworkComponent? subscribingDevice = null,
DeviceNetworkComponent? targetDevice = null)
{
if (subscriberId == targetId)
return;
if (!Resolve(subscriberId, ref subscribingDevice) || !Resolve(targetId, ref targetDevice))
return;
targetDevice.ShutdownSubscribers.Add(subscriberId);
subscribingDevice.ShutdownSubscribers.Add(targetId);
}
public void UnsubscribeFromDeviceShutdown(
EntityUid subscriberId, EntityUid targetId,
DeviceNetworkComponent? subscribingDevice = null,
DeviceNetworkComponent? targetDevice = null)
{
if (subscriberId == targetId)
return;
if (!Resolve(subscriberId, ref subscribingDevice) || !Resolve(targetId, ref targetDevice))
return;
targetDevice.ShutdownSubscribers.Remove(subscriberId);
subscribingDevice.ShutdownSubscribers.Remove(targetId);
}
/// <summary>
/// Try to find a device on a network using its address.
/// </summary>
@@ -408,4 +449,11 @@ namespace Content.Server.DeviceNetwork.Systems
Data = data;
}
}
/// <summary>
/// Gets raised on entities that subscribed to shutdown event of the shut down entity
/// </summary>
/// <param name="ShutDownEntityUid">The entity that was shut down</param>
[ByRefEvent]
public readonly record struct DeviceShutDownEvent(EntityUid ShutDownEntityUid);
}