NetworkServer.activeHost to replace .localClientActive

This commit is contained in:
vis2k 2022-12-12 15:35:42 +01:00
parent 6088591269
commit a4f3872a01
4 changed files with 22 additions and 18 deletions

View File

@ -412,7 +412,7 @@ protected void SendTargetRPCInternal(NetworkConnection conn, string functionFull
// {
// int oldValue = health;
// SetSyncVar(value, ref health, 1uL);
// if (NetworkServer.localClientActive && !GetSyncVarHookGuard(1uL))
// if (NetworkServer.activeHost && !GetSyncVarHookGuard(1uL))
// {
// SetSyncVarHookGuard(1uL, value: true);
// OnChanged(oldValue, value);
@ -436,7 +436,7 @@ public void GeneratedSyncVarSetter<T>(T value, ref T field, ulong dirtyBit, Acti
// in client-only mode, OnDeserialize would call it.
// we use hook guard to protect against deadlock where hook
// changes syncvar, calling hook again.
if (NetworkServer.localClientActive && !GetSyncVarHookGuard(dirtyBit))
if (NetworkServer.activeHost && !GetSyncVarHookGuard(dirtyBit))
{
SetSyncVarHookGuard(dirtyBit, true);
OnChanged(oldValue, value);
@ -463,7 +463,7 @@ public void GeneratedSyncVarSetter_GameObject(GameObject value, ref GameObject f
// in client-only mode, OnDeserialize would call it.
// we use hook guard to protect against deadlock where hook
// changes syncvar, calling hook again.
if (NetworkServer.localClientActive && !GetSyncVarHookGuard(dirtyBit))
if (NetworkServer.activeHost && !GetSyncVarHookGuard(dirtyBit))
{
SetSyncVarHookGuard(dirtyBit, true);
OnChanged(oldValue, value);
@ -490,7 +490,7 @@ public void GeneratedSyncVarSetter_NetworkIdentity(NetworkIdentity value, ref Ne
// in client-only mode, OnDeserialize would call it.
// we use hook guard to protect against deadlock where hook
// changes syncvar, calling hook again.
if (NetworkServer.localClientActive && !GetSyncVarHookGuard(dirtyBit))
if (NetworkServer.activeHost && !GetSyncVarHookGuard(dirtyBit))
{
SetSyncVarHookGuard(dirtyBit, true);
OnChanged(oldValue, value);
@ -518,7 +518,7 @@ public void GeneratedSyncVarSetter_NetworkBehaviour<T>(T value, ref T field, ulo
// in client-only mode, OnDeserialize would call it.
// we use hook guard to protect against deadlock where hook
// changes syncvar, calling hook again.
if (NetworkServer.localClientActive && !GetSyncVarHookGuard(dirtyBit))
if (NetworkServer.activeHost && !GetSyncVarHookGuard(dirtyBit))
{
SetSyncVarHookGuard(dirtyBit, true);
OnChanged(oldValue, value);

View File

@ -36,7 +36,8 @@ public static partial class NetworkServer
public static NetworkConnectionToClient localConnection { get; private set; }
/// <summary>True is a local client is currently active on the server</summary>
public static bool localClientActive => localConnection != null;
[Obsolete("NetworkServer.localClientActive was renamed to .activeHost to be more obvious")] // DEPRECATED 2022-12-12
public static bool localClientActive => activeHost;
/// <summary>Dictionary of all server connections, with connectionId as key</summary>
public static Dictionary<int, NetworkConnectionToClient> connections =
@ -55,9 +56,12 @@ public static partial class NetworkServer
// see also: https://github.com/vis2k/Mirror/pull/2595
public static bool dontListen;
/// <summary>active checks if the server has been started</summary>
/// <summary>active checks if the server has been started either has standalone or as host server.</summary>
public static bool active { get; internal set; }
/// <summary>active checks if the server has been started in host mode.</summary>
public static bool activeHost => localConnection != null;
// scene loading
public static bool isLoadingScene;
@ -1439,7 +1443,7 @@ static void DestroyObject(NetworkIdentity identity, DestroyMode mode)
identity.ClearObservers();
// in host mode, call OnStopClient/OnStopLocalPlayer manually
if (NetworkClient.active && localClientActive)
if (NetworkClient.active && activeHost)
{
if (identity.isLocalPlayer)
identity.OnStopLocalPlayer();

View File

@ -223,15 +223,15 @@ public void RemoveLocalConnection()
}
[Test]
public void LocalClientActive()
public void ActiveHost()
{
// listen
NetworkServer.Listen(1);
Assert.That(NetworkServer.localClientActive, Is.False);
Assert.That(NetworkServer.activeHost, Is.False);
// set local connection
NetworkServer.SetLocalConnection(new LocalConnectionToClient());
Assert.That(NetworkServer.localClientActive, Is.True);
Assert.That(NetworkServer.activeHost, Is.True);
}
[Test]
@ -1181,7 +1181,7 @@ public void ShutdownCleanup()
Assert.That(NetworkServer.spawned.Count, Is.EqualTo(0));
Assert.That(NetworkServer.localConnection, Is.Null);
Assert.That(NetworkServer.localClientActive, Is.False);
Assert.That(NetworkServer.activeHost, Is.False);
Assert.That(NetworkServer.OnConnectedEvent, Is.Null);
Assert.That(NetworkServer.OnDisconnectedEvent, Is.Null);

View File

@ -32,8 +32,8 @@ public void StaticMethod_HookCalledFromSyncVarSetter()
const int serverValue = 24;
// hooks setters are only called if localClientActive
Assert.That(NetworkServer.localClientActive, Is.True);
// hooks setters are only called if activeHost
Assert.That(NetworkServer.activeHost, Is.True);
int hookcallCount = 0;
StaticHookBehaviour.HookCalled += (oldValue, newValue) =>
@ -60,8 +60,8 @@ public void VirtualHook_HookCalledWhenSyncingChangedValued()
const int serverValue = 24;
// hooks setters are only called if localClientActive
Assert.That(NetworkServer.localClientActive, Is.True);
// hooks setters are only called if activeHost
Assert.That(NetworkServer.activeHost, Is.True);
int baseCallCount = 0;
comp.BaseHookCalled += (oldValue, newValue) =>
@ -88,8 +88,8 @@ public void VirtualOverrideHook_HookCalledWhenSyncingChangedValued()
const int serverValue = 24;
// hooks setters are only called if localClientActive
Assert.That(NetworkServer.localClientActive, Is.True);
// hooks setters are only called if activeHost
Assert.That(NetworkServer.activeHost, Is.True);
// hook should change it on client
int overrideCallCount = 0;