From 8e31dbe62492557b725d2850a862d26b81845fe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= <2108U9@gmail.com> Date: Thu, 19 Apr 2018 15:39:34 +0200 Subject: [PATCH] Removed allocations from NetId --- MLAPI/Data/NetId.cs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/MLAPI/Data/NetId.cs b/MLAPI/Data/NetId.cs index cae1a56..4521704 100644 --- a/MLAPI/Data/NetId.cs +++ b/MLAPI/Data/NetId.cs @@ -1,5 +1,4 @@ using MLAPI.MonoBehaviours.Core; -using System; namespace MLAPI.Data { @@ -66,20 +65,15 @@ namespace MLAPI.Data else Meta = 0; } - - - private static byte[] tempUIntBytes = new byte[4]; - private static byte[] tempUShortBytes = new byte[2]; /// /// Initializes a new instance of the netId struct from a clientId /// /// Client identifier. public NetId(uint clientId) { - tempUIntBytes = BitConverter.GetBytes(clientId); - HostId = tempUIntBytes[0]; - ConnectionId = BitConverter.ToUInt16(tempUIntBytes, 1); - Meta = tempUIntBytes[3]; + HostId = (byte)(clientId & 0xFF); + ConnectionId = (ushort)((byte)((clientId >> 8) & 0xFF) | (ushort)(((clientId >> 16) & 0xFF) << 8)); + Meta = (byte)((clientId >> 24) & 0xFF); } /// /// Gets the clientId. @@ -87,12 +81,7 @@ namespace MLAPI.Data /// The client identifier. public uint GetClientId() { - tempUShortBytes = BitConverter.GetBytes(ConnectionId); - tempUIntBytes[0] = HostId; - tempUIntBytes[1] = tempUShortBytes[0]; - tempUIntBytes[2] = tempUShortBytes[1]; - tempUIntBytes[3] = Meta; - return BitConverter.ToUInt32(tempUIntBytes, 0); + return (uint)HostId | (ushort)((uint)(byte)(ConnectionId & 0xFF) << 8) | (ushort)((uint)(byte)((ConnectionId >> 8) & 0xFF) << 16) | (ushort)((uint)Meta << 24); } // Rider generated vvv ///