Skip to content

Commit

Permalink
perf!: changing NetworkIdentity events to use new c# only events
Browse files Browse the repository at this point in the history
UnityEvent take a lot of time for unity to create when when an Object is Instantiated. using a list of c# events instead avoids this.

BREAKING CHANGE: Inspector events removed from NetworkIdentity, use Mirage v147.4.0 first to convert before updating to next version
  • Loading branch information
James-Frowen committed Mar 25, 2024
1 parent 7fa963b commit 3aaab68
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
28 changes: 14 additions & 14 deletions Assets/Mirage/Runtime/NetworkIdentity.cs
Expand Up @@ -410,13 +410,13 @@ internal ulong Editor_SceneId


[Header("Events")]
[SerializeField] private AddLateEvent _onStartServer = new AddLateEvent();
[SerializeField] private AddLateEvent _onStartClient = new AddLateEvent();
[SerializeField] private AddLateEvent _onStartLocalPlayer = new AddLateEvent();
[SerializeField] private BoolAddLateEvent _onAuthorityChanged = new BoolAddLateEvent();
[SerializeField] private NetworkPlayerAddLateEvent _onOwnerChanged = new NetworkPlayerAddLateEvent();
[SerializeField] private AddLateEvent _onStopClient = new AddLateEvent();
[SerializeField] private AddLateEvent _onStopServer = new AddLateEvent();
[SerializeField] private readonly AddLateEvent_new _onStartServer = new AddLateEvent_new();
[SerializeField] private readonly AddLateEvent_new _onStartClient = new AddLateEvent_new();
[SerializeField] private readonly AddLateEvent_new _onStartLocalPlayer = new AddLateEvent_new();
[SerializeField] private readonly AddLateEvent_new<bool> _onAuthorityChanged = new AddLateEvent_new<bool>();
[SerializeField] private readonly AddLateEvent_new<INetworkPlayer> _onOwnerChanged = new AddLateEvent_new<INetworkPlayer>();
[SerializeField] private readonly AddLateEvent_new _onStopClient = new AddLateEvent_new();
[SerializeField] private readonly AddLateEvent_new _onStopServer = new AddLateEvent_new();
private bool _clientStarted;
private bool _localPlayerStarted;
private bool _hadAuthority;
Expand All @@ -427,21 +427,21 @@ internal ulong Editor_SceneId
/// <para>This will be called for objects on a "host" as well as for object on a dedicated server.</para>
/// <para>OnStartServer is invoked before this object is added to collection of spawned objects</para>
/// </summary>
public IAddLateEvent OnStartServer => _onStartServer;
public IAddLateEvent_new OnStartServer => _onStartServer;

/// <summary>
/// Called on every NetworkBehaviour when it is activated on a client.
/// <para>Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized
/// correctly with the latest state from the server when this function is called on the client.</para>
/// </summary>
public IAddLateEvent OnStartClient => _onStartClient;
public IAddLateEvent_new OnStartClient => _onStartClient;

/// <summary>
/// Called when the local player object has been set up.
/// <para>This happens after OnStartClient(), as it is triggered by an ownership message from the server. This is an appropriate place to activate components or
/// functionality that should only be active for the local player, such as cameras and input.</para>
/// </summary>
public IAddLateEvent OnStartLocalPlayer => _onStartLocalPlayer;
public IAddLateEvent_new OnStartLocalPlayer => _onStartLocalPlayer;

/// <summary>
/// This is invoked on behaviours that have authority given or removed, see <see cref="HasAuthority">NetworkIdentity.hasAuthority</see>
Expand All @@ -455,27 +455,27 @@ internal ulong Editor_SceneId
/// </para>
/// <para>NOTE: this even is only called for client and host</para>
/// </summary>
public IAddLateEvent<bool> OnAuthorityChanged => _onAuthorityChanged;
public IAddLateEvent_new<bool> OnAuthorityChanged => _onAuthorityChanged;

/// <summary>
/// This is invoked on behaviours that have an owner assigned.
/// <para>This even is only called on server</para>
/// <para>See <see cref="OnAuthorityChanged"/> for more comments on owner and authority</para>
/// </summary>
public IAddLateEvent<INetworkPlayer> OnOwnerChanged => _onOwnerChanged;
public IAddLateEvent_new<INetworkPlayer> OnOwnerChanged => _onOwnerChanged;

/// <summary>
/// This is invoked on clients when the server has caused this object to be destroyed.
/// <para>This can be used as a hook to invoke effects or do client specific cleanup.</para>
/// </summary>
///<summary>Called on clients when the server destroys the GameObject.</summary>
public IAddLateEvent OnStopClient => _onStopClient;
public IAddLateEvent_new OnStopClient => _onStopClient;

/// <summary>
/// This is called on the server when the object is unspawned
/// </summary>
/// <remarks>Can be used as hook to save player information</remarks>
public IAddLateEvent OnStopServer => _onStopServer;
public IAddLateEvent_new OnStopServer => _onStopServer;

