Skip to content

fix: Fixed an exception when EnsureNetworkVariableLengthSafety is enabled #1487

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
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
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

- Fixed NetworkObjects not being despawned before they are destroyed during shutdown for client, host, and server instances. (#1390)
- Fixed client player object being destroyed on server when the client's player object has DontDestroyWithOwner set. (#1433)
- Fixed an exception being thrown during NetworkVariableDeltaMessage serialization when EnsureNetworkVariableLengthSafety is enabled (#1487)
- Fixed: NetworkVariables containing more than 1300 bytes of data (such as large NetworkLists) no longer cause an OverflowException (the limit on data size is now whatever limit the chosen transport imposes on fragmented NetworkDelivery mechanisms) (#1481)
- Fixed KeyNotFound exception when removing ownership of a newly spawned NetworkObject that is already owned by the server. (#1500)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void Serialize(FastBufferWriter writer)
// This var does not belong to the currently iterating delivery group.
if (NetworkBehaviour.NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety)
{
writer.WriteValueSafe((short)0);
writer.WriteValueSafe((ushort)0);
}
else
{
Expand Down Expand Up @@ -71,7 +71,11 @@ public void Serialize(FastBufferWriter writer)
var tmpWriter = new FastBufferWriter(MessagingSystem.NON_FRAGMENTED_MESSAGE_MAX_SIZE, Allocator.Temp, short.MaxValue);
NetworkBehaviour.NetworkVariableFields[k].WriteDelta(tmpWriter);

writer.WriteValueSafe((ushort)tmpWriter.Length);
if (!writer.TryBeginWrite(FastBufferWriter.GetWriteSize<ushort>() + tmpWriter.Length))
{
throw new OverflowException($"Not enough space in the buffer to write {nameof(NetworkVariableDeltaMessage)}");
}
writer.WriteValue((ushort)tmpWriter.Length);
tmpWriter.CopyTo(writer);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public void Awake()
public bool ListDelegateTriggered;
}

[TestFixture(true)]
[TestFixture(false)]
public class NetworkVariableTests : BaseMultiInstanceTest
{
private const string k_FixedStringTestValue = "abcdefghijklmnopqrstuvwxyz";
Expand All @@ -73,6 +75,13 @@ public class NetworkVariableTests : BaseMultiInstanceTest

private bool m_TestWithHost;

private bool m_EnsureLengthSafety;

public NetworkVariableTests(bool ensureLengthSafety)
{
m_EnsureLengthSafety = ensureLengthSafety;
}

[UnitySetUp]
public override IEnumerator Setup()
{
Expand All @@ -82,6 +91,12 @@ public override IEnumerator Setup()
playerPrefab.AddComponent<NetworkVariableTest>();
});

m_ServerNetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety = m_EnsureLengthSafety;
foreach (var client in m_ClientNetworkManagers)
{
client.NetworkConfig.EnsureNetworkVariableLengthSafety = m_EnsureLengthSafety;
}

// These are the *SERVER VERSIONS* of the *CLIENT PLAYER 1 & 2*
var result = new MultiInstanceHelpers.CoroutineResultWrapper<NetworkObject>();

Expand Down