Remove MarkForReset (#1747)

* Remove MarkForReset

* Update Assets/Mirror/Runtime/NetworkIdentity.cs

* Update Assets/Mirror/Tests/Editor/NetworkServerTest.cs
This commit is contained in:
Paul Pacheco 2020-04-20 13:07:22 -05:00 committed by GitHub
parent 5d4bc47d46
commit 3dd709ac35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 10 additions and 43 deletions

View File

@ -442,7 +442,7 @@ public static void DestroyAllClientObjects()
} }
else else
{ {
identity.MarkForReset(); identity.Reset();
identity.gameObject.SetActive(false); identity.gameObject.SetActive(false);
} }
} }
@ -453,8 +453,6 @@ public static void DestroyAllClientObjects()
static void ApplySpawnPayload(NetworkIdentity identity, SpawnMessage msg) static void ApplySpawnPayload(NetworkIdentity identity, SpawnMessage msg)
{ {
identity.Reset();
if (msg.assetId != Guid.Empty) if (msg.assetId != Guid.Empty)
identity.assetId = msg.assetId; identity.assetId = msg.assetId;
@ -628,7 +626,7 @@ static void DestroyObject(uint netId)
} }
} }
NetworkIdentity.spawned.Remove(netId); NetworkIdentity.spawned.Remove(netId);
localObject.MarkForReset(); localObject.Reset();
} }
else else
{ {

View File

@ -50,10 +50,6 @@ public sealed class NetworkIdentity : MonoBehaviour
// configuration // configuration
NetworkBehaviour[] networkBehavioursCache; NetworkBehaviour[] networkBehavioursCache;
// member used to mark a identity for future reset
// check MarkForReset for more information.
bool reset;
/// <summary> /// <summary>
/// Returns true if running as a client and this object was spawned by a server. /// Returns true if running as a client and this object was spawned by a server.
/// </summary> /// </summary>
@ -527,15 +523,9 @@ void OnDestroy()
sceneIds.Remove(sceneId); sceneIds.Remove(sceneId);
sceneIds.Remove(sceneId & 0x00000000FFFFFFFF); sceneIds.Remove(sceneId & 0x00000000FFFFFFFF);
// Only call NetworkServer.Destroy on server and only if reset is false // If false the object has already been unspawned
// reset will be false from incorrect use of Destroy instead of NetworkServer.Destroy // if it is still true, then we need to unspawn it
// reset will be true if NetworkServer.Destroy was correctly invoked to begin with if (isServer)
// Users are supposed to call NetworkServer.Destroy instead of just regular Destroy for networked objects.
// This is a safeguard in case users accidentally call regular Destroy instead.
// We cover their mistake by calling NetworkServer.Destroy for them.
// If, however, they call NetworkServer.Destroy correctly, which leads to NetworkIdentity.MarkForReset,
// then we don't need to call it again, so the check for reset is needed to prevent the doubling.
if (isServer && !reset)
{ {
// Do not add logging to this (see above) // Do not add logging to this (see above)
NetworkServer.Destroy(gameObject); NetworkServer.Destroy(gameObject);
@ -1275,21 +1265,11 @@ public void RemoveClientAuthority()
// marks the identity for future reset, this is because we cant reset the identity during destroy // marks the identity for future reset, this is because we cant reset the identity during destroy
// as people might want to be able to read the members inside OnDestroy(), and we have no way // as people might want to be able to read the members inside OnDestroy(), and we have no way
// of invoking reset after OnDestroy is called. // of invoking reset after OnDestroy is called.
internal void MarkForReset() => reset = true;
// check if it was marked for reset
internal bool IsMarkedForReset() => reset;
// if we have marked an identity for reset we do the actual reset.
internal void Reset() internal void Reset()
{ {
if (!reset)
return;
clientStarted = false; clientStarted = false;
isClient = false; isClient = false;
isServer = false; isServer = false;
reset = false;
netId = 0; netId = 0;
connectionToServer = null; connectionToServer = null;

View File

@ -737,7 +737,7 @@ void CleanupNetworkIdentities()
{ {
foreach (NetworkIdentity identity in Resources.FindObjectsOfTypeAll<NetworkIdentity>()) foreach (NetworkIdentity identity in Resources.FindObjectsOfTypeAll<NetworkIdentity>())
{ {
identity.MarkForReset(); identity.Reset();
} }
} }

View File

@ -676,7 +676,6 @@ public static bool AddPlayerForConnection(NetworkConnection conn, GameObject pla
Debug.Log("AddPlayer: playerGameObject has no NetworkIdentity. Please add a NetworkIdentity to " + player); Debug.Log("AddPlayer: playerGameObject has no NetworkIdentity. Please add a NetworkIdentity to " + player);
return false; return false;
} }
identity.Reset();
// cannot have a player object in "Add" version // cannot have a player object in "Add" version
if (conn.identity != null) if (conn.identity != null)
@ -904,7 +903,6 @@ internal static void SpawnObject(GameObject obj, NetworkConnection ownerConnecti
Debug.LogError("SpawnObject " + obj + " has no NetworkIdentity. Please add a NetworkIdentity to " + obj); Debug.LogError("SpawnObject " + obj + " has no NetworkIdentity. Please add a NetworkIdentity to " + obj);
return; return;
} }
identity.Reset();
identity.connectionToClient = (NetworkConnectionToClient)ownerConnection; identity.connectionToClient = (NetworkConnectionToClient)ownerConnection;
// special case to make sure hasAuthority is set // special case to make sure hasAuthority is set
@ -1101,7 +1099,7 @@ static void DestroyObject(NetworkIdentity identity, bool destroyServerObject)
{ {
UnityEngine.Object.Destroy(identity.gameObject); UnityEngine.Object.Destroy(identity.gameObject);
} }
identity.MarkForReset(); identity.Reset();
} }
/// <summary> /// <summary>
@ -1175,7 +1173,6 @@ public static bool SpawnObjects()
if (ValidateSceneObject(identity)) if (ValidateSceneObject(identity))
{ {
if (LogFilter.Debug) Debug.Log("SpawnObjects sceneId:" + identity.sceneId.ToString("X") + " name:" + identity.gameObject.name); if (LogFilter.Debug) Debug.Log("SpawnObjects sceneId:" + identity.sceneId.ToString("X") + " name:" + identity.gameObject.name);
identity.Reset();
identity.gameObject.SetActive(true); identity.gameObject.SetActive(true);
} }
} }

