Skip to content

Commit

Permalink
refactor: changing indexer to try get (#720)
Browse files Browse the repository at this point in the history
* refactor: changing indexer to try get

It would be better to return bool if not found to avoid doing null check

BREAKING CHANGE: ObjectLocator now has TryGet method instead of indexer that returns null

* updating uses of objectLocator

* fixing names not being the same
  • Loading branch information
James-Frowen authored Mar 24, 2021
1 parent ff95634 commit 01ca9bb
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 39 deletions.
8 changes: 2 additions & 6 deletions Assets/Mirage/Runtime/ClientObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -603,13 +603,9 @@ private void OnServerRpcReply(INetworkPlayer player, ServerRpcReply reply)
private readonly Dictionary<int, Action<NetworkReader>> callbacks = new Dictionary<int, Action<NetworkReader>>();
private int replyId;

public NetworkIdentity this[uint netId]
public bool TryGetIdentity(uint netId, out NetworkIdentity identity)
{
get
{
SpawnedObjects.TryGetValue(netId, out NetworkIdentity identity);
return identity;
}
return SpawnedObjects.TryGetValue(netId, out identity) && identity != null;
}

/// <summary>
Expand Down
15 changes: 6 additions & 9 deletions Assets/Mirage/Runtime/GameObjectSyncvar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Mirage.Serialization;
using Mirage.Serialization;
using UnityEngine;

namespace Mirage
Expand Down Expand Up @@ -28,11 +28,9 @@ public GameObject Value
if (gameObject != null)
return gameObject;

if (objectLocator != null)
if (objectLocator != null && objectLocator.TryGetIdentity(NetId, out NetworkIdentity result))
{
NetworkIdentity result = objectLocator[netId];
if (result != null)
return result.gameObject;
return result.gameObject;
}

return null;
Expand Down Expand Up @@ -60,15 +58,14 @@ public static GameObjectSyncvar ReadGameObjectSyncVar(this NetworkReader reader)
uint netId = reader.ReadPackedUInt32();

NetworkIdentity identity = null;
if (!(reader.ObjectLocator is null))
identity = reader.ObjectLocator[netId];
bool hasValue = reader.ObjectLocator?.TryGetIdentity(netId, out identity) ?? false;

return new GameObjectSyncvar
{
objectLocator = reader.ObjectLocator,
netId = netId,
gameObject = identity != null ? identity.gameObject : null
gameObject = hasValue ? identity.gameObject : null
};
}
}
}
}
5 changes: 3 additions & 2 deletions Assets/Mirage/Runtime/IObjectLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public interface IObjectLocator
/// Finds a network identity by id
/// </summary>
/// <param name="netId">the id of the object to find</param>
/// <returns>The NetworkIdentity matching the netid or null if none is found</returns>
NetworkIdentity this[uint netId] { get; }
/// <param name="identity">The NetworkIdentity matching the netid or null if none is found</param>
/// <returns>true if identity is found and is not null</returns>
bool TryGetIdentity(uint netId, out NetworkIdentity identity);
}
}
16 changes: 7 additions & 9 deletions Assets/Mirage/Runtime/NetworkBehaviorSyncvar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Mirage.Serialization;
using Mirage.Serialization;

namespace Mirage
{
Expand Down Expand Up @@ -29,13 +29,12 @@ public NetworkBehaviour Value
if (component != null)
return component;

if (objectLocator != null)
if (objectLocator != null && objectLocator.TryGetIdentity(NetId, out NetworkIdentity result))
{
NetworkIdentity identity = objectLocator[netId];
if (identity != null)
return identity.NetworkBehaviours[componentId];
return result.NetworkBehaviours[componentId];
}


return null;
}

Expand Down Expand Up @@ -66,16 +65,15 @@ public static NetworkBehaviorSyncvar ReadNetworkBehaviourSyncVar(this NetworkRea
int componentId = reader.ReadPackedInt32();

NetworkIdentity identity = null;
if (!(reader.ObjectLocator is null))
identity = reader.ObjectLocator[netId];
bool hasValue = reader.ObjectLocator?.TryGetIdentity(netId, out identity) ?? false;

return new NetworkBehaviorSyncvar
{
objectLocator = reader.ObjectLocator,
netId = netId,
componentId = componentId,
component = identity != null ? identity.NetworkBehaviours[componentId] : null
component = hasValue ? identity.NetworkBehaviours[componentId] : null
};
}
}
}
}
11 changes: 5 additions & 6 deletions Assets/Mirage/Runtime/NetworkIdentitySyncvar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Mirage.Serialization;
using Mirage.Serialization;

namespace Mirage
{
Expand Down Expand Up @@ -27,9 +27,9 @@ public NetworkIdentity Value
if (identity != null)
return identity;

if (objectLocator != null)
if (objectLocator != null && objectLocator.TryGetIdentity(NetId, out NetworkIdentity result))
{
return objectLocator[netId];
return result;
}

return null;
Expand Down Expand Up @@ -57,8 +57,7 @@ public static NetworkIdentitySyncvar ReadNetworkIdentitySyncVar(this NetworkRead
uint netId = reader.ReadPackedUInt32();

NetworkIdentity identity = null;
if (!(reader.ObjectLocator is null))
identity = reader.ObjectLocator[netId];
reader.ObjectLocator?.TryGetIdentity(netId, out identity);

return new NetworkIdentitySyncvar
{
Expand All @@ -68,4 +67,4 @@ public static NetworkIdentitySyncvar ReadNetworkIdentitySyncVar(this NetworkRead
};
}
}
}
}
5 changes: 4 additions & 1 deletion Assets/Mirage/Runtime/Serialization/NetworkReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,10 @@ public static NetworkIdentity ReadNetworkIdentity(this NetworkReader reader)

if (reader.ObjectLocator != null)
{
return reader.ObjectLocator[netId];
// if not found return c# null
return reader.ObjectLocator.TryGetIdentity(netId, out NetworkIdentity identity)
? identity
: null;
}

if (logger.WarnEnabled()) logger.LogFormat(LogType.Warning, "ReadNetworkIdentity netId:{0} not found in spawned", netId);
Expand Down
8 changes: 2 additions & 6 deletions Assets/Mirage/Runtime/ServerObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,9 @@ public class ServerObjectManager : MonoBehaviour, IServerObjectManager, IObjectL
public readonly HashSet<NetworkIdentity> DirtyObjects = new HashSet<NetworkIdentity>();
private readonly List<NetworkIdentity> DirtyObjectsTmp = new List<NetworkIdentity>();

public NetworkIdentity this[uint netId]
public bool TryGetIdentity(uint netId, out NetworkIdentity identity)
{
get
{
SpawnedObjects.TryGetValue(netId, out NetworkIdentity identity);
return identity;
}
return SpawnedObjects.TryGetValue(netId, out identity) && identity != null;
}

public void Start()
Expand Down

0 comments on commit 01ca9bb

Please sign in to comment.