/// <summary>
        /// Send a spawn message.
        /// </summary>
        /// <param name="netIdentity">The TinyNetIdentity of the object to spawn.</param>
        /// <param name="targetPeer">If null, send to all connected peers.</param>
        public void SendSpawnMessage(TinyNetIdentity netIdentity, TinyNetConnection targetConn = null)
        {
            if (netIdentity.ServerOnly)
            {
                return;
            }

            TinyNetObjectSpawnMessage msg = new TinyNetObjectSpawnMessage();

            msg.networkID  = netIdentity.TinyInstanceID.NetworkID;
            msg.assetIndex = TinyNetGameManager.instance.GetAssetIdFromAssetGUID(netIdentity.assetGUID);
            msg.position   = netIdentity.transform.position;
            msg.frameTick  = ServerTick;

            // Include state of TinyNetObjects.
            s_recycleWriter.Reset();
            netIdentity.TinySerialize(s_recycleWriter, true);

            if (s_recycleWriter.Length > 0)
            {
                msg.initialState = s_recycleWriter.CopyData();
            }

            if (targetConn != null)
            {
                SendMessageByChannelToTargetConnection(msg, DeliveryMethod.ReliableOrdered, targetConn);
            }
            else
            {
                SendMessageByChannelToAllConnections(msg, DeliveryMethod.ReliableOrdered);
            }
        }
        //============ Connections Methods ==================//

        /// <summary>
        /// Always call this from a TinyNetConnection ShowObjectToConnection, or you will have sync issues.
        /// </summary>
        /// <param name="tinyNetId">The tiny net identifier.</param>
        /// <param name="conn">The connection.</param>
        public void ShowForConnection(TinyNetIdentity tinyNetId, TinyNetConnection conn)
        {
            if (conn.isReady)
            {
                instance.SendSpawnMessage(tinyNetId, conn);
            }
        }
        //============ TinyNetIdentity Functions ============//

        /// <summary>
        /// Applies the initial state of an object (it's <see cref="TinyNetSyncVar"/>).
        /// </summary>
        /// <param name="tinyNetId">The <see cref="TinyNetIdentity"/>r.</param>
        /// <param name="position">The position.</param>
        /// <param name="initialState">The initial state.</param>
        /// <param name="networkID">The network identifier.</param>
        /// <param name="newGameObject">The new <see cref="GameObject"/>.</param>
        void ApplyInitialState(TinyNetIdentity tinyNetId, Vector3 position, byte[] initialState, int networkID, GameObject newGameObject)
        {
            if (!tinyNetId.gameObject.activeSelf)
            {
                tinyNetId.gameObject.SetActive(true);
            }

            tinyNetId.transform.position = position;

            tinyNetId.OnNetworkCreate();

            if (initialState != null && initialState.Length > 0)
            {
                var initialStateReader = new NetDataReader(initialState);
                tinyNetId.DeserializeAllTinyNetObjects(initialStateReader, true);
            }

            if (newGameObject == null)
            {
                return;
            }

            newGameObject.SetActive(true);
            tinyNetId.ReceiveNetworkID(networkID);
            AddTinyNetIdentityToList(tinyNetId);

            // If the object was spawned as part of the initial replication (s_TineNetObjectSpawnFinishedMessage.state == 0) it will have it's OnStartClient called by OnObjectSpawnFinished.
            if (_isSpawnFinished)
            {
                tinyNetId.OnStartClient();
            }
        }
        /// <summary>
        /// Always call this from a TinyNetConnection RemoveFromVisList, or you will have sync issues.
        /// </summary>
        /// <param name="tinyNetId">The tiny net identifier.</param>
        /// <param name="conn">The connection.</param>
        public void HideForConnection(TinyNetIdentity tinyNetId, TinyNetConnection conn)
        {
            TinyNetObjectHideMessage msg = new TinyNetObjectHideMessage();

            msg.networkID = tinyNetId.TinyInstanceID.NetworkID;

            SendMessageByChannelToTargetConnection(msg, DeliveryMethod.ReliableOrdered, conn);
        }
