Skip to content

Commit

Permalink
fix: hasAuthority is now visible in all overrides (#1251)
Browse files Browse the repository at this point in the history
See #1250

Before:
```
Void Awake() hasAuthority=False isClient=False isLocalPlayer=False
Void OnStartServer() hasAuthority=False isClient=False isLocalPlayer=False
Void OnStartClient() hasAuthority=False isClient=True isLocalPlayer=False
Void OnStartAuthority() hasAuthority=True isClient=True isLocalPlayer=True
Void OnStartLocalPlayer() hasAuthority=True isClient=True isLocalPlayer=True
Void Start() hasAuthority=True isClient=True isLocalPlayer=True
```

After:
```
Void Awake() hasAuthority=False isClient=False isLocalPlayer=False
Void OnStartServer() hasAuthority=True isClient=False isLocalPlayer=False
Void OnStartClient() hasAuthority=True isClient=True isLocalPlayer=False
Void OnStartAuthority() hasAuthority=True isClient=True isLocalPlayer=True
Void OnStartLocalPlayer() hasAuthority=True isClient=True isLocalPlayer=True
Void Start() hasAuthority=True isClient=True isLocalPlayer=True
```
  • Loading branch information
paulpach committed Nov 29, 2019
1 parent 7aa7815 commit 2f19c7c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
7 changes: 4 additions & 3 deletions Assets/Mirror/Runtime/ClientScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ static void ApplySpawnPayload(NetworkIdentity identity, SpawnMessage msg)
identity.transform.localPosition = msg.position;
identity.transform.localRotation = msg.rotation;
identity.transform.localScale = msg.scale;
identity.hasAuthority = msg.isOwner;

// deserialize components if any payload
// (Count is 0 if there were no components)
Expand All @@ -490,14 +491,13 @@ static void ApplySpawnPayload(NetworkIdentity identity, SpawnMessage msg)

identity.netId = msg.netId;
NetworkIdentity.spawned[msg.netId] = identity;
identity.pendingAuthority = msg.isOwner;

// objects spawned as part of initial state are started on a second pass
if (isSpawnFinished)
{
identity.NotifyAuthority();
identity.OnStartClient();
CheckForLocalPlayer(identity);
identity.hasAuthority = identity.pendingAuthority;
}
}

Expand Down Expand Up @@ -595,9 +595,9 @@ internal static void OnObjectSpawnFinished(ObjectSpawnFinishedMessage _)
// use data from scene objects
foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values.OrderBy(uv => uv.netId))
{
identity.hasAuthority = identity.pendingAuthority;
if (!identity.isClient)
{
identity.NotifyAuthority();
identity.OnStartClient();
CheckForLocalPlayer(identity);
}
Expand Down Expand Up @@ -669,6 +669,7 @@ internal static void OnHostClientSpawn(SpawnMessage msg)
{
localObject.hasAuthority = msg.isOwner;
localObject.OnSetHostVisibility(true);
localObject.NotifyAuthority();
}
}

Expand Down
36 changes: 13 additions & 23 deletions Assets/Mirror/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,7 @@ public bool isServer
/// </summary>
bool isOwner;

public bool hasAuthority
{
get => isOwner;
set
{
bool previous = isOwner;
isOwner = value;

if (previous && !isOwner)
{
OnStopAuthority();
}
if (!previous && isOwner)
{
OnStartAuthority();
}
}
}

// whether this object has been spawned with authority
// we need hasAuthority and pendingOwner because
// we need to wait until all of them spawn before updating hasAuthority
internal bool pendingAuthority { get; set; }
public bool hasAuthority { get; internal set; }

/// <summary>
/// The set of network connections (players) that can see this object.
Expand Down Expand Up @@ -237,6 +215,7 @@ internal void SetNotLocalPlayer()
return;
}
hasAuthority = false;
NotifyAuthority();
}

// this is used when a connection is destroyed, since the "observers" property is read-only
Expand Down Expand Up @@ -572,6 +551,16 @@ internal void OnStartClient()
}
}

bool hadAuthority;
internal void NotifyAuthority()
{
if (!hadAuthority && hasAuthority)
OnStartAuthority();
if (hadAuthority && !hasAuthority)
OnStopAuthority();
hadAuthority = hasAuthority;
}

void OnStartAuthority()
{
if (networkBehavioursCache == null)
Expand Down Expand Up @@ -886,6 +875,7 @@ internal void SetLocalPlayer()
{
isLocalPlayer = true;
hasAuthority = true;
NotifyAuthority();

foreach (NetworkBehaviour comp in networkBehavioursCache)
{
Expand Down
9 changes: 9 additions & 0 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,10 @@ public static bool AddPlayerForConnection(NetworkConnection conn, GameObject pla
// Set the connection on the NetworkIdentity on the server, NetworkIdentity.SetLocalPlayer is not called on the server (it is on clients)
identity.connectionToClient = (NetworkConnectionToClient)conn;

// special case, we are in host mode, set hasAuthority to true so that all overrides see it
if (conn is ULocalConnectionToClient)
identity.hasAuthority = true;

// set ready if not set yet
SetClientReady(conn);

Expand Down Expand Up @@ -1050,6 +1054,11 @@ internal static void SpawnObject(GameObject obj, NetworkConnection ownerConnecti
identity.Reset();
identity.connectionToClient = (NetworkConnectionToClient)ownerConnection;

// special case to make sure hasAuthority is set
// on start server in host mode
if (ownerConnection is ULocalConnectionToClient)
identity.hasAuthority = true;

identity.OnStartServer(false);

if (LogFilter.Debug) Debug.Log("SpawnObject instance ID " + identity.netId + " asset ID " + identity.assetId);
Expand Down

0 comments on commit 2f19c7c

Please sign in to comment.