ConnectionArray: syntax improved to simplify code

This commit is contained in:
vis2k 2018-06-13 11:54:43 +02:00
parent b7d128ba73
commit c81c670c95

View File

@ -1,6 +1,7 @@
#if ENABLE_UNET #if ENABLE_UNET
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace UnityEngine.Networking namespace UnityEngine.Networking
{ {
@ -68,11 +69,7 @@ public NetworkConnection Get(int connId)
// call this if the connection may not exist (in disconnect handler) // call this if the connection may not exist (in disconnect handler)
public NetworkConnection GetUnsafe(int connId) public NetworkConnection GetUnsafe(int connId)
{ {
if (connId < 0 || connId >= m_Connections.Count) return (0 <= connId && connId < connections.Count) ? m_Connections[connId] : null;
{
return null;
}
return m_Connections[connId];
} }
public void Remove(int connId) public void Remove(int connId)
@ -83,7 +80,7 @@ public void Remove(int connId)
return; return;
} }
if (connId < 0 || connId >= m_Connections.Count) if (connId >= m_Connections.Count)
{ {
if (LogFilter.logWarn) { Debug.LogWarning("ConnectionArray Remove invalid index " + connId); } if (LogFilter.logWarn) { Debug.LogWarning("ConnectionArray Remove invalid index " + connId); }
return; return;
@ -108,17 +105,11 @@ public bool ContainsPlayer(GameObject player, out NetworkConnection conn)
for (int i = LocalIndex; i < m_Connections.Count; i++) for (int i = LocalIndex; i < m_Connections.Count; i++)
{ {
conn = Get(i); conn = Get(i);
if (conn != null) if (conn != null && conn.playerControllers.Any(pc => pc.IsValid && pc.gameObject == player))
{
for (int j = 0; j < conn.playerControllers.Count; j++)
{
if (conn.playerControllers[j].IsValid && conn.playerControllers[j].gameObject == player)
{ {
return true; return true;
} }
} }
}
}
return false; return false;
} }
} }