/// <summary>
/// this is used when a connection is destroyed, since the "observers" property is read-only
Expand Down
22 changes: 10 additions & 12 deletions Assets/Tests/Editor/NetworkIdentityCallbackTests.cs
Expand Up @@ -3,11 +3,9 @@
using Cysharp.Threading.Tasks;
using Mirage.Serialization;
using Mirage.Tests.EnterRuntime;
using Mirage.Tests.Runtime;
using NSubstitute;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Events;

namespace Mirage.Tests.NetworkIdentityCallbacks
{
Expand Down Expand Up @@ -142,8 +140,8 @@ protected override async UniTask ExtraSetup()
public void OnStartServerTest()
{
// lets add a component to check OnStartserver
var func1 = Substitute.For<UnityAction>();
var func2 = Substitute.For<UnityAction>();
var func1 = Substitute.For<Action>();
var func2 = Substitute.For<Action>();

identity.OnStartServer.AddListener(func1);
identity.OnStartServer.AddListener(func2);
Expand Down Expand Up @@ -244,7 +242,7 @@ public void RemoveObserverInternal()
public void OnStartServerCallsComponentsAndCatchesExceptions()
{
// make a mock delegate
var func = Substitute.For<UnityAction>();
var func = Substitute.For<Action>();

// add it to the listener
identity.OnStartServer.AddListener(func);
Expand All @@ -271,7 +269,7 @@ public void OnStartServerCallsComponentsAndCatchesExceptions()
public void OnStartClientCallsComponentsAndCatchesExceptions()
{
// add component
var func = Substitute.For<UnityAction>();
var func = Substitute.For<Action>();
identity.OnStartClient.AddListener(func);

func
Expand All @@ -297,7 +295,7 @@ public void OnStartClientCallsComponentsAndCatchesExceptions()
public void OnAuthorityChangedCallsComponentsAndCatchesExceptions()
{
// add component
var func = Substitute.For<UnityAction<bool>>();
var func = Substitute.For<Action<bool>>();
identity.OnAuthorityChanged.AddListener(func);

func
Expand Down Expand Up @@ -484,8 +482,8 @@ public void OnDeserializeSafelyShouldDetectAndHandleDeSerializationMismatch()
public void OnStartLocalPlayer()
{
// add components
var funcEx = Substitute.For<UnityAction>();
var func = Substitute.For<UnityAction>();
var funcEx = Substitute.For<Action>();
var func = Substitute.For<Action>();

identity.OnStartLocalPlayer.AddListener(funcEx);
identity.OnStartLocalPlayer.AddListener(func);
Expand Down Expand Up @@ -521,7 +519,7 @@ public void OnStartLocalPlayer()
[Test]
public void OnStopClient()
{
var mockCallback = Substitute.For<UnityAction>();
var mockCallback = Substitute.For<Action>();
identity.OnStopClient.AddListener(mockCallback);

identity.StopClient();
Expand All @@ -532,7 +530,7 @@ public void OnStopClient()
[Test]
public void OnStopServer()
{
var mockCallback = Substitute.For<UnityAction>();
var mockCallback = Substitute.For<Action>();
identity.OnStopServer.AddListener(mockCallback);

identity.StopServer();
Expand All @@ -543,7 +541,7 @@ public void OnStopServer()
[Test]
public void OnStopServerEx()
{
var mockCallback = Substitute.For<UnityAction>();
var mockCallback = Substitute.For<Action>();
mockCallback
.When(f => f.Invoke())
.Do(f => { throw new Exception("Some exception"); });
Expand Down
7 changes: 3 additions & 4 deletions Assets/Tests/Runtime/Host/NetworkIdentityTests.cs
Expand Up @@ -4,7 +4,6 @@
using NSubstitute;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.TestTools;
using InvalidOperationException = System.InvalidOperationException;

Expand All @@ -15,7 +14,7 @@ public class NetworkIdentityTests : HostSetup
private GameObject gameObject;
private NetworkIdentity testIdentity;

protected async override UniTask ExtraSetup()
protected override async UniTask ExtraSetup()
{
await base.ExtraSetup();
testIdentity = CreateNetworkIdentity();
Expand Down Expand Up @@ -169,7 +168,7 @@ public void RemoveClientAuthority()
{
serverObjectManager.Spawn(gameObject);
var mockHandler = Substitute.For<UnityAction>();
var mockHandler = Substitute.For<Action>();
testIdentity.OnStopServer.AddListener(mockHandler);
serverObjectManager.Destroy(gameObject, false);
Expand Down Expand Up @@ -228,7 +227,7 @@ protected override UniTask LateSetup()
[UnityTest]
public IEnumerator ClientNotNullAfterSpawnInStarted() => UniTask.ToCoroutine(async () =>
{
await AsyncUtil.WaitUntilWithTimeout(() => (testIdentity.Client as NetworkClient) == client);
await AsyncUtil.WaitUntilWithTimeout(() => testIdentity.Client == client);
});
}
}

0 comments on commit 3aaab68

Please sign in to comment.