-
Notifications
You must be signed in to change notification settings - Fork 464
fix: mixed authority nested NetworkTransform stops updating non-authority nested NetworkTransforms #4169
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
base: develop-2.0.0
Are you sure you want to change the base?
fix: mixed authority nested NetworkTransform stops updating non-authority nested NetworkTransforms #4169
Changes from all commits
6b2f648
e98ed18
ff196a3
bef6418
1bef954
8920574
674b7d2
fd5b4f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,77 +1,93 @@ | ||
| using System.Collections; | ||
| using System.Text; | ||
| using NUnit.Framework; | ||
| using Unity.Netcode.Components; | ||
| using Unity.Netcode.TestHelpers.Runtime; | ||
| using UnityEngine; | ||
| using UnityEngine.TestTools; | ||
|
|
||
| namespace Unity.Netcode.RuntimeTests | ||
| { | ||
| [TestFixture(HostOrServer.Host, NetworkTransform.AuthorityModes.Server)] | ||
| [TestFixture(HostOrServer.Host, NetworkTransform.AuthorityModes.Owner)] | ||
| [TestFixture(HostOrServer.Server, NetworkTransform.AuthorityModes.Server)] | ||
| [TestFixture(HostOrServer.Server, NetworkTransform.AuthorityModes.Owner)] | ||
|
Comment on lines
+11
to
+14
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need both host and server tests here? Seems overkill maybe?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking into this. |
||
| internal class NetworkTransformMixedAuthorityTests : IntegrationTestWithApproximation | ||
| { | ||
| private const float k_MotionMagnitude = 5.5f; | ||
| private const int k_Iterations = 4; | ||
|
|
||
| protected override int NumberOfClients => 2; | ||
|
|
||
| private readonly NetworkTransform.AuthorityModes m_RootAuthorityMode; | ||
| private readonly NetworkTransform.AuthorityModes m_ChildAuthorityMode; | ||
|
|
||
| private StringBuilder m_ErrorMsg = new StringBuilder(); | ||
|
|
||
| public NetworkTransformMixedAuthorityTests(HostOrServer hostOrServer, NetworkTransform.AuthorityModes rootAuthorityMode) : base(hostOrServer) | ||
| { | ||
| m_RootAuthorityMode = rootAuthorityMode; | ||
| m_ChildAuthorityMode = rootAuthorityMode == NetworkTransform.AuthorityModes.Server ? NetworkTransform.AuthorityModes.Owner : NetworkTransform.AuthorityModes.Server; | ||
| } | ||
|
|
||
| protected override void OnCreatePlayerPrefab() | ||
| { | ||
| m_PlayerPrefab.AddComponent<NetworkTransform>(); | ||
| m_PlayerPrefab.AddComponent<NetworkTransform>().AuthorityMode = m_RootAuthorityMode; | ||
|
|
||
| var childGameObject = new GameObject(); | ||
| childGameObject.transform.parent = m_PlayerPrefab.transform; | ||
| var childNetworkTransform = childGameObject.AddComponent<NetworkTransform>(); | ||
| childNetworkTransform.AuthorityMode = NetworkTransform.AuthorityModes.Owner; | ||
| childNetworkTransform.AuthorityMode = m_ChildAuthorityMode; | ||
| childNetworkTransform.InLocalSpace = true; | ||
|
|
||
| base.OnCreatePlayerPrefab(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the instance of <paramref name="player"/>'s player object that has authority over a | ||
| /// <see cref="NetworkTransform"/> using the <paramref name="authorityMode"/> authority mode. | ||
| /// </summary> | ||
| private NetworkObject GetAuthorityInstance(NetworkManager player, NetworkTransform.AuthorityModes authorityMode) | ||
| { | ||
| var authority = authorityMode == NetworkTransform.AuthorityModes.Server ? m_ServerNetworkManager : player; | ||
| return authority.SpawnManager.SpawnedObjects[player.LocalClient.PlayerObject.NetworkObjectId]; | ||
|
Comment on lines
+50
to
+53
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably worth moving a function like this down into the base
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed it belongs in the base. Moving it there. |
||
| } | ||
|
|
||
| private void MovePlayers() | ||
| { | ||
| foreach (var networkManager in m_NetworkManagers) | ||
| foreach (var networkManager in m_ClientNetworkManagers) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we only want the client managers rather than all managers?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reviewing an alternate approach for this. |
||
| { | ||
| var direction = GetRandomVector3(-1.0f, 1.0f); | ||
| var playerObject = networkManager.LocalClient.PlayerObject; | ||
| var playerObjectId = networkManager.LocalClient.PlayerObject.NetworkObjectId; | ||
| // Server authoritative | ||
| var serverPlayerClone = m_ServerNetworkManager.SpawnManager.SpawnedObjects[playerObjectId]; | ||
| serverPlayerClone.transform.position += direction * k_MotionMagnitude; | ||
| // Owner authoritative | ||
| var childTransform = networkManager.LocalClient.PlayerObject.transform.GetChild(0); | ||
| childTransform.localPosition += direction * k_MotionMagnitude; | ||
| GetAuthorityInstance(networkManager, m_RootAuthorityMode).transform.position += direction * k_MotionMagnitude; | ||
| GetAuthorityInstance(networkManager, m_ChildAuthorityMode).transform.GetChild(0).localPosition += direction * k_MotionMagnitude; | ||
| } | ||
| } | ||
|
|
||
| private bool AllInstancePositionsMatch() | ||
| { | ||
| m_ErrorMsg.Clear(); | ||
| foreach (var networkManager in m_NetworkManagers) | ||
| foreach (var networkManager in m_ClientNetworkManagers) | ||
| { | ||
| var playerObject = networkManager.LocalClient.PlayerObject; | ||
| var playerObjectId = networkManager.LocalClient.PlayerObject.NetworkObjectId; | ||
| var serverRootPosition = m_ServerNetworkManager.SpawnManager.SpawnedObjects[playerObjectId].transform.position; | ||
| var ownerChildPosition = networkManager.LocalClient.PlayerObject.transform.GetChild(0).localPosition; | ||
| var authorityRootPosition = GetAuthorityInstance(networkManager, m_RootAuthorityMode).transform.position; | ||
| var authorityChildPosition = GetAuthorityInstance(networkManager, m_ChildAuthorityMode).transform.GetChild(0).localPosition; | ||
|
|
||
| // The authority instances are compared too, as an instance with authority over one nested | ||
| // NetworkTransform is still non-authority for the other. | ||
| foreach (var client in m_NetworkManagers) | ||
| { | ||
| if (client == networkManager) | ||
| { | ||
| continue; | ||
| } | ||
| var playerClone = client.SpawnManager.SpawnedObjects[playerObjectId]; | ||
| var cloneRootPosition = playerClone.transform.position; | ||
| var cloneChildPosition = playerClone.transform.GetChild(0).localPosition; | ||
|
|
||
| if (!Approximately(serverRootPosition, cloneRootPosition)) | ||
| if (!Approximately(authorityRootPosition, cloneRootPosition)) | ||
| { | ||
| m_ErrorMsg.AppendLine($"[{playerObject.name}][{playerClone.name}] Root mismatch ({GetVector3Values(serverRootPosition)})({GetVector3Values(cloneRootPosition)})!"); | ||
| m_ErrorMsg.AppendLine($"[Client-{client.LocalClientId}][{playerClone.name}] Root mismatch ({GetVector3Values(authorityRootPosition)})({GetVector3Values(cloneRootPosition)})!"); | ||
| } | ||
|
|
||
| if (!Approximately(ownerChildPosition, cloneChildPosition)) | ||
| if (!Approximately(authorityChildPosition, cloneChildPosition)) | ||
| { | ||
| m_ErrorMsg.AppendLine($"[{playerObject.name}][{playerClone.name}] Child mismatch ({GetVector3Values(ownerChildPosition)})({GetVector3Values(cloneChildPosition)})!"); | ||
| m_ErrorMsg.AppendLine($"[Client-{client.LocalClientId}][{playerClone.name}] Child mismatch ({GetVector3Values(authorityChildPosition)})({GetVector3Values(cloneChildPosition)})!"); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -81,8 +97,8 @@ private bool AllInstancePositionsMatch() | |
| /// <summary> | ||
| /// Client-Server Only | ||
| /// Validates that mixed authority is working properly | ||
| /// Root -- Server Authoritative | ||
| /// |--Child -- Owner Authoritative | ||
| /// Root -- Server or Owner authoritative | ||
| /// |--Child -- The inverse of the root's authority mode | ||
| /// </summary> | ||
| [UnityTest] | ||
| public IEnumerator MixedAuthorityTest() | ||
|
|
@@ -91,7 +107,31 @@ public IEnumerator MixedAuthorityTest() | |
| { | ||
| MovePlayers(); | ||
| yield return WaitForConditionOrTimeOut(AllInstancePositionsMatch); | ||
| AssertOnTimeout($"Transforms failed to synchronize!"); | ||
| AssertOnTimeout($"Transforms failed to synchronize!\n{m_ErrorMsg}"); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The update registration is per-NetworkObject while the authority motion model is per-NetworkTransform, | ||
| /// so an instance stays registered for as long as any one of its nested NetworkTransform components is | ||
| /// non-authority. | ||
| /// </summary> | ||
| [Test] | ||
| public void MixedAuthorityUpdateRegistration() | ||
| { | ||
| foreach (var networkManager in m_ClientNetworkManagers) | ||
| { | ||
| var playerObjectId = networkManager.LocalClient.PlayerObject.NetworkObjectId; | ||
| foreach (var client in m_NetworkManagers) | ||
| { | ||
| var playerClone = client.SpawnManager.SpawnedObjects[playerObjectId]; | ||
| var hasNonAuthority = false; | ||
| foreach (var networkTransform in playerClone.NetworkTransforms) | ||
| { | ||
| hasNonAuthority |= !networkTransform.CanCommitToTransform; | ||
| } | ||
| Assert.AreEqual(hasNonAuthority, client.NetworkTransformUpdate.ContainsKey(playerObjectId), $"[Client-{client.LocalClientId}][{playerClone.name}] Unexpected update registration!"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #if COM_UNITY_MODULES_PHYSICS | ||
| using System.Collections; | ||
| using NUnit.Framework; | ||
| using Unity.Netcode.Components; | ||
| using Unity.Netcode.TestHelpers.Runtime; | ||
| using UnityEngine; | ||
| using UnityEngine.TestTools; | ||
|
|
||
| namespace Unity.Netcode.RuntimeTests | ||
| { | ||
| internal class NetworkTransformMixedMotionModelTests : NetcodeIntegrationTest | ||
| { | ||
| protected override int NumberOfClients => 2; | ||
|
|
||
| private GameObject m_MixedMotionModelPrefab; | ||
|
|
||
| protected override void OnServerAndClientsCreated() | ||
| { | ||
| m_MixedMotionModelPrefab = CreateNetworkObjectPrefab("MixedMotionModel"); | ||
|
|
||
| // The root is owner authoritative and driven by the rigidbody, which places it in the fixed update registration | ||
| var rootNetworkTransform = m_MixedMotionModelPrefab.AddComponent<NetworkTransform>(); | ||
| rootNetworkTransform.AuthorityMode = NetworkTransform.AuthorityModes.Owner; | ||
| var rigidbody = m_MixedMotionModelPrefab.AddComponent<Rigidbody>(); | ||
| rigidbody.useGravity = false; | ||
| rigidbody.detectCollisions = false; | ||
| m_MixedMotionModelPrefab.AddComponent<NetworkRigidbody>().UseRigidBodyForMotion = true; | ||
|
|
||
| // The nested child is server authoritative and driven by the transform, which places it in the update registration | ||
| var childGameObject = new GameObject(); | ||
| childGameObject.transform.parent = m_MixedMotionModelPrefab.transform; | ||
| var childNetworkTransform = childGameObject.AddComponent<NetworkTransform>(); | ||
| childNetworkTransform.AuthorityMode = NetworkTransform.AuthorityModes.Server; | ||
| childNetworkTransform.InLocalSpace = true; | ||
|
|
||
| base.OnServerAndClientsCreated(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A NetworkObject that mixes both the authority motion model and the rigidbody motion model has each nested | ||
| /// NetworkTransform registered under a different update. Gaining authority over the instance in one update | ||
| /// should not leave it registered for the other. | ||
| /// </summary> | ||
| [UnityTest] | ||
| public IEnumerator UpdateRegistrationFollowsMotionModel() | ||
| { | ||
| var instance = SpawnObject(m_MixedMotionModelPrefab, m_ServerNetworkManager).GetComponent<NetworkObject>(); | ||
| yield return WaitForSpawnedOnAllOrTimeOut(instance); | ||
| AssertOnTimeout($"Failed to spawn {instance.name} on all clients!"); | ||
|
|
||
| var newOwner = m_ClientNetworkManagers[0]; | ||
|
|
||
| // Establish the baseline before ownership is transferred, otherwise the check below would still pass if this instance was never registered for the fixed update to begin with. | ||
| Assert.True(newOwner.NetworkTransformFixedUpdate.ContainsKey(instance.NetworkObjectId), $"Client-{newOwner.LocalClientId} should initially be registered for the fixed update!"); | ||
|
|
||
| instance.ChangeOwnership(newOwner.LocalClientId); | ||
| yield return WaitForConditionOrTimeOut(() => newOwner.SpawnManager.SpawnedObjects[instance.NetworkObjectId].OwnerClientId == newOwner.LocalClientId); | ||
|
NoelStephensUnity marked this conversation as resolved.
|
||
| AssertOnTimeout($"Client-{newOwner.LocalClientId} never gained ownership of {instance.name}!"); | ||
|
|
||
| // The new owner is the authority for the rigidbody driven root, so nothing on this instance needs the fixed | ||
| // update any longer. The server authoritative child still needs the standard update. | ||
| Assert.False(newOwner.NetworkTransformFixedUpdate.ContainsKey(instance.NetworkObjectId), $"Client-{newOwner.LocalClientId} is still registered for the fixed update!"); | ||
| Assert.True(newOwner.NetworkTransformUpdate.ContainsKey(instance.NetworkObjectId), $"Client-{newOwner.LocalClientId} is not registered for the update!"); | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.