using System.Linq;
using Content.Shared.DeviceNetwork.Components;
namespace Content.Shared.DeviceNetwork.Systems;
public abstract class SharedDeviceListSystem : EntitySystem
{
///
/// Updates the device list stored on this entity.
///
/// The entity to update.
/// The devices to store.
/// Whether to merge or replace the devices stored.
/// Device list component
public DeviceListUpdateResult UpdateDeviceList(EntityUid uid, IEnumerable devices, bool merge = false, DeviceListComponent? deviceList = null)
{
if (!Resolve(uid, ref deviceList))
return DeviceListUpdateResult.NoComponent;
var oldDevices = deviceList.Devices.ToList();
var newDevices = merge ? new HashSet(deviceList.Devices) : new();
var devicesList = devices.ToList();
newDevices.UnionWith(devicesList);
if (newDevices.Count > deviceList.DeviceLimit)
{
return DeviceListUpdateResult.TooManyDevices;
}
deviceList.Devices = newDevices;
UpdateShutdownSubscription(uid, devicesList, oldDevices);
RaiseLocalEvent(uid, new DeviceListUpdateEvent(oldDevices, devicesList));
Dirty(deviceList);
return DeviceListUpdateResult.UpdateOk;
}
public IEnumerable GetAllDevices(EntityUid uid, DeviceListComponent? component = null)
{
if (!Resolve(uid, ref component))
{
return new EntityUid[] { };
}
return component.Devices;
}
protected virtual void UpdateShutdownSubscription(EntityUid uid, List devicesList, List oldDevices)
{
}
}
public sealed class DeviceListUpdateEvent : EntityEventArgs
{
public DeviceListUpdateEvent(List oldDevices, List devices)
{
OldDevices = oldDevices;
Devices = devices;
}
public List OldDevices { get; }
public List Devices { get; }
}
public enum DeviceListUpdateResult : byte
{
NoComponent,
TooManyDevices,
UpdateOk
}