mirror of
https://github.com/space-syndicate/space-station-14.git
synced 2026-10-10 10:51:14 +03:00
Signed-off-by: Prole <172158352+Prole0@users.noreply.github.com> Co-authored-by: PJBot <pieterjan.briers+bot@gmail.com> Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> Co-authored-by: Samuka-C <47865393+Samuka-C@users.noreply.github.com> Co-authored-by: ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com> Co-authored-by: Partmedia <kevinz5000@gmail.com> Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com> Co-authored-by: themias <89101928+themias@users.noreply.github.com> Co-authored-by: Victor Shen <71985089+Vexerot@users.noreply.github.com> Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com> Co-authored-by: Milon <milonpl.git@proton.me> Co-authored-by: Kirus59 <145689588+Kirus59@users.noreply.github.com> Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com> Co-authored-by: Stomf <5dorkydorks@gmail.com> Co-authored-by: drakewill-CRL <46307022+drakewill-CRL@users.noreply.github.com> Co-authored-by: PraxisMapper <praxismapper@gmail.com> Co-authored-by: EmoGarbage404 <retron404@gmail.com> Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com> Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com> Co-authored-by: IProduceWidgets <107586145+IProduceWidgets@users.noreply.github.com> Co-authored-by: TytosB <54259736+TytosB@users.noreply.github.com> Co-authored-by: abadaba695 <spacestation13thingy@gmail.com> Co-authored-by: kosticia <kosticia46@gmail.com> Co-authored-by: Thinbug <101073555+Thinbug0@users.noreply.github.com> Co-authored-by: pathetic meowmeow <uhhadd@gmail.com> Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com> Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com> Co-authored-by: ActiveMammmoth <140334666+ActiveMammmoth@users.noreply.github.com> Co-authored-by: Myra <vasilis@pikachu.systems> Co-authored-by: Whatstone <166147148+whatston3@users.noreply.github.com> Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com> Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com> Co-authored-by: Gentleman-Bird <dcgreen406@gmail.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: BIGZi0348 <svalker0348@gmail.com> Co-authored-by: LaCumbiaDelCoronavirus <90893484+LaCumbiaDelCoronavirus@users.noreply.github.com> Co-authored-by: imatsoup <93290208+imatsoup@users.noreply.github.com> Co-authored-by: Matthew Herber <32679887+happyrobot33@users.noreply.github.com> Co-authored-by: Ertanic <36124833+Ertanic@users.noreply.github.com> Co-authored-by: MissKay1994 <15877268+MissKay1994@users.noreply.github.com> Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com> Co-authored-by: eoineoineoin <helloworld@eoinrul.es> Co-authored-by: Tiniest Shark <head.rebel@yahoo.com> Co-authored-by: nikitosych <boriszyn@gmail.com> Co-authored-by: Tayrtahn <tayrtahn@gmail.com> Co-authored-by: Perry Fraser <perryprog@users.noreply.github.com> Co-authored-by: YoungThug <ramialanbagy@gmail.com> Co-authored-by: beck-thompson <107373427+beck-thompson@users.noreply.github.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com> Co-authored-by: Southbridge <7013162+southbridge-fur@users.noreply.github.com> Co-authored-by: Vladislav Suchkov <20380250+murolem@users.noreply.github.com> Co-authored-by: Prole <172158352+Prole0@users.noreply.github.com> Co-authored-by: Unkn0wn_Gh0st <shadowstalkermll@gmail.com> Co-authored-by: 3nderall <101940324+3nderall@users.noreply.github.com> Co-authored-by: Radezolid <snappednexus@gmail.com> Co-authored-by: J <billsmith116@gmail.com> Co-authored-by: Ghagliiarghii <68826635+Ghagliiarghii@users.noreply.github.com> Co-authored-by: chromiumboy <50505512+chromiumboy@users.noreply.github.com> Co-authored-by: youtissoum <51883137+youtissoum@users.noreply.github.com> Co-authored-by: Minemoder5000 <minemoder50000@gmail.com> Co-authored-by: Spanky <scott@wearejacob.com> Co-authored-by: Spessmann <156740760+Spessmann@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: brainfood1183 <113240905+brainfood1183@users.noreply.github.com> Co-authored-by: Deerstop <edainturner@gmail.com> Co-authored-by: B_Kirill <153602297+B-Kirill@users.noreply.github.com> Co-authored-by: archee1 <archee3@hotmail.co.uk> Co-authored-by: Cojoke <83733158+Cojoke-dot@users.noreply.github.com> Co-authored-by: Quantum-cross <7065792+Quantum-cross@users.noreply.github.com> Co-authored-by: poklj <compgeek223@gmail.com> Co-authored-by: Krunklehorn <42424291+Krunklehorn@users.noreply.github.com> Co-authored-by: OnyxTheBrave <131422822+OnyxTheBrave@users.noreply.github.com> Co-authored-by: UpAndLeaves <92269094+Alpha-Two@users.noreply.github.com> Co-authored-by: Flareguy <78941145+Flareguy@users.noreply.github.com> Co-authored-by: Zalycon <84675130+Zalycon@users.noreply.github.com> Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com> Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com> Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com> Co-authored-by: ScarKy0 <scarky0@onet.eu> Co-authored-by: Dmitry <57028746+dimm00n@users.noreply.github.com>
238 lines
7.4 KiB
C#
238 lines
7.4 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>
|
|
public readonly Dictionary<string, DeviceNetworkComponent> Devices = new();
|
|
|
|
/// <summary>
|
|
/// Devices listening on a given frequency.
|
|
/// </summary>
|
|
public readonly Dictionary<uint, HashSet<DeviceNetworkComponent>> ListeningDevices = new();
|
|
|
|
/// <summary>
|
|
/// Devices listening to all packets on a given frequency, regardless of the intended recipient.
|
|
/// </summary>
|
|
public readonly Dictionary<uint, HashSet<DeviceNetworkComponent>> 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(DeviceNetworkComponent device)
|
|
{
|
|
if (device.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))
|
|
device.Address = GenerateValidAddress(device.Prefix);
|
|
|
|
Devices[device.Address] = device;
|
|
}
|
|
|
|
if (device.ReceiveFrequency is not uint 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(DeviceNetworkComponent device)
|
|
{
|
|
if (device.Address == null || !Devices.Remove(device.Address))
|
|
return false;
|
|
|
|
if (device.ReceiveFrequency is not uint 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>
|
|
/// Give an existing device a new randomly generated address. Useful if the device's address prefix was updated
|
|
/// and they want a new address to reflect that, or something like that.
|
|
/// </summary>
|
|
public bool RandomizeAddress(string oldAddress, string? prefix = null)
|
|
{
|
|
if (!Devices.Remove(oldAddress, out var device))
|
|
return false;
|
|
|
|
device.Address = GenerateValidAddress(prefix ?? device.Prefix);
|
|
device.CustomAddress = false;
|
|
Devices[device.Address] = device;
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update the address of an existing device.
|
|
/// </summary>
|
|
public bool UpdateAddress(string oldAddress, string newAddress)
|
|
{
|
|
if (Devices.ContainsKey(newAddress))
|
|
return false;
|
|
|
|
if (!Devices.Remove(oldAddress, out var device))
|
|
return false;
|
|
|
|
device.Address = newAddress;
|
|
device.CustomAddress = true;
|
|
Devices[newAddress] = device;
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Make an existing network device listen to a new frequency.
|
|
/// </summary>
|
|
public bool UpdateReceiveFrequency(string address, uint? newFrequency)
|
|
{
|
|
if (!Devices.TryGetValue(address, out var device))
|
|
return false;
|
|
|
|
if (device.ReceiveFrequency == newFrequency)
|
|
return true;
|
|
|
|
if (device.ReceiveFrequency is uint freq)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
device.ReceiveFrequency = newFrequency;
|
|
|
|
if (newFrequency == null)
|
|
return true;
|
|
|
|
if (!ListeningDevices.TryGetValue(newFrequency.Value, out var devices))
|
|
ListeningDevices[newFrequency.Value] = devices = new();
|
|
|
|
devices.Add(device);
|
|
|
|
if (!device.ReceiveAll)
|
|
return true;
|
|
|
|
if (!ReceiveAllDevices.TryGetValue(newFrequency.Value, out var receiveAlldevices))
|
|
ReceiveAllDevices[newFrequency.Value] = receiveAlldevices = new();
|
|
|
|
receiveAlldevices.Add(device);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Make an existing network device listen to a new frequency.
|
|
/// </summary>
|
|
public bool UpdateReceiveAll(string address, bool receiveAll)
|
|
{
|
|
if (!Devices.TryGetValue(address, out var device))
|
|
return false;
|
|
|
|
if (device.ReceiveAll == receiveAll)
|
|
return true;
|
|
|
|
device.ReceiveAll = receiveAll;
|
|
|
|
if (device.ReceiveFrequency is not uint freq)
|
|
return true;
|
|
|
|
// remove or add to set of listening devices
|
|
|
|
HashSet<DeviceNetworkComponent>? devices;
|
|
if (receiveAll)
|
|
{
|
|
if (!ReceiveAllDevices.TryGetValue(freq, out devices))
|
|
ReceiveAllDevices[freq] = devices = new();
|
|
devices.Add(device);
|
|
}
|
|
else if (ReceiveAllDevices.TryGetValue(freq, out devices))
|
|
{
|
|
devices.Remove(device);
|
|
if (devices.Count == 0)
|
|
ReceiveAllDevices.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;
|
|
}
|
|
}
|