Unrestrict device net ids (#8332)

This commit is contained in:
wrexbe
2022-05-22 07:36:21 -07:00
committed by GitHub
parent a1b2a9c142
commit 6fa431cfce
3 changed files with 24 additions and 33 deletions

View File

@@ -8,25 +8,21 @@ namespace Content.Server.DeviceNetwork.Components
[Friend(typeof(DeviceNetworkSystem), typeof(DeviceNet))]
public sealed class DeviceNetworkComponent : Component
{
/// <summary>
/// Valid device network NetIDs. The netID is used to separate device networks that shouldn't interact with
/// each other e.g. wireless and wired.
/// </summary>
[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;
/// <summary>
/// The frequency that this device is listening on.

View File

@@ -31,12 +31,12 @@ public sealed class DeviceNet
public readonly Dictionary<uint, HashSet<DeviceNetworkComponent>> 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;
}
/// <summary>

View File

@@ -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<int, DeviceNet> _networks = new(4);
private readonly Queue<DeviceNetworkPacketEvent> _packets = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DeviceNetworkComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<DeviceNetworkComponent, ComponentShutdown>(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);
/// <summary>
/// Automatically attempt to connect some devices when a map starts.
/// </summary>
@@ -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;
}
/// <summary>
/// Automatically disconnect when an entity with a DeviceNetworkComponent shuts down.
@@ -191,7 +186,7 @@ namespace Content.Server.DeviceNetwork.Systems
/// <summary>
/// Try to find a device on a network using its address.
/// </summary>
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
{
/// <summary>
/// The type of network that this packet is being sent on.
/// The id of the network that this packet is being sent on.
/// </summary>
public ConnectionType NetId;
public int NetId;
/// <summary>
/// The frequency the packet is sent on.
@@ -318,7 +313,7 @@ namespace Content.Server.DeviceNetwork.Systems
/// </summary>
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;