Skip to content

Commit 8d211cf

Browse files
ShadauxCat0xFA11
andauthored
fix: Fixed NetworkVariables being truncated to short.MaxValue when NetworkConfig.EnsureNetworkVariableLengthSafety is enabled. (#1811)
* fix: Fixed NetworkVariables being truncated to short.MaxValue when NetworkConfig.EnsureNetworkVariableLengthSafety is enabled. In the process it was discovered that FRAGMENTED_MESSAGE_MAX_SIZE was actually larger than BytePacker.WriteValueBitPacked can support, so that was reduced to the actual correct max value. * changelog * minor syntax fix Co-authored-by: Fatih Mar <[email protected]>
1 parent 0841a44 commit 8d211cf

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2525
- Fixed issue where NetworkManager would continue starting even if the NetworkTransport selected failed. (#1780)
2626
- Fixed issue when spawning new player if an already existing player exists it does not remove IsPlayer from the previous player (#1779)
2727
- Fixed lack of notification that NetworkManager and NetworkObject cannot be added to the same GameObject with in-editor notifications (#1777)
28+
- Network variable updates are no longer limited to 32,768 bytes when NetworkConfig.EnsureNetworkVariableLengthSafety is enabled. The limits are now determined by what the transport can send in a message. (#1811)
2829

2930
## [1.0.0-pre.6] - 2022-03-02
3031

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/NetworkVariableDeltaMessage.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void Serialize(FastBufferWriter writer)
5757
{
5858
if (!shouldWrite)
5959
{
60-
writer.WriteValueSafe((ushort)0);
60+
BytePacker.WriteValueBitPacked(writer, 0);
6161
}
6262
}
6363
else
@@ -69,15 +69,15 @@ public void Serialize(FastBufferWriter writer)
6969
{
7070
if (NetworkBehaviour.NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
7171
{
72-
var tempWriter = new FastBufferWriter(MessagingSystem.NON_FRAGMENTED_MESSAGE_MAX_SIZE, Allocator.Temp, short.MaxValue);
73-
networkVariable.WriteDelta(tempWriter);
72+
var tempWriter = new FastBufferWriter(MessagingSystem.NON_FRAGMENTED_MESSAGE_MAX_SIZE, Allocator.Temp, MessagingSystem.FRAGMENTED_MESSAGE_MAX_SIZE);
73+
NetworkBehaviour.NetworkVariableFields[i].WriteDelta(tempWriter);
74+
BytePacker.WriteValueBitPacked(writer, tempWriter.Length);
7475

75-
if (!writer.TryBeginWrite(FastBufferWriter.GetWriteSize<ushort>() + tempWriter.Length))
76+
if (!writer.TryBeginWrite(tempWriter.Length))
7677
{
7778
throw new OverflowException($"Not enough space in the buffer to write {nameof(NetworkVariableDeltaMessage)}");
7879
}
7980

80-
writer.WriteValue((ushort)tempWriter.Length);
8181
tempWriter.CopyTo(writer);
8282
}
8383
else
@@ -135,10 +135,10 @@ public void Handle(ref NetworkContext context)
135135
{
136136
for (int i = 0; i < networkBehaviour.NetworkVariableFields.Count; i++)
137137
{
138-
ushort varSize = 0;
138+
int varSize = 0;
139139
if (networkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
140140
{
141-
m_ReceivedNetworkVariableData.ReadValueSafe(out varSize);
141+
ByteUnpacker.ReadValueBitPacked(m_ReceivedNetworkVariableData, out varSize);
142142

143143
if (varSize == 0)
144144
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ internal uint GetMessageType(Type t)
6767
}
6868

6969
public const int NON_FRAGMENTED_MESSAGE_MAX_SIZE = 1300;
70-
public const int FRAGMENTED_MESSAGE_MAX_SIZE = int.MaxValue;
70+
public const int FRAGMENTED_MESSAGE_MAX_SIZE = BytePacker.BitPackedIntMax;
7171

7272
internal struct MessageWithHandler
7373
{

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Unity.Netcode
1010
public static class BytePacker
1111
{
1212
#if UNITY_NETCODE_DEBUG_NO_PACKING
13-
13+
1414
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1515
public void WriteValuePacked<T>(FastBufferWriter writer, T value) where T: unmanaged => writer.WriteValueSafe(value);
1616
#else
@@ -277,10 +277,21 @@ public static void WriteValuePacked(FastBufferWriter writer, string s)
277277

278278

279279
#if UNITY_NETCODE_DEBUG_NO_PACKING
280-
280+
281281
[MethodImpl(MethodImplOptions.AggressiveInlining)]
282282
public void WriteValueBitPacked<T>(FastBufferWriter writer, T value) where T: unmanaged => writer.WriteValueSafe(value);
283283
#else
284+
285+
public const ushort BitPackedUshortMax = (1 << 15) - 1;
286+
public const short BitPackedShortMax = (1 << 14) - 1;
287+
public const short BitPackedShortMin = -(1 << 14);
288+
public const uint BitPackedUintMax = (1 << 30) - 1;
289+
public const int BitPackedIntMax = (1 << 29) - 1;
290+
public const int BitPackedIntMin = -(1 << 29);
291+
public const ulong BitPackedULongMax = (1L << 61) - 1;
292+
public const long BitPackedLongMax = (1L << 60) - 1;
293+
public const long BitPackedLongMin = -(1L << 60);
294+
284295
/// <summary>
285296
/// Writes a 14-bit signed short to the buffer in a bit-encoded packed format.
286297
/// The first bit indicates whether the value is 1 byte or 2.
@@ -307,7 +318,7 @@ public static void WriteValuePacked(FastBufferWriter writer, string s)
307318
public static void WriteValueBitPacked(FastBufferWriter writer, ushort value)
308319
{
309320
#if DEVELOPMENT_BUILD || UNITY_EDITOR
310-
if (value >= 0b1000_0000_0000_0000)
321+
if (value >= BitPackedUshortMax)
311322
{
312323
throw new ArgumentException("BitPacked ushorts must be <= 15 bits");
313324
}
@@ -356,7 +367,7 @@ public static void WriteValueBitPacked(FastBufferWriter writer, ushort value)
356367
public static void WriteValueBitPacked(FastBufferWriter writer, uint value)
357368
{
358369
#if DEVELOPMENT_BUILD || UNITY_EDITOR
359-
if (value >= 0b0100_0000_0000_0000_0000_0000_0000_0000)
370+
if (value > BitPackedUintMax)
360371
{
361372
throw new ArgumentException("BitPacked uints must be <= 30 bits");
362373
}
@@ -396,7 +407,7 @@ public static void WriteValueBitPacked(FastBufferWriter writer, uint value)
396407
public static void WriteValueBitPacked(FastBufferWriter writer, ulong value)
397408
{
398409
#if DEVELOPMENT_BUILD || UNITY_EDITOR
399-
if (value >= 0b0010_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000)
410+
if (value > BitPackedULongMax)
400411
{
401412
throw new ArgumentException("BitPacked ulongs must be <= 61 bits");
402413
}

0 commit comments

Comments
 (0)