Skip to content

Commit ae5c41a

Browse files
committed
Test fixes
1 parent 58ead20 commit ae5c41a

File tree

1 file changed

+37
-68
lines changed

1 file changed

+37
-68
lines changed

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

Lines changed: 37 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -403,21 +403,21 @@ public void DeferDespawn(int tickOffset, bool destroy = true)
403403
{
404404
if (NetworkManager.LogLevel == LogLevel.Error)
405405
{
406-
NetworkLog.LogErrorServer($"[Attempted deferred despawn while un-spawned]");
406+
NetworkLog.LogErrorServer("[Attempted deferred despawn while un-spawned]");
407407
}
408408

409409
return;
410410
}
411411

412412
if (!NetworkManagerOwner.DistributedAuthorityMode)
413413
{
414-
NetworkLog.LogError($"This method is only available in distributed authority mode.");
414+
NetworkLog.LogErrorServer("This method is only available in distributed authority mode.");
415415
return;
416416
}
417417

418418
if (!HasAuthority)
419419
{
420-
NetworkLog.LogError($"Only the authority can invoke {nameof(DeferDespawn)} and local Client-{NetworkManagerOwner.LocalClientId} is not the authority of {name}!");
420+
NetworkLog.LogErrorServer($"Only the authority can invoke {nameof(DeferDespawn)} and local Client-{NetworkManagerOwner.LocalClientId} is not the authority of {name}!");
421421
return;
422422
}
423423

@@ -1139,15 +1139,8 @@ public bool HasOwnershipStatus(OwnershipStatus status)
11391139
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11401140
private bool InternalHasAuthority()
11411141
{
1142-
if (!IsSpawned)
1143-
{
1144-
if (NetworkManager.LogLevel == LogLevel.Error)
1145-
{
1146-
NetworkLog.LogErrorServer("[Attempted authority check while un-spawned]");
1147-
}
1148-
return false;
1149-
}
1150-
return NetworkManagerOwner.DistributedAuthorityMode ? OwnerClientId == NetworkManagerOwner.LocalClientId : NetworkManagerOwner.IsServer;
1142+
var networkManager = NetworkManager;
1143+
return networkManager.DistributedAuthorityMode ? OwnerClientId == networkManager.LocalClientId : networkManager.IsServer;
11511144
}
11521145

11531146
/// <summary>
@@ -1536,7 +1529,6 @@ public static void NetworkShow(List<NetworkObject> networkObjects, ulong clientI
15361529

15371530
if (!networkObject.IsSpawned)
15381531
{
1539-
// CHECK replace this by NetworkLog.LogWarning($"Trying to show object but is not spawned!"); continue; (change all in another PR)
15401532
throw new SpawnStateException("Object is not spawned");
15411533
}
15421534

@@ -1726,7 +1718,6 @@ public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientI
17261718
}
17271719
}
17281720