Example #5
0
        /// <summary>
        /// Gets a <see cref="TinyNetIdentity"/> by it's network identifier.
        /// </summary>
        /// <param name="nId">The NetworkID.</param>
        /// <returns></returns>
        public static TinyNetIdentity GetTinyNetIdentityByNetworkID(int nId)
        {
            TinyNetIdentity reference = null;

            _localIdentityObjects.TryGetValue(nId, out reference);
            //return _localIdentityObjects.ContainsKey(nId) ? _localIdentityObjects[nId] : null;
            return(reference);
        }
        /// <summary>
        /// Adds an object to the list of owned objects.
        /// </summary>
        /// <param name="obj">The <see cref="TinyNetIdentity"/> of the object to own.</param>
        public void AddOwnedObject(TinyNetIdentity obj)
        {
            if (_ownedObjectsId == null)
            {
                _ownedObjectsId = new HashSet <int>();
            }

            _ownedObjectsId.Add(obj.NetworkID);
        }
        /// <summary>
        /// Removes the owned object from the list.
        /// </summary>
        /// <param name="obj">The <see cref="TinyNetIdentity"/> of the object to remove.</param>
        public void RemoveOwnedObject(TinyNetIdentity obj)
        {
            if (_ownedObjectsId == null)
            {
                return;
            }

            _ownedObjectsId.Remove(obj.NetworkID);
        }
        /// <summary>
        /// Called when a scene object is spawned and we are a Listen Server.
        /// </summary>
        /// <param name="netMsg">A wrapper for a <see cref="TinyNetObjectSpawnSceneMessage"/>.</param>
        void OnLocalClientObjectSpawnScene(TinyNetMessageReader netMsg)
        {
            netMsg.ReadMessage(s_TinyNetObjectSpawnSceneMessage);

            TinyNetIdentity localObject = _localIdentityObjects[s_TinyNetObjectSpawnSceneMessage.networkID];

            if (localObject != null)
            {
                localObject.OnSetLocalVisibility(true);
            }
        }
Example #9
0
        /// <summary>
        /// Called when a scene object is spawned and we are a Listen Server.
        /// </summary>
        /// <param name="netMsg">A wrapper for a <see cref="TinyNetObjectSpawnSceneMessage"/>.</param>
        void OnLocalClientObjectSpawnScene(TinyNetMessageReader netMsg)
        {
            netMsg.ReadMessage(s_TinyNetObjectSpawnSceneMessage);

            TinyNetIdentity localObject = GetTinyNetIdentityByNetworkID(s_TinyNetObjectSpawnSceneMessage.networkID);

            if (localObject != null)
            {
                localObject.OnGiveLocalVisibility();
            }
        }
Example #10
0
        /// <summary>
        /// Spawns the scene object.
        /// </summary>
        /// <param name="sceneId">The scene identifier.</param>
        /// <returns></returns>
        TinyNetIdentity SpawnSceneObject(int sceneId)
        {
            if (_sceneIdentityObjectsToSpawn.ContainsKey(sceneId))
            {
                TinyNetIdentity foundId = _sceneIdentityObjectsToSpawn[sceneId];
                _sceneIdentityObjectsToSpawn.Remove(sceneId);

                return(foundId);
            }

            return(null);
        }
Example #11
0
        /// <summary>Gets a <see cref="ITinyNetComponent"/> by it's network identifier.</summary>
        /// <param name="networkId">The network identifier.</param>
        /// <param name="localId">The local identifier on the TinyNetIdentity.</param>
        /// <returns></returns>
        public static ITinyNetComponent GetTinyNetComponentByNetworkID(int networkId, byte localId)
        {
            ITinyNetComponent reference = null;

            //return _localNetObjects.ContainsKey(nId) ? _localNetObjects[nId] : null;
            if (networkId >= 0)
            {
                TinyNetIdentity tinyNetRef = GetTinyNetIdentityByNetworkID(networkId);
                reference = tinyNetRef.GetComponentById(localId);
            }
            return(reference);
        }
