NetworkIdentity: syntax improvements to simplify code

This commit is contained in:
vis2k 2018-06-11 11:19:57 +02:00
parent ebb0844bcc
commit d8d3ee677e

View File

@ -120,19 +120,14 @@ public ReadOnlyCollection<NetworkConnection> observers
{ {
get get
{ {
if (m_Observers == null) return m_Observers != null ? new ReadOnlyCollection<NetworkConnection>(m_Observers) : null;
return null;
return new ReadOnlyCollection<NetworkConnection>(m_Observers);
} }
} }
static uint s_NextNetworkId = 1; static uint s_NextNetworkId = 1;
static internal NetworkInstanceId GetNextNetworkId() static internal NetworkInstanceId GetNextNetworkId()
{ {
uint newId = s_NextNetworkId; return new NetworkInstanceId(s_NextNetworkId++);
s_NextNetworkId += 1;
return new NetworkInstanceId(newId);
} }
static NetworkWriter s_UpdateWriter = new NetworkWriter(); static NetworkWriter s_UpdateWriter = new NetworkWriter();
@ -300,11 +295,7 @@ internal void OnStartServer(bool allowNonZeroNetId)
} }
else else
{ {
if (allowNonZeroNetId) if (!allowNonZeroNetId)
{
//allowed
}
else
{ {
if (LogFilter.logError) { Debug.LogError("Object has non-zero netId " + netId + " for " + gameObject); } if (LogFilter.logError) { Debug.LogError("Object has non-zero netId " + netId + " for " + gameObject); }
return; return;
@ -506,25 +497,15 @@ internal void HandleClientAuthority(bool authority)
bool GetInvokeComponent(int cmdHash, Type invokeClass, out NetworkBehaviour invokeComponent) bool GetInvokeComponent(int cmdHash, Type invokeClass, out NetworkBehaviour invokeComponent)
{ {
// dont use GetComponent(), already have a list - avoids an allocation // dont use GetComponent(), already have a list - avoids an allocation
NetworkBehaviour foundComp = null; invokeComponent = Array.Find(m_NetworkBehaviours,
for (int i = 0; i < m_NetworkBehaviours.Length; i++) comp => comp.GetType() == invokeClass || comp.GetType().IsSubclassOf(invokeClass)
{ );
var comp = m_NetworkBehaviours[i]; if (invokeComponent == null)
if (comp.GetType() == invokeClass || comp.GetType().IsSubclassOf(invokeClass))
{
// found matching class
foundComp = comp;
break;
}
}
if (foundComp == null)
{ {
string errorCmdName = NetworkBehaviour.GetCmdHashHandlerName(cmdHash); string errorCmdName = NetworkBehaviour.GetCmdHashHandlerName(cmdHash);
if (LogFilter.logError) { Debug.LogError("Found no behaviour for incoming [" + errorCmdName + "] on " + gameObject + ", the server and client should have the same NetworkBehaviour instances [netId=" + netId + "]."); } if (LogFilter.logError) { Debug.LogError("Found no behaviour for incoming [" + errorCmdName + "] on " + gameObject + ", the server and client should have the same NetworkBehaviour instances [netId=" + netId + "]."); }
invokeComponent = null;
return false; return false;
} }
invokeComponent = foundComp;
return true; return true;
} }
@ -816,9 +797,7 @@ internal void SetConnectionToClient(NetworkConnection conn, short newPlayerContr
internal void OnNetworkDestroy() internal void OnNetworkDestroy()
{ {
for (int i = 0; for (int i = 0; m_NetworkBehaviours != null && i < m_NetworkBehaviours.Length; i++)
m_NetworkBehaviours != null && i < m_NetworkBehaviours.Length;
i++)
{ {
NetworkBehaviour comp = m_NetworkBehaviours[i]; NetworkBehaviour comp = m_NetworkBehaviours[i];
comp.OnNetworkDestroy(); comp.OnNetworkDestroy();
@ -833,8 +812,7 @@ internal void ClearObservers()
int count = m_Observers.Count; int count = m_Observers.Count;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
var c = m_Observers[i]; m_Observers[i].RemoveFromVisList(this, true);
c.RemoveFromVisList(this, true);
} }
m_Observers.Clear(); m_Observers.Clear();
m_ObserverConnections.Clear(); m_ObserverConnections.Clear();
@ -897,16 +875,14 @@ public void RebuildObservers(bool initialize)
for (int i = 0; i < NetworkServer.connections.Count; i++) for (int i = 0; i < NetworkServer.connections.Count; i++)
{ {
var conn = NetworkServer.connections[i]; var conn = NetworkServer.connections[i];
if (conn == null) continue; if (conn != null && conn.isReady)
if (conn.isReady)
AddObserver(conn); AddObserver(conn);
} }
for (int i = 0; i < NetworkServer.localConnections.Count; i++) for (int i = 0; i < NetworkServer.localConnections.Count; i++)
{ {
var conn = NetworkServer.localConnections[i]; var conn = NetworkServer.localConnections[i];
if (conn == null) continue; if (conn != null && conn.isReady)
if (conn.isReady)
AddObserver(conn); AddObserver(conn);
} }
} }
@ -959,9 +935,8 @@ public void RebuildObservers(bool initialize)
} }
} }
if (!changed) if (changed)
return; {
m_Observers = new List<NetworkConnection>(newObservers); m_Observers = new List<NetworkConnection>(newObservers);
// rebuild hashset once we have the final set of new observers // rebuild hashset once we have the final set of new observers
@ -971,6 +946,7 @@ public void RebuildObservers(bool initialize)
m_ObserverConnections.Add(m_Observers[i].connectionId); m_ObserverConnections.Add(m_Observers[i].connectionId);
} }
} }
}
public bool RemoveClientAuthority(NetworkConnection conn) public bool RemoveClientAuthority(NetworkConnection conn)
{ {
@ -1098,7 +1074,6 @@ static internal void UNetDomainReload()
{ {
NetworkManager.OnDomainReload(); NetworkManager.OnDomainReload();
} }
#endif #endif
// this is invoked by the UnityEngine // this is invoked by the UnityEngine