mirror of
https://github.com/space-syndicate/space-station-14.git
synced 2026-10-09 18:31:17 +03:00
* Refactor DeviceNetwork * Singleton * NetworkPayloads refactor * Reorganize DeviceNetworkSystem * Surveillance cameras * Fix code * Fix build * Fix test fail by rewriting entire singleton logic * Properly predict examine * Update DeviceNetworkSystem.API.cs * Update EntityTest.cs * Update DeviceNetworkSystem.cs * Alright I'll do this expensive check fine * Fix disposals debug assert * Implement device network static events support * Use frozen dictionaries for performance * Update SharedDeviceNetworkSystem.Payloads.cs * Parallel payload handling support * Fix some spagetti-code * Optimize NetworkPayload cancellation * Parallel payloads are now cancellable * Fix screens * Fix robotics console * Allow new methods for shared * Mail fix * Turret fix * Fix network system method usage + signal and borg fix * finally fix atmos devices OH MY GOOOD * Remove stupid parallelism from fax * Fix surveiollance cameras * Working on a single thing for 2 days straight award * Fix the test? * Update units.yml * Add IDevice interface because why not * Add more device network suffixes * Fix camera router failing test * yaml linter peak * Improve docs for DeviceNetworkHandlers * fix build * Update CartridgeLoaderSystem.Relay.cs * Remove parallel system and EntitySystem post init * Add more docs and also events * Remove unnecessary caching * Update DevicePayloadComponentSystem.cs * Back to entity events * Implement DeviceData * Remove serializable from payloads * Update DeviceData.cs * Add a prototype for a singleton * Fix tests * Fix air alarms * Update DeviceNetworkSystem.cs * Fix cameras * Make camera router event-less * Fix atmos sync payloads, add more docs * Remove singleton + instant packets * oops remove public access * Fix tests? * Why the hell overload effect makes puddle sparks * Apparently it was redundant * Update gates.yml * Finish documentation * Review * Mark relay properties as obsolete * Struct payloads support (#blazinglyfastmemorysafe) * Update DeviceNetworkTest.cs * Make device structs stuff better * Move address prefix to DeviceNetworkComponent * Locale fixes * Fix tests * Doesn't know boolean logic award * Update AtmosAlarmableSystem.cs * Fix address predicted examine * Partial fix to air alarmables * Fix deployable turrets UI * Fix firelocks, maybe * Apparently Activatable UI was enough lol * Fix surveillance cameras * Rename `QueuePacket` into `SendPacket` * Simplify DeviceData as much as possible * Update DeviceData.cs * Update FaxMachineComponent.cs * Remove DeviceData because its stupid * Update ScreenSystem.cs * Fix build (camera map broken still) * Fix map * fix usings * Review changes * Build fix
132 lines
4.2 KiB
C#
132 lines
4.2 KiB
C#
using Robust.Shared.Random;
|
|
using Content.Shared.DeviceNetwork.Components;
|
|
|
|
namespace Content.Shared.DeviceNetwork;
|
|
|
|
/// <summary>
|
|
/// Data class for storing and retrieving information about devices connected to a device network.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This basically just makes <see cref="DeviceNetworkComponent"/> accessible via their addresses and frequencies on
|
|
/// some network.
|
|
/// </remarks>
|
|
public sealed class DeviceNet
|
|
{
|
|
/// <summary>
|
|
/// Devices, mapped by their "Address", which is just an int that gets converted to Hex for displaying to users.
|
|
/// This dictionary contains all devices connected to this network, though they may not be listening to any
|
|
/// specific frequency.
|
|
/// </summary>
|
|
[ViewVariables]
|
|
public readonly Dictionary<string, Device> Devices = new();
|
|
|
|
/// <summary>
|
|
/// Devices listening on a given frequency.
|
|
/// </summary>
|
|
[ViewVariables]
|
|
public readonly Dictionary<uint, HashSet<Device>> ListeningDevices = new();
|
|
|
|
/// <summary>
|
|
/// Devices listening to all packets on a given frequency, regardless of the intended recipient.
|
|
/// </summary>
|
|
[ViewVariables]
|
|
public readonly Dictionary<uint, HashSet<Device>> ReceiveAllDevices = new();
|
|
|
|
private readonly IRobustRandom _random;
|
|
public readonly int NetId;
|
|
|
|
public DeviceNet(int netId, IRobustRandom random)
|
|
{
|
|
_random = random;
|
|
NetId = netId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a device to the network.
|
|
/// </summary>
|
|
public bool Add(Entity<DeviceNetworkComponent> ent)
|
|
{
|
|
var deviceComp = ent.Comp;
|
|
var device = new Device(ent);
|
|
if (deviceComp.CustomAddress)
|
|
{
|
|
// Only add if the device's existing address is available.
|
|
if (!Devices.TryAdd(device.Address, device))
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
// Randomly generate a new address if the existing random one is invalid. Otherwise, keep the existing address
|
|
if (string.IsNullOrWhiteSpace(device.Address) || Devices.ContainsKey(device.Address))
|
|
{
|
|
deviceComp.Address = GenerateValidAddress(deviceComp.Prefix);
|
|
device = new Device(ent); // Reallocate because the data had changed
|
|
}
|
|
|
|
Devices[device.Address] = device;
|
|
}
|
|
|
|
if (device.ReceiveFrequency is not { } freq)
|
|
return true;
|
|
|
|
if (!ListeningDevices.TryGetValue(freq, out var devices))
|
|
ListeningDevices[freq] = devices = new();
|
|
|
|
devices.Add(device);
|
|
|
|
if (!device.ReceiveAll)
|
|
return true;
|
|
|
|
if (!ReceiveAllDevices.TryGetValue(freq, out var receiveAlldevices))
|
|
ReceiveAllDevices[freq] = receiveAlldevices = new();
|
|
|
|
receiveAlldevices.Add(device);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove a device from the network.
|
|
/// </summary>
|
|
public bool Remove(Entity<DeviceNetworkComponent> ent)
|
|
{
|
|
var deviceComp = ent.Comp;
|
|
var device = new Device(ent);
|
|
if (!Devices.Remove(device.Address))
|
|
return false;
|
|
|
|
if (device.ReceiveFrequency is not { } freq)
|
|
return true;
|
|
|
|
if (ListeningDevices.TryGetValue(freq, out var listening))
|
|
{
|
|
listening.Remove(device);
|
|
if (listening.Count == 0)
|
|
ListeningDevices.Remove(freq);
|
|
}
|
|
|
|
if (device.ReceiveAll && ReceiveAllDevices.TryGetValue(freq, out var receiveAll))
|
|
{
|
|
receiveAll.Remove(device);
|
|
if (receiveAll.Count == 0)
|
|
ListeningDevices.Remove(freq);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates a valid address by randomly generating one and checking if it already exists on the network.
|
|
/// </summary>
|
|
private string GenerateValidAddress(string? prefix)
|
|
{
|
|
prefix = string.IsNullOrWhiteSpace(prefix) ? null : Loc.GetString(prefix);
|
|
string address;
|
|
do
|
|
{
|
|
var num = _random.Next();
|
|
address = $"{prefix}{num >> 16:X4}-{num & 0xFFFF:X4}";
|
|
} while (Devices.ContainsKey(address));
|
|
|
|
return address;
|
|
}
|
|
}
|