Example #12
0
        static bool GetTinyNetIdentity(GameObject go, out TinyNetIdentity view)
        {
            view = go.GetComponent <TinyNetIdentity>();

            if (view == null)
            {
                if (TinyNetLogLevel.logError)
                {
                    TinyLogger.LogError("TinyBirdNet failure. GameObject doesn't have TinyNetIdentity.");
                }
                return(false);
            }

            return(true);
        }
Example #13
0
        /// <summary>
        /// Sends the RPC to all clients.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="rpcMethodIndex">Index of the RPC method.</param>
        /// <param name="iObj">The object.</param>
        public void SendRPCToAllClients(NetDataWriter stream, int rpcMethodIndex, ITinyNetComponent iObj)
        {
            //TODO: Pack rpc messages
            var msg = new TinyNetRPCMessage();

            msg.networkID      = iObj.TinyInstanceID.NetworkID;
            msg.componentID    = iObj.TinyInstanceID.ComponentID;
            msg.rpcMethodIndex = rpcMethodIndex;
            msg.frameTick      = ServerTick;
            msg.parameters     = stream.Data;

            TinyNetIdentity tni = GetTinyNetIdentityByNetworkID(iObj.TinyInstanceID.NetworkID);

            SendMessageByChannelToAllObserversOf(tni, msg, DeliveryMethod.ReliableOrdered);
        }
        /// <summary>
        /// Always call this to spawn an object to a client, or you will have sync issues.
        /// </summary>
        /// <param name="tni">The <see cref="TinyNetIdentity"/> of the object to spawn.</param>
        public void ShowObjectToConnection(TinyNetIdentity tni)
        {
            if (_observingNetObjects.Contains(tni))
            {
                if (TinyNetLogLevel.logDev)
                {
                    TinyLogger.Log("ShowObjectToConnection() called but object with networkdID: " + tni.NetworkID + " is already shown");
                }
                return;
            }

            _observingNetObjects.Add(tni);

            // spawn tiny for this conn
            TinyNetServer.instance.ShowForConnection(tni, this);
        }
Example #15
0
        /// <summary>
        /// Called when an object is hidden and we are a Listen Server.
        /// </summary>
        /// <param name="netMsg">A wrapper for a <see cref="TinyNetObjectHideMessage"/>.</param>
        void OnLocalClientObjectHide(TinyNetMessageReader netMsg)
        {
            netMsg.ReadMessage(s_TinyNetObjectHideMessage);

            if (TinyNetLogLevel.logDebug)
            {
                TinyLogger.Log("TinyNetClient::OnLocalObjectObjHide netId:" + s_TinyNetObjectHideMessage.networkID);
            }

            TinyNetIdentity localObject = GetTinyNetIdentityByNetworkID(s_TinyNetObjectHideMessage.networkID);

            if (localObject != null)
            {
                localObject.OnRemoveLocalVisibility();
            }
        }
Example #16
0
        /// <summary>
        /// Called when we receive an Authorithy message from the server.
        /// </summary>
        /// <param name="netMsg">A wrapper for a <see cref="TinyNetClientAuthorityMessage"/>.</param>
        void OnClientAuthorityMessage(TinyNetMessageReader netMsg)
        {
            netMsg.ReadMessage(s_TinyNetClientAuthorityMessage);

            if (TinyNetLogLevel.logDebug)
            {
                TinyLogger.Log("TinyNetClient::OnClientAuthority for  connectionId=" + netMsg.tinyNetConn.ConnectId + " netId: " + s_TinyNetClientAuthorityMessage.networkID);
            }

            TinyNetIdentity tni = GetTinyNetIdentityByNetworkID(s_TinyNetClientAuthorityMessage.networkID);

            if (tni != null)
            {
                tni.HandleClientAuthority(s_TinyNetClientAuthorityMessage.authority);
            }
        }
