Re-organize all projects (#4166)
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
using System.Collections.Generic;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.DeviceNetwork.Connections
|
||||
{
|
||||
public abstract class BaseNetworkConnection : IDeviceNetworkConnection
|
||||
{
|
||||
protected readonly DeviceNetworkConnection Connection;
|
||||
|
||||
protected OnReceiveNetMessage MessageHandler;
|
||||
|
||||
[ViewVariables]
|
||||
public bool Open => Connection.Open;
|
||||
[ViewVariables]
|
||||
public string Address => Connection.Address;
|
||||
[ViewVariables]
|
||||
public int Frequency => Connection.Frequency;
|
||||
|
||||
protected BaseNetworkConnection(int netId, int frequency, OnReceiveNetMessage onReceive, bool receiveAll)
|
||||
{
|
||||
var network = IoCManager.Resolve<IDeviceNetwork>();
|
||||
Connection = network.Register(netId, frequency, OnReceiveNetMessage, receiveAll);
|
||||
MessageHandler = onReceive;
|
||||
|
||||
}
|
||||
|
||||
public bool Send(int frequency, string address, Dictionary<string, string> payload)
|
||||
{
|
||||
var data = ManipulatePayload(payload);
|
||||
var metadata = GetMetadata();
|
||||
return Connection.Send(frequency, address, data, metadata);
|
||||
}
|
||||
|
||||
public bool Send(string address, Dictionary<string, string> payload)
|
||||
{
|
||||
return Send(0, address, payload);
|
||||
}
|
||||
|
||||
public bool Broadcast(int frequency, Dictionary<string, string> payload)
|
||||
{
|
||||
var data = ManipulatePayload(payload);
|
||||
var metadata = GetMetadata();
|
||||
return Connection.Broadcast(frequency, data, metadata);
|
||||
}
|
||||
|
||||
public bool Broadcast(Dictionary<string, string> payload)
|
||||
{
|
||||
return Broadcast(0, payload);
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
Connection.Close();
|
||||
}
|
||||
|
||||
private void OnReceiveNetMessage(int frequency, string sender, IReadOnlyDictionary<string, string> payload, Metadata metadata, bool broadcast)
|
||||
{
|
||||
if (CanReceive(frequency, sender, payload, metadata, broadcast))
|
||||
{
|
||||
MessageHandler(frequency, sender, payload, metadata, broadcast);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract bool CanReceive(int frequency, string sender, IReadOnlyDictionary<string, string> payload, Metadata metadata, bool broadcast);
|
||||
protected abstract Dictionary<string, string> ManipulatePayload(Dictionary<string, string> payload);
|
||||
protected abstract Metadata GetMetadata();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System.Collections.Generic;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.DeviceNetwork.Connections
|
||||
{
|
||||
public class DeviceNetworkConnection : IDeviceNetworkConnection
|
||||
{
|
||||
private readonly DeviceNetwork _network;
|
||||
[ViewVariables]
|
||||
private readonly int _netId;
|
||||
|
||||
[ViewVariables]
|
||||
public bool Open { get; private set; }
|
||||
[ViewVariables]
|
||||
public string Address { get; private set; }
|
||||
[ViewVariables]
|
||||
public int Frequency { get; private set; }
|
||||
|
||||
[ViewVariables]
|
||||
public bool ReceiveAll
|
||||
{
|
||||
get => _network.GetDeviceReceiveAll(_netId, Frequency, Address);
|
||||
set => _network.SetDeviceReceiveAll(_netId, Frequency, Address, value);
|
||||
}
|
||||
|
||||
public DeviceNetworkConnection(DeviceNetwork network, int netId, string address, int frequency)
|
||||
{
|
||||
_network = network;
|
||||
_netId = netId;
|
||||
Open = true;
|
||||
Address = address;
|
||||
Frequency = frequency;
|
||||
}
|
||||
|
||||
public bool Send(int frequency, string address, IReadOnlyDictionary<string, string> payload, Metadata metadata)
|
||||
{
|
||||
return Open && _network.EnqueuePackage(_netId, frequency, address, payload, Address, metadata);
|
||||
}
|
||||
|
||||
public bool Send(int frequency, string address, Dictionary<string, string> payload)
|
||||
{
|
||||
return Send(frequency, address, payload, new Metadata());
|
||||
}
|
||||
|
||||
public bool Send(string address, Dictionary<string, string> payload)
|
||||
{
|
||||
return Send(0, address, payload);
|
||||
}
|
||||
|
||||
public bool Broadcast(int frequency, IReadOnlyDictionary<string, string> payload, Metadata metadata)
|
||||
{
|
||||
return Open && _network.EnqueuePackage(_netId, frequency, "", payload, Address, metadata, true);
|
||||
}
|
||||
|
||||
public bool Broadcast(int frequency, Dictionary<string, string> payload)
|
||||
{
|
||||
return Broadcast(frequency, payload, new Metadata());
|
||||
}
|
||||
|
||||
public bool Broadcast(Dictionary<string, string> payload)
|
||||
{
|
||||
return Broadcast(0, payload);
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
_network.RemoveDevice(_netId, Frequency, Address);
|
||||
Open = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Content.Server.DeviceNetwork.Connections
|
||||
{
|
||||
public interface IDeviceNetworkConnection
|
||||
{
|
||||
public int Frequency { get; }
|
||||
/// <summary>
|
||||
/// Sends a package to a specific device
|
||||
/// </summary>
|
||||
/// <param name="frequency">The frequency the package should be send on</param>
|
||||
/// <param name="address">The target devices address</param>
|
||||
/// <param name="payload"></param>
|
||||
/// <returns></returns>
|
||||
public bool Send(int frequency, string address, Dictionary<string, string> payload);
|
||||
/// <see cref="Send(int, string, Dictionary{string, string})"/>
|
||||
public bool Send(string address, Dictionary<string, string> payload);
|
||||
/// <summary>
|
||||
/// Sends a package to all devices
|
||||
/// </summary>
|
||||
/// <param name="frequency">The frequency the package should be send on</param>
|
||||
/// <param name="payload"></param>
|
||||
/// <returns></returns>
|
||||
public bool Broadcast(int frequency, Dictionary<string, string> payload);
|
||||
/// <see cref="Broadcast(int, Dictionary{string, string})"/>
|
||||
public bool Broadcast(Dictionary<string, string> payload);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Server.NodeContainer;
|
||||
using Content.Server.NodeContainer.NodeGroups;
|
||||
using Content.Server.Power.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.DeviceNetwork.Connections
|
||||
{
|
||||
public class WiredNetworkConnection : BaseNetworkConnection
|
||||
{
|
||||
public const string WIRENET = "powernet";
|
||||
|
||||
private readonly IEntity _owner;
|
||||
|
||||
public WiredNetworkConnection(OnReceiveNetMessage onReceive, bool receiveAll, IEntity owner) : base(NetworkUtils.WIRED, 0, onReceive, receiveAll)
|
||||
{
|
||||
_owner = owner;
|
||||
}
|
||||
|
||||
protected override bool CanReceive(int frequency, string sender, IReadOnlyDictionary<string, string> payload, Metadata metadata, bool broadcast)
|
||||
{
|
||||
if (_owner.Deleted)
|
||||
{
|
||||
Connection.Close();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_owner.TryGetComponent<PowerReceiverComponent>(out var powerReceiver)
|
||||
&& TryGetWireNet(powerReceiver, out var ownNet)
|
||||
&& metadata.TryParseMetadata<INodeGroup>(WIRENET, out var senderNet))
|
||||
{
|
||||
return ownNet.Equals(senderNet);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected override Metadata GetMetadata()
|
||||
{
|
||||
if (_owner.Deleted)
|
||||
{
|
||||
Connection.Close();
|
||||
return new Metadata();
|
||||
}
|
||||
|
||||
if (_owner.TryGetComponent<PowerReceiverComponent>(out var powerReceiver)
|
||||
&& TryGetWireNet(powerReceiver, out var net))
|
||||
{
|
||||
var metadata = new Metadata
|
||||
{
|
||||
{WIRENET, net }
|
||||
};
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
return new Metadata();
|
||||
}
|
||||
|
||||
protected override Dictionary<string, string> ManipulatePayload(Dictionary<string, string> payload)
|
||||
{
|
||||
return payload;
|
||||
}
|
||||
|
||||
private bool TryGetWireNet(PowerReceiverComponent powerReceiver, [NotNullWhen(true)] out INodeGroup? net)
|
||||
{
|
||||
if (powerReceiver.Provider is PowerProviderComponent provider &&
|
||||
provider.ProviderOwner.TryGetComponent<NodeContainerComponent>(out var nodeContainer))
|
||||
{
|
||||
var nodes = nodeContainer.Nodes;
|
||||
|
||||
foreach (var node in nodes.Values)
|
||||
{
|
||||
if (node.NodeGroupID == NodeGroupID.WireNet)
|
||||
{
|
||||
net = node.NodeGroup;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
net = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Maths;
|
||||
|
||||
namespace Content.Server.DeviceNetwork.Connections
|
||||
{
|
||||
public class WirelessNetworkConnection : BaseNetworkConnection
|
||||
{
|
||||
public const string WIRELESS_POSITION = "position";
|
||||
|
||||
private readonly IEntity _owner;
|
||||
|
||||
private float _range;
|
||||
public float Range { get => _range; set => _range = Math.Abs(value); }
|
||||
|
||||
public WirelessNetworkConnection(int frequency, OnReceiveNetMessage onReceive, bool receiveAll, IEntity owner, float range) : base(NetworkUtils.WIRELESS, frequency, onReceive, receiveAll)
|
||||
{
|
||||
_owner = owner;
|
||||
Range = range;
|
||||
}
|
||||
|
||||
protected override bool CanReceive(int frequency, string sender, IReadOnlyDictionary<string, string> payload, Metadata metadata, bool broadcast)
|
||||
{
|
||||
if (_owner.Deleted)
|
||||
{
|
||||
Connection.Close();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (metadata.TryParseMetadata<Vector2>(WIRELESS_POSITION, out var position))
|
||||
{
|
||||
var ownPosition = _owner.Transform.WorldPosition;
|
||||
var distance = (ownPosition - position).Length;
|
||||
return distance <= Range;
|
||||
}
|
||||
//Only receive packages with the same frequency
|
||||
return frequency == Frequency;
|
||||
}
|
||||
|
||||
protected override Metadata GetMetadata()
|
||||
{
|
||||
if (_owner.Deleted)
|
||||
{
|
||||
Connection.Close();
|
||||
return new Metadata();
|
||||
}
|
||||
|
||||
var position = _owner.Transform.WorldPosition;
|
||||
var metadata = new Metadata
|
||||
{
|
||||
{WIRELESS_POSITION, position}
|
||||
};
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
protected override Dictionary<string, string> ManipulatePayload(Dictionary<string, string> payload)
|
||||
{
|
||||
return payload;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user