Skip to content

Commit 98d190e

Browse files
authored
fix: Fixed enum networkvariables throwing exceptions (#1999)
1 parent 9f950ce commit 98d190e

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableSerialization.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,25 @@ private static INetworkVariableSerializer<T> GetSerializer()
207207
{
208208
return new UnmanagedTypeSerializer<T>();
209209
}
210+
if (typeof(Enum).IsAssignableFrom(typeof(T)))
211+
{
212+
return new UnmanagedTypeSerializer<T>();
213+
}
210214

211215
if (typeof(INetworkSerializable).IsAssignableFrom(typeof(T)))
212216
{
217+
// Obtains "Open Instance Delegates" for the type's NetworkSerialize() methods -
218+
// one for an instance of the generic method taking BufferSerializerWriter as T,
219+
// one for an instance of the generic method taking BufferSerializerReader as T
213220
var writeMethod = (NetworkSerializableSerializer<T>.WriteValueDelegate)Delegate.CreateDelegate(typeof(NetworkSerializableSerializer<T>.WriteValueDelegate), null, typeof(T).GetMethod(nameof(INetworkSerializable.NetworkSerialize)).MakeGenericMethod(typeof(BufferSerializerWriter)));
214221
var readMethod = (NetworkSerializableSerializer<T>.ReadValueDelegate)Delegate.CreateDelegate(typeof(NetworkSerializableSerializer<T>.ReadValueDelegate), null, typeof(T).GetMethod(nameof(INetworkSerializable.NetworkSerialize)).MakeGenericMethod(typeof(BufferSerializerReader)));
215222
return new NetworkSerializableSerializer<T> { WriteValue = writeMethod, ReadValue = readMethod };
216223
}
217224

218225
if (typeof(IUTF8Bytes).IsAssignableFrom(typeof(T)) && typeof(INativeList<byte>).IsAssignableFrom(typeof(T)))
219226
{
227+
// Get "OpenInstanceDelegates" for the Length property (get and set, which are prefixed
228+
// with "get_" and "set_" under the hood and emitted as methods) and GetUnsafePtr()
220229
var getLength = (FixedStringSerializer<T>.GetLengthDelegate)Delegate.CreateDelegate(typeof(FixedStringSerializer<T>.GetLengthDelegate), null, typeof(T).GetMethod("get_" + nameof(INativeList<byte>.Length)));
221230
var setLength = (FixedStringSerializer<T>.SetLengthDelegate)Delegate.CreateDelegate(typeof(FixedStringSerializer<T>.SetLengthDelegate), null, typeof(T).GetMethod("set_" + nameof(INativeList<byte>.Length)));
222231
var getUnsafePtr = (FixedStringSerializer<T>.GetUnsafePtrDelegate)Delegate.CreateDelegate(typeof(FixedStringSerializer<T>.GetUnsafePtrDelegate), null, typeof(T).GetMethod(nameof(IUTF8Bytes.GetUnsafePtr)));

com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariableTests.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,14 @@ public override int GetHashCode()
271271

272272
public class NetworkVariableTest : NetworkBehaviour
273273
{
274+
public enum SomeEnum
275+
{
276+
A,
277+
B,
278+
C
279+
}
274280
public readonly NetworkVariable<int> TheScalar = new NetworkVariable<int>();
281+
public readonly NetworkVariable<SomeEnum> TheEnum = new NetworkVariable<SomeEnum>();
275282
public readonly NetworkList<int> TheList = new NetworkList<int>();
276283
public readonly NetworkList<FixedString128Bytes> TheLargeList = new NetworkList<FixedString128Bytes>();
277284

@@ -598,7 +605,7 @@ public IEnumerator TestNetworkVariableStruct([Values(true, false)] bool useHost)
598605
bool VerifyStructure()
599606
{
600607
return m_Player1OnClient1.TheStruct.Value.SomeBool == m_Player1OnServer.TheStruct.Value.SomeBool &&
601-
m_Player1OnClient1.TheStruct.Value.SomeInt == m_Player1OnServer.TheStruct.Value.SomeInt;
608+
m_Player1OnClient1.TheStruct.Value.SomeInt == m_Player1OnServer.TheStruct.Value.SomeInt;
602609
}
603610

604611
m_Player1OnServer.TheStruct.Value = new TestStruct() { SomeInt = k_TestUInt, SomeBool = false };
@@ -608,6 +615,23 @@ bool VerifyStructure()
608615
yield return WaitForConditionOrTimeOut(VerifyStructure);
609616
}
610617

618+
[UnityTest]
619+
public IEnumerator TestNetworkVariableEnum([Values(true, false)] bool useHost)
620+
{
621+
yield return InitializeServerAndClients(useHost);
622+
623+
bool VerifyStructure()
624+
{
625+
return m_Player1OnClient1.TheEnum.Value == NetworkVariableTest.SomeEnum.C;
626+
}
627+
628+
m_Player1OnServer.TheEnum.Value = NetworkVariableTest.SomeEnum.C;
629+
m_Player1OnServer.TheEnum.SetDirty(true);
630+
631+
// Wait for the client-side to notify it is finished initializing and spawning.
632+
yield return WaitForConditionOrTimeOut(VerifyStructure);
633+
}
634+
611635
[UnityTest]
612636
public IEnumerator TestINetworkSerializableCallsNetworkSerialize([Values(true, false)] bool useHost)
613637
{

0 commit comments

Comments
 (0)