Skip to content

Commit 6fa431c

Browse files
authored
Unrestrict device net ids (#8332)
1 parent a1b2a9c commit 6fa431c

File tree

3 files changed

+24
-33
lines changed

3 files changed

+24
-33
lines changed

Content.Server/DeviceNetwork/Components/DeviceNetworkComponent.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,21 @@ namespace Content.Server.DeviceNetwork.Components
88
[Friend(typeof(DeviceNetworkSystem), typeof(DeviceNet))]
99
public sealed class DeviceNetworkComponent : Component
1010
{
11-
/// <summary>
12-
/// Valid device network NetIDs. The netID is used to separate device networks that shouldn't interact with
13-
/// each other e.g. wireless and wired.
14-
/// </summary>
15-
[Serializable]
16-
public enum ConnectionType
11+
public enum DeviceNetIdDefaults
1712
{
1813
Private,
1914
Wired,
2015
Wireless,
21-
Apc
16+
Apc,
17+
Reserved = 100,
18+
// Ids outside this enum may exist
19+
// This exists to let yml use nice names instead of numbers
2220
}
23-
// TODO allow devices to join more than one network?
24-
25-
// TODO if wireless/wired is determined by ConnectionType, what is the point of WirelessNetworkComponent & the
26-
// other network-type-specific components? Shouldn't DeviceNetId determine conectivity checks?
2721

2822
[DataField("deviceNetId")]
29-
public ConnectionType DeviceNetId { get; set; } = ConnectionType.Private;
23+
public DeviceNetIdDefaults NetIdEnum { get; set; }
24+
25+
public int DeviceNetId => (int) NetIdEnum;
3026

3127
/// <summary>
3228
/// The frequency that this device is listening on.

Content.Server/DeviceNetwork/DeviceNet.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public sealed class DeviceNet
3131
public readonly Dictionary<uint, HashSet<DeviceNetworkComponent>> ReceiveAllDevices = new();
3232

3333
private readonly IRobustRandom _random;
34-
public readonly ConnectionType Type;
34+
public readonly int NetId;
3535

36-
public DeviceNet(ConnectionType netType, IRobustRandom random)
36+
public DeviceNet(int netId, IRobustRandom random)
3737
{
3838
_random = random;
39-
Type = netType;
39+
NetId = netId;
4040
}
4141

4242
/// <summary>

Content.Server/DeviceNetwork/Systems/DeviceNetworkSystem.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,13 @@ public sealed class DeviceNetworkSystem : EntitySystem
2121
[Dependency] private readonly IPrototypeManager _protoMan = default!;
2222
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
2323

24-
private readonly DeviceNet[] _networks = new DeviceNet[4]; // Number of ConnectionType enum values
24+
private readonly Dictionary<int, DeviceNet> _networks = new(4);
2525
private readonly Queue<DeviceNetworkPacketEvent> _packets = new();
2626

2727
public override void Initialize()
2828
{
29-
base.Initialize();
30-
3129
SubscribeLocalEvent<DeviceNetworkComponent, MapInitEvent>(OnMapInit);
3230
SubscribeLocalEvent<DeviceNetworkComponent, ComponentShutdown>(OnNetworkShutdown);
33-
34-
InitNetwork(ConnectionType.Private);
35-
InitNetwork(ConnectionType.Wired);
36-
InitNetwork(ConnectionType.Wireless);
37-
InitNetwork(ConnectionType.Apc);
3831
}
3932

4033
public override void Update(float frameTime)
@@ -66,10 +59,6 @@ public void QueuePacket(EntityUid uid, string? address, NetworkPayload data, uin
6659
if (frequency != null)
6760
_packets.Enqueue(new DeviceNetworkPacketEvent(device.DeviceNetId, address, frequency.Value, device.Address, uid, data));
6861
}
69-
70-
private void InitNetwork(ConnectionType connectionType) =>
71-
_networks[(int) connectionType] = new(connectionType, _random);
72-
7362
/// <summary>
7463
/// Automatically attempt to connect some devices when a map starts.
7564
/// </summary>
@@ -93,8 +82,14 @@ private void OnMapInit(EntityUid uid, DeviceNetworkComponent device, MapInitEven
9382
ConnectDevice(uid, device);
9483
}
9584

96-
private DeviceNet GetNetwork(ConnectionType connectionType) =>
97-
_networks[(int) connectionType];
85+
private DeviceNet GetNetwork(int netId)
86+
{
87+
if (_networks.TryGetValue(netId, out var deviceNet))
88+
return deviceNet;
89+
var newDeviceNet = new DeviceNet(netId, _random);
90+
_networks[netId] = newDeviceNet;
91+
return newDeviceNet;
92+
}
9893

9994
/// <summary>
10095
/// Automatically disconnect when an entity with a DeviceNetworkComponent shuts down.
@@ -191,7 +186,7 @@ public void RandomizeAddress(EntityUid uid, DeviceNetworkComponent? device = nul
191186
/// <summary>
192187
/// Try to find a device on a network using its address.
193188
/// </summary>
194-
private bool TryGetDevice(ConnectionType netId, string address, [NotNullWhen(true)] out DeviceNetworkComponent? device) =>
189+
private bool TryGetDevice(int netId, string address, [NotNullWhen(true)] out DeviceNetworkComponent? device) =>
195190
GetNetwork(netId).Devices.TryGetValue(address, out device);
196191

197192
private void SendPacket(DeviceNetworkPacketEvent packet)
@@ -289,9 +284,9 @@ public BeforePacketSentEvent(EntityUid sender, TransformComponent xform, Vector2
289284
public sealed class DeviceNetworkPacketEvent : EntityEventArgs
290285
{
291286
/// <summary>
292-
/// The type of network that this packet is being sent on.
287+
/// The id of the network that this packet is being sent on.
293288
/// </summary>
294-
public ConnectionType NetId;
289+
public int NetId;
295290

296291
/// <summary>
297292
/// The frequency the packet is sent on.
@@ -318,7 +313,7 @@ public sealed class DeviceNetworkPacketEvent : EntityEventArgs
318313
/// </summary>
319314
public readonly NetworkPayload Data;
320315

321-
public DeviceNetworkPacketEvent(ConnectionType netId, string? address, uint frequency, string senderAddress, EntityUid sender, NetworkPayload data)
316+
public DeviceNetworkPacketEvent(int netId, string? address, uint frequency, string senderAddress, EntityUid sender, NetworkPayload data)
322317
{
323318
NetId = netId;
324319
Address = address;

0 commit comments

Comments
 (0)