Skip to content

Commit

Permalink
refactor: NetIdentity to Identity
Browse files Browse the repository at this point in the history
matches NetworkPlayer.Identity and makes it faster to type

BREAKING CHANGE: NetIdentity renamed to Identity
  • Loading branch information
James-Frowen committed Sep 5, 2021
1 parent 5493eae commit dc00532
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions Assets/Mirage/Components/LobbyReady.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public void SendToReady<T>(NetworkIdentity identity, T msg, bool includeOwner =

foreach (ObjectReady objectReady in ObjectReadyList)
{
bool isOwner = objectReady.NetIdentity == identity;
bool isOwner = objectReady.Identity == identity;
if ((!isOwner || includeOwner) && objectReady.IsReady)
{
playerCache.Add(objectReady.NetIdentity.Owner);
playerCache.Add(objectReady.Identity.Owner);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage/Components/NetworkAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ bool SendMessagesAllowed
//
// So we check here for a connectionToClient and if it is null we will
// let the server send animation data until we receive an owner.
if (NetIdentity != null && NetIdentity.Owner == null)
if (Identity != null && Identity.Owner == null)
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions Assets/Mirage/Components/NetworkProximityChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ public class NetworkProximityChecker : NetworkVisibility

public void Awake()
{
NetIdentity.OnStartServer.AddListener(() =>
Identity.OnStartServer.AddListener(() =>
{
InvokeRepeating(nameof(RebuildObservers), 0, VisibilityUpdateInterval);
});

NetIdentity.OnStopServer.AddListener(() =>
Identity.OnStopServer.AddListener(() =>
{
CancelInvoke(nameof(RebuildObservers));
});
}

void RebuildObservers()
{
NetIdentity.RebuildObservers(false);
Identity.RebuildObservers(false);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions Assets/Mirage/Components/NetworkSceneChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void OnStartServer()
if (!sceneCheckerObjects.ContainsKey(currentScene))
sceneCheckerObjects.Add(currentScene, new HashSet<NetworkIdentity>());

sceneCheckerObjects[currentScene].Add(NetIdentity);
sceneCheckerObjects[currentScene].Add(Identity);
}

[Server(error = false)]
Expand All @@ -57,7 +57,7 @@ void Update()
// and the new scene need to rebuild their respective observers lists.

// Remove this object from the hashset of the scene it just left
sceneCheckerObjects[currentScene].Remove(NetIdentity);
sceneCheckerObjects[currentScene].Remove(Identity);

// RebuildObservers of all NetworkIdentity's in the scene this object just left
RebuildSceneObservers();
Expand All @@ -70,7 +70,7 @@ void Update()
sceneCheckerObjects.Add(currentScene, new HashSet<NetworkIdentity>());

// Add this object to the hashset of the new scene
sceneCheckerObjects[currentScene].Add(NetIdentity);
sceneCheckerObjects[currentScene].Add(Identity);

// RebuildObservers of all NetworkIdentity's in the scene this object just entered
RebuildSceneObservers();
Expand Down
48 changes: 24 additions & 24 deletions Assets/Mirage/Runtime/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ public abstract class NetworkBehaviour : MonoBehaviour
/// Returns true if this object is active on an active server.
/// <para>This is only true if the object has been spawned. This is different from NetworkServer.active, which is true if the server itself is active rather than this object being active.</para>
/// </summary>
public bool IsServer => NetIdentity.IsServer;
public bool IsServer => Identity.IsServer;

/// <summary>
/// Returns true if running as a client and this object was spawned by a server.
/// </summary>
public bool IsClient => NetIdentity.IsClient;
public bool IsClient => Identity.IsClient;

/// <summary>
/// Returns true if we're on host mode.
/// </summary>
public bool IsLocalClient => NetIdentity.IsLocalClient;
public bool IsLocalClient => Identity.IsLocalClient;

/// <summary>
/// This returns true if this object is the one that represents the player on the local machine.
/// <para>In multiplayer games, there are multiple instances of the Player object. The client needs to know which one is for "themselves" so that only that player processes input and potentially has a camera attached. The IsLocalPlayer function will return true only for the player instance that belongs to the player on the local machine, so it can be used to filter out input for non-local players.</para>
/// </summary>
public bool IsLocalPlayer => NetIdentity.IsLocalPlayer;
public bool IsLocalPlayer => Identity.IsLocalPlayer;

/// <summary>
/// True if this object only exists on the server
Expand All @@ -81,40 +81,40 @@ public abstract class NetworkBehaviour : MonoBehaviour
/// This returns true if this object is the authoritative version of the object in the distributed network application.
/// <para>The <see cref="NetworkIdentity.HasAuthority">NetworkIdentity.hasAuthority</see> value on the NetworkIdentity determines how authority is determined. For most objects, authority is held by the server. For objects with <see cref="NetworkIdentity.HasAuthority">NetworkIdentity.hasAuthority</see> set, authority is held by the client of that player.</para>
/// </summary>
public bool HasAuthority => NetIdentity.HasAuthority;
public bool HasAuthority => Identity.HasAuthority;

/// <summary>
/// The unique network Id of this object.
/// <para>This is assigned at runtime by the network server and will be unique for all objects for that network session.</para>
/// </summary>
public uint NetId => NetIdentity.NetId;
public uint NetId => Identity.NetId;

/// <summary>
/// The <see cref="NetworkServer">NetworkClient</see> associated to this object.
/// </summary>
public INetworkServer Server => NetIdentity.Server;
public INetworkServer Server => Identity.Server;

/// <summary>
/// Quick Reference to the NetworkIdentities ServerObjectManager. Present only for server/host instances.
/// </summary>
public ServerObjectManager ServerObjectManager => NetIdentity.ServerObjectManager;
public ServerObjectManager ServerObjectManager => Identity.ServerObjectManager;

/// <summary>
/// The <see cref="NetworkClient">NetworkClient</see> associated to this object.
/// </summary>
public INetworkClient Client => NetIdentity.Client;
public INetworkClient Client => Identity.Client;

/// <summary>
/// Quick Reference to the NetworkIdentities ClientObjectManager. Present only for instances instances.
/// </summary>
public ClientObjectManager ClientObjectManager => NetIdentity.ClientObjectManager;
public ClientObjectManager ClientObjectManager => Identity.ClientObjectManager;

/// <summary>
/// The <see cref="NetworkPlayer"/> associated with this <see cref="NetworkIdentity" /> This is only valid for player objects on the server.
/// </summary>
public INetworkPlayer Owner => NetIdentity.Owner;
public INetworkPlayer Owner => Identity.Owner;

public NetworkWorld World => NetIdentity.World;
public NetworkWorld World => Identity.World;

/// <summary>
/// Returns the appropriate NetworkTime instance based on if this NetworkBehaviour is running as a Server or Client.
Expand Down Expand Up @@ -145,35 +145,35 @@ protected internal void SetSyncVarHookGuard(ulong dirtyBit, bool value)
/// <summary>
/// NetworkIdentity component caching for easier access
/// </summary>
NetworkIdentity netIdentityCache;
NetworkIdentity _identity;

/// <summary>
/// Returns the NetworkIdentity of this object
/// </summary>
public NetworkIdentity NetIdentity
public NetworkIdentity Identity
{
get
{
// in this specific case, we want to know if we have set it before
// so we can compare if the reference is null
// instead of calling unity's MonoBehaviour == operator
if (netIdentityCache is null)
if (_identity is null)
{
// GetComponentInParent doesn't works on disabled gameObject
// and GetComponentsInParent(false)[0] isn't allocation free, so
// we just drop child support in this specific case
if (gameObject.activeSelf)
netIdentityCache = GetComponentInParent<NetworkIdentity>();
_identity = GetComponentInParent<NetworkIdentity>();
else
netIdentityCache = GetComponent<NetworkIdentity>();
_identity = GetComponent<NetworkIdentity>();

// do this 2nd check inside first if so that we are not checking == twice on unity Object
if (netIdentityCache is null)
if (_identity is null)
{
logger.LogError("There is no NetworkIdentity on " + name + ". Please add one.");
}
}
return netIdentityCache;
return _identity;
}
}

Expand All @@ -189,9 +189,9 @@ public int ComponentIndex
return componentIndex.Value;

// note: FindIndex causes allocations, we search manually instead
for (int i = 0; i < NetIdentity.NetworkBehaviours.Length; i++)
for (int i = 0; i < Identity.NetworkBehaviours.Length; i++)
{
NetworkBehaviour component = NetIdentity.NetworkBehaviours[i];
NetworkBehaviour component = Identity.NetworkBehaviours[i];
if (component == this)
{
componentIndex = i;
Expand Down Expand Up @@ -219,7 +219,7 @@ private void SyncObject_OnChange()
{
if (IsServer)
{
Server.SyncVarSender.AddDirtyObject(NetIdentity);
Server.SyncVarSender.AddDirtyObject(Identity);
}
}

Expand Down Expand Up @@ -318,7 +318,7 @@ protected internal void SendRpcInternal(Type invokeClass, string rpcName, Networ
// The public facing parameter is excludeOwner in [ClientRpc]
// so we negate it here to logically align with SendToReady.
bool includeOwner = !excludeOwner;
NetIdentity.SendToRemoteObservers(message, includeOwner, channelId);
Identity.SendToRemoteObservers(message, includeOwner, channelId);
}

protected internal void SendTargetRpcInternal(INetworkPlayer player, Type invokeClass, string rpcName, NetworkWriter writer, int channelId)
Expand Down Expand Up @@ -376,7 +376,7 @@ public void SetDirtyBit(ulong dirtyBit)
{
SyncVarDirtyBits |= dirtyBit;
if (IsServer)
Server.SyncVarSender.AddDirtyObject(NetIdentity);
Server.SyncVarSender.AddDirtyObject(Identity);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void WriteNetworkBehaviour(this NetworkWriter writer, NetworkBehav
writer.WriteNetworkIdentity(null);
return;
}
writer.WriteNetworkIdentity(value.NetIdentity);
writer.WriteNetworkIdentity(value.Identity);
writer.WriteByte((byte)value.ComponentIndex);
}
public static void WriteGameObject(this NetworkWriter writer, GameObject value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class PlayerController : NetworkBehaviour

void Awake()
{
NetIdentity.OnStartLocalPlayer.AddListener(OnStartLocalPlayer);
Identity.OnStartLocalPlayer.AddListener(OnStartLocalPlayer);
}

void OnValidate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class RandomColor : NetworkBehaviour
{
void Awake()
{
NetIdentity.OnStartServer.AddListener(OnStartServer);
Identity.OnStartServer.AddListener(OnStartServer);
}

public void OnStartServer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void Start()

void Update()
{
if (IsServer && NetIdentity.observers.Count > 0)
if (IsServer && Identity.observers.Count > 0)
ShootNearestPlayer();

if (IsClient)
Expand All @@ -36,7 +36,7 @@ void ShootNearestPlayer()
GameObject target = null;
float distance = 100f;

foreach (INetworkPlayer networkConnection in NetIdentity.observers)
foreach (INetworkPlayer networkConnection in Identity.observers)
{
GameObject tempTarget = networkConnection.Identity.gameObject;
float tempDistance = Vector3.Distance(tempTarget.transform.position, transform.position);
Expand Down
6 changes: 3 additions & 3 deletions Assets/Mirage/Samples~/Basic/Scripts/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ private static int GetNextPlayerId()

void Awake()
{
NetIdentity.OnStartServer.AddListener(OnStartServer);
NetIdentity.OnStartClient.AddListener(OnStartClient);
NetIdentity.OnStartLocalPlayer.AddListener(OnStartLocalPlayer);
Identity.OnStartServer.AddListener(OnStartServer);
Identity.OnStartClient.AddListener(OnStartClient);
Identity.OnStartLocalPlayer.AddListener(OnStartLocalPlayer);
}

// This is called by the hook of playerData SyncVar above
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage/Samples~/Pong/Scripts/Ball.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Ball : NetworkBehaviour

void Awake()
{
NetIdentity.OnStartServer.AddListener(OnStartServer);
Identity.OnStartServer.AddListener(OnStartServer);
}

public void OnStartServer()
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage/Samples~/Tanks/Scripts/Projectile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Projectile : NetworkBehaviour

void Awake()
{
NetIdentity.OnStartServer.AddListener(OnStartServer);
Identity.OnStartServer.AddListener(OnStartServer);
}

[Header("Game Stats")]
Expand Down
4 changes: 2 additions & 2 deletions Assets/Tests/Performance/Runtime/10KL/Scripts/Health.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class Health : NetworkBehaviour
[SyncVar] public int health = 10;
private void Awake()
{
NetIdentity.OnStartServer.AddListener(OnStartServer);
NetIdentity.OnStopServer.AddListener(OnStopServer);
Identity.OnStartServer.AddListener(OnStartServer);
Identity.OnStopServer.AddListener(OnStopServer);
}

public void OnStartServer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class MonsterBehavior : NetworkBehaviour

public void Awake()
{
NetIdentity.OnStartServer.AddListener(StartServer);
NetIdentity.OnStopServer.AddListener(StopServer);
Identity.OnStartServer.AddListener(StartServer);
Identity.OnStopServer.AddListener(StopServer);
}

private void StopServer()
Expand Down
4 changes: 2 additions & 2 deletions Assets/Tests/Runtime/SyncVarTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void TestSyncIntervalAndClearDirtyComponents()

// ClearDirtyComponents should do nothing since syncInterval is not
// elapsed yet
player.NetIdentity.ClearDirtyComponentsDirtyBits();
player.Identity.ClearDirtyComponentsDirtyBits();

// set lastSyncTime far enough back to be ready for syncing
player.lastSyncTime = Time.time - player.syncInterval;
Expand Down Expand Up @@ -111,7 +111,7 @@ public void TestSyncIntervalAndClearAllComponents()

// ClearAllComponents should clear dirty even if syncInterval not
// elapsed yet
player.NetIdentity.ClearAllComponentsDirtyBits();
player.Identity.ClearAllComponentsDirtyBits();

// set lastSyncTime far enough back to be ready for syncing
player.lastSyncTime = Time.time - player.syncInterval;
Expand Down

0 comments on commit dc00532

Please sign in to comment.