Minor device network changes (#2499)

* Device network changes

* Update too

* Update Content.Server/GameObjects/EntitySystems/DeviceNetworkSystem.cs

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com>
This commit is contained in:
metalgearsloth
2020-11-06 04:04:21 +11:00
committed by GitHub
parent e62df15ef9
commit 864fa0a57c
7 changed files with 53 additions and 51 deletions

View File

@@ -49,6 +49,7 @@ namespace Content.Client.GameObjects.Components.Wires
{ {
base.Dispose(disposing); base.Dispose(disposing);
_menu.OnClose -= Close;
_menu.Close(); _menu.Close();
} }
} }

View File

@@ -1,4 +1,5 @@
using Content.Server.Interfaces; using System;
using Content.Server.Interfaces;
using Robust.Shared.Interfaces.Random; using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using System.Collections.Generic; using System.Collections.Generic;
@@ -11,7 +12,8 @@ namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
{ {
private const int PACKAGES_PER_TICK = 30; private const int PACKAGES_PER_TICK = 30;
private readonly IRobustRandom _random = IoCManager.Resolve<IRobustRandom>(); [Dependency] private readonly IRobustRandom _random = default!;
private readonly Dictionary<int, List<NetworkDevice>> _devices = new Dictionary<int, List<NetworkDevice>>(); private readonly Dictionary<int, List<NetworkDevice>> _devices = new Dictionary<int, List<NetworkDevice>>();
private readonly Queue<NetworkPackage> _packages = new Queue<NetworkPackage>(); private readonly Queue<NetworkPackage> _packages = new Queue<NetworkPackage>();
@@ -40,11 +42,9 @@ namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
public void Update() public void Update()
{ {
var i = PACKAGES_PER_TICK; var count = Math.Min(PACKAGES_PER_TICK, _packages.Count);
while (_packages.Count > 0 && i > 0) for (var i = 0; i < count; i++)
{ {
i--;
var package = _packages.Dequeue(); var package = _packages.Dequeue();
if (package.Broadcast) if (package.Broadcast)
@@ -132,7 +132,7 @@ namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
{ {
var devices = DevicesForFrequency(netId, frequency); var devices = DevicesForFrequency(netId, frequency);
var device = devices.Find(device => device.Address == address); var device = devices.Find(dvc => dvc.Address == address);
return device; return device;
} }
@@ -192,7 +192,6 @@ namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
public IReadOnlyDictionary<string, string> Data { get; set; } public IReadOnlyDictionary<string, string> Data { get; set; }
public Metadata Metadata; public Metadata Metadata;
public string Sender; public string Sender;
} }
} }
} }

View File

