#if UNITY_2020_3_OR_NEWER && UNITY_EDITOR_WIN
using FishNet.CodeAnalysis.Annotations;
#endif
using FishNet.Connection;
using FishNet.Documenting;
using FishNet.Object.Synchronizing.Internal;
using UnityEngine;
namespace FishNet.Object
{
public abstract partial class NetworkBehaviour : MonoBehaviour
{
#region Public.
///
/// True if OnStartServer has been called.
///
[APIExclude]
public bool OnStartServerCalled { get; private set; }
///
/// True if OnStartClient has been called.
///
[APIExclude]
public bool OnStartClientCalled { get; private set; }
#endregion
#region Private.
///
/// True if OnStartNetwork has been called.
///
private bool _onStartNetworkCalled;
///
/// True if OnStopNetwork has been called.
///
private bool _onStopNetworkCalled;
#endregion
///
/// Invokes cached callbacks on SyncTypes which were held until OnStartXXXXX was called.
///
///
internal void InvokeSyncTypeCallbacks(bool asServer)
{
foreach (SyncBase item in _syncVars.Values)
item.OnStartCallback(asServer);
foreach (SyncBase item in _syncObjects.Values)
item.OnStartCallback(asServer);
}
///
/// Invokes the OnStart/StopNetwork.
///
///
internal void InvokeOnNetwork(bool start)
{
if (start)
{
if (_onStartNetworkCalled)
return;
OnStartNetwork();
}
else
{
if (_onStopNetworkCalled)
return;
OnStopNetwork();
}
}
///
/// Called when the network has initialized this object. May be called for server or client but will only be called once.
/// When as host or server this method will run before OnStartServer.
/// When as client only the method will run before OnStartClient.
///
#if UNITY_2020_3_OR_NEWER && UNITY_EDITOR_WIN
[OverrideMustCallBase(BaseCallMustBeFirstStatement = true)]
#endif
public virtual void OnStartNetwork()
{
_onStartNetworkCalled = true;
_onStopNetworkCalled = false;
}
///
/// Called when the network is deinitializing this object. May be called for server or client but will only be called once.
/// When as host or server this method will run after OnStopServer.
/// When as client only this method will run after OnStopClient.
///
#if UNITY_2020_3_OR_NEWER && UNITY_EDITOR_WIN
[OverrideMustCallBase(BaseCallMustBeFirstStatement = true)]
#endif
public virtual void OnStopNetwork()
{
_onStopNetworkCalled = true;
_onStartNetworkCalled = false;
}
///
/// Called on the server after initializing this object.
/// SyncTypes modified before or during this method will be sent to clients in the spawn message.
///
#if UNITY_2020_3_OR_NEWER && UNITY_EDITOR_WIN
[OverrideMustCallBase(BaseCallMustBeFirstStatement = true)]
#endif
public virtual void OnStartServer()
{
OnStartServerCalled = true;
}
///
/// Called on the server before deinitializing this object.
///
#if UNITY_2020_3_OR_NEWER && UNITY_EDITOR_WIN
[OverrideMustCallBase(BaseCallMustBeFirstStatement = true)]
#endif
public virtual void OnStopServer()
{
OnStartServerCalled = false;
ReturnRpcLinks();
}
///
/// Called on the server after ownership has changed.
///
/// Previous owner of this object.
#if UNITY_2020_3_OR_NEWER && UNITY_EDITOR_WIN
[OverrideMustCallBase(BaseCallMustBeFirstStatement = true)]
#endif
public virtual void OnOwnershipServer(NetworkConnection prevOwner)
{
//When switching ownership always clear replicate cache on server.
ClearReplicateCache_Internal(true);
}
///
/// Called on the server after a spawn message for this object has been sent to clients.
/// Useful for sending remote calls or data to clients.
///
/// Connection the object is being spawned for.
#if UNITY_2020_3_OR_NEWER && UNITY_EDITOR_WIN
[OverrideMustCallBase(BaseCallMustBeFirstStatement = true)]
#endif
public virtual void OnSpawnServer(NetworkConnection connection) { }
///
/// Called on the server before a despawn message for this object has been sent to connection.
/// Useful for sending remote calls or actions to clients.
///
#if UNITY_2020_3_OR_NEWER && UNITY_EDITOR_WIN
[OverrideMustCallBase(BaseCallMustBeFirstStatement = true)]
#endif
public virtual void OnDespawnServer(NetworkConnection connection) { }
///
/// Called on the client after initializing this object.
///
#if UNITY_2020_3_OR_NEWER && UNITY_EDITOR_WIN
[OverrideMustCallBase(BaseCallMustBeFirstStatement = true)]
#endif
public virtual void OnStartClient()
{
OnStartClientCalled = true;
}
///
/// Called on the client before deinitializing this object.
///
#if UNITY_2020_3_OR_NEWER && UNITY_EDITOR_WIN
[OverrideMustCallBase(BaseCallMustBeFirstStatement = true)]
#endif
public virtual void OnStopClient()
{
OnStartClientCalled = false;
}
///
/// Called on the client after gaining or losing ownership.
///
/// Previous owner of this object.
#if UNITY_2020_3_OR_NEWER && UNITY_EDITOR_WIN
[OverrideMustCallBase(BaseCallMustBeFirstStatement = true)]
#endif
public virtual void OnOwnershipClient(NetworkConnection prevOwner)
{
//If losing or gaining ownership then clear replicate cache.
if (IsOwner || prevOwner == LocalConnection)
ClearReplicateCache_Internal(false);
}
}
}