using System.Linq; using Content.Shared.DeviceNetwork.Components; using Robust.Shared.GameStates; namespace Content.Shared.DeviceNetwork; public abstract class SharedDeviceListSystem : EntitySystem { public override void Initialize() { SubscribeLocalEvent(GetDeviceListState); SubscribeLocalEvent(HandleDeviceListState); } /// /// 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; 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; } private void GetDeviceListState(EntityUid uid, DeviceListComponent comp, ref ComponentGetState args) { args.State = new DeviceListComponentState(comp.Devices, comp.IsAllowList, comp.HandleIncomingPackets); } private void HandleDeviceListState(EntityUid uid, DeviceListComponent comp, ref ComponentHandleState args) { if (args.Current is not DeviceListComponentState state) { return; } comp.Devices = state.Devices; comp.HandleIncomingPackets = state.HandleIncomingPackets; comp.IsAllowList = state.IsAllowList; } } 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 }