using FishNet.CodeAnalysis.Annotations; using FishNet.Component.ColliderRollback; using FishNet.Connection; using FishNet.Managing; using FishNet.Managing.Client; using FishNet.Managing.Observing; using FishNet.Managing.Predicting; using FishNet.Managing.Scened; using FishNet.Managing.Server; using FishNet.Managing.Timing; using FishNet.Managing.Transporting; using FishNet.Observing; using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using UnityEngine; namespace FishNet.Object { public abstract partial class NetworkBehaviour : MonoBehaviour { #region Public. #region Obsoletes //Remove on v5 [Obsolete("Use IsClientOnlyInitialized. Note the difference between IsClientOnlyInitialized and IsClientOnlyStarted.")] public bool IsClientOnly => IsClientOnlyInitialized; [Obsolete("Use IsServerOnlyInitialized. Note the difference between IsServerOnlyInitialized and IsServerOnlyStarted.")] public bool IsServerOnly => IsServerOnlyInitialized; [Obsolete("Use IsHostInitialized. Note the difference between IsHostInitialized and IsHostStarted.")] public bool IsHost => IsHostInitialized; [Obsolete("Use IsClientInitialized. Note the difference between IsClientInitialized and IsClientStarted.")] public bool IsClient => IsClientInitialized; [Obsolete("Use IsServerInitialized. Note the difference between IsServerInitialized and IsServerStarted.")] public bool IsServer => IsServerInitialized; #endregion /// /// True if the NetworkObject for this NetworkBehaviour is deinitializing. /// public bool IsDeinitializing => _networkObjectCache.IsDeinitializing; /// /// NetworkManager for this object. /// public NetworkManager NetworkManager => _networkObjectCache.NetworkManager; /// /// ServerManager for this object. /// public ServerManager ServerManager => _networkObjectCache.ServerManager; /// /// ClientManager for this object. /// public ClientManager ClientManager => _networkObjectCache.ClientManager; /// /// ObserverManager for this object. /// public ObserverManager ObserverManager => _networkObjectCache.ObserverManager; /// /// TransportManager for this object. /// public TransportManager TransportManager => _networkObjectCache.TransportManager; /// /// TimeManager for this object. /// public TimeManager TimeManager => _networkObjectCache.TimeManager; /// /// SceneManager for this object. /// public SceneManager SceneManager => _networkObjectCache.SceneManager; /// /// PredictionManager for this object. /// public PredictionManager PredictionManager => _networkObjectCache.PredictionManager; /// /// RollbackManager for this object. /// public RollbackManager RollbackManager => _networkObjectCache.RollbackManager; /// /// NetworkObserver on this object. /// public NetworkObserver NetworkObserver => _networkObjectCache.NetworkObserver; /// /// True if this object has been initialized on the client side. /// This is set true right before client start callbacks and after stop callbacks. /// public bool IsClientInitialized => _networkObjectCache.IsClientInitialized; /// /// True if the client is started and authenticated. /// public bool IsClientStarted => _networkObjectCache.IsClientStarted; /// /// True if this object has been initialized only on the client side. /// This is set true right before server start callbacks and after stop callbacks. public bool IsClientOnlyInitialized => _networkObjectCache.IsClientOnlyInitialized; /// /// True if only the client is started and authenticated. /// public bool IsClientOnlyStarted => _networkObjectCache.IsClientOnlyStarted; /// /// True if this object has been initialized on the server side. /// This is set true right before server start callbacks and after stop callbacks. /// public bool IsServerInitialized => _networkObjectCache.IsServerInitialized; /// /// True if server is started. /// public bool IsServerStarted => _networkObjectCache.IsServerStarted; /// /// True if this object has been initialized only on the server side. /// This is set true right before server start callbacks and after stop callbacks. public bool IsServerOnlyInitialized => _networkObjectCache.IsServerOnlyInitialized; /// /// True if only the server is started. /// public bool IsServerOnlyStarted => _networkObjectCache.IsServerOnlyStarted; /// /// True if this object has been initialized on the server and client side. /// public bool IsHostInitialized => _networkObjectCache.IsHostInitialized; /// /// True if client and server are started. /// public bool IsHostStarted => _networkObjectCache.IsHostStarted; /// /// True if client nor server are started. /// public bool IsOffline => _networkObjectCache.IsOffline; /// /// True if the object will always initialize as a networked object. When false the object will not automatically initialize over the network. Using Spawn() on an object will always set that instance as networked. /// To check if server or client has been initialized on this object use IsXYZInitialized. /// [Obsolete("Use GetIsNetworked.")] //Remove on V5. public bool IsNetworked => GetIsNetworked(); /// /// True if the object will always initialize as a networked object. When false the object will not automatically initialize over the network. Using Spawn() on an object will always set that instance as networked. /// To check if server or client has been initialized on this object use IsXYZInitialized. /// public bool GetIsNetworked() => _networkObjectCache.GetIsNetworked(); /// /// Sets IsNetworked value. This method must be called before Start. /// /// New IsNetworked value. public void SetIsNetworked(bool value) => _networkObjectCache.SetIsNetworked(value); /// /// True if a reconcile is occuring on the PredictionManager. Note the difference between this and IsBehaviourReconciling. /// public bool IsManagerReconciling => _networkObjectCache.IsManagerReconciling; /// /// Observers for this NetworkBehaviour. /// public HashSet Observers => _networkObjectCache.Observers; /// /// True if the local client is the owner of this object. /// [PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "OnStartServer", "")] [PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "OnStartNetwork", " Use base.Owner.IsLocalClient instead.")] [PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "Awake", "")] [PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "Start", "")] public bool IsOwner => _networkObjectCache.IsOwner; /// /// True if IsOwner, or if IsServerInitialized with no Owner. /// [PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "OnStartServer", "")] [PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "OnStartNetwork", " Use (base.Owner.IsLocalClient || (base.IsServerInitialized && !Owner.Isvalid) instead.")] [PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "Awake", "")] [PreventUsageInside("global::FishNet.Object.NetworkBehaviour", "Start", "")] public bool IsController => (_networkObjectCache.IsOwner || (_networkObjectCache.IsServerInitialized && !_networkObjectCache.Owner.IsValid)); [Obsolete("Use IsController.")] public bool HasAuthority => IsController; /// /// Owner of this object. /// public NetworkConnection Owner { get { //Ensures a null Owner is never returned. if (_networkObjectCache == null) return FishNet.Managing.NetworkManager.EmptyConnection; return _networkObjectCache.Owner; } } /// /// ClientId for this NetworkObject owner. /// public int OwnerId => _networkObjectCache.OwnerId; /// /// Unique Id for this _networkObjectCache. This does not represent the object owner. /// public int ObjectId => _networkObjectCache.ObjectId; /// /// The local connection of the client calling this method. /// public NetworkConnection LocalConnection => _networkObjectCache.LocalConnection; #endregion /// /// Returns if a connection is the owner of this object. /// /// /// public bool OwnerMatches(NetworkConnection connection) { return (_networkObjectCache.Owner == connection); } /// /// Despawns a GameObject. Only call from the server. /// /// GameObject to despawn. /// What happens to the object after being despawned. public void Despawn(GameObject go, DespawnType? despawnType = null) { if (!IsNetworkObjectNull(true)) _networkObjectCache.Despawn(go, despawnType); } /// /// Despawns a NetworkObject. Only call from the server. /// /// NetworkObject to despawn. /// What happens to the object after being despawned. public void Despawn(NetworkObject nob, DespawnType? despawnType = null) { if (!IsNetworkObjectNull(true)) _networkObjectCache.Despawn(nob, despawnType); } /// /// Despawns this _networkObjectCache. Can only be called on the server. /// /// What happens to the object after being despawned. public void Despawn(DespawnType? despawnType = null) { if (!IsNetworkObjectNull(true)) _networkObjectCache.Despawn(despawnType); } /// /// Spawns an object over the network. Can only be called on the server. /// /// GameObject instance to spawn. /// Connection to give ownership to. public void Spawn(GameObject go, NetworkConnection ownerConnection = null, UnityEngine.SceneManagement.Scene scene = default) { if (IsNetworkObjectNull(true)) return; _networkObjectCache.Spawn(go, ownerConnection, scene); } /// /// Spawns an object over the network. Can only be called on the server. /// /// GameObject instance to spawn. /// Connection to give ownership to. public void Spawn(NetworkObject nob, NetworkConnection ownerConnection = null, UnityEngine.SceneManagement.Scene scene = default) { if (IsNetworkObjectNull(true)) return; _networkObjectCache.Spawn(nob, ownerConnection, scene); } /// /// Returns if NetworkObject is null. /// /// True to throw a warning if null. /// private bool IsNetworkObjectNull(bool warn) { bool isNull = (_networkObjectCache == null); if (isNull && warn) NetworkManager.LogWarning($"NetworkObject is null. This can occur if this object is not spawned, or initialized yet."); return isNull; } /// /// Removes ownership from all clients. /// public void RemoveOwnership() => _networkObjectCache.RemoveOwnership(); /// /// Gives ownership to newOwner. /// public void GiveOwnership(NetworkConnection newOwner) => _networkObjectCache.GiveOwnership(newOwner, asServer: true, recursive: false); /// /// Gives ownership to newOwner. /// public void GiveOwnership(NetworkConnection newOwner, bool includeNested) => _networkObjectCache.GiveOwnership(newOwner, asServer: true, includeNested); #region Registered components /// /// Invokes an action when a specified component becomes registered. Action will invoke immediately if already registered. /// /// Component type. /// Action to invoke. public void RegisterInvokeOnInstance(Action handler) where T : UnityEngine.Component => _networkObjectCache.RegisterInvokeOnInstance(handler); /// /// Removes an action to be invoked when a specified component becomes registered. /// /// Component type. /// Action to invoke. public void UnregisterInvokeOnInstance(Action handler) where T : UnityEngine.Component => _networkObjectCache.UnregisterInvokeOnInstance(handler); /// /// Returns class of type if found within CodegenBase classes. /// /// /// public T GetInstance() where T : UnityEngine.Component => _networkObjectCache.GetInstance(); /// /// Registers a new component to this NetworkManager. /// /// Type to register. /// Reference of the component being registered. /// True to replace existing references. public void RegisterInstance(T component, bool replace = true) where T : UnityEngine.Component => _networkObjectCache.RegisterInstance(component, replace); /// /// Tries to registers a new component to this NetworkManager. /// This will not register the instance if another already exists. /// /// Type to register. /// Reference of the component being registered. /// True if was able to register, false if an instance is already registered. public bool TryRegisterInstance(T component) where T : UnityEngine.Component => _networkObjectCache.TryRegisterInstance(component); /// /// Unregisters a component from this NetworkManager. /// /// Type to unregister. public void UnregisterInstance() where T : UnityEngine.Component => _networkObjectCache.UnregisterInstance(); #endregion } }