From 6fa431cfce29228bd7f6eab96a427425d9d16290 Mon Sep 17 00:00:00 2001
From: wrexbe <81056464+wrexbe@users.noreply.github.com>
Date: Sun, 22 May 2022 07:36:21 -0700
Subject: [PATCH] Unrestrict device net ids (#8332)
---
.../Components/DeviceNetworkComponent.cs | 20 +++++-------
Content.Server/DeviceNetwork/DeviceNet.cs | 6 ++--
.../Systems/DeviceNetworkSystem.cs | 31 ++++++++-----------
3 files changed, 24 insertions(+), 33 deletions(-)
diff --git a/Content.Server/DeviceNetwork/Components/DeviceNetworkComponent.cs b/Content.Server/DeviceNetwork/Components/DeviceNetworkComponent.cs
index 3e7b975577..f283b496d1 100644
--- a/Content.Server/DeviceNetwork/Components/DeviceNetworkComponent.cs
+++ b/Content.Server/DeviceNetwork/Components/DeviceNetworkComponent.cs
@@ -8,25 +8,21 @@ namespace Content.Server.DeviceNetwork.Components
[Friend(typeof(DeviceNetworkSystem), typeof(DeviceNet))]
public sealed class DeviceNetworkComponent : Component
{
- ///
- /// Valid device network NetIDs. The netID is used to separate device networks that shouldn't interact with
- /// each other e.g. wireless and wired.
- ///
- [Serializable]
- public enum ConnectionType
+ public enum DeviceNetIdDefaults
{
Private,
Wired,
Wireless,
- Apc
+ Apc,
+ Reserved = 100,
+ // Ids outside this enum may exist
+ // This exists to let yml use nice names instead of numbers
}
- // TODO allow devices to join more than one network?
-
- // TODO if wireless/wired is determined by ConnectionType, what is the point of WirelessNetworkComponent & the
- // other network-type-specific components? Shouldn't DeviceNetId determine conectivity checks?
[DataField("deviceNetId")]
- public ConnectionType DeviceNetId { get; set; } = ConnectionType.Private;
+ public DeviceNetIdDefaults NetIdEnum { get; set; }
+
+ public int DeviceNetId => (int) NetIdEnum;
///
/// The frequency that this device is listening on.
diff --git a/Content.Server/DeviceNetwork/DeviceNet.cs b/Content.Server/DeviceNetwork/DeviceNet.cs
index dee192affa..7d25985efe 100644
--- a/Content.Server/DeviceNetwork/DeviceNet.cs
+++ b/Content.Server/DeviceNetwork/DeviceNet.cs
@@ -31,12 +31,12 @@ public sealed class DeviceNet
public readonly Dictionary> ReceiveAllDevices = new();
private readonly IRobustRandom _random;
- public readonly ConnectionType Type;
+ public readonly int NetId;
- public DeviceNet(ConnectionType netType, IRobustRandom random)
+ public DeviceNet(int netId, IRobustRandom random)
{
_random = random;
- Type = netType;
+ NetId = netId;
}
///
diff --git a/Content.Server/DeviceNetwork/Systems/DeviceNetworkSystem.cs b/Content.Server/DeviceNetwork/Systems/DeviceNetworkSystem.cs
index e497528179..ae8858ba5c 100644
--- a/Content.Server/DeviceNetwork/Systems/DeviceNetworkSystem.cs
+++ b/Content.Server/DeviceNetwork/Systems/DeviceNetworkSystem.cs
@@ -21,20 +21,13 @@ namespace Content.Server.DeviceNetwork.Systems
[Dependency] private readonly IPrototypeManager _protoMan = default!;
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
- private readonly DeviceNet[] _networks = new DeviceNet[4]; // Number of ConnectionType enum values
+ private readonly Dictionary _networks = new(4);
private readonly Queue _packets = new();
public override void Initialize()
{
- base.Initialize();
-
SubscribeLocalEvent(OnMapInit);
SubscribeLocalEvent(OnNetworkShutdown);
-
- InitNetwork(ConnectionType.Private);
- InitNetwork(ConnectionType.Wired);
- InitNetwork(ConnectionType.Wireless);
- InitNetwork(ConnectionType.Apc);
}
public override void Update(float frameTime)
@@ -66,10 +59,6 @@ namespace Content.Server.DeviceNetwork.Systems
if (frequency != null)
_packets.Enqueue(new DeviceNetworkPacketEvent(device.DeviceNetId, address, frequency.Value, device.Address, uid, data));
}
-
- private void InitNetwork(ConnectionType connectionType) =>
- _networks[(int) connectionType] = new(connectionType, _random);
-
///
/// Automatically attempt to connect some devices when a map starts.
///
@@ -93,8 +82,14 @@ namespace Content.Server.DeviceNetwork.Systems
ConnectDevice(uid, device);
}
- private DeviceNet GetNetwork(ConnectionType connectionType) =>
- _networks[(int) connectionType];
+ private DeviceNet GetNetwork(int netId)
+ {
+ if (_networks.TryGetValue(netId, out var deviceNet))
+ return deviceNet;
+ var newDeviceNet = new DeviceNet(netId, _random);
+ _networks[netId] = newDeviceNet;
+ return newDeviceNet;
+ }
///
/// Automatically disconnect when an entity with a DeviceNetworkComponent shuts down.
@@ -191,7 +186,7 @@ namespace Content.Server.DeviceNetwork.Systems
///
/// Try to find a device on a network using its address.
///
- private bool TryGetDevice(ConnectionType netId, string address, [NotNullWhen(true)] out DeviceNetworkComponent? device) =>
+ private bool TryGetDevice(int netId, string address, [NotNullWhen(true)] out DeviceNetworkComponent? device) =>
GetNetwork(netId).Devices.TryGetValue(address, out device);
private void SendPacket(DeviceNetworkPacketEvent packet)
@@ -289,9 +284,9 @@ namespace Content.Server.DeviceNetwork.Systems
public sealed class DeviceNetworkPacketEvent : EntityEventArgs
{
///
- /// The type of network that this packet is being sent on.
+ /// The id of the network that this packet is being sent on.
///
- public ConnectionType NetId;
+ public int NetId;
///
/// The frequency the packet is sent on.
@@ -318,7 +313,7 @@ namespace Content.Server.DeviceNetwork.Systems
///
public readonly NetworkPayload Data;
- public DeviceNetworkPacketEvent(ConnectionType netId, string? address, uint frequency, string senderAddress, EntityUid sender, NetworkPayload data)
+ public DeviceNetworkPacketEvent(int netId, string? address, uint frequency, string senderAddress, EntityUid sender, NetworkPayload data)
{
NetId = netId;
Address = address;