Example #17
0
        /// <summary>
        /// Sends the message by a specific channel to all observers of a <see cref="TinyNetIdentity"/>.
        /// </summary>
        /// <param name="tni">The <see cref="TinyNetIdentity"/>.</param>
        /// <param name="msg">The message.</param>
        /// <param name="sendOptions">The send options.</param>
        public virtual void SendMessageByChannelToAllObserversOf(TinyNetIdentity tni, ITinyNetMessage msg, DeliveryMethod sendOptions)
        {
            recycleWriter.Reset();

            recycleWriter.Put(msg.msgType);
            msg.Serialize(recycleWriter);

            for (int i = 0; i < tinyNetConns.Count; i++)
            {
                if (!tinyNetConns[i].IsObservingNetIdentity(tni))
                {
                    return;
                }
                tinyNetConns[i].Send(recycleWriter, sendOptions);
            }
        }
Example #18
0
        /// <summary>
        /// Calls TinyDeserialize in all Components that we received updates for.
        /// </summary>
        /// <param name="netMsg">A wrapper for a <see cref="TinyNetMsgType.StateUpdate"/> message.</param>
        void OnStateUpdateMessage(TinyNetMessageReader netMsg)
        {
            LastServerTick = netMsg.reader.GetUShort();

            if (TinyNetLogLevel.logDev)
            {
                TinyLogger.Log("TinyNetClient::OnStateUpdateMessage frame: " + LastServerTick + " channel: " + netMsg.channelId);
            }

            while (netMsg.reader.AvailableBytes > 0)
            {
                int networkID = netMsg.reader.GetInt();

                int rSize = netMsg.reader.GetInt();

                _stateUpdateReader.Clear();
                //Debug.Log("OnStateUpdate: RawDataSize: " + netMsg.reader.RawDataSize + ", Position: " + netMsg.reader.Position + ", rSize: " + rSize);
                _stateUpdateReader.SetSource(netMsg.reader.RawData, netMsg.reader.Position, rSize + netMsg.reader.Position);
                _stateUpdateReader.SetFrameTick(LastServerTick);
                //Debug.Log("OnStateUpdate: _stateUpdateReader " + _stateUpdateReader.RawDataSize + ", Position: " + _stateUpdateReader.Position);

                TinyNetIdentity localObject = GetTinyNetIdentityByNetworkID(networkID);
                if (localObject != null)
                {
                    localObject.TinyDeserialize(_stateUpdateReader, false);
                }
                else
                {
                    if (TinyNetLogLevel.logWarn)
                    {
                        TinyLogger.LogWarning("Did not find target for sync message for " + networkID);
                    }
                }

                // We jump the reader position to the amount of data we read.
                //netMsg.reader.SetSource(netMsg.reader.RawData, netMsg.reader.Position + rSize, netMsg.reader.RawDataSize);
                netMsg.reader.SkipBytes(rSize);
            }

            foreach (TinyNetIdentity tinyNetId in LocalIdentityObjects.Values)
            {
                tinyNetId.OnStateUpdate();
            }
        }
