Skip to content

feat: Supporting managed types in NetworkVariable [MTT-4594] #2219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
- The send queues of `UnityTransport` are now dynamically-sized. This means that there shouldn't be any need anymore to tweak the 'Max Send Queue Size' value. In fact, this field is now removed from the inspector and will not be serialized anymore. It is still possible to set it manually using the `MaxSendQueueSize` property, but it is not recommended to do so aside from some specific needs (e.g. limiting the amount of memory used by the send queues in very constrained environments). (#2212)
- As a consequence of the above change, the `UnityTransport.InitialMaxSendQueueSize` field is now deprecated. There is no default value anymore since send queues are dynamically-sized. (#2212)
- The debug simulator in `UnityTransport` is now non-deterministic. Its random number generator used to be seeded with a constant value, leading to the same pattern of packet drops, delays, and jitter in every run. (#2196)
- `NetworkVariable<>` now supports managed `INetworkSerializable` types, as well as other managed types with serialization/deserialization delegates registered to `UserNetworkVariableSerialization<T>.WriteValue` and `UserNetworkVariableSerialization<T>.ReadValue` (#2219)
- `NetworkVariable<>` and `BufferSerializer<BufferSerializerReader>` now deserialize `INetworkSerializable` types in-place, rather than constructing new ones. (#2219)

### Fixed

Expand Down
13 changes: 13 additions & 0 deletions com.unity.netcode.gameobjects/Editor/CodeGen/CodeGenHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ public static bool HasInterface(this TypeReference typeReference, string interfa
try
{
var typeDef = typeReference.Resolve();
// Note: this won't catch generics correctly.
//
// class Foo<T>: IInterface<T> {}
// class Bar: Foo<int> {}
//
// Bar.HasInterface(IInterface<int>) -> returns false even though it should be true.
//
// This can be fixed (see GetAllFieldsAndResolveGenerics() in NetworkBehaviourILPP to understand how)
// but right now we don't need that to work so it's left alone to reduce complexity
if (typeDef.BaseType.HasInterface(interfaceTypeFullName))
{
return true;
}
var typeFaces = typeDef.Interfaces;
return typeFaces.Any(iface => iface.InterfaceType.FullName == interfaceTypeFullName);
}
Expand Down
264 changes: 264 additions & 0 deletions com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public override void ReadField(FastBufferReader reader)
reader.ReadValueSafe(out ushort count);
for (int i = 0; i < count; i++)
{
NetworkVariableSerialization<T>.Read(reader, out T value);
var value = new T();
NetworkVariableSerialization<T>.Read(reader, ref value);
m_List.Add(value);
}
}
Expand All @@ -167,7 +168,8 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
{
case NetworkListEvent<T>.EventType.Add:
{
NetworkVariableSerialization<T>.Read(reader, out T value);
var value = new T();
NetworkVariableSerialization<T>.Read(reader, ref value);
m_List.Add(value);

if (OnListChanged != null)
Expand Down Expand Up @@ -195,7 +197,8 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
case NetworkListEvent<T>.EventType.Insert:
{
reader.ReadValueSafe(out int index);
NetworkVariableSerialization<T>.Read(reader, out T value);
var value = new T();
NetworkVariableSerialization<T>.Read(reader, ref value);

if (index < m_List.Length)
{
Expand Down Expand Up @@ -231,7 +234,8 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
break;
case NetworkListEvent<T>.EventType.Remove:
{
NetworkVariableSerialization<T>.Read(reader, out T value);
var value = new T();
NetworkVariableSerialization<T>.Read(reader, ref value);
int index = m_List.IndexOf(value);
if (index == -1)
{
Expand Down Expand Up @@ -293,7 +297,8 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
case NetworkListEvent<T>.EventType.Value:
{
reader.ReadValueSafe(out int index);
NetworkVariableSerialization<T>.Read(reader, out T value);
var value = new T();
NetworkVariableSerialization<T>.Read(reader, ref value);
if (index >= m_List.Length)
{
throw new Exception("Shouldn't be here, index is higher than list length");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using UnityEngine;
using System;
using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;

namespace Unity.Netcode
{
Expand All @@ -10,7 +8,7 @@ namespace Unity.Netcode
/// </summary>
/// <typeparam name="T">the unmanaged type for <see cref="NetworkVariable{T}"/> </typeparam>
[Serializable]
public class NetworkVariable<T> : NetworkVariableBase where T : unmanaged
public class NetworkVariable<T> : NetworkVariableBase
{
/// <summary>
/// Delegate type for value changed event
Expand Down Expand Up @@ -52,7 +50,7 @@ public virtual T Value
set
{
// Compare bitwise
if (ValueEquals(ref m_InternalValue, ref value))
if (NetworkVariableSerialization<T>.AreEqual(ref m_InternalValue, ref value))
{
return;
}
Expand All @@ -66,20 +64,6 @@ public virtual T Value
}
}

// Compares two values of the same unmanaged type by underlying memory
// Ignoring any overridden value checks
// Size is fixed
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe bool ValueEquals(ref T a, ref T b)
{
// get unmanaged pointers
var aptr = UnsafeUtility.AddressOf(ref a);
var bptr = UnsafeUtility.AddressOf(ref b);

// compare addresses
return UnsafeUtility.MemCmp(aptr, bptr, sizeof(T)) == 0;
}

/// <summary>
/// Sets the <see cref="Value"/>, marks the <see cref="NetworkVariable{T}"/> dirty, and invokes the <see cref="OnValueChanged"/> callback
/// if there are subscribers to that event.
Expand Down Expand Up @@ -115,7 +99,7 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
// would be stored in different fields

T previousValue = m_InternalValue;
NetworkVariableSerialization<T>.Read(reader, out m_InternalValue);
NetworkVariableSerialization<T>.Read(reader, ref m_InternalValue);

if (keepDirtyDelta)
{
Expand All @@ -128,7 +112,7 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
/// <inheritdoc />
public override void ReadField(FastBufferReader reader)
{
NetworkVariableSerialization<T>.Read(reader, out m_InternalValue);
NetworkVariableSerialization<T>.Read(reader, ref m_InternalValue);
}

/// <inheritdoc />
Expand Down
Loading