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

View File

@ -50,10 +50,6 @@ public sealed class NetworkIdentity : MonoBehaviour
// configuration
NetworkBehaviour[] networkBehavioursCache;
// member used to mark a identity for future reset
// check MarkForReset for more information.
bool reset;
/// <summary>
/// Returns true if running as a client and this object was spawned by a server.
/// </summary>
@ -527,15 +523,9 @@ void OnDestroy()
sceneIds.Remove(sceneId);
sceneIds.Remove(sceneId & 0x00000000FFFFFFFF);
// Only call NetworkServer.Destroy on server and only if reset is false
// reset will be false from incorrect use of Destroy instead of NetworkServer.Destroy
// reset will be true if NetworkServer.Destroy was correctly invoked to begin with
// 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)
// If false the object has already been unspawned
// if it is still true, then we need to unspawn it
if (isServer)
{
// Do not add logging to this (see above)
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
// 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.
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()
{
if (!reset)
return;
clientStarted = false;
isClient = false;
isServer = false;
reset = false;
netId = 0;
connectionToServer = null;

View File

@ -737,7 +737,7 @@ void CleanupNetworkIdentities()
{
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);
return false;
}
identity.Reset();
// cannot have a player object in "Add" version
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);
return;
}
identity.Reset();
identity.connectionToClient = (NetworkConnectionToClient)ownerConnection;
// special case to make sure hasAuthority is set
@ -1101,7 +1099,7 @@ static void DestroyObject(NetworkIdentity identity, bool destroyServerObject)
{
UnityEngine.Object.Destroy(identity.gameObject);
}
identity.MarkForReset();
identity.Reset();
}
/// <summary>
@ -1175,7 +1173,6 @@ public static bool SpawnObjects()
if (ValidateSceneObject(identity))
{
if (LogFilter.Debug) Debug.Log("SpawnObjects sceneId:" + identity.sceneId.ToString("X") + " name:" + identity.gameObject.name);
identity.Reset();
identity.gameObject.SetActive(true);
}
}

View File

@ -1215,15 +1215,7 @@ public void Reset()
identity.connectionToServer = new NetworkConnectionToServer();
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
identity.MarkForReset();
identity.Reset();
Assert.That(identity.isClient, Is.False);
Assert.That(identity.netId, Is.EqualTo(0));

View File

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