Example #19
0
        /// <summary>
        /// Called when an object is spawned and we are a Listen Server.
        /// </summary>
        /// <param name="netMsg">A wrapper for a <see cref="TinyNetObjectSpawnMessage"/>.</param>
        void OnLocalClientObjectSpawn(TinyNetMessageReader netMsg)
        {
            netMsg.ReadMessage(s_TinyNetObjectSpawnMessage);

            TinyNetIdentity localObject = GetTinyNetIdentityByNetworkID(s_TinyNetObjectSpawnMessage.networkID);

            if (localObject != null)
            {
                localObject.OnStartClient();
                localObject.OnGiveLocalVisibility();
            }
            else
            {
                if (TinyNetLogLevel.logDebug)
                {
                    TinyLogger.Log("TinyNetClient::OnLocalClientObjectSpawn called but object has never been spawned to client netId:" + s_TinyNetObjectSpawnMessage.networkID);
                }
            }
        }
        /// <summary>
        /// Always call this to hide an object from a client, or you will have sync issues.
        /// </summary>
        /// <param name="tni">The <see cref="TinyNetIdentity"/> of the object to hide.</param>
        public void HideObjectToConnection(TinyNetIdentity tni, bool isDestroyed)
        {
            if (!_observingNetObjects.Contains(tni))
            {
                if (TinyNetLogLevel.logDev)
                {
                    TinyLogger.LogWarning("RemoveFromVisList() called but object with networkdID: " + tni.NetworkID + " is not shown");
                }
                return;
            }

            _observingNetObjects.Remove(tni);

            if (!isDestroyed)
            {
                // hide tni for this conn
                TinyNetServer.instance.HideForConnection(tni, this);
            }
        }
Example #21
0
        //============ TinyNetIdentity Functions ============//

        /// <summary>
        /// Applies the initial state of an object (it's <see cref="TinyNetSyncVar"/>).
        /// </summary>
        /// <param name="tinyNetId">The <see cref="TinyNetIdentity"/>r.</param>
        /// <param name="position">The position.</param>
        /// <param name="initialState">The initial state.</param>
        /// <param name="networkID">The network identifier.</param>
        /// <param name="newGameObject">The new <see cref="GameObject"/>.</param>
        void ApplyInitialState(TinyNetIdentity tinyNetId, Vector3 position, byte[] initialState, int networkID, GameObject newGameObject, ushort frameTick)
        {
            if (!tinyNetId.gameObject.activeSelf)
            {
                tinyNetId.gameObject.SetActive(true);
            }

            tinyNetId.transform.position = position;

            tinyNetId.OnNetworkCreate();

            if (newGameObject != null)
            {
                tinyNetId.ReceiveNetworkID(new TinyNetworkID(networkID));
            }

            if (initialState != null && initialState.Length > 0)
            {
                _stateUpdateReader.Clear();
                _stateUpdateReader.SetSource(initialState);
                _stateUpdateReader.SetFrameTick(frameTick);
                tinyNetId.TinyDeserialize(_stateUpdateReader, true);
            }

            // If this is null then this object already existed (already have a networkID and was registered in TinyNetScene), just apply the update to existing object
            if (newGameObject == null)
            {
                return;
            }

            newGameObject.SetActive(true);
            tinyNetId.ReceiveNetworkID(new TinyNetworkID(networkID));
            AddTinyNetIdentityToList(tinyNetId);

            // If the object was spawned as part of the initial replication (s_TineNetObjectSpawnFinishedMessage.state == 0) it will have it's OnStartClient called by OnObjectSpawnFinished.
            if (_isSpawnFinished)
            {
                tinyNetId.OnStartClient();
            }
        }
