Skip to content

Commit 009c121

Browse files
committed
Revert forced failure
1 parent d98a645 commit 009c121

5 files changed

Lines changed: 7 additions & 23 deletions

File tree

com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,9 @@ internal void CleanupDisconnectedClients()
503503
m_DisconnectedClients.Clear();
504504
}
505505

506-
public static int CreateMessageAndGetVersion<T>() where T : INetworkMessage, new()
506+
public static int CreateMessageAndGetVersion<T>() where T : struct, INetworkMessage
507507
{
508-
return new T().Version;
508+
return default(T).Version;
509509
}
510510

511511
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -538,18 +538,10 @@ internal int GetMessageVersion(Type type, ulong clientId, bool forReceive = fals
538538

539539

540540

541-
public static void ReceiveMessage<T>(FastBufferReader reader, ref NetworkContext context, NetworkMessageManager manager) where T : INetworkMessage, new()
541+
public static void ReceiveMessage<T>(FastBufferReader reader, ref NetworkContext context, NetworkMessageManager manager) where T : struct, INetworkMessage
542542
{
543543
var messageType = typeof(T);
544-
545-
// new(T) is boxed by Mono and so will make an allocation even when T is a struct. default(T) avoids the allocation.
546-
var message = new T();
547-
// If T is a class, default(T) will be null. Users can register classes as custom messages, so we need to ensure we create a valid instance.
548-
// if (message == null)
549-
// {
550-
// message = new T();
551-
// }
552-
544+
var message = default(T);
553545
var messageVersion = 0;
554546

555547
// Special cases because these are the messages that carry the version info - thus the version info isn't

com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferReader.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -493,14 +493,7 @@ public unsafe byte[] ToArray()
493493
/// <exception cref="NotImplementedException">Thrown if the type T does not properly implement NetworkSerialize</exception>
494494
public void ReadNetworkSerializable<T>(out T value) where T : INetworkSerializable, new()
495495
{
496-
// new T() will always cause an allocation.
497-
// Assign default first to see if T is a value type with a valid default
498-
value = default;
499-
if (value == null)
500-
{
501-
// The allocation is unavoidable, ensure T is created.
502-
value = new T();
503-
}
496+
value = new T();
504497
var bufferSerializer = new BufferSerializer<BufferSerializerReader>(new BufferSerializerReader(this));
505498
value.NetworkSerialize(bufferSerializer);
506499
}

com.unity.netcode.gameobjects/Tests/Editor/Messaging/MessageReceivingTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private struct TestMessage : INetworkMessage, INetworkSerializeByMemcpy
1717
public int C;
1818
public static bool Deserialized;
1919
public static bool Handled;
20-
public static List<TestMessage> DeserializedValues = new List<TestMessage>(100000);
20+
public static List<TestMessage> DeserializedValues = new List<TestMessage>();
2121

2222
public void Serialize(FastBufferWriter writer, int targetVersion)
2323
{

com.unity.netcode.gameobjects/Tests/Runtime/Helpers/MessageCatcher.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using Unity.Collections;
4-
using UnityEngine;
54

65
namespace Unity.Netcode.RuntimeTests
76
{

com.unity.netcode.gameobjects/Tests/Runtime/Messaging/MessageReceiveAllocationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class MessageReceiveAllocationTests : NetcodeIntegrationTest
2727
{
2828
protected override int NumberOfClients => 1;
2929

30-
public MessageReceiveAllocationTests(HostOrServer hostOrServer) : base(hostOrServer) {}
30+
public MessageReceiveAllocationTests(HostOrServer hostOrServer) : base(hostOrServer) { }
3131

3232
private GameObject m_Prefab;
3333

0 commit comments

Comments
 (0)