View File

@ -1215,15 +1215,7 @@ public void Reset()
identity.connectionToServer = new NetworkConnectionToServer(); identity.connectionToServer = new NetworkConnectionToServer();
identity.observers[43] = new NetworkConnectionToClient(2); identity.observers[43] = new NetworkConnectionToClient(2);
// calling reset shouldn't do anything unless it was marked for reset
identity.Reset();
Assert.That(identity.isClient, Is.True);
Assert.That(identity.netId, Is.EqualTo(netId));
Assert.That(identity.connectionToClient, !Is.Null);
Assert.That(identity.connectionToServer, !Is.Null);
// mark for reset and reset // mark for reset and reset
identity.MarkForReset();
identity.Reset(); identity.Reset();
Assert.That(identity.isClient, Is.False); Assert.That(identity.isClient, Is.False);
Assert.That(identity.netId, Is.EqualTo(0)); Assert.That(identity.netId, Is.EqualTo(0));

View File

@ -1036,13 +1036,13 @@ public void UnSpawn()
identity.sceneId = 42; identity.sceneId = 42;
// spawned objects are active // spawned objects are active
go.SetActive(true); go.SetActive(true);
Assert.That(identity.IsMarkedForReset(), Is.False); identity.netId = 123;
// unspawn // unspawn
NetworkServer.UnSpawn(go); NetworkServer.UnSpawn(go);
// it should have been marked for reset now // it should have been reset now
Assert.That(identity.IsMarkedForReset(), Is.True); Assert.That(identity.netId, Is.Zero);
// clean up // clean up
GameObject.DestroyImmediate(go); GameObject.DestroyImmediate(go);