Make NetMessage name and group virtual to remove required boilerplate (#1770)

This commit is contained in:
DrSmugleaf
2021-05-31 08:53:46 +02:00
committed by GitHub
parent 196a6047f1
commit ac1705e41f
56 changed files with 206 additions and 338 deletions

View File

@@ -36,7 +36,7 @@ namespace Robust.Client.Console
Message = message;
}
}
/// <inheritdoc cref="IClientConsoleHost" />
internal class ClientConsoleHost : ConsoleHost, IClientConsoleHost
{
@@ -45,9 +45,9 @@ namespace Robust.Client.Console
/// <inheritdoc />
public void Initialize()
{
NetManager.RegisterNetMessage<MsgConCmdReg>(MsgConCmdReg.NAME, HandleConCmdReg);
NetManager.RegisterNetMessage<MsgConCmdAck>(MsgConCmdAck.NAME, HandleConCmdAck);
NetManager.RegisterNetMessage<MsgConCmd>(MsgConCmd.NAME, ProcessCommand);
NetManager.RegisterNetMessage<MsgConCmdReg>(HandleConCmdReg);
NetManager.RegisterNetMessage<MsgConCmdAck>(HandleConCmdAck);
NetManager.RegisterNetMessage<MsgConCmd>(ProcessCommand);
Reset();
LogManager.RootSawmill.AddHandler(new DebugConsoleLogHandler(this));

View File

@@ -17,11 +17,11 @@ namespace Robust.Client.Console
public void Initialize()
{
_netManager.RegisterNetMessage<MsgScriptStop>(MsgScriptStop.NAME);
_netManager.RegisterNetMessage<MsgScriptEval>(MsgScriptEval.NAME);
_netManager.RegisterNetMessage<MsgScriptStart>(MsgScriptStart.NAME);
_netManager.RegisterNetMessage<MsgScriptResponse>(MsgScriptResponse.NAME, ReceiveScriptResponse);
_netManager.RegisterNetMessage<MsgScriptStartAck>(MsgScriptStartAck.NAME, ReceiveScriptStartAckResponse);
_netManager.RegisterNetMessage<MsgScriptStop>();
_netManager.RegisterNetMessage<MsgScriptEval>();
_netManager.RegisterNetMessage<MsgScriptStart>();
_netManager.RegisterNetMessage<MsgScriptResponse>(ReceiveScriptResponse);
_netManager.RegisterNetMessage<MsgScriptStartAck>(ReceiveScriptStartAckResponse);
}
private void ReceiveScriptStartAckResponse(MsgScriptStartAck message)

View File

@@ -1,12 +1,12 @@
using Robust.Shared.IoC;
using Robust.Shared.Network.Messages;
using System;
using System.Collections.Generic;
using Robust.Client.Graphics;
using Robust.Shared.Maths;
using Robust.Shared.Timing;
using Robust.Shared.Enums;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Network.Messages;
using Robust.Shared.Timing;
namespace Robust.Client.Debugging
{
@@ -54,7 +54,7 @@ namespace Robust.Client.Debugging
public void Initialize()
{
_net.RegisterNetMessage<MsgRay>(MsgRay.NAME, HandleDrawRay);
_net.RegisterNetMessage<MsgRay>(HandleDrawRay);
}
private void HandleDrawRay(MsgRay msg)

View File

@@ -1,14 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Prometheus;
using Robust.Client.GameStates;
using Robust.Shared.Exceptions;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Network.Messages;
using Robust.Shared.Timing;
@@ -67,7 +62,7 @@ namespace Robust.Client.GameObjects
/// <inheritdoc />
public void SetupNetworking()
{
_networkManager.RegisterNetMessage<MsgEntity>(MsgEntity.NAME, HandleEntityNetworkMessage);
_networkManager.RegisterNetMessage<MsgEntity>(HandleEntityNetworkMessage);
}
public override void TickUpdate(float frameTime, Histogram? histogram)

View File

@@ -1,3 +1,5 @@
// ReSharper disable once RedundantUsingDirective
// Used in EXCEPTION_TOLERANCE preprocessor
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -5,19 +7,18 @@ using System.Linq;
using Robust.Client.GameObjects;
using Robust.Client.Input;
using Robust.Client.Map;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Network.Messages;
using Robust.Client.Player;
using Robust.Client.Timing;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.Exceptions;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Input;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Network.Messages;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
@@ -80,8 +81,8 @@ namespace Robust.Client.GameStates
{
_processor = new GameStateProcessor(_timing);
_network.RegisterNetMessage<MsgState>(MsgState.NAME, HandleStateMessage);
_network.RegisterNetMessage<MsgStateAck>(MsgStateAck.NAME);
_network.RegisterNetMessage<MsgState>(HandleStateMessage);
_network.RegisterNetMessage<MsgStateAck>();
_client.RunLevelChanged += RunLevelChanged;
_config.OnValueChanged(CVars.NetInterp, b => _processor.Interpolation = b, true);

View File

@@ -156,7 +156,7 @@ namespace Robust.Client.Placement
{
_drawingShader = _prototypeManager.Index<ShaderPrototype>("unshaded").Instance();
NetworkManager.RegisterNetMessage<MsgPlacement>(MsgPlacement.NAME, HandlePlacementMessage);
NetworkManager.RegisterNetMessage<MsgPlacement>(HandlePlacementMessage);
_modeDictionary.Clear();
foreach (var type in ReflectionManager.GetAllChildren<PlacementMode>())

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Robust.Shared.Configuration;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
@@ -82,8 +81,8 @@ namespace Robust.Client.Player
{
_client.RunLevelChanged += OnRunLevelChanged;
_network.RegisterNetMessage<MsgPlayerListReq>(MsgPlayerListReq.NAME);
_network.RegisterNetMessage<MsgPlayerList>(MsgPlayerList.NAME, HandlePlayerList);
_network.RegisterNetMessage<MsgPlayerListReq>();
_network.RegisterNetMessage<MsgPlayerList>(HandlePlayerList);
}
/// <inheritdoc />

View File

@@ -30,7 +30,7 @@ namespace Robust.Client.Prototypes
{
base.Initialize();
_netManager.RegisterNetMessage<MsgReloadPrototypes>(MsgReloadPrototypes.NAME, accept: NetMessageAccept.Server);
_netManager.RegisterNetMessage<MsgReloadPrototypes>(accept: NetMessageAccept.Server);
_clyde.OnWindowFocused += WindowFocusedChanged;

View File

@@ -16,7 +16,7 @@ using Robust.Shared.Network.Messages;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using NumberType = Robust.Client.ViewVariables.Editors.VVPropEditorNumeric.NumberType;
using static Robust.Client.ViewVariables.Editors.VVPropEditorNumeric;
namespace Robust.Client.ViewVariables
{
@@ -43,17 +43,13 @@ namespace Robust.Client.ViewVariables
public void Initialize()
{
_netManager.RegisterNetMessage<MsgViewVariablesOpenSession>(MsgViewVariablesOpenSession.NAME,
_netMessageOpenSession);
_netManager.RegisterNetMessage<MsgViewVariablesRemoteData>(MsgViewVariablesRemoteData.NAME,
_netMessageRemoteData);
_netManager.RegisterNetMessage<MsgViewVariablesCloseSession>(MsgViewVariablesCloseSession.NAME,
_netMessageCloseSession);
_netManager.RegisterNetMessage<MsgViewVariablesDenySession>(MsgViewVariablesDenySession.NAME,
_netMessageDenySession);
_netManager.RegisterNetMessage<MsgViewVariablesModifyRemote>(MsgViewVariablesModifyRemote.NAME);
_netManager.RegisterNetMessage<MsgViewVariablesReqSession>(MsgViewVariablesReqSession.NAME);
_netManager.RegisterNetMessage<MsgViewVariablesReqData>(MsgViewVariablesReqData.NAME);
_netManager.RegisterNetMessage<MsgViewVariablesOpenSession>(_netMessageOpenSession);
_netManager.RegisterNetMessage<MsgViewVariablesRemoteData>(_netMessageRemoteData);
_netManager.RegisterNetMessage<MsgViewVariablesCloseSession>(_netMessageCloseSession);
_netManager.RegisterNetMessage<MsgViewVariablesDenySession>(_netMessageDenySession);
_netManager.RegisterNetMessage<MsgViewVariablesModifyRemote>();
_netManager.RegisterNetMessage<MsgViewVariablesReqSession>();
_netManager.RegisterNetMessage<MsgViewVariablesReqData>();
}
public VVPropEditor PropertyFor(Type? type)

View File

@@ -62,15 +62,14 @@ namespace Robust.Server.Console
var sudoShell = new SudoShell(this, localShell, shell);
ExecuteInShell(sudoShell, argStr.Substring("sudo ".Length));
});
LoadConsoleCommands();
// setup networking with clients
NetManager.RegisterNetMessage<MsgConCmd>(MsgConCmd.NAME, ProcessCommand);
NetManager.RegisterNetMessage<MsgConCmdAck>(MsgConCmdAck.NAME);
NetManager.RegisterNetMessage<MsgConCmd>(ProcessCommand);
NetManager.RegisterNetMessage<MsgConCmdAck>();
NetManager.RegisterNetMessage<MsgConCmdReg>(MsgConCmdReg.NAME,
message => HandleRegistrationRequest(message.MsgChannel));
NetManager.RegisterNetMessage<MsgConCmdReg>(message => HandleRegistrationRequest(message.MsgChannel));
}
private void ExecuteInShell(IConsoleShell shell, string command)

View File

@@ -14,7 +14,7 @@ namespace Robust.Server.Debugging
public void Initialize()
{
_net.RegisterNetMessage<MsgRay>(MsgRay.NAME);
_net.RegisterNetMessage<MsgRay>();
// TODO _physics.DebugDrawRay += data => PhysicsOnDebugDrawRay(data);
}

View File

@@ -9,7 +9,6 @@ using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Network.Messages;
using Robust.Shared.Prototypes;
@@ -108,7 +107,7 @@ namespace Robust.Server.GameObjects
/// <inheritdoc />
public void SetupNetworking()
{
_networkManager.RegisterNetMessage<MsgEntity>(MsgEntity.NAME, HandleEntityNetworkMessage);
_networkManager.RegisterNetMessage<MsgEntity>(HandleEntityNetworkMessage);
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;

View File

@@ -12,7 +12,6 @@ using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Network.Messages;
using Robust.Shared.Players;
@@ -63,8 +62,8 @@ namespace Robust.Server.GameStates
/// <inheritdoc />
public void Initialize()
{
_networkManager.RegisterNetMessage<MsgState>(MsgState.NAME);
_networkManager.RegisterNetMessage<MsgStateAck>(MsgStateAck.NAME, HandleStateAck);
_networkManager.RegisterNetMessage<MsgState>();
_networkManager.RegisterNetMessage<MsgStateAck>(HandleStateAck);
_networkManager.Connected += HandleClientConnected;
_networkManager.Disconnect += HandleClientDisconnect;
@@ -211,7 +210,10 @@ namespace Robust.Server.GameStates
_logger.Log(LogLevel.Error, e, string.Empty);
}
return (new MsgState(session.ConnectedClient), null);
var msg = _networkManager.CreateNetMessage<MsgState>();
msg.MsgChannel = session.ConnectedClient;
return (msg, null);
}
var mailBag = _playerManager.GetAllPlayers()

View File

@@ -1,13 +1,13 @@
using System;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using System.Collections.Generic;
using System.Linq;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Network.Messages;
@@ -34,7 +34,7 @@ namespace Robust.Server.Placement
public void Initialize()
{
_networkManager.RegisterNetMessage<MsgPlacement>(MsgPlacement.NAME, HandleNetMessage);
_networkManager.RegisterNetMessage<MsgPlacement>(HandleNetMessage);
}
/// <summary>

View File

@@ -107,8 +107,8 @@ namespace Robust.Server.Player
MaxPlayers = maxPlayers;
_network.RegisterNetMessage<MsgPlayerListReq>(MsgPlayerListReq.NAME, HandlePlayerListReq);
_network.RegisterNetMessage<MsgPlayerList>(MsgPlayerList.NAME);
_network.RegisterNetMessage<MsgPlayerListReq>(HandlePlayerListReq);
_network.RegisterNetMessage<MsgPlayerList>();
_network.Connecting += OnConnecting;
_network.Connected += NewSession;

View File

@@ -24,7 +24,7 @@ namespace Robust.Server.Prototypes
{
base.Initialize();
_netManager.RegisterNetMessage<MsgReloadPrototypes>(MsgReloadPrototypes.NAME, HandleReloadPrototypes, NetMessageAccept.Server);
_netManager.RegisterNetMessage<MsgReloadPrototypes>(HandleReloadPrototypes, NetMessageAccept.Server);
}
private void HandleReloadPrototypes(MsgReloadPrototypes msg)

View File

@@ -36,11 +36,11 @@ namespace Robust.Server.Scripting
public void Initialize()
{
_netManager.RegisterNetMessage<MsgScriptStop>(MsgScriptStop.NAME, ReceiveScriptEnd);
_netManager.RegisterNetMessage<MsgScriptEval>(MsgScriptEval.NAME, ReceiveScriptEval);
_netManager.RegisterNetMessage<MsgScriptStart>(MsgScriptStart.NAME, ReceiveScriptStart);
_netManager.RegisterNetMessage<MsgScriptResponse>(MsgScriptResponse.NAME);
_netManager.RegisterNetMessage<MsgScriptStartAck>(MsgScriptStartAck.NAME);
_netManager.RegisterNetMessage<MsgScriptStop>(ReceiveScriptEnd);
_netManager.RegisterNetMessage<MsgScriptEval>(ReceiveScriptEval);
_netManager.RegisterNetMessage<MsgScriptStart>(ReceiveScriptStart);
_netManager.RegisterNetMessage<MsgScriptResponse>();
_netManager.RegisterNetMessage<MsgScriptStartAck>();
_playerManager.PlayerStatusChanged += PlayerManagerOnPlayerStatusChanged;
}

View File

@@ -13,7 +13,7 @@ using Robust.Shared.Prototypes;
using Robust.Shared.Reflection;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using DenyReason = Robust.Shared.Network.Messages.MsgViewVariablesDenySession.DenyReason;
using static Robust.Shared.Network.Messages.MsgViewVariablesDenySession;
namespace Robust.Server.ViewVariables
{
@@ -34,16 +34,13 @@ namespace Robust.Server.ViewVariables
public void Initialize()
{
_netManager.RegisterNetMessage<MsgViewVariablesReqSession>(MsgViewVariablesReqSession.NAME,
_msgReqSession);
_netManager.RegisterNetMessage<MsgViewVariablesReqData>(MsgViewVariablesReqData.NAME, _msgReqData);
_netManager.RegisterNetMessage<MsgViewVariablesModifyRemote>(MsgViewVariablesModifyRemote.NAME,
_msgModifyRemote);
_netManager.RegisterNetMessage<MsgViewVariablesCloseSession>(MsgViewVariablesCloseSession.NAME,
_msgCloseSession);
_netManager.RegisterNetMessage<MsgViewVariablesDenySession>(MsgViewVariablesDenySession.NAME);
_netManager.RegisterNetMessage<MsgViewVariablesOpenSession>(MsgViewVariablesOpenSession.NAME);
_netManager.RegisterNetMessage<MsgViewVariablesRemoteData>(MsgViewVariablesRemoteData.NAME);
_netManager.RegisterNetMessage<MsgViewVariablesReqSession>(_msgReqSession);
_netManager.RegisterNetMessage<MsgViewVariablesReqData>(_msgReqData);
_netManager.RegisterNetMessage<MsgViewVariablesModifyRemote>(_msgModifyRemote);
_netManager.RegisterNetMessage<MsgViewVariablesCloseSession>(_msgCloseSession);
_netManager.RegisterNetMessage<MsgViewVariablesDenySession>();
_netManager.RegisterNetMessage<MsgViewVariablesOpenSession>();
_netManager.RegisterNetMessage<MsgViewVariablesRemoteData>();
}
private void _msgCloseSession(MsgViewVariablesCloseSession message)

View File

@@ -82,7 +82,7 @@ namespace Robust.Shared.Configuration
_netManager.Disconnect += PeerDisconnected;
}
_netManager.RegisterNetMessage<MsgConVars>(MsgConVars.NAME, HandleNetVarMessage);
_netManager.RegisterNetMessage<MsgConVars>(HandleNetVarMessage);
}
private void PeerConnected(object? sender, NetChannelArgs e)

View File

@@ -128,10 +128,25 @@ namespace Robust.Shared.Network
/// The side of the network this message is accepted on.
/// If we are not on the side specified, the receive callback will not be registered even if provided.
/// </param>
[Obsolete("Use the method without a name argument instead")]
void RegisterNetMessage<T>(string name, ProcessMessage<T>? rxCallback = null,
NetMessageAccept accept = NetMessageAccept.Both)
where T : NetMessage;
/// <summary>
/// Registers a NetMessage to be sent or received.
/// </summary>
/// <typeparam name="T">Type to register.</typeparam>
/// <param name="name">String ID of the message.</param>
/// <param name="rxCallback">Callback function to process the received message.</param>
/// <param name="accept">
/// The side of the network this message is accepted on.
/// If we are not on the side specified, the receive callback will not be registered even if provided.
/// </param>
void RegisterNetMessage<T>(ProcessMessage<T>? rxCallback = null,
NetMessageAccept accept = NetMessageAccept.Both)
where T : NetMessage, new();
/// <summary>
/// Creates a new NetMessage to be sent.
/// </summary>

View File

@@ -6,9 +6,9 @@ namespace Robust.Shared.Network.Messages.Handshake
{
internal sealed class MsgEncryptionRequest : NetMessage
{
public MsgEncryptionRequest() : base("", MsgGroups.Core)
{
}
public override string MsgName => string.Empty;
public override MsgGroups MsgGroup => MsgGroups.Core;
public byte[] VerifyToken;
public byte[] PublicKey;

View File

@@ -7,9 +7,9 @@ namespace Robust.Shared.Network.Messages.Handshake
{
internal sealed class MsgEncryptionResponse : NetMessage
{
public MsgEncryptionResponse() : base("", MsgGroups.Core)
{
}
public override string MsgName => string.Empty;
public override MsgGroups MsgGroup => MsgGroups.Core;
public Guid UserId;
public byte[] SharedSecret;

View File

@@ -10,9 +10,9 @@ namespace Robust.Shared.Network.Messages.Handshake
// **NOTE**: This is a special message sent during the client<->server handshake.
// It doesn't actually get sent normally and as such doesn't have the "normal" boilerplate.
// It's basically just a sane way to encapsulate the message write/read logic.
public MsgLoginStart() : base("", MsgGroups.Core)
{
}
public override string MsgName => string.Empty;
public override MsgGroups MsgGroup => MsgGroups.Core;
public string UserName;
public ImmutableArray<byte> HWId;

View File

@@ -7,10 +7,9 @@ namespace Robust.Shared.Network.Messages.Handshake
internal sealed class MsgLoginSuccess : NetMessage
{
// Same deal as MsgLogin, helper for NetManager only.
public override string MsgName => string.Empty;
public MsgLoginSuccess() : base("", MsgGroups.Core)
{
}
public override MsgGroups MsgGroup => MsgGroups.Core;
public NetUserData UserData;
public LoginType Type;

View File

@@ -6,11 +6,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgConCmd : NetMessage
{
#region REQUIRED
public static readonly MsgGroups GROUP = MsgGroups.Command;
public static readonly string NAME = nameof(MsgConCmd);
public MsgConCmd(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.Core;
public string Text { get; set; }

View File

@@ -6,11 +6,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgConCmdAck : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.String;
public static readonly string NAME = nameof(MsgConCmdAck);
public MsgConCmdAck(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.String;
public string Text { get; set; }
public bool Error { get; set; }

View File

@@ -7,11 +7,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgConCmdReg : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.String;
public static readonly string NAME = nameof(MsgConCmdReg);
public MsgConCmdReg(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.String;
public Command[] Commands { get; set; }

View File

@@ -13,11 +13,7 @@ namespace Robust.Shared.Network.Messages
private const int MaxNameSize = 4 * 32; // UTF8 Max char size is 4 bytes, 32 chars.
private const int MaxStringValSize = 4 * 256; // UTF8 Max char size is 4 bytes, 256 chars.
#region REQUIRED
public static readonly MsgGroups GROUP = MsgGroups.Command;
public static readonly string NAME = nameof(MsgConVars);
public MsgConVars(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.String;
public GameTick Tick;
public List<(string name, object value)> NetworkedVars = null!;

View File

@@ -1,10 +1,10 @@
using System;
using Lidgren.Network;
using Robust.Shared.GameObjects;
using System.IO;
using Robust.Shared.IoC;
using System.Collections.Generic;
using System.IO;
using Lidgren.Network;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
@@ -14,11 +14,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgEntity : NetMessage
{
#region REQUIRED
public static readonly MsgGroups GROUP = MsgGroups.EntityEvent;
public static readonly string NAME = nameof(MsgEntity);
public MsgEntity(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.EntityEvent;
public EntityMessageType Type { get; set; }

View File

@@ -4,7 +4,6 @@ using Robust.Shared.Serialization;
namespace Robust.Shared.Network.Messages
{
/// <summary>
/// The client part of the string-exchange handshake, sent after the
/// client receives the mapping hash and after the client receives a
@@ -21,11 +20,7 @@ namespace Robust.Shared.Network.Messages
[UsedImplicitly]
internal class MsgMapStrClientHandshake : NetMessage
{
public MsgMapStrClientHandshake(INetChannel ch)
: base(nameof(MsgMapStrClientHandshake), MsgGroups.Core)
{
}
public override MsgGroups MsgGroup => MsgGroups.Core;
/// <value>
/// <c>true</c> if the client needs a new copy of the mapping,
@@ -38,7 +33,5 @@ namespace Robust.Shared.Network.Messages
public override void WriteToBuffer(NetOutgoingMessage buffer)
=> buffer.Write(NeedsStrings);
}
}

View File

@@ -5,7 +5,6 @@ using Robust.Shared.Serialization;
namespace Robust.Shared.Network.Messages
{
/// <summary>
/// The server part of the string-exchange handshake. Sent as the
/// first message in the handshake. Tells the client the hash of
@@ -16,11 +15,7 @@ namespace Robust.Shared.Network.Messages
[UsedImplicitly]
internal class MsgMapStrServerHandshake : NetMessage
{
public MsgMapStrServerHandshake(INetChannel ch)
: base(nameof(MsgMapStrServerHandshake), MsgGroups.Core)
{
}
public override MsgGroups MsgGroup => MsgGroups.Core;
/// <value>
/// The hash of the current string mapping held by the server.
@@ -48,7 +43,5 @@ namespace Robust.Shared.Network.Messages
buffer.WriteVariableInt32(Hash.Length);
buffer.Write(Hash);
}
}
}

View File

@@ -15,11 +15,7 @@ namespace Robust.Shared.Network.Messages
[UsedImplicitly]
internal class MsgMapStrStrings : NetMessage
{
public MsgMapStrStrings(INetChannel ch)
: base(nameof(MsgMapStrStrings), MsgGroups.Core)
{
}
public override MsgGroups MsgGroup => MsgGroups.Core;
/// <value>
/// The raw bytes of the string mapping held by the server.
@@ -48,7 +44,5 @@ namespace Robust.Shared.Network.Messages
throw new InvalidOperationException("Not all of the bytes were written to the message.");
}
}
}
}

View File

@@ -11,11 +11,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgPlacement : NetMessage
{
#region REQUIRED
public static readonly MsgGroups GROUP = MsgGroups.Command;
public static readonly string NAME = nameof(MsgPlacement);
public MsgPlacement(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
public PlacementManagerMessage PlaceType { get; set; }
public string Align { get; set; }

View File

@@ -9,11 +9,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgPlayerList : NetMessage
{
#region REQUIRED
public static readonly MsgGroups GROUP = MsgGroups.Core;
public static readonly string NAME = nameof(MsgPlayerList);
public MsgPlayerList(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.Core;
public byte PlyCount { get; set; }
public List<PlayerState> Plyrs { get; set; }

View File

@@ -6,11 +6,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgPlayerListReq : NetMessage
{
#region REQUIRED
public static readonly MsgGroups GROUP = MsgGroups.Core;
public static readonly string NAME = nameof(MsgPlayerListReq);
public MsgPlayerListReq(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.Core;
public override void ReadFromBuffer(NetIncomingMessage buffer)
{

View File

@@ -7,16 +7,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgRay : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgRay);
public MsgRay(INetChannel channel) : base(NAME, GROUP)
{
}
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
public Vector2 RayOrigin { get; set; }
public Vector2 RayHit { get; set; }

View File

@@ -5,16 +5,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgReloadPrototypes : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgReloadPrototypes);
public MsgReloadPrototypes(INetChannel channel) : base(NAME, GROUP)
{
}
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
public ResourcePath[] Paths = default!;

View File

@@ -6,16 +6,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgScriptEval : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgScriptEval);
public MsgScriptEval(INetChannel channel) : base(NAME, GROUP)
{
}
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
public int ScriptSession { get; set; }
public string Code { get; set; }

View File

@@ -10,16 +10,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgScriptResponse : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgScriptResponse);
public MsgScriptResponse(INetChannel channel) : base(NAME, GROUP)
{
}
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
public int ScriptSession { get; set; }
public bool WasComplete { get; set; }

View File

@@ -6,16 +6,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgScriptStart : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgScriptStart);
public MsgScriptStart(INetChannel channel) : base(NAME, GROUP)
{
}
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
public int ScriptSession { get; set; }

View File

@@ -6,16 +6,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgScriptStartAck : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgScriptStartAck);
public MsgScriptStartAck(INetChannel channel) : base(NAME, GROUP)
{
}
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
public bool WasAccepted { get; set; }
public int ScriptSession { get; set; }

View File

@@ -6,16 +6,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgScriptStop : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgScriptStop);
public MsgScriptStop(INetChannel channel) : base(NAME, GROUP)
{
}
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
public int ScriptSession { get; set; }

View File

@@ -1,8 +1,8 @@
using System;
using System.IO;
using Lidgren.Network;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using System.IO;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
@@ -17,16 +17,7 @@ namespace Robust.Shared.Network.Messages
// (due to being in many parts).
public const int ReliableThreshold = 1300;
#region REQUIRED
public static readonly MsgGroups GROUP = MsgGroups.Entity;
public static readonly string NAME = nameof(MsgState);
public MsgState(INetChannel channel) : base(NAME, GROUP)
{
}
#endregion
public override MsgGroups MsgGroup => MsgGroups.Entity;
public GameState State;

View File

@@ -7,11 +7,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgStateAck : NetMessage
{
#region REQUIRED
public static readonly MsgGroups GROUP = MsgGroups.Entity;
public static readonly string NAME = nameof(MsgStateAck);
public MsgStateAck(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.Entity;
public GameTick Sequence { get; set; }

View File

@@ -9,13 +9,7 @@ namespace Robust.Shared.Network.Messages
/// </summary>
public class MsgViewVariablesCloseSession : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgViewVariablesCloseSession);
public MsgViewVariablesCloseSession(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
/// <summary>
/// The session ID to close, which was agreed upon in <see cref="MsgViewVariablesOpenSession.SessionId"/>.

View File

@@ -9,13 +9,7 @@ namespace Robust.Shared.Network.Messages
/// </summary>
public class MsgViewVariablesDenySession : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgViewVariablesDenySession);
public MsgViewVariablesDenySession(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
/// <summary>
/// The request ID to identify WHICH request has been denied.

View File

@@ -10,13 +10,7 @@ namespace Robust.Shared.Network.Messages
{
public class MsgViewVariablesModifyRemote : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgViewVariablesModifyRemote);
public MsgViewVariablesModifyRemote(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
/// <summary>
/// The session ID of the session to modify.

View File

@@ -9,13 +9,7 @@ namespace Robust.Shared.Network.Messages
/// </summary>
public class MsgViewVariablesOpenSession : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgViewVariablesOpenSession);
public MsgViewVariablesOpenSession(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
/// <summary>
/// The request ID to identify WHICH request has been granted.

View File

@@ -13,16 +13,7 @@ namespace Robust.Shared.Network.Messages
/// </summary>
public class MsgViewVariablesRemoteData : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgViewVariablesRemoteData);
public MsgViewVariablesRemoteData(INetChannel channel) : base(NAME, GROUP)
{
}
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
/// <summary>
/// The request ID equal to the ID sent in <see cref="RequestId"/>,

View File

@@ -13,16 +13,7 @@ namespace Robust.Shared.Network.Messages
/// </summary>
public class MsgViewVariablesReqData : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgViewVariablesReqData);
public MsgViewVariablesReqData(INetChannel channel) : base(NAME, GROUP)
{
}
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
/// <summary>
/// The request ID that will be sent in <see cref="MsgViewVariablesRemoteData"/> to

View File

@@ -13,13 +13,7 @@ namespace Robust.Shared.Network.Messages
/// </summary>
public class MsgViewVariablesReqSession : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(MsgViewVariablesReqSession);
public MsgViewVariablesReqSession(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.Command;
/// <summary>
/// An ID the client assigns so it knows which request was accepted/denied through

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
@@ -868,22 +867,32 @@ namespace Robust.Shared.Network
return;
}
var constructor = packetType.GetConstructor(new[] {typeof(INetChannel)})!;
DebugTools.AssertNotNull(constructor);
var dynamicMethod = new DynamicMethod($"_netMsg<>{name}", typeof(NetMessage), new[] {typeof(INetChannel)},
packetType, false);
dynamicMethod.DefineParameter(1, ParameterAttributes.In, "channel");
var gen = dynamicMethod.GetILGenerator();
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Newobj, constructor);
gen.Emit(OpCodes.Ret);
var gen = dynamicMethod.GetILGenerator().GetRobustGen();
var @delegate =
(Func<INetChannel, NetMessage>) dynamicMethod.CreateDelegate(typeof(Func<INetChannel, NetMessage>));
// Obsolete path for content
if (packetType.GetConstructor(new[] {typeof(INetChannel)}) is { } constructor)
{
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Newobj, constructor);
gen.Emit(OpCodes.Ret);
}
else
{
constructor = packetType.GetConstructor(Type.EmptyTypes)!;
DebugTools.AssertNotNull(constructor);
gen.DeclareLocal(typeof(NetMessage));
gen.Emit(OpCodes.Newobj, constructor);
gen.Emit(OpCodes.Ret);
}
var @delegate = dynamicMethod.CreateDelegate<Func<INetChannel, NetMessage>>();
ref var entry = ref _netMsgFunctions[id];
entry.CreateFunction = @delegate;
@@ -919,6 +928,14 @@ namespace Robust.Shared.Network
CacheBlankFunction(typeof(T));
}
/// <inheritdoc />
public void RegisterNetMessage<T>(ProcessMessage<T>? rxCallback = null,
NetMessageAccept accept = NetMessageAccept.Both)
where T : NetMessage, new()
{
RegisterNetMessage(new T().MsgName, rxCallback, accept);
}
/// <inheritdoc />
public T CreateNetMessage<T>()
where T : NetMessage
@@ -928,16 +945,25 @@ namespace Robust.Shared.Network
private void CacheBlankFunction(Type type)
{
var constructor = type.GetConstructor(new[] {typeof(INetChannel)})!;
DebugTools.AssertNotNull(constructor);
var dynamicMethod = new DynamicMethod($"_netMsg<>{type.Name}", typeof(NetMessage), Array.Empty<Type>(),
type, false);
var gen = dynamicMethod.GetILGenerator();
gen.Emit(OpCodes.Ldnull);
gen.Emit(OpCodes.Newobj, constructor);
gen.Emit(OpCodes.Ret);
var gen = dynamicMethod.GetILGenerator().GetRobustGen();
// Obsolete path for content
if (type.GetConstructor(new[] {typeof(INetChannel)}) is { } constructor)
{
gen.Emit(OpCodes.Ldnull);
gen.Emit(OpCodes.Newobj, constructor);
gen.Emit(OpCodes.Ret);
}
else
{
constructor = type.GetConstructor(Type.EmptyTypes)!;
DebugTools.AssertNotNull(constructor);
gen.Emit(OpCodes.Newobj, constructor);
gen.Emit(OpCodes.Ret);
}
var @delegate = (Func<NetMessage>) dynamicMethod.CreateDelegate(typeof(Func<NetMessage>));

View File

@@ -49,17 +49,17 @@ namespace Robust.Shared.Network
/// <summary>
/// String identifier of the message type.
/// </summary>
public string MsgName { get; }
public virtual string MsgName { get; }
/// <summary>
/// The group this message type belongs to.
/// </summary>
public MsgGroups MsgGroup { get; }
public virtual MsgGroups MsgGroup { get; }
/// <summary>
/// The channel that this message came in on.
/// </summary>
public INetChannel MsgChannel { get; set; }
public INetChannel MsgChannel { get; set; } = default!;
/// <summary>
/// The size of this packet in bytes.
@@ -71,12 +71,18 @@ namespace Robust.Shared.Network
/// </summary>
/// <param name="name">String identifier of the message type.</param>
/// <param name="group">The group this message type belongs to.</param>
[Obsolete("Use NetMessageAttribute and no constructor instead")]
protected NetMessage(string name, MsgGroups group)
{
MsgName = name;
MsgGroup = group;
}
protected NetMessage()
{
MsgName = GetType().Name;
}
/// <summary>
/// Deserializes the NetIncomingMessage into this NetMessage class.
/// </summary>

View File

@@ -61,8 +61,7 @@ namespace Robust.Shared.Network
_callback = callback;
_updateCallback = updateCallback;
_network.RegisterNetMessage<MsgStringTableEntries>(MsgStringTableEntries.NAME, ReceiveEntries,
NetMessageAccept.Client);
_network.RegisterNetMessage<MsgStringTableEntries>(ReceiveEntries, NetMessageAccept.Client);
Reset();
}
@@ -113,9 +112,9 @@ namespace Robust.Shared.Network
_initialized = false;
// manually register the id on the client so it can bootstrap itself with incoming table entries
if (!TryFindStringId(MsgStringTableEntries.NAME, out _))
if (!TryFindStringId(nameof(MsgStringTableEntries), out _))
{
_strings.Add(StringTablePacketId, MsgStringTableEntries.NAME);
_strings.Add(StringTablePacketId, nameof(MsgStringTableEntries));
if (_network.IsClient)
{
@@ -123,7 +122,7 @@ namespace Robust.Shared.Network
new MsgStringTableEntries.Entry
{
Id = StringTablePacketId,
String = MsgStringTableEntries.NAME
String = nameof(MsgStringTableEntries)
}
});
}
@@ -278,11 +277,7 @@ namespace Robust.Shared.Network
/// </summary>
public class MsgStringTableEntries : NetMessage
{
#region REQUIRED
public static readonly MsgGroups GROUP = MsgGroups.String;
public static readonly string NAME = nameof(MsgStringTableEntries);
public MsgStringTableEntries(INetChannel channel) : base(NAME, GROUP) { }
#endregion
public override MsgGroups MsgGroup => MsgGroups.String;
public Entry[] Entries { get; set; } = default!;

View File

@@ -282,14 +282,29 @@ namespace Robust.UnitTesting
_callbacks.Add(typeof(T), msg => rxCallback((T) msg));
}
public void RegisterNetMessage<T>(ProcessMessage<T>? rxCallback = null, NetMessageAccept accept = NetMessageAccept.Both) where T : NetMessage, new()
{
RegisterNetMessage(new T().MsgName, rxCallback, accept);
}
public T CreateNetMessage<T>() where T : NetMessage
{
if (!_registeredMessages.Contains(typeof(T)))
var type = typeof(T);
if (!_registeredMessages.Contains(type))
{
throw new ArgumentException("Net message type is not registered.");
}
return (T) Activator.CreateInstance(typeof(T), (INetChannel?) null)!;
// Obsolete path for content
if (type.GetConstructor(new[] {typeof(INetChannel)}) != null)
{
return (T) Activator.CreateInstance(typeof(T), (INetChannel?) null)!;
}
else
{
return Activator.CreateInstance<T>();
}
}
public byte[]? RsaPublicKey => null;

View File

@@ -17,12 +17,12 @@ namespace Robust.UnitTesting.Server.GameObjects
var tickA = new GameTick(5);
var tickB = new GameTick(3);
var channel = new Mock<INetChannel>().Object;
var msgA = new MsgEntity(channel) {Type = EntityMessageType.SystemMessage, SourceTick = tickA, Sequence = 10};
var msgB = new MsgEntity(channel) {Type = EntityMessageType.SystemMessage, SourceTick = tickA, Sequence = 13};
var msgC = new MsgEntity(channel) {Type = EntityMessageType.SystemMessage, SourceTick = tickA, Sequence = 12};
var msgD = new MsgEntity(channel) {Type = EntityMessageType.SystemMessage, SourceTick = tickA, Sequence = 14};
var msgE = new MsgEntity(channel) {Type = EntityMessageType.SystemMessage, SourceTick = tickB, Sequence = 7};
var msgF = new MsgEntity(channel) {Type = EntityMessageType.SystemMessage, SourceTick = tickB, Sequence = 4};
var msgA = new MsgEntity() {MsgChannel = channel, Type = EntityMessageType.SystemMessage, SourceTick = tickA, Sequence = 10};
var msgB = new MsgEntity() {MsgChannel = channel, Type = EntityMessageType.SystemMessage, SourceTick = tickA, Sequence = 13};
var msgC = new MsgEntity() {MsgChannel = channel, Type = EntityMessageType.SystemMessage, SourceTick = tickA, Sequence = 12};
var msgD = new MsgEntity() {MsgChannel = channel, Type = EntityMessageType.SystemMessage, SourceTick = tickA, Sequence = 14};
var msgE = new MsgEntity() {MsgChannel = channel, Type = EntityMessageType.SystemMessage, SourceTick = tickB, Sequence = 7};
var msgF = new MsgEntity() {MsgChannel = channel, Type = EntityMessageType.SystemMessage, SourceTick = tickB, Sequence = 4};
var pq = new PriorityQueue<MsgEntity>(new ServerEntityManager.MessageSequenceComparer())
{