Example #22
0
        /// <summary>
        /// Destroys the object.
        /// </summary>
        /// <param name="tni">The <see cref="TinyNetIdentity"/> of the object.</param>
        /// <param name="destroyServerObject">if set to <c>true</c> destroy the object on server too.</param>
        public void DestroyObject(TinyNetIdentity tni, bool destroyServerObject)
        {
            if (TinyNetLogLevel.logDebug)
            {
                TinyLogger.Log("DestroyObject instance:" + tni.TinyInstanceID);
            }

            /*if (_localIdentityObjects.ContainsKey(tni.TinyInstanceID.NetworkID)) {
             *      _localIdentityObjects.Remove(tni.TinyInstanceID.NetworkID);
             * }*/
            RemoveTinyNetIdentityFromList(tni);

            if (tni.ConnectionToOwnerClient != null)
            {
                tni.ConnectionToOwnerClient.RemoveOwnedObject(tni);
            }

            TinyNetObjectDestroyMessage msg = new TinyNetObjectDestroyMessage();

            msg.networkID = tni.TinyInstanceID.NetworkID;
            SendMessageByChannelToAllObserversOf(tni, msg, DeliveryMethod.ReliableOrdered);

            for (int i = 0; i < tinyNetConns.Count; i++)
            {
                tinyNetConns[i].HideObjectToConnection(tni, true);
            }

            /*if (TinyNetGameManager.instance.isListenServer) {
             *      tni.OnNetworkDestroy();
             * }*/

            tni.OnNetworkDestroy();
            // when unspawning, dont destroy the server's object
            if (destroyServerObject)
            {
                Object.Destroy(tni.gameObject);
            }

            tni.ReceiveNetworkID(new TinyNetworkID(-1));
        }
Example #23
0
        /// <summary>
        /// Called when an object is destroyed.
        /// </summary>
        /// <param name="netMsg">A wrapper for a <see cref="TinyNetObjectDestroyMessage"/>.</param>
        void OnObjectDestroy(TinyNetMessageReader netMsg)
        {
            netMsg.ReadMessage(s_TinyNetObjectDestroyMessage);

            if (TinyNetLogLevel.logDebug)
            {
                TinyLogger.Log("TinyNetClient::OnObjDestroy networkID:" + s_TinyNetObjectDestroyMessage.networkID);
            }

            TinyNetIdentity localObject = GetTinyNetIdentityByNetworkID(s_TinyNetObjectDestroyMessage.networkID);

            if (localObject != null)
            {
                RemoveTinyNetIdentityFromList(localObject);
                localObject.OnNetworkDestroy();

                if (!TinyNetGameManager.instance.InvokeUnSpawnHandler(localObject.assetGUID, localObject.gameObject))
                {
                    // default handling
                    if (localObject.sceneID == 0)
                    {
                        Object.Destroy(localObject.gameObject);
                    }
                    else
                    {
                        // scene object.. disable it in scene instead of destroying
                        localObject.gameObject.SetActive(false);
                        _sceneIdentityObjectsToSpawn[localObject.sceneID] = localObject;
                    }
                }
            }
            else
            {
                if (TinyNetLogLevel.logDebug)
                {
                    TinyLogger.LogWarning("Did not find target for destroy message for " + s_TinyNetObjectDestroyMessage.networkID);
                }
            }
        }
Example #24
0
        /// <summary>
        /// Called when a scene object is spawned.
        /// </summary>
        /// <param name="netMsg">A wrapper for a <see cref="TinyNetObjectSpawnSceneMessage"/>.</param>
        void OnObjectSpawnScene(TinyNetMessageReader netMsg)
        {
            netMsg.ReadMessage(s_TinyNetObjectSpawnSceneMessage);

            if (TinyNetLogLevel.logDebug)
            {
                TinyLogger.Log("Client spawn scene handler instantiating [networkID: " + s_TinyNetObjectSpawnSceneMessage.networkID + " sceneId: " + s_TinyNetObjectSpawnSceneMessage.sceneId + " pos: " + s_TinyNetObjectSpawnSceneMessage.position);
            }

            TinyNetIdentity localTinyNetIdentity = GetTinyNetIdentityByNetworkID(s_TinyNetObjectSpawnSceneMessage.networkID);

            if (localTinyNetIdentity != null)
            {
                // this object already exists (was in the scene)
                ApplyInitialState(localTinyNetIdentity, s_TinyNetObjectSpawnSceneMessage.position, s_TinyNetObjectSpawnSceneMessage.initialState, s_TinyNetObjectSpawnSceneMessage.networkID, localTinyNetIdentity.gameObject, s_TinyNetObjectSpawnSceneMessage.frameTick);
                return;
            }

            TinyNetIdentity spawnedId = SpawnSceneObject(s_TinyNetObjectSpawnSceneMessage.sceneId);

            if (spawnedId == null)
            {
                if (TinyNetLogLevel.logError)
                {
                    TinyLogger.LogError("Spawn scene object not found for " + s_TinyNetObjectSpawnSceneMessage.sceneId);
                }
                return;
            }

            if (TinyNetLogLevel.logDebug)
            {
                TinyLogger.Log("Client spawn for [networkID :" + s_TinyNetObjectSpawnSceneMessage.networkID + "] [sceneId: " + s_TinyNetObjectSpawnSceneMessage.sceneId + "] obj: " + spawnedId.gameObject.name);
            }

            ApplyInitialState(spawnedId, s_TinyNetObjectSpawnSceneMessage.position, s_TinyNetObjectSpawnSceneMessage.initialState, s_TinyNetObjectSpawnSceneMessage.networkID, spawnedId.gameObject, s_TinyNetObjectSpawnSceneMessage.frameTick);
        }
