Unrestrict device net ids (#8332)
This commit is contained in:
@@ -8,25 +8,21 @@ namespace Content.Server.DeviceNetwork.Components
|
|||||||
[Friend(typeof(DeviceNetworkSystem), typeof(DeviceNet))]
|
[Friend(typeof(DeviceNetworkSystem), typeof(DeviceNet))]
|
||||||
public sealed class DeviceNetworkComponent : Component
|
public sealed class DeviceNetworkComponent : Component
|
||||||
{
|
{
|
||||||
/// <summary>
|
public enum DeviceNetIdDefaults
|
||||||
/// 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
|
|
||||||
{
|
{
|
||||||
Private,
|
Private,
|
||||||
Wired,
|
Wired,
|
||||||
Wireless,
|
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")]
|
[DataField("deviceNetId")]
|
||||||
public ConnectionType DeviceNetId { get; set; } = ConnectionType.Private;
|
public DeviceNetIdDefaults NetIdEnum { get; set; }
|
||||||
|
|
||||||
|
public int DeviceNetId => (int) NetIdEnum;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The frequency that this device is listening on.
|
/// The frequency that this device is listening on.
|
||||||
|
|||||||
@@ -31,12 +31,12 @@ public sealed class DeviceNet
|
|||||||
public readonly Dictionary<uint, HashSet<DeviceNetworkComponent>> ReceiveAllDevices = new();
|
public readonly Dictionary<uint, HashSet<DeviceNetworkComponent>> ReceiveAllDevices = new();
|
||||||
|
|
||||||
private readonly IRobustRandom _random;
|
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;
|
_random = random;
|
||||||
Type = netType;
|
NetId = netId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -21,20 +21,13 @@ namespace Content.Server.DeviceNetwork.Systems
|
|||||||
[Dependency] private readonly IPrototypeManager _protoMan = default!;
|
[Dependency] private readonly IPrototypeManager _protoMan = default!;
|
||||||
[Dependency] private readonly SharedTransformSystem _transformSystem = 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();
|
private readonly Queue<DeviceNetworkPacketEvent> _packets = new();
|
||||||
|
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
base.Initialize();
|
|
||||||
|
|
||||||
SubscribeLocalEvent<DeviceNetworkComponent, MapInitEvent>(OnMapInit);
|
SubscribeLocalEvent<DeviceNetworkComponent, MapInitEvent>(OnMapInit);
|
||||||
SubscribeLocalEvent<DeviceNetworkComponent, ComponentShutdown>(OnNetworkShutdown);
|
SubscribeLocalEvent<DeviceNetworkComponent, ComponentShutdown>(OnNetworkShutdown);
|
||||||
|
|
||||||
InitNetwork(ConnectionType.Private);
|
|
||||||
InitNetwork(ConnectionType.Wired);
|
|
||||||
InitNetwork(ConnectionType.Wireless);
|
|
||||||
InitNetwork(ConnectionType.Apc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Update(float frameTime)
|
public override void Update(float frameTime)
|
||||||
@@ -66,10 +59,6 @@ namespace Content.Server.DeviceNetwork.Systems
|
|||||||
if (frequency != null)
|
if (frequency != null)
|
||||||
_packets.Enqueue(new DeviceNetworkPacketEvent(device.DeviceNetId, address, frequency.Value, device.Address, uid, data));
|
_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>
|
/// <summary>
|
||||||
/// Automatically attempt to connect some devices when a map starts.
|
/// Automatically attempt to connect some devices when a map starts.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -93,8 +82,14 @@ namespace Content.Server.DeviceNetwork.Systems
|
|||||||
ConnectDevice(uid, device);
|
ConnectDevice(uid, device);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DeviceNet GetNetwork(ConnectionType connectionType) =>
|
private DeviceNet GetNetwork(int netId)
|
||||||
_networks[(int) connectionType];
|
{
|
||||||
|
if (_networks.TryGetValue(netId, out var deviceNet))
|
||||||
|
return deviceNet;
|
||||||
|
var newDeviceNet = new DeviceNet(netId, _random);
|
||||||
|
_networks[netId] = newDeviceNet;
|
||||||
|
return newDeviceNet;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Automatically disconnect when an entity with a DeviceNetworkComponent shuts down.
|
/// Automatically disconnect when an entity with a DeviceNetworkComponent shuts down.
|
||||||
@@ -191,7 +186,7 @@ namespace Content.Server.DeviceNetwork.Systems
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Try to find a device on a network using its address.
|
/// Try to find a device on a network using its address.
|
||||||
/// </summary>
|
/// </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);
|
GetNetwork(netId).Devices.TryGetValue(address, out device);
|
||||||
|
|
||||||
private void SendPacket(DeviceNetworkPacketEvent packet)
|
private void SendPacket(DeviceNetworkPacketEvent packet)
|
||||||
@@ -289,9 +284,9 @@ namespace Content.Server.DeviceNetwork.Systems
|
|||||||
public sealed class DeviceNetworkPacketEvent : EntityEventArgs
|
public sealed class DeviceNetworkPacketEvent : EntityEventArgs
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
public ConnectionType NetId;
|
public int NetId;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The frequency the packet is sent on.
|
/// The frequency the packet is sent on.
|
||||||
@@ -318,7 +313,7 @@ namespace Content.Server.DeviceNetwork.Systems
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly NetworkPayload Data;
|
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;
|
NetId = netId;
|
||||||
Address = address;
|
Address = address;
|
||||||
|
|||||||
Reference in New Issue
Block a user