Simplify network connection clientOwnedObjects (#758)

* Simplify network connection clientOwnedObjects

* Whoops, missed this null check in NetworkServer. Thanks vis
This commit is contained in:
rodolphito 2019-04-07 09:57:38 -07:00 committed by vis2k
parent 146988a6bb
commit b7ae7f006c
2 changed files with 10 additions and 17 deletions

View File

@ -16,7 +16,7 @@ public class NetworkConnection : IDisposable
public string address; public string address;
public float lastMessageTime; public float lastMessageTime;
public NetworkIdentity playerController { get; internal set; } public NetworkIdentity playerController { get; internal set; }
public HashSet<uint> clientOwnedObjects; public HashSet<uint> clientOwnedObjects = new HashSet<uint>();
public bool logNetworkMessages; public bool logNetworkMessages;
// this is always true for regular connections, false for local // this is always true for regular connections, false for local
@ -59,17 +59,14 @@ public void Dispose()
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
if (clientOwnedObjects != null) foreach (uint netId in clientOwnedObjects)
{ {
foreach (uint netId in clientOwnedObjects) if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity))
{ {
if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity)) identity.clientAuthorityOwner = null;
{
identity.clientAuthorityOwner = null;
}
} }
} }
clientOwnedObjects = null; clientOwnedObjects.Clear();
} }
public void Disconnect() public void Disconnect()
@ -278,13 +275,12 @@ public virtual bool TransportSend(int channelId, byte[] bytes, out byte error)
internal void AddOwnedObject(NetworkIdentity obj) internal void AddOwnedObject(NetworkIdentity obj)
{ {
clientOwnedObjects = clientOwnedObjects ?? new HashSet<uint>();
clientOwnedObjects.Add(obj.netId); clientOwnedObjects.Add(obj.netId);
} }
internal void RemoveOwnedObject(NetworkIdentity obj) internal void RemoveOwnedObject(NetworkIdentity obj)
{ {
clientOwnedObjects?.Remove(obj.netId); clientOwnedObjects.Remove(obj.netId);
} }
} }
} }

View File

@ -977,15 +977,12 @@ public static void DestroyPlayerForConnection(NetworkConnection conn)
// so we need null checks for both of them. // so we need null checks for both of them.
// => destroy what we can destroy. // => destroy what we can destroy.
if (conn.clientOwnedObjects != null) HashSet<uint> tmp = new HashSet<uint>(conn.clientOwnedObjects);
foreach (uint netId in tmp)
{ {
HashSet<uint> tmp = new HashSet<uint>(conn.clientOwnedObjects); if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity))
foreach (uint netId in tmp)
{ {
if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity)) Destroy(identity.gameObject);
{
Destroy(identity.gameObject);
}
} }
} }