@@ -11,14 +11,14 @@ namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
private readonly int _netId; private readonly int _netId;
[ViewVariables] [ViewVariables]
public bool Open { get; internal set; } public bool Open { get; private set; }
[ViewVariables] [ViewVariables]
public string Address { get; internal set; } public string Address { get; private set; }
[ViewVariables] [ViewVariables]
public int Frequency { get; internal set; } public int Frequency { get; private set; }
[ViewVariables] [ViewVariables]
public bool RecieveAll public bool ReceiveAll
{ {
get => _network.GetDeviceReceiveAll(_netId, Frequency, Address); get => _network.GetDeviceReceiveAll(_netId, Frequency, Address);
set => _network.SetDeviceReceiveAll(_netId, Frequency, Address, value); set => _network.SetDeviceReceiveAll(_netId, Frequency, Address, value);

View File

@@ -8,9 +8,7 @@ using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -28,18 +26,24 @@ namespace Content.Server.GameObjects.Components
private Regex _validation; private Regex _validation;
public event Action<Dictionary<string, string>> OnConfigUpdate; public override void OnAdd()
public override void Initialize()
{ {
base.Initialize(); base.OnAdd();
if (UserInterface != null) if (UserInterface != null)
{ {
UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage; UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
} }
} }
public override void OnRemove()
{
base.OnRemove();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage -= UserInterfaceOnReceiveMessage;
}
}
public override void ExposeData(ObjectSerializer serializer) public override void ExposeData(ObjectSerializer serializer)
{ {
base.ExposeData(serializer); base.ExposeData(serializer);
@@ -48,7 +52,7 @@ namespace Content.Server.GameObjects.Components
(list) => FillConfiguration(list, _config, ""), (list) => FillConfiguration(list, _config, ""),
() => _config.Keys.ToList()); () => _config.Keys.ToList());
serializer.DataReadFunction("vailidation", "^[a-zA-Z0-9 ]*$", value => _validation = new Regex("^[a-zA-Z0-9 ]*$", RegexOptions.Compiled)); serializer.DataReadFunction("validation", "^[a-zA-Z0-9 ]*$", value => _validation = new Regex("^[a-zA-Z0-9 ]*$", RegexOptions.Compiled));
} }
public string GetConfig(string name) public string GetConfig(string name)
@@ -90,22 +94,19 @@ namespace Content.Server.GameObjects.Components
{ {
var value = msg.Config.GetValueOrDefault(key); var value = msg.Config.GetValueOrDefault(key);
if (_validation != null && !_validation.IsMatch(value) && value != "") if (value == null || _validation != null && !_validation.IsMatch(value) && value != "")
continue; continue;
_config[key] = value; _config[key] = value;
} }
OnConfigUpdate(_config); SendMessage(new ConfigUpdatedComponentMessage(config));
} }
} }
private void UpdateUserInterface() private void UpdateUserInterface()
{ {
if (UserInterface == null) UserInterface?.SetState(new ConfigurationBoundUserInterfaceState(_config));
return;
UserInterface.SetState(new ConfigurationBoundUserInterfaceState(_config));
} }
private static void FillConfiguration<T>(List<string> list, Dictionary<string, T> configuration, T value){ private static void FillConfiguration<T>(List<string> list, Dictionary<string, T> configuration, T value){

View File

@@ -13,6 +13,7 @@ using Content.Server.GameObjects.EntitySystems.DoAfter;
using Content.Server.Interfaces; using Content.Server.Interfaces;
using Content.Server.Interfaces.GameObjects.Components.Items; using Content.Server.Interfaces.GameObjects.Components.Items;
using Content.Server.Utility; using Content.Server.Utility;
using Content.Shared.GameObjects.Components;
using Content.Shared.GameObjects.Components.Body; using Content.Shared.GameObjects.Components.Body;
using Content.Shared.GameObjects.Components.Disposal; using Content.Shared.GameObjects.Components.Disposal;
using Content.Shared.GameObjects.EntitySystems; using Content.Shared.GameObjects.EntitySystems;
@@ -603,12 +604,7 @@ namespace Content.Server.GameObjects.Components.Disposal
UserInterface.OnReceiveMessage += OnUiReceiveMessage; UserInterface.OnReceiveMessage += OnUiReceiveMessage;
} }
var network = IoCManager.Resolve<IDeviceNetwork>();
_connection = new WiredNetworkConnection(OnReceiveNetMessage, false, Owner); _connection = new WiredNetworkConnection(OnReceiveNetMessage, false, Owner);
if (Owner.TryGetComponent<ConfigurationComponent>(out var configuration))
configuration.OnConfigUpdate += OnConfigUpdate;
UpdateInterface(); UpdateInterface();
} }
@@ -673,6 +669,9 @@ namespace Content.Server.GameObjects.Components.Disposal
switch (message) switch (message)
{ {
case SharedConfigurationComponent.ConfigUpdatedComponentMessage msg:
OnConfigUpdate(msg.Config);
break;
case RelayMovementEntityMessage msg: case RelayMovementEntityMessage msg:
if (!msg.Entity.TryGetComponent(out HandsComponent? hands) || if (!msg.Entity.TryGetComponent(out HandsComponent? hands) ||
hands.Count == 0 || hands.Count == 0 ||

View File

@@ -1,27 +1,16 @@
using Content.Server.Interfaces; using Content.Server.Interfaces;
using Robust.Shared.GameObjects.Systems; using Robust.Shared.GameObjects.Systems;
using Robust.Shared.IoC; using Robust.Shared.IoC;
namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
{ {
public class DeviceNetworkSystem : EntitySystem internal sealed class DeviceNetworkSystem : EntitySystem
{ {
private IDeviceNetwork _network; [Dependency] private readonly IDeviceNetwork _network = default!;
public override void Initialize()
{
base.Initialize();
_network = IoCManager.Resolve<IDeviceNetwork>();
}
public override void Update(float frameTime) public override void Update(float frameTime)
{ {
base.Update(frameTime); base.Update(frameTime);
if (_network == null)
return;
//(ノ°Д°)ノ︵ ┻━┻
_network.Update(); _network.Update();
} }
} }

View File

@@ -13,7 +13,7 @@ namespace Content.Shared.GameObjects.Components
[Serializable, NetSerializable] [Serializable, NetSerializable]
public class ConfigurationBoundUserInterfaceState : BoundUserInterfaceState public class ConfigurationBoundUserInterfaceState : BoundUserInterfaceState
{ {
public readonly Dictionary<string, string> Config; public Dictionary<string, string> Config { get; }
public ConfigurationBoundUserInterfaceState(Dictionary<string, string> config) public ConfigurationBoundUserInterfaceState(Dictionary<string, string> config)
{ {
@@ -21,13 +21,26 @@ namespace Content.Shared.GameObjects.Components
} }
} }
/// <summary>
/// Message sent to other components on this entity when DeviceNetwork configuration updated.
/// </summary>
public class ConfigUpdatedComponentMessage : ComponentMessage
{
public Dictionary<string, string> Config { get; }
public ConfigUpdatedComponentMessage(Dictionary<string, string> config)
{
Config = config;
}
}
/// <summary> /// <summary>
/// Message data sent from client to server when the device configuration is updated. /// Message data sent from client to server when the device configuration is updated.
/// </summary> /// </summary>
[Serializable, NetSerializable] [Serializable, NetSerializable]
public class ConfigurationUpdatedMessage : BoundUserInterfaceMessage public class ConfigurationUpdatedMessage : BoundUserInterfaceMessage
{ {
public readonly Dictionary<string, string> Config; public Dictionary<string, string> Config { get; }
public ConfigurationUpdatedMessage(Dictionary<string, string> config) public ConfigurationUpdatedMessage(Dictionary<string, string> config)
{ {
@@ -38,7 +51,7 @@ namespace Content.Shared.GameObjects.Components
[Serializable, NetSerializable] [Serializable, NetSerializable]
public class ValidationUpdateMessage : BoundUserInterfaceMessage public class ValidationUpdateMessage : BoundUserInterfaceMessage
{ {
public readonly string ValidationString; public string ValidationString { get; }
public ValidationUpdateMessage(string validationString) public ValidationUpdateMessage(string validationString)
{ {