1729-
// CHECK, should I address this comment and replace all exceptions with log warnings/errors? (yes, in another PR, same as the comment below)
17301721
// CLIENT SPAWNING TODO: Log error and continue as opposed to throwing an exception
17311722
if (!networkObject.IsSpawned)
17321723
{
@@ -1761,9 +1752,7 @@ private void OnDestroy()
17611752

17621753
// An authorized destroy is when done by the authority instance or done due to a scene event and the NetworkObject
17631754
// was marked as destroy pending scene event (which means the destroy with scene property was set).
1764-
var isAuthorityDestroy = HasAuthority || networkManager.DAHost || DestroyPendingSceneEvent;
1765-
1766-
if (IsSpawned && !isAuthorityDestroy && networkManager.IsListening &&
1755+
if (IsSpawned && !(HasAuthority || networkManager.DAHost || DestroyPendingSceneEvent) && networkManager.IsListening &&
17671756
(IsSceneObject == null || IsSceneObject.Value != true))
17681757
{
17691758
// If we destroyed a GameObject with a NetworkObject component on the non-authority side, handle cleaning up the SceneMigrationSynchronization.
@@ -1791,11 +1780,6 @@ private void OnDestroy()
17911780
}
17921781
return;
17931782
}
1794-
else
1795-
{
1796-
// If the destroy was authority scene event triggered, then mark this destroy as authority triggered.
1797-
isAuthorityDestroy = true;
1798-
}
17991783
}
18001784
// Otherwise, clients can despawn NetworkObjects while shutting down and should not generate any messages when this happens
18011785
}
@@ -2343,7 +2327,8 @@ internal bool InternalTrySetParent(NetworkObject parent, bool worldPositionStays
23432327

23442328
private void OnTransformParentChanged()
23452329
{
2346-
if (!AutoObjectParentSync)
2330+
var networkManager = NetworkManager;
2331+
if (!AutoObjectParentSync || networkManager.ShutdownInProgress)
23472332
{
23482333
return;
23492334
}
@@ -2353,16 +2338,11 @@ private void OnTransformParentChanged()
23532338
return;
23542339
}
23552340

2356-
var networkManager = NetworkManager;
2357-
if (!IsSpawned || !networkManager.IsListening)
2341+
if (networkManager == null || !networkManager.IsListening)
23582342
{
2359-
if (networkManager.ShutdownInProgress)
2360-
{
2361-
return;
2362-
}
23632343
// DANGO-TODO: Review as to whether we want to provide a better way to handle changing parenting of objects when the
23642344
// object is not spawned. Really, we shouldn't care about these types of changes.
2365-
if (NetworkManager.DistributedAuthorityMode && m_CachedParent != null && transform.parent == null)
2345+
if (networkManager.DistributedAuthorityMode && m_CachedParent != null && transform.parent == null)
23662346
{
23672347
m_CachedParent = null;
23682348
return;
@@ -2371,29 +2351,6 @@ private void OnTransformParentChanged()
23712351
Debug.LogException(new NotListeningException($"{nameof(networkManager)} is not listening, start a server or host before reparenting"));
23722352
return;
23732353
}
2374-
var isAuthority = false;
2375-
// With distributed authority, we need to track "valid authoritative" parenting changes.
2376-
// So, either the authority or AuthorityAppliedParenting is considered a "valid parenting change".
2377-
isAuthority = HasAuthority || AuthorityAppliedParenting || (AllowOwnerToParent && IsOwner);
2378-
var distributedAuthority = NetworkManagerOwner.DistributedAuthorityMode;
2379-
2380-
// If we do not have authority
2381-
if (!isAuthority)
2382-
{
2383-
// If the cached parent has not already been set, and we are in distributed authority mode, then log an exception and exit early as a non-authority instance
2384-
// is trying to set the parent.
2385-
if (distributedAuthority)
2386-
{
2387-
transform.parent = m_CachedParent;
2388-
NetworkLog.LogError($"[Not Owner] Only the owner-authority of child {gameObject.name}'s {nameof(NetworkObject)} component can reparent it!");
2389-
}
2390-
else
2391-
{
2392-
transform.parent = m_CachedParent;
2393-
Debug.LogException(new NotServerException($"Only the server can reparent {nameof(NetworkObject)}s"));
2394-
}
2395-
return;
2396-
}
23972354

23982355
if (!IsSpawned)
23992356
{
@@ -2412,11 +2369,32 @@ private void OnTransformParentChanged()
24122369
}
24132370
return;
24142371
}
2372+
2373+
// With distributed authority, we need to track "valid authoritative" parenting changes.
2374+
// So, either the authority or AuthorityAppliedParenting is considered a "valid parenting change".
2375+
// If we do not have authority and we are spawned
2376+
if (!(HasAuthority || AuthorityAppliedParenting || (AllowOwnerToParent && IsOwner)))
2377+
{
2378+
transform.parent = m_CachedParent;
2379+
if (networkManager.LogLevel >= LogLevel.Normal)
2380+
{
2381+
if (networkManager.DistributedAuthorityMode)
2382+
{
2383+
NetworkLog.LogError($"[Not Owner] Only the owner-authority of child {gameObject.name}'s {nameof(NetworkObject)} component can reparent it!");
2384+
}
2385+
else
2386+
{
2387+
Debug.LogException(new NotServerException($"Only the server can reparent {nameof(NetworkObject)}s"));
2388+
}
2389+
}
2390+
return;
2391+
}
2392+
24152393
var removeParent = false;
24162394
var parentTransform = transform.parent;
24172395
if (parentTransform != null)
24182396
{
2419-
if (!parentTransform.TryGetComponent(out NetworkObject parentObject))
2397+
if (!transform.parent.TryGetComponent(out NetworkObject parentObject))
24202398
{
24212399
transform.parent = m_CachedParent;
24222400
AuthorityAppliedParenting = false;
@@ -2465,28 +2443,28 @@ private void OnTransformParentChanged()
24652443
}
24662444

24672445
// If we're not the server, we should tell the server about this parent change
2468-
if (!NetworkManagerOwner.IsServer)
2446+
if (!networkManager.IsServer)
24692447
{
24702448
// Don't send a message in DA mode if we're the only observers of this object (we're the only authority).
2471-
if (distributedAuthority && Observers.Count <= 1)
2449+
if (networkManager.DistributedAuthorityMode && Observers.Count <= 1)
24722450
{
24732451
return;
24742452
}
24752453

2476-
NetworkManagerOwner.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, NetworkManager.ServerClientId);
2454+
networkManager.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, NetworkManager.ServerClientId);
24772455
return;
24782456
}
24792457

24802458
// Otherwise we are a Server (client-server or DAHost). Send to all observers
2481-
foreach (var clientId in NetworkManagerOwner.ConnectionManager.ConnectedClientIds)
2459+
foreach (var clientId in networkManager.ConnectionManager.ConnectedClientIds)
24822460
{
24832461
if (clientId == NetworkManager.ServerClientId)
24842462
{
24852463
continue;
24862464
}
24872465
if (Observers.Contains(clientId))
24882466
{
2489-
NetworkManagerOwner.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, clientId);
2467+
networkManager.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, clientId);
24902468
}
24912469
}
24922470
}
@@ -2702,15 +2680,6 @@ internal void InternalInSceneNetworkObjectsSpawned()
27022680

27032681
internal void InvokeBehaviourNetworkDespawn()
27042682
{
2705-
if (!IsSpawned)
2706-
{
2707-
if (NetworkManager.LogLevel == LogLevel.Error)
2708-
{
2709-
NetworkLog.LogErrorServer("[Attempted network despawn behavior invoke while un-spawned]");
2710-
}
2711-
2712-
return;
2713-
}
27142683
// Invoke OnNetworkPreDespawn on all child behaviours
27152684
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
27162685
{

0 commit comments

Comments
 (0)