Skip to content

Commit d70f417

Browse files
feat: MPSNGM-294 patch DA network variables (#2958)
* feat: MPSNGM-294 patch DA network variables * fix Fixing merge issue. Updating some methods where it was using writer as opposed to reader. * style adding whitespace. removing System.Runtime.Serialization using directive as it is not needed. * Remove unnecessary files --------- Co-authored-by: NoelStephensUnity <[email protected]>
1 parent 558fc67 commit d70f417

17 files changed

+649
-170
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,11 +1130,6 @@ internal void WriteNetworkVariableData(FastBufferWriter writer, ulong targetClie
11301130
// Note: In distributed authority mode, all clients can read
11311131
if (NetworkVariableFields[j].CanClientRead(targetClientId))
11321132
{
1133-
if (networkManager.DistributedAuthorityMode)
1134-
{
1135-
writer.WriteValueSafe(NetworkVariableFields[j].Type);
1136-
}
1137-
11381133
if (networkManager.DistributedAuthorityMode || networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
11391134
{
11401135
var writePos = writer.Position;
@@ -1224,8 +1219,6 @@ internal void SetNetworkVariableData(FastBufferReader reader, ulong clientId)
12241219

12251220
if (networkManager.DistributedAuthorityMode)
12261221
{
1227-
// Explicit setting of the NetworkVariableType is only needed for CMB Runtime
1228-
reader.ReadValueSafe(out NetworkVariableType _);
12291222
reader.ReadValueSafe(out ushort size);
12301223
var start_marker = reader.Position;
12311224
NetworkVariableFields[j].ReadField(reader);

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

Lines changed: 18 additions & 17 deletions
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"/>
@@ -94,18 +93,18 @@ public override void WriteDelta(FastBufferWriter writer)
9493
{
9594
case NetworkListEvent<T>.EventType.Add:
9695
{
97-
NetworkVariableSerialization<T>.Write(writer, ref element.Value);
96+
NetworkVariableSerialization<T>.Serializer.Write(writer, ref element.Value);
9897
}
9998
break;
10099
case NetworkListEvent<T>.EventType.Insert:
101100
{
102101
BytePacker.WriteValueBitPacked(writer, element.Index);
103-
NetworkVariableSerialization<T>.Write(writer, ref element.Value);
102+
NetworkVariableSerialization<T>.Serializer.Write(writer, ref element.Value);
104103
}
105104
break;
106105
case NetworkListEvent<T>.EventType.Remove:
107106
{
108-
NetworkVariableSerialization<T>.Write(writer, ref element.Value);
107+
NetworkVariableSerialization<T>.Serializer.Write(writer, ref element.Value);
109108
}
110109
break;
111110
case NetworkListEvent<T>.EventType.RemoveAt:
@@ -116,7 +115,7 @@ public override void WriteDelta(FastBufferWriter writer)
116115
case NetworkListEvent<T>.EventType.Value:
117116
{
118117
BytePacker.WriteValueBitPacked(writer, element.Index);
119-
NetworkVariableSerialization<T>.Write(writer, ref element.Value);
118+
NetworkVariableSerialization<T>.Serializer.Write(writer, ref element.Value);
120119
}
121120
break;
122121
case NetworkListEvent<T>.EventType.Clear:
@@ -133,13 +132,14 @@ public override void WriteField(FastBufferWriter writer)
133132
{
134133
if (m_NetworkManager.DistributedAuthorityMode)
135134
{
136-
writer.WriteValueSafe(NetworkVariableSerialization<T>.Type);
137-
if (NetworkVariableSerialization<T>.Type == CollectionItemType.Unmanaged)
135+
SerializationTools.WriteType(writer, NetworkVariableType.NetworkList);
136+
writer.WriteValueSafe(NetworkVariableSerialization<T>.Serializer.Type);
137+
if (NetworkVariableSerialization<T>.Serializer.Type == NetworkVariableType.Unmanaged)
138138
{
139139
// 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.
140140
var placeholder = new T();
141141
var startPos = writer.Position;
142-
NetworkVariableSerialization<T>.Write(writer, ref placeholder);
142+
NetworkVariableSerialization<T>.Serializer.Write(writer, ref placeholder);
143143
var size = writer.Position - startPos;
144144
writer.Seek(startPos);
145145
BytePacker.WriteValueBitPacked(writer, size);
@@ -148,7 +148,7 @@ public override void WriteField(FastBufferWriter writer)
148148
writer.WriteValueSafe((ushort)m_List.Length);
149149
for (int i = 0; i < m_List.Length; i++)
150150
{
151-
NetworkVariableSerialization<T>.Write(writer, ref m_List.ElementAt(i));
151+
NetworkVariableSerialization<T>.Serializer.Write(writer, ref m_List.ElementAt(i));
152152
}
153153
}
154154

@@ -158,9 +158,10 @@ public override void ReadField(FastBufferReader reader)
158158
m_List.Clear();
159159
if (m_NetworkManager.DistributedAuthorityMode)
160160
{
161-
// Collection item type is used by the CMB rust service, drop value here.
162-
reader.ReadValueSafe(out CollectionItemType type);
163-
if (type == CollectionItemType.Unmanaged)
161+
reader.ReadValueSafe(out NetworkVariableType _);
162+
SerializationTools.ReadType(reader, NetworkVariableSerialization<T>.Serializer);
163+
// Collection item type is used by the DA server, drop value here.
164+
if (NetworkVariableSerialization<T>.Serializer.Type == NetworkVariableType.Unmanaged)
164165
{
165166
ByteUnpacker.ReadValueBitPacked(reader, out int _);
166167
}
@@ -169,7 +170,7 @@ public override void ReadField(FastBufferReader reader)
169170
for (int i = 0; i < count; i++)
170171
{
171172
var value = new T();
172-
NetworkVariableSerialization<T>.Read(reader, ref value);
173+
NetworkVariableSerialization<T>.Serializer.Read(reader, ref value);
173174
m_List.Add(value);
174175
}
175176
}
@@ -186,7 +187,7 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
186187
case NetworkListEvent<T>.EventType.Add:
187188
{
188189
var value = new T();
189-
NetworkVariableSerialization<T>.Read(reader, ref value);
190+
NetworkVariableSerialization<T>.Serializer.Read(reader, ref value);
190191
m_List.Add(value);
191192

192193
if (OnListChanged != null)
@@ -215,7 +216,7 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
215216
{
216217
ByteUnpacker.ReadValueBitPacked(reader, out int index);
217218
var value = new T();
218-
NetworkVariableSerialization<T>.Read(reader, ref value);
219+
NetworkVariableSerialization<T>.Serializer.Read(reader, ref value);
219220

220221
if (index < m_List.Length)
221222
{
@@ -252,7 +253,7 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
252253
case NetworkListEvent<T>.EventType.Remove:
253254
{
254255
var value = new T();
255-
NetworkVariableSerialization<T>.Read(reader, ref value);
256+
NetworkVariableSerialization<T>.Serializer.Read(reader, ref value);
256257
int index = m_List.IndexOf(value);
257258
if (index == -1)
258259
{
@@ -315,7 +316,7 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
315316
{
316317
ByteUnpacker.ReadValueBitPacked(reader, out int index);
317318
var value = new T();
318-
NetworkVariableSerialization<T>.Read(reader, ref value);
319+
NetworkVariableSerialization<T>.Serializer.Read(reader, ref value);
319320
if (index >= m_List.Length)
320321
{
321322
throw new Exception("Shouldn't be here, index is higher than list length");

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

Lines changed: 0 additions & 2 deletions
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>

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

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ public abstract class NetworkVariableBase : IDisposable
3434
private protected NetworkBehaviour m_NetworkBehaviour;
3535

3636
private NetworkManager m_InternalNetworkManager;
37-
38-
internal virtual NetworkVariableType Type => NetworkVariableType.Custom;
39-
4037
private protected NetworkManager m_NetworkManager
4138
{
4239
get
@@ -327,47 +324,4 @@ public virtual void Dispose()
327324
m_InternalNetworkManager = null;
328325
}
329326
}
330-
331-
/// <summary>
332-
/// Enum representing the different types of Network Variables.
333-
/// </summary>
334-
public enum NetworkVariableType : byte
335-
{
336-
/// <summary>
337-
/// Value
338-
/// Used for all of the basic NetworkVariables that contain a single value
339-
/// </summary>
340-
Value = 0,
341-
342-
/// <summary>
343-
/// Custom
344-
/// For any custom implemented extension of the NetworkVariableBase
345-
/// </summary>
346-
Custom = 1,
347-
348-
/// <summary>
349-
/// NetworkList
350-
/// </summary>
351-
NetworkList = 2
352-
}
353-
354-
public enum CollectionItemType : byte
355-
{
356-
/// <summary>
357-
/// For any type that is not valid inside a NetworkVariable collection
358-
/// </summary>
359-
Unknown = 0,
360-
361-
/// <summary>
362-
/// The following types are valid types inside of NetworkVariable collections
363-
/// </summary>
364-
Short = 1,
365-
UShort = 2,
366-
Int = 3,
367-
UInt = 4,
368-
Long = 5,
369-
ULong = 6,
370-
Unmanaged = 7,
371-
}
372-
373327
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#if UNITY_EDITOR
2+
#endif
3+
4+
namespace Unity.Netcode
5+
{
6+
/// <summary>
7+
/// Enum representing the different types of Network Variables that can be sent over the network.
8+
/// The values cannot be changed, as they are used to serialize and deserialize variables on the DA server.
9+
/// Adding new variables should be done by adding new values to the end of the enum
10+
/// using the next free value.
11+
/// </summary>
12+
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
13+
/// Add any new Variable types to this table at the END with incremented index value
14+
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
15+
internal enum NetworkVariableType : byte
16+
{
17+
/// <summary>
18+
/// For any type that is not known at runtime
19+
/// </summary>
20+
Unknown = 0,
21+
/// <summary>
22+
/// Value
23+
/// Used for all of the basic NetworkVariables that contain a single value
24+
/// </summary>
25+
Value = 1,
26+
27+
/// <summary>
28+
/// NetworkList
29+
/// </summary>
30+
NetworkList = 2,
31+
32+
// The following types are valid types inside of NetworkVariable collections
33+
Short = 3,
34+
UShort = 4,
35+
Int = 5,
36+
UInt = 6,
37+
Long = 7,
38+
ULong = 8,
39+
Unmanaged = 9,
40+
}
41+
}

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

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ 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+
1720
private void ThrowArgumentError()
1821
{
1922
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}.");
@@ -79,6 +82,11 @@ public void Duplicate(in T value, ref T duplicatedValue)
7982
}
8083
UserNetworkVariableSerialization<T>.DuplicateValue(value, ref duplicatedValue);
8184
}
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();
8290
}
8391

8492
// RuntimeAccessModifiersILPP will make this `public`

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,27 @@
33
namespace Unity.Netcode
44
{
55
/// <summary>
6-
/// Interface used by NetworkVariables to serialize them
6+
/// Interface used by NetworkVariables to serialize them with additional information for the DA runtime
77
/// </summary>
88
///
99
/// <typeparam name="T"></typeparam>
10-
internal interface INetworkVariableSerializer<T>
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>
1127
{
1228
// Write has to be taken by ref here because of INetworkSerializable
1329
// Open Instance Delegates (pointers to methods without an instance attached to them)

0 commit comments

Comments
 (0)