Skip to content

Commit aea56fe

Browse files
EmandMNoelStephensUnity
authored andcommitted
Fix tests
1 parent 0a417e1 commit aea56fe

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

com.unity.netcode.gameobjects/Runtime/Components/NetworkAnimator.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ internal class TransitionStateinfo
213213
// [Layer][DestinationState][TransitionStateInfo]
214214
private Dictionary<int, Dictionary<int, TransitionStateinfo>> m_DestinationStateToTransitioninfo = new Dictionary<int, Dictionary<int, TransitionStateinfo>>();
215215

216+
// Named differently to avoid serialization conflicts with NetworkBehaviour
217+
private NetworkManager m_LocalNetworkManager;
218+
219+
internal bool DistributedAuthorityMode;
220+
216221
/// <summary>
217222
/// Builds the m_DestinationStateToTransitioninfo lookup table
218223
/// </summary>
@@ -509,7 +514,12 @@ internal bool IsServerAuthoritative()
509514
/// </remarks>
510515
protected virtual bool OnIsServerAuthoritative()
511516
{
512-
return NetworkManager ? !NetworkManager.DistributedAuthorityMode : true;
517+
if (!m_LocalNetworkManager)
518+
{
519+
return true;
520+
}
521+
522+
return !DistributedAuthorityMode;
513523
}
514524

