Skip to content

Commit 51fa366

Browse files
committed
Convert HasAuthority method into a field
Update it on Spawn, InvokeBehaviourOnOwnershipChanged and ResetOnDespawn
1 parent 84fe50f commit 51fa366

1 file changed

Lines changed: 19 additions & 22 deletions

File tree

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

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ public void DeferDespawn(int tickOffset, bool destroy = true)
416416
return;
417417
}
418418

419-
if (!InternalHasAuthority())
419+
if (!m_HasAuthority)
420420
{
421421
if (NetworkManagerOwner.LogLevel <= LogLevel.Error)
422422
{
@@ -633,7 +633,7 @@ public bool SetOwnershipLock(bool lockOwnership = true)
633633
}
634634

635635
// If we don't have authority exit early
636-
if (!InternalHasAuthority())
636+
if (!m_HasAuthority)
637637
{
638638
if (NetworkManager.LogLevel <= LogLevel.Error)
639639
{
@@ -909,7 +909,7 @@ internal void OwnershipRequest(ulong clientRequestingOwnership)
909909

910910
// This action is always authorized as long as the client still has authority.
911911
// We need to pass in that this is a request approval ownership change.
912-
NetworkManagerOwner.SpawnManager.ChangeOwnership(this, clientRequestingOwnership, InternalHasAuthority(), true);
912+
NetworkManagerOwner.SpawnManager.ChangeOwnership(this, clientRequestingOwnership, m_HasAuthority, true);
913913
}
914914
else
915915
{
@@ -1155,14 +1155,9 @@ public bool HasOwnershipStatus(OwnershipStatus status)
11551155
/// <remarks>
11561156
/// When in client-server mode, authority should is not considered the same as ownership.
11571157
/// </remarks>
1158-
public bool HasAuthority => InternalHasAuthority();
1158+
public bool HasAuthority => IsSpawned ? m_HasAuthority : NetworkManager.IsServer;
11591159

1160-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1161-
private bool InternalHasAuthority()
1162-
{
1163-
var networkManager = NetworkManager;
1164-
return networkManager.DistributedAuthorityMode ? OwnerClientId == networkManager.LocalClientId : networkManager.IsServer;
1165-
}
1160+
private bool m_HasAuthority;
11661161

11671162
/// <summary>
11681163
/// The NetworkManager that owns this NetworkObject.
@@ -1506,7 +1501,7 @@ public void NetworkShow(ulong clientId)
15061501
return;
15071502
}
15081503

1509-
if (!InternalHasAuthority())
1504+
if (!m_HasAuthority)
15101505
{
15111506
if (NetworkManagerOwner.DistributedAuthorityMode)
15121507
{
@@ -1601,7 +1596,7 @@ public void NetworkHide(ulong clientId)
16011596
return;
16021597
}
16031598

1604-
if (!InternalHasAuthority())
1599+
if (!m_HasAuthority)
16051600
{
16061601
if (NetworkManagerOwner.DistributedAuthorityMode)
16071602
{
@@ -1760,7 +1755,7 @@ private void OnDestroy()
17601755
{
17611756
// An authorized destroy is when done by the authority instance or done due to a scene event and the NetworkObject
17621757
// was marked as destroy pending scene event (which means the destroy with scene property was set).
1763-
var isAuthorityDestroy = InternalHasAuthority() || NetworkManager.DAHost || DestroyPendingSceneEvent;
1758+
var isAuthorityDestroy = m_HasAuthority || NetworkManager.DAHost || DestroyPendingSceneEvent;
17641759

17651760
// If the NetworkObject's GameObject is still valid and the scene is still valid and loaded, then we are still valid
17661761
var isStillValid = gameObject != null && gameObject.scene.IsValid() && gameObject.scene.isLoaded;
@@ -2005,8 +2000,8 @@ public NetworkObject InstantiateAndSpawn(NetworkManager networkManager, ulong ow
20052000
/// <param name="destroyWithScene">Should the object be destroyed when the scene is changed</param>
20062001
public void Spawn(bool destroyWithScene = false)
20072002
{
2008-
var networkManager = NetworkManager;
2009-
var clientId = networkManager.DistributedAuthorityMode ? networkManager.LocalClientId : NetworkManager.ServerClientId;
2003+
var clientId = NetworkManager.DistributedAuthorityMode ? NetworkManager.LocalClientId : NetworkManager.ServerClientId;
2004+
m_HasAuthority = NetworkManager.DistributedAuthorityMode ? OwnerClientId == NetworkManager.LocalClientId : NetworkManager.IsServer;
20102005
SpawnInternal(destroyWithScene, clientId, false);
20112006
}
20122007

@@ -2062,6 +2057,7 @@ internal void ResetOnDespawn()
20622057
{
20632058
// Always clear out the observers list when despawned
20642059
Observers.Clear();
2060+
m_HasAuthority = false;
20652061
IsSpawnAuthority = false;
20662062
IsSpawned = false;
20672063
DeferredDespawnTick = 0;
@@ -2099,7 +2095,7 @@ public void ChangeOwnership(ulong newOwnerClientId)
20992095
}
21002096
return;
21012097
}
2102-
NetworkManagerOwner.SpawnManager.ChangeOwnership(this, newOwnerClientId, InternalHasAuthority());
2098+
NetworkManagerOwner.SpawnManager.ChangeOwnership(this, newOwnerClientId, m_HasAuthority);
21032099
}
21042100

21052101
/// <summary>
@@ -2122,6 +2118,8 @@ internal void InvokeBehaviourOnOwnershipChanged(ulong originalOwnerClientId, ulo
21222118
var isPreviousOwner = originalOwnerClientId == NetworkManagerOwner.LocalClientId;
21232119
var isNewOwner = newOwnerClientId == NetworkManagerOwner.LocalClientId;
21242120

2121+
m_HasAuthority = distributedAuthorityMode ? OwnerClientId == NetworkManagerOwner.LocalClientId : NetworkManagerOwner.IsServer;
2122+
21252123
if (distributedAuthorityMode || isPreviousOwner)
21262124
{
21272125
NetworkManagerOwner.SpawnManager.UpdateOwnershipTable(this, originalOwnerClientId, true);
@@ -2334,7 +2332,7 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
23342332

23352333
// DANGO-TODO: Do we want to worry about ownership permissions here?
23362334
// It wouldn't make sense to not allow parenting, but keeping this note here as a reminder.
2337-
var isAuthority = InternalHasAuthority() || (AllowOwnerToParent && IsOwner);
2335+
var isAuthority = m_HasAuthority || (AllowOwnerToParent && IsOwner);
23382336

23392337
// If we don't have authority and we are not shutting down, then don't allow any parenting.
23402338
// If we are shutting down and don't have authority then allow it.
@@ -2409,7 +2407,7 @@ private void OnTransformParentChanged()
24092407

24102408
// With distributed authority, we need to track "valid authoritative" parenting changes.
24112409
// So, either the authority or AuthorityAppliedParenting is considered a "valid parenting change".
2412-
var isParentingAuthority = InternalHasAuthority() || AuthorityAppliedParenting || (AllowOwnerToParent && IsOwner);
2410+
var isParentingAuthority = m_HasAuthority || AuthorityAppliedParenting || (AllowOwnerToParent && IsOwner);
24132411
// If we are spawned and don't have authority; reset the parent back to the cached parent and exit
24142412
if (!isParentingAuthority)
24152413
{
@@ -3526,15 +3524,14 @@ internal void SceneChangedUpdate(Scene scene, bool notify = false)
35263524
return;
35273525
}
35283526

3529-
var isAuthority = InternalHasAuthority();
35303527
SceneOriginHandle = scene.handle;
35313528

35323529
// non-authority needs to update the NetworkSceneHandle
3533-
if (!isAuthority && NetworkManagerOwner.SceneManager.ClientSceneHandleToServerSceneHandle.ContainsKey(SceneOriginHandle))
3530+
if (!m_HasAuthority && NetworkManagerOwner.SceneManager.ClientSceneHandleToServerSceneHandle.ContainsKey(SceneOriginHandle))
35343531
{
35353532
NetworkSceneHandle = NetworkManagerOwner.SceneManager.ClientSceneHandleToServerSceneHandle[SceneOriginHandle];
35363533
}
3537-
else if (isAuthority)
3534+
else if (m_HasAuthority)
35383535
{
35393536
// Since the authority is the source of truth for the NetworkSceneHandle,
35403537
// the NetworkSceneHandle is the same as the SceneOriginHandle.
@@ -3561,7 +3558,7 @@ internal void SceneChangedUpdate(Scene scene, bool notify = false)
35613558
OnMigratedToNewScene?.Invoke();
35623559

35633560
// Only the authority side will notify clients of non-parented NetworkObject scene changes
3564-
if (isAuthority && notify && !transform.parent)
3561+
if (m_HasAuthority && notify && !transform.parent)
35653562
{
35663563
NetworkManagerOwner.SceneManager.NotifyNetworkObjectSceneChanged(this);
35673564
}

0 commit comments

Comments
 (0)