Skip to content

Commit

Permalink
fix: #2954 calling StopClient in host mode does not reset nextNetId a…
Browse files Browse the repository at this point in the history
…nymore
  • Loading branch information
miwarnec committed Mar 23, 2022
1 parent 3d6c41a commit 6f98122
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
11 changes: 7 additions & 4 deletions Assets/Mirror/Runtime/NetworkClient.cs
Expand Up @@ -1485,10 +1485,13 @@ public static void Shutdown()
handlers.Clear();
spawnableObjects.Clear();

// sets nextNetworkId to 1
// sets clientAuthorityCallback to null
// sets previousLocalPlayer to null
NetworkIdentity.ResetStatics();
// IMPORTANT: do NOT call NetworkIdentity.ResetStatics() here!
// calling StopClient() in host mode would reset nextNetId to 1,
// causing next connection to have a duplicate netId accidentally.
// => see also: https://github.com/vis2k/Mirror/issues/2954
//NetworkIdentity.ResetStatics();
// => instead, reset only the client sided statics.
NetworkIdentity.ResetClientStatics();

// disconnect the client connection.
// we do NOT call Transport.Shutdown, because someone only called
Expand Down
22 changes: 18 additions & 4 deletions Assets/Mirror/Runtime/NetworkIdentity.cs
Expand Up @@ -246,14 +246,28 @@ internal set
static readonly Dictionary<ulong, NetworkIdentity> sceneIds =
new Dictionary<ulong, NetworkIdentity>();

// reset only client sided statics.
// don't touch server statics when calling StopClient in host mode.
// https://github.com/vis2k/Mirror/issues/2954
internal static void ResetClientStatics()
{
previousLocalPlayer = null;
clientAuthorityCallback = null;
}

internal static void ResetServerStatics()
{
nextNetworkId = 1;
}

// RuntimeInitializeOnLoadMethod -> fast playmode without domain reload
// internal so it can be called from NetworkServer & NetworkClient
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
internal static void ResetStatics()
{
nextNetworkId = 1;
clientAuthorityCallback = null;
previousLocalPlayer = null;
// reset ALL statics
ResetClientStatics();
ResetServerStatics();
}

/// <summary>Gets the NetworkIdentity from the sceneIds dictionary with the corresponding id</summary>
Expand Down Expand Up @@ -754,7 +768,7 @@ internal void OnStopClient()
// ownership change due to sync to owner feature.
// Without this static, the second time we get the spawn message we
// would call OnStartLocalPlayer again on the same object
static NetworkIdentity previousLocalPlayer = null;
internal static NetworkIdentity previousLocalPlayer = null;
internal void OnStartLocalPlayer()
{
if (previousLocalPlayer == this)
Expand Down

0 comments on commit 6f98122

Please sign in to comment.