Skip to content

Commit

Permalink
fix: Revert "NetworkIdentity.observers dictionary is always created, …
Browse files Browse the repository at this point in the history
…but always empty on clients. Gets rid of all null checks." to fix server-only bug not allowing movement on client, e.g. in uMMORPG

This reverts commit e8d9d85.
  • Loading branch information
miwarnec committed Apr 24, 2019
1 parent 0484a50 commit f56507f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Assets/Mirror/Editor/NetworkIdentityEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override void OnInspectorGUI()

EditorGUILayout.Separator();

if (networkIdentity.observers.Count > 0)
if (networkIdentity.observers != null && networkIdentity.observers.Count > 0)
{
showObservers = EditorGUILayout.Foldout(showObservers, "Observers");
if (showObservers)
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirror/Editor/NetworkInformationPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public override void OnPreviewGUI(Rect r, GUIStyle background)
lastY = behaviourRect.y;
}

if (identity.observers.Count > 0)
if (identity.observers != null && identity.observers.Count > 0)
{
Rect observerRect = new Rect(initialX, lastY + 10, 200, 20);

Expand Down
28 changes: 20 additions & 8 deletions Assets/Mirror/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public bool isServer
public bool isLocalPlayer { get; private set; }
public bool hasAuthority { get; private set; }

// <connectionId, NetworkConnection>. always empty on clients.
public Dictionary<int, NetworkConnection> observers = new Dictionary<int, NetworkConnection>();
// <connectionId, NetworkConnection>
public Dictionary<int, NetworkConnection> observers;

public uint netId { get; internal set; }
public ulong sceneId => m_SceneId;
Expand Down Expand Up @@ -150,7 +150,7 @@ internal void SetNotLocalPlayer()
// this is used when a connection is destroyed, since the "observers" property is read-only
internal void RemoveObserverInternal(NetworkConnection conn)
{
observers.Remove(conn.connectionId);
observers?.Remove(conn.connectionId);
}

void Awake()
Expand Down Expand Up @@ -401,7 +401,7 @@ internal void OnStartServer(bool allowNonZeroNetId)
m_IsServer = true;
hasAuthority = !localPlayerAuthority;

observers.Clear();
observers = new Dictionary<int, NetworkConnection>();

// If the instance/net ID is invalid here then this is an object instantiated from a prefab and the server should assign a valid ID
if (netId == 0)
Expand Down Expand Up @@ -778,15 +778,24 @@ internal void OnNetworkDestroy()

internal void ClearObservers()
{
foreach (NetworkConnection conn in observers.Values)
if (observers != null)
{
conn.RemoveFromVisList(this, true);
foreach (NetworkConnection conn in observers.Values)
{
conn.RemoveFromVisList(this, true);
}
observers.Clear();
}
observers.Clear();
}

internal void AddObserver(NetworkConnection conn)
{
if (observers == null)
{
Debug.LogError("AddObserver for " + gameObject + " observer list is null");
return;
}

if (observers.ContainsKey(conn.connectionId))
{
// if we try to add a connectionId that was already added, then
Expand All @@ -802,6 +811,9 @@ internal void AddObserver(NetworkConnection conn)

public void RebuildObservers(bool initialize)
{
if (observers == null)
return;

bool changed = false;
bool result = false;
HashSet<NetworkConnection> oldObservers = new HashSet<NetworkConnection>(observers.Values);
Expand Down Expand Up @@ -1016,7 +1028,7 @@ internal void MirrorUpdate()
{
// SendToReady sends to all observers. no need to serialize if we
// don't have any.
if (observers.Count == 0)
if (observers == null || observers.Count == 0)
return;

// serialize all the dirty components and send (if any were dirty)
Expand Down
8 changes: 4 additions & 4 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static bool SendToObservers(NetworkIdentity identity, short msgType, MessageBase
{
if (LogFilter.Debug) Debug.Log("Server.SendToObservers id:" + msgType);

if (identity != null)
if (identity != null && identity.observers != null)
{
// pack message into byte[] once
byte[] bytes = MessagePacker.PackMessage((ushort)msgType, msg);
Expand All @@ -190,7 +190,7 @@ static bool SendToObservers(NetworkIdentity identity, short msgType, MessageBase
{
if (LogFilter.Debug) Debug.Log("Server.SendToObservers id:" + typeof(T));

if (identity != null)
if (identity != null && identity.observers != null)
{
// pack message into byte[] once
byte[] bytes = MessagePacker.Pack(msg);
Expand Down Expand Up @@ -242,7 +242,7 @@ public static bool SendToReady(NetworkIdentity identity, short msgType, MessageB
{
if (LogFilter.Debug) Debug.Log("Server.SendToReady msgType:" + msgType);

if (identity != null)
if (identity != null && identity.observers != null)
{
// pack message into byte[] once
byte[] bytes = MessagePacker.PackMessage((ushort)msgType, msg);
Expand All @@ -265,7 +265,7 @@ public static bool SendToReady(NetworkIdentity identity, short msgType, MessageB
{
if (LogFilter.Debug) Debug.Log("Server.SendToReady msgType:" + typeof(T));

if (identity != null)
if (identity != null && identity.observers != null)
{
// pack message into byte[] once
byte[] bytes = MessagePacker.Pack(msg);
Expand Down

0 comments on commit f56507f

Please sign in to comment.