515525
private int[] m_TransitionHash;
@@ -713,6 +723,10 @@ internal AnimationMessage GetAnimationMessage()
713723
/// <inheritdoc/>
714724
public override void OnNetworkSpawn()
715725
{
726+
// Save internal state references
727+
m_LocalNetworkManager = NetworkManager;
728+
DistributedAuthorityMode = m_LocalNetworkManager.DistributedAuthorityMode;
729+
716730
// If there is no assigned Animator then generate a server network warning (logged locally and if applicable on the server-host side as well).
717731
if (m_Animator == null)
718732
{
@@ -963,7 +977,7 @@ internal void CheckForAnimatorChanges()
963977

964978
if (m_Animator.runtimeAnimatorController == null)
965979
{
966-
if (NetworkManager.LogLevel == LogLevel.Developer)
980+
if (m_LocalNetworkManager.LogLevel == LogLevel.Developer)
967981
{
968982
Debug.LogError($"[{GetType().Name}] Could not find an assigned {nameof(RuntimeAnimatorController)}! Cannot check {nameof(Animator)} for changes in state!");
969983
}
@@ -998,9 +1012,9 @@ internal void CheckForAnimatorChanges()
9981012
{
9991013
// Just notify all remote clients and not the local server
10001014
m_ClientSendList.Clear();
1001-
foreach (var clientId in NetworkManager.ConnectionManager.ConnectedClientIds)
1015+
foreach (var clientId in m_LocalNetworkManager.ConnectionManager.ConnectedClientIds)
10021016
{
1003-
if (clientId == NetworkManager.LocalClientId || !NetworkObject.Observers.Contains(clientId))
1017+
if (clientId == m_LocalNetworkManager.LocalClientId || !NetworkObject.Observers.Contains(clientId))
10041018
{
10051019
continue;
10061020
}
@@ -1028,7 +1042,7 @@ private void SendParametersUpdate(ClientRpcParams clientRpcParams = default, boo
10281042
}
10291043
else
10301044
{
1031-
Debug.LogError($"[{name}][Client-{NetworkManager.LocalClientId}] Attempting to send parameter updates but not the owner!");
1045+
Debug.LogError($"[{name}][Client-{m_LocalNetworkManager.LocalClientId}] Attempting to send parameter updates but not the owner!");
10321046
}
10331047
}
10341048
else
@@ -1266,12 +1280,12 @@ internal void UpdateAnimationState(AnimationState animationState)
12661280
// Cross fade from the current to the destination state for the transitions duration while starting at the server's current normalized time of the transition
12671281
m_Animator.CrossFade(transitionStateInfo.DestinationState, transitionStateInfo.TransitionDuration, transitionStateInfo.Layer, 0.0f, animationState.NormalizedTime);
12681282
}
1269-
else if (NetworkManager.LogLevel == LogLevel.Developer)
1283+
else if (m_LocalNetworkManager.LogLevel == LogLevel.Developer)
12701284
{
12711285
NetworkLog.LogWarning($"Current State Hash ({currentState.fullPathHash}) != AnimationState.StateHash ({animationState.StateHash})");
12721286
}
12731287
}
1274-
else if (NetworkManager.LogLevel == LogLevel.Developer)
1288+
else if (m_LocalNetworkManager.LogLevel == LogLevel.Developer)
12751289
{
12761290
NetworkLog.LogError($"[DestinationState To Transition Info] Layer ({animationState.Layer}) sub-table does not contain destination state ({animationState.DestinationStateHash})!");
12771291
}
@@ -1314,7 +1328,8 @@ private unsafe void SendParametersUpdateServerRpc(ParametersUpdateMessage parame
13141328
return;
13151329
}
13161330
UpdateParameters(ref parametersUpdate);
1317-
if (NetworkManager.ConnectedClientsIds.Count > (IsHost ? 2 : 1))
1331+
var connectedClientIds = m_LocalNetworkManager.ConnectionManager.ConnectedClientIds;
1332+
if (connectedClientIds.Count <= (IsHost ? 2 : 1))
13181333
{
13191334
m_ClientSendList.Clear();
13201335
foreach (var clientId in NetworkManager.ConnectionManager.ConnectedClientIds)
@@ -1377,7 +1392,8 @@ private void SendAnimStateServerRpc(AnimationMessage animationMessage, ServerRpc
13771392
UpdateAnimationState(animationState);
13781393
}
13791394

1380-
if (NetworkManager.ConnectedClientsIds.Count > (IsHost ? 2 : 1))
1395+
var connectedClientIds = m_LocalNetworkManager.ConnectionManager.ConnectedClientIds;
1396+
if (connectedClientIds.Count <= (IsHost ? 2 : 1))
13811397
{
13821398
m_ClientSendList.Clear();
13831399
foreach (var clientId in NetworkManager.ConnectionManager.ConnectedClientIds)
@@ -1416,7 +1432,7 @@ private void ProcessAnimStates(AnimationMessage animationMessage)
14161432
{
14171433
if (HasAuthority)
14181434
{
1419-
if (NetworkManager.LogLevel == LogLevel.Developer)
1435+
if (m_LocalNetworkManager.LogLevel == LogLevel.Developer)
14201436
{
14211437
var hostOrOwner = NetworkManager.DistributedAuthorityMode ? "Owner" : "Host";
14221438
var clientServerOrDAMode = NetworkManager.DistributedAuthorityMode ? "distributed authority" : "client-server";
@@ -1443,7 +1459,7 @@ internal void SendAnimTriggerServerRpc(AnimationTriggerMessage animationTriggerM
14431459
// Ignore if a non-owner sent this.
14441460
if (serverRpcParams.Receive.SenderClientId != OwnerClientId)
14451461
{
1446-
if (NetworkManager.LogLevel == LogLevel.Developer)
1462+
if (m_LocalNetworkManager.LogLevel == LogLevel.Developer)
14471463
{
14481464
NetworkLog.LogWarning($"[Owner Authoritative] Detected the a non-authoritative client is sending the server animation trigger updates. If you recently changed ownership of the {name} object, then this could be the reason.");
14491465
}
@@ -1453,6 +1469,8 @@ internal void SendAnimTriggerServerRpc(AnimationTriggerMessage animationTriggerM
14531469
// set the trigger locally on the server
14541470
InternalSetTrigger(animationTriggerMessage.Hash, animationTriggerMessage.IsTriggerSet);
14551471

1472+
var connectedClientIds = m_LocalNetworkManager.ConnectionManager.ConnectedClientIds;
1473+
14561474
m_ClientSendList.Clear();
14571475
foreach (var clientId in NetworkManager.ConnectionManager.ConnectedClientIds)
14581476
{

com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/Components/ObjectNameIdentifier.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ public override void OnDestroy()
108108
DeRegisterNetworkObject();
109109
// This is required otherwise it will try to continue to update the NetworkBehaviour even if
110110
// it has been destroyed (most likely integration test specific)
111-
if (m_NetworkObject.ChildNetworkBehaviours != null && m_NetworkObject.ChildNetworkBehaviours.Contains(this))
112-
{
113-
NetworkObject.ChildNetworkBehaviours.Remove(this);
114-
}
111+
m_NetworkObject.ChildNetworkBehaviours?.Remove(this);
115112
m_NetworkObject = null;
116113
}
117114
base.OnDestroy();

com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ private void InternalOnOneTimeSetup()
576576
IsRunning = true;
577577
m_EnableVerboseDebug = OnSetVerboseDebug();
578578
IntegrationTestSceneHandler.VerboseDebugMode = m_EnableVerboseDebug;
579+
NetworkManagerHelper.VerboseDebugMode = m_EnableVerboseDebug;
579580
VerboseDebug($"Entering {nameof(OneTimeSetup)}");
580581

581582
m_NetworkManagerInstatiationMode = OnSetIntegrationTestMode();

com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetworkManagerHelper.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public static class NetworkManagerHelper
4444
/// </summary>
4545
public static NetworkManagerOperatingMode CurrentNetworkManagerMode;
4646

47+
/// <summary>
48+
/// When true, logs will be generated for <see cref="NetworkManager"/> lifecycle events.
49+
/// </summary>
50+
public static bool VerboseDebugMode;
51+
4752
/// <summary>
4853
/// This provides the ability to start NetworkManager in various modes
4954
/// </summary>
@@ -97,7 +102,7 @@ public static bool StartNetworkManager(out NetworkManager networkManager, Networ
97102
return false;
98103
}
99104

100-
Debug.Log($"{nameof(NetworkManager)} Instantiated.");
105+
VerboseLog($"{nameof(NetworkManager)} Instantiated.");
101106

102107
var unityTransport = NetworkManagerGameObject.AddComponent<UnityTransport>();
103108
if (networkConfig == null)
@@ -212,7 +217,7 @@ private static void StartNetworkManagerMode(NetworkManagerOperatingMode managerM
212217
}
213218

214219
// Only log this if we started an netcode session
215-
Debug.Log($"{CurrentNetworkManagerMode} started.");
220+
VerboseLog($"{CurrentNetworkManagerMode} started.");
216221
}
217222
}
218223

@@ -223,7 +228,7 @@ private static void StopNetworkManagerMode()
223228
{
224229
NetworkManagerObject.Shutdown();
225230

226-
Debug.Log($"{CurrentNetworkManagerMode} stopped.");
231+
VerboseLog($"{CurrentNetworkManagerMode} stopped.");
227232
CurrentNetworkManagerMode = NetworkManagerOperatingMode.None;
228233
}
229234

@@ -243,11 +248,11 @@ public static void ShutdownNetworkManager()
243248

244249
if (NetworkManagerGameObject != null)
245250
{
246-
Debug.Log($"{nameof(NetworkManager)} shutdown.");
251+
VerboseLog($"{nameof(NetworkManager)} shutdown.");
247252

248253
StopNetworkManagerMode();
249254
UnityEngine.Object.DestroyImmediate(NetworkManagerGameObject);
250-
Debug.Log($"{nameof(NetworkManager)} destroyed.");
255+
VerboseLog($"{nameof(NetworkManager)} destroyed.");
251256
}
252257
NetworkManagerGameObject = null;
253258
NetworkManagerObject = null;
@@ -304,5 +309,13 @@ public static bool BuffersMatch(int indexOffset, long targetSize, byte[] sourceA
304309
}
305310
return true;
306311
}
312+
313+
private static void VerboseLog(string message)
314+
{
315+
if (VerboseDebugMode)
316+
{
317+
Debug.unityLogger.Log(message);
318+
}
319+
}
307320
}
308321
}

0 commit comments

Comments
 (0)