Skip to content

Commit 3c4f1cc

Browse files
authored
feat: Remove DA NetworkVariable modifications (#3155)
1 parent 218a647 commit 3c4f1cc

15 files changed

+17
-573
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1515

1616
### Fixed
1717

18+
- Fixed issue where an exception was thrown when calling `NetworkManager.Shutdown` after calling `UnityTransport.Shutdown`. (#3118)
1819
- Fixed issue where `NetworkList` properties on in-scene placed `NetworkObject`s could cause small memory leaks when entering playmode. (#3147)
1920
- Fixed in-scene `NertworkObject` synchronization issue when loading a scene with currently connected clients connected to a session created by a `NetworkManager` started as a server (i.e. not as a host). (#3133)
2021
- Fixed issue where a `NetworkManager` started as a server would not add itself as an observer to in-scene placed `NetworkObject`s instantiated and spawned by a scene loading event. (#3133)
@@ -26,6 +27,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2627

2728
### Changed
2829

30+
- Optimised `NetworkVariable` and `NetworkTransform` related packets when in Distributed Authority mode.
2931
- The Debug Simulator section of the Unity Transport component was removed. This section was not functional anymore and users are now recommended to use the more featureful [Network Simulator](https://docs-multiplayer.unity3d.com/tools/current/tools-network-simulator/) tool from the Multiplayer Tools package instead. (#3121)
3032

3133
## [2.1.1] - 2024-10-18

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

+6-36
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ public virtual void OnGainedOwnership() { }
826826
internal void InternalOnGainedOwnership()
827827
{
828828
UpdateNetworkProperties();
829-
// New owners need to assure any NetworkVariables they have write permissions
829+
// New owners need to assure any NetworkVariables they have write permissions
830830
// to are updated so the previous and original values are aligned with the
831831
// current value (primarily for collections).
832832
if (OwnerClientId == NetworkManager.LocalClientId)
@@ -1181,14 +1181,8 @@ internal void WriteNetworkVariableData(FastBufferWriter writer, ulong targetClie
11811181
{
11821182
// Create any values that require accessing the NetworkManager locally (it is expensive to access it in NetworkBehaviour)
11831183
var networkManager = NetworkManager;
1184-
var distributedAuthority = networkManager.DistributedAuthorityMode;
11851184
var ensureLengthSafety = networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety;
11861185

1187-
// Always write the NetworkVariable count even if zero for distributed authority (used by comb server)
1188-
if (distributedAuthority)
1189-
{
1190-
writer.WriteValueSafe((ushort)NetworkVariableFields.Count);
1191-
}
11921186

11931187
// Exit early if there are no NetworkVariables
11941188
if (NetworkVariableFields.Count == 0)
@@ -1203,14 +1197,8 @@ internal void WriteNetworkVariableData(FastBufferWriter writer, ulong targetClie
12031197
if (NetworkVariableFields[j].CanClientRead(targetClientId))
12041198
{
12051199
// Write additional NetworkVariable information when length safety is enabled or when in distributed authority mode
1206-
if (ensureLengthSafety || distributedAuthority)
1200+
if (ensureLengthSafety)
12071201
{
1208-
// Write the type being serialized for distributed authority (only for comb-server)
1209-
if (distributedAuthority)
1210-
{
1211-
writer.WriteValueSafe(NetworkVariableFields[j].Type);
1212-
}
1213-
12141202
var writePos = writer.Position;
12151203
// Note: This value can't be packed because we don't know how large it will be in advance
12161204
// we reserve space for it, then write the data, then come back and fill in the space
@@ -1261,20 +1249,8 @@ internal void SetNetworkVariableData(FastBufferReader reader, ulong clientId)
12611249
{
12621250
// Stack cache any values that requires accessing the NetworkManager (it is expensive to access it in NetworkBehaviour)
12631251
var networkManager = NetworkManager;
1264-
var distributedAuthority = networkManager.DistributedAuthorityMode;
12651252
var ensureLengthSafety = networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety;
12661253

1267-
// Always read the NetworkVariable count when in distributed authority (sanity check if comb-server matches what client has locally)
1268-
if (distributedAuthority)
1269-
{
1270-
reader.ReadValueSafe(out ushort variableCount);
1271-
if (variableCount != NetworkVariableFields.Count)
1272-
{
1273-
Debug.LogError($"[{name}][NetworkObjectId: {NetworkObjectId}][NetworkBehaviourId: {NetworkBehaviourId}] NetworkVariable count mismatch! (Read: {variableCount} vs. Expected: {NetworkVariableFields.Count})");
1274-
return;
1275-
}
1276-
}
1277-
12781254
// Exit early if nothing else to read
12791255
if (NetworkVariableFields.Count == 0)
12801256
{
@@ -1289,14 +1265,8 @@ internal void SetNetworkVariableData(FastBufferReader reader, ulong clientId)
12891265
// Distributed Authority: All clients have read permissions, always try to read the value
12901266
if (NetworkVariableFields[j].CanClientRead(clientId))
12911267
{
1292-
if (ensureLengthSafety || distributedAuthority)
1268+
if (ensureLengthSafety)
12931269
{
1294-
// Read the type being serialized and discard it (for now) when in a distributed authority network topology (only used by comb-server)
1295-
if (distributedAuthority)
1296-
{
1297-
reader.ReadValueSafe(out NetworkVariableType _);
1298-
}
1299-
13001270
reader.ReadValueSafe(out varSize);
13011271
if (varSize == 0)
13021272
{
@@ -1320,11 +1290,11 @@ internal void SetNetworkVariableData(FastBufferReader reader, ulong clientId)
13201290
continue;
13211291
}
13221292

1323-
// Read the NetworkVarible value
1293+
// Read the NetworkVariable value
13241294
NetworkVariableFields[j].ReadField(reader);
13251295

1326-
// When EnsureNetworkVariableLengthSafety or DistributedAuthorityMode always do a bounds check
1327-
if (ensureLengthSafety || distributedAuthority)
1296+
// When EnsureNetworkVariableLengthSafety always do a bounds check
1297+
if (ensureLengthSafety)
13281298
{
13291299
if (reader.Position > (readStartPos + varSize))
13301300
{

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

-22
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,6 @@ public NetworkMessageManager(INetworkMessageSender sender, object owner, INetwor
141141
{
142142
RegisterMessageType(type);
143143
}
144-
145-
#if UNITY_EDITOR
146-
if (EnableMessageOrderConsoleLog)
147-
{
148-
// DANGO-TODO: Remove this when we have some form of message type indices stability in place
149-
// For now, just log the messages and their assigned types for reference purposes.
150-
var networkManager = m_Owner as NetworkManager;
151-
if (networkManager != null)
152-
{
153-
if (networkManager.DistributedAuthorityMode)
154-
{
155-
var messageListing = new StringBuilder();
156-
messageListing.AppendLine("NGO Message Index to Type Listing:");
157-
foreach (var message in m_MessageTypes)
158-
{
159-
messageListing.AppendLine($"[{message.Value}][{message.Key.Name}]");
160-
}
161-
Debug.Log(messageListing);
162-
}
163-
}
164-
}
165-
#endif
166144
}
167145
catch (Exception)
168146
{

com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs

-24
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public class NetworkList<T> : NetworkVariableBase where T : unmanaged, IEquatabl
2424
/// The callback to be invoked when the list gets changed
2525
/// </summary>
2626
public event OnListChangedDelegate OnListChanged;
27-
internal override NetworkVariableType Type => NetworkVariableType.NetworkList;
2827

2928
/// <summary>
3029
/// Constructor method for <see cref="NetworkList"/>
@@ -136,20 +135,6 @@ public override void WriteDelta(FastBufferWriter writer)
136135
/// <inheritdoc />
137136
public override void WriteField(FastBufferWriter writer)
138137
{
139-
if (m_NetworkManager.DistributedAuthorityMode)
140-
{
141-
writer.WriteValueSafe(NetworkVariableSerialization<T>.Serializer.Type);
142-
if (NetworkVariableSerialization<T>.Serializer.Type == NetworkVariableType.Unmanaged)
143-
{
144-
// Write the size of the unmanaged serialized type as it has a fixed size. This allows the CMB runtime to correctly read the unmanged type.
145-
var placeholder = new T();
146-
var startPos = writer.Position;
147-
NetworkVariableSerialization<T>.Serializer.Write(writer, ref placeholder);
148-
var size = writer.Position - startPos;
149-
writer.Seek(startPos);
150-
BytePacker.WriteValueBitPacked(writer, size);
151-
}
152-
}
153138
writer.WriteValueSafe((ushort)m_List.Length);
154139
for (int i = 0; i < m_List.Length; i++)
155140
{
@@ -161,15 +146,6 @@ public override void WriteField(FastBufferWriter writer)
161146
public override void ReadField(FastBufferReader reader)
162147
{
163148
m_List.Clear();
164-
if (m_NetworkManager.DistributedAuthorityMode)
165-
{
166-
SerializationTools.ReadType(reader, NetworkVariableSerialization<T>.Serializer);
167-
// Collection item type is used by the DA server, drop value here.
168-
if (NetworkVariableSerialization<T>.Serializer.Type == NetworkVariableType.Unmanaged)
169-
{
170-
ByteUnpacker.ReadValueBitPacked(reader, out int _);
171-
}
172-
}
173149
reader.ReadValueSafe(out ushort count);
174150
for (int i = 0; i < count; i++)
175151
{

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public override void OnInitialize()
4545
NetworkVariableSerialization<T>.Duplicate(m_InternalValue, ref m_PreviousValue);
4646
}
4747

48-
internal override NetworkVariableType Type => NetworkVariableType.Value;
49-
5048
/// <summary>
5149
/// Constructor for <see cref="NetworkVariable{T}"/>
5250
/// </summary>
@@ -92,7 +90,7 @@ public void Reset(T value = default)
9290
// The introduction of standard .NET collections caused an issue with permissions since there is no way to detect changes in the
9391
// collection without doing a full comparison. While this approach does consume more memory per collection instance, it is the
9492
// lowest risk approach to resolving the issue where a client with no write permissions could make changes to a collection locally
95-
// which can cause a myriad of issues.
93+
// which can cause a myriad of issues.
9694
private protected T m_InternalOriginalValue;
9795

9896
private protected T m_PreviousValue;
@@ -305,7 +303,7 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
305303
/// This should be always invoked (client & server) to assure the previous values are set
306304
/// !! IMPORTANT !!
307305
/// When a server forwards delta updates to connected clients, it needs to preserve the previous dirty value(s)
308-
/// until it is done serializing all valid NetworkVariable field deltas (relative to each client). This is invoked
306+
/// until it is done serializing all valid NetworkVariable field deltas (relative to each client). This is invoked
309307
/// after it is done forwarding the deltas at the end of the <see cref="NetworkVariableDeltaMessage.Handle(ref NetworkContext)"/> method.
310308
/// </summary>
311309
internal override void PostDeltaRead()

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ public abstract class NetworkVariableBase : IDisposable
3535

3636
private NetworkManager m_InternalNetworkManager;
3737

38-
internal virtual NetworkVariableType Type => NetworkVariableType.Unknown;
39-
4038
internal string GetWritePermissionError()
4139
{
4240
return $"|Client-{m_NetworkManager.LocalClientId}|{m_NetworkBehaviour.name}|{Name}| Write permissions ({WritePerm}) for this client instance is not allowed!";
@@ -351,7 +349,7 @@ internal ulong OwnerClientId()
351349
/// This should be always invoked (client & server) to assure the previous values are set
352350
/// !! IMPORTANT !!
353351
/// When a server forwards delta updates to connected clients, it needs to preserve the previous dirty value(s)
354-
/// until it is done serializing all valid NetworkVariable field deltas (relative to each client). This is invoked
352+
/// until it is done serializing all valid NetworkVariable field deltas (relative to each client). This is invoked
355353
/// after it is done forwarding the deltas at the end of the <see cref="NetworkVariableDeltaMessage.Handle(ref NetworkContext)"/> method.
356354
/// </summary>
357355
internal virtual void PostDeltaRead()

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

-40
This file was deleted.

com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableTypes.cs.meta

-3
This file was deleted.

com.unity.netcode.gameobjects/Runtime/NetworkVariable/Serialization/FallbackSerializer.cs

-8
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ namespace Unity.Netcode
1414
/// <typeparam name="T"></typeparam>
1515
internal class FallbackSerializer<T> : INetworkVariableSerializer<T>
1616
{
17-
public NetworkVariableType Type => NetworkVariableType.Unknown;
18-
public bool IsDistributedAuthorityOptimized => true;
19-
2017
private void ThrowArgumentError()
2118
{
2219
throw new ArgumentException($"Serialization has not been generated for type {typeof(T).FullName}. This can be addressed by adding a [{nameof(GenerateSerializationForGenericParameterAttribute)}] to your generic class that serializes this value (if you are using one), adding [{nameof(GenerateSerializationForTypeAttribute)}(typeof({typeof(T).FullName})] to the class or method that is attempting to serialize it, or creating a field on a {nameof(NetworkBehaviour)} of type {nameof(NetworkVariable<T>)}. If this error continues to appear after doing one of those things and this is a type you can change, then either implement {nameof(INetworkSerializable)} or mark it as serializable by memcpy by adding {nameof(INetworkSerializeByMemcpy)} to its interface list to enable automatic serialization generation. If not, assign serialization code to {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.WriteValue)}, {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.ReadValue)}, and {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.DuplicateValue)}, or if it's serializable by memcpy (contains no pointers), wrap it in {typeof(ForceNetworkSerializeByMemcpy<>).Name}.");
@@ -82,11 +79,6 @@ public void Duplicate(in T value, ref T duplicatedValue)
8279
}
8380
UserNetworkVariableSerialization<T>.DuplicateValue(value, ref duplicatedValue);
8481
}
85-
86-
public void WriteDistributedAuthority(FastBufferWriter writer, ref T value) => ThrowArgumentError();
87-
public void ReadDistributedAuthority(FastBufferReader reader, ref T value) => ThrowArgumentError();
88-
public void WriteDeltaDistributedAuthority(FastBufferWriter writer, ref T value, ref T previousValue) => ThrowArgumentError();
89-
public void ReadDeltaDistributedAuthority(FastBufferReader reader, ref T value) => ThrowArgumentError();
9082
}
9183

9284
// RuntimeAccessModifiersILPP will make this `public`

com.unity.netcode.gameobjects/Runtime/NetworkVariable/Serialization/INetworkVariableSerializer.cs

+1-21
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,8 @@
22

33
namespace Unity.Netcode
44
{
5-
/// <summary>
6-
/// Interface used by NetworkVariables to serialize them with additional information for the DA runtime
7-
/// </summary>
8-
///
95
/// <typeparam name="T"></typeparam>
10-
internal interface IDistributedAuthoritySerializer<T>
11-
{
12-
/// <summary>
13-
/// The Type tells the DA server how to parse this type.
14-
/// The user should never be able to override this value, as it is meaningful for the DA server
15-
/// </summary>
16-
public NetworkVariableType Type { get; }
17-
public bool IsDistributedAuthorityOptimized { get; }
18-
public void WriteDistributedAuthority(FastBufferWriter writer, ref T value);
19-
public void ReadDistributedAuthority(FastBufferReader reader, ref T value);
20-
public void WriteDeltaDistributedAuthority(FastBufferWriter writer, ref T value, ref T previousValue);
21-
public void ReadDeltaDistributedAuthority(FastBufferReader reader, ref T value);
22-
}
23-
24-
25-
/// <typeparam name="T"></typeparam>
26-
internal interface INetworkVariableSerializer<T> : IDistributedAuthoritySerializer<T>
6+
internal interface INetworkVariableSerializer<T>
277
{
288
// Write has to be taken by ref here because of INetworkSerializable
299
// Open Instance Delegates (pointers to methods without an instance attached to them)

0 commit comments

Comments
 (0)