Example #25
0
 /// <summary>
 /// Removes the <see cref="TinyNetIdentity"/> from the list.
 /// </summary>
 /// <param name="netIdentity">The net identity.</param>
 public static void RemoveTinyNetIdentityFromList(TinyNetIdentity netIdentity)
 {
     _localIdentityObjects.Remove(netIdentity.NetworkID);
 }
Example #26
0
 /// <summary>
 /// Unspawn an object.
 /// </summary>
 /// <param name="obj">The <see cref="TinyNetIdentity"/> to unspawn.</param>
 public void UnSpawnObject(TinyNetIdentity tni)
 {
     DestroyObject(tni, false);
 }
Example #27
0
        /// <summary>
        /// Called when an object is spawned.
        /// </summary>
        /// <param name="netMsg">A wrapper for a <see cref="TinyNetObjectSpawnMessage"/>.</param>
        void OnObjectSpawn(TinyNetMessageReader netMsg)
        {
            netMsg.ReadMessage(s_TinyNetObjectSpawnMessage);

            if (s_TinyNetObjectSpawnMessage.assetIndex < 0 || s_TinyNetObjectSpawnMessage.assetIndex > int.MaxValue || s_TinyNetObjectSpawnMessage.assetIndex > TinyNetGameManager.instance.GetAmountOfRegisteredAssets())
            {
                if (TinyNetLogLevel.logError)
                {
                    TinyLogger.LogError("OnObjSpawn networkID: " + s_TinyNetObjectSpawnMessage.networkID + " has invalid asset Id");
                }
                return;
            }
            if (TinyNetLogLevel.logDev)
            {
                TinyLogger.Log("Client spawn handler instantiating [networkID:" + s_TinyNetObjectSpawnMessage.networkID + " asset ID:" + s_TinyNetObjectSpawnMessage.assetIndex + " pos:" + s_TinyNetObjectSpawnMessage.position + "]");
            }

            TinyNetIdentity localTinyNetIdentity = GetTinyNetIdentityByNetworkID(s_TinyNetObjectSpawnMessage.networkID);

            // Check if this object was already registered in the scene:
            if (localTinyNetIdentity != null)
            {
                // this object already exists (was in the scene), just apply the update to existing object.
                ApplyInitialState(localTinyNetIdentity, s_TinyNetObjectSpawnMessage.position, s_TinyNetObjectSpawnMessage.initialState, s_TinyNetObjectSpawnMessage.networkID, null, s_TinyNetObjectSpawnMessage.frameTick);
                return;
            }

            GameObject    prefab;
            SpawnDelegate handler;

            prefab = TinyNetGameManager.instance.GetPrefabFromAssetId(s_TinyNetObjectSpawnMessage.assetIndex);
            // Check if the prefab is registered in the list of spawnable prefabs.
            if (prefab != null)
            {
                var obj = (GameObject)Object.Instantiate(prefab, s_TinyNetObjectSpawnMessage.position, Quaternion.identity);

                localTinyNetIdentity = obj.GetComponent <TinyNetIdentity>();

                if (localTinyNetIdentity == null)
                {
                    if (TinyNetLogLevel.logError)
                    {
                        TinyLogger.LogError("Client object spawned for " + s_TinyNetObjectSpawnMessage.assetIndex + " does not have a TinyNetidentity");
                    }
                    return;
                }

                ApplyInitialState(localTinyNetIdentity, s_TinyNetObjectSpawnMessage.position, s_TinyNetObjectSpawnMessage.initialState, s_TinyNetObjectSpawnMessage.networkID, obj, s_TinyNetObjectSpawnMessage.frameTick);
                // If not, check if the prefab have a spawn handler registered.
            }
            else if (TinyNetGameManager.instance.GetSpawnHandler(s_TinyNetObjectSpawnMessage.assetIndex, out handler))
            {
                GameObject obj = handler(s_TinyNetObjectSpawnMessage.position, s_TinyNetObjectSpawnMessage.assetIndex);
                if (obj == null)
                {
                    if (TinyNetLogLevel.logWarn)
                    {
                        TinyLogger.LogWarning("Client spawn handler for " + s_TinyNetObjectSpawnMessage.assetIndex + " returned null");
                    }
                    return;
                }

                localTinyNetIdentity = obj.GetComponent <TinyNetIdentity>();
                if (localTinyNetIdentity == null)
                {
                    if (TinyNetLogLevel.logError)
                    {
                        TinyLogger.LogError("Client object spawned for " + s_TinyNetObjectSpawnMessage.assetIndex + " does not have a network identity");
                    }
                    return;
                }

                localTinyNetIdentity.SetDynamicAssetGUID(TinyNetGameManager.instance.GetAssetGUIDFromAssetId(s_TinyNetObjectSpawnMessage.assetIndex));
                ApplyInitialState(localTinyNetIdentity, s_TinyNetObjectSpawnMessage.position, s_TinyNetObjectSpawnMessage.initialState, s_TinyNetObjectSpawnMessage.networkID, obj, s_TinyNetObjectSpawnMessage.frameTick);
                // If also not, we literally cannot spawn this object and you should feel bad.
            }
            else
            {
                if (TinyNetLogLevel.logError)
                {
                    TinyLogger.LogError("Failed to spawn server object, assetId=" + s_TinyNetObjectSpawnMessage.assetIndex + " networkID=" + s_TinyNetObjectSpawnMessage.networkID);
                }
            }
        }
