Disposal mailing (#2194)
* Implement device networking * Implement device configuration menu * Fix device network * Implement disposal mailing unit * Implement base network connection Implement wired and wireless network connection Implement device network metadata * Fix dereference null error * Fix wired network null checks * Change BaseNetworks enum to NetworkUtils class Add PingResponse function to NetworkUtils Change device network file structure * Add doc comments * Apply suggestions from code review Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Add tag validation to disposal mailing unit * Add tag validation to the mailing unit component * Address reviews Change WiredNetwork can connect check Change device networking string literals to constants * Address reviews Revert changes to PowerProvider and PowerReceiver Add new NodeGroup WELP * Fix recursive access to Owner property * Integrate suggested changes * Fix TryGetWireNet acting on NullPowerProvider Fix network connections not checking if their owner has been deleted * Close device network connection when the owning entity got deleted Fix mailing unit not closing the device network connection on remove * Remove GetWireNet from NullPowerProvider Co-authored-by: Julian Giebel <j.giebel@netrocks.info> Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using Content.Server.Interfaces;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
|
||||
{
|
||||
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,84 @@
|
||||
using Content.Server.GameObjects.Components.NodeContainer;
|
||||
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
|
||||
using Content.Server.GameObjects.Components.Power.ApcNetComponents;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
|
||||
{
|
||||
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, out INodeGroup net)
|
||||
{
|
||||
if (powerReceiver.Provider is PowerProviderComponent && powerReceiver.Provider.ProviderOwner.TryGetComponent<NodeContainerComponent>(out var nodeContainer))
|
||||
{
|
||||
var nodes = nodeContainer.Nodes;
|
||||
for (var index = 0; index < nodes.Count; index++)
|
||||
{
|
||||
if (nodes[index].NodeGroupID == NodeGroupID.WireNet)
|
||||
{
|
||||
net = nodes[index].NodeGroup;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
net = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Maths;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
|
||||
{
|
||||
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