Example #28
0
        //============ Object Networking ====================//

        /// <summary>
        /// Adds the <see cref="TinyNetIdentity"/> to list.
        /// </summary>
        /// <param name="netIdentity">The net identity.</param>
        public static void AddTinyNetIdentityToList(TinyNetIdentity netIdentity)
        {
            _localIdentityObjects.Add(netIdentity.NetworkID, netIdentity);
        }
        //============ Network Identity =====================//

        /// <summary>
        /// Determines whether this instance is observing the specified <see cref="TinyNetIdentity"/>.
        /// </summary>
        /// <param name="tni">The <see cref="TinyNetIdentity"/>.</param>
        /// <returns>
        ///   <c>true</c> if is observing the specified <see cref="TinyNetIdentity"/>; otherwise, <c>false</c>.
        /// </returns>
        public bool IsObservingNetIdentity(TinyNetIdentity tni)
        {
            return(_observingNetObjects.Contains(tni));
        }
Example #30
0
 /// <summary>
 /// Removes the <see cref="TinyNetIdentity"/> from the list.
 /// </summary>
 /// <param name="netIdentity">The net identity.</param>
 public static bool RemoveTinyNetIdentityFromList(TinyNetIdentity netIdentity)
 {
     return(_localIdentityObjects.Remove(netIdentity.TinyInstanceID.NetworkID));
 }