Ejemplo n.º 1
0
        public void DestroySelf()
        {
            if (!mDestroyed)
            {
                mDestroyed = true;

                if (TNManager.IsInChannel(channelID))
                {
                    if (TNManager.IsChannelLocked(channelID))
                    {
                        Debug.LogWarning("Trying to destroy an object in a locked channel. Call will be ignored.");
                    }
                    else
                    {
                        Invoke("EnsureDestroy", 5f);
                        BinaryWriter bw = TNManager.BeginSend(Packet.RequestDestroyObject);
                        bw.Write(channelID);
                        bw.Write(uid);
                        TNManager.EndSend(channelID, true);
                    }
                }
                else
                {
                    if (onDestroy != null)
                    {
                        onDestroy();
                    }
                    Object.Destroy(gameObject);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Send a new RFC call to the specified target.
        /// </summary>

        void SendRFC(byte rfcID, string rfcName, Target target, bool reliable, params object[] objs)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return;
            }
#endif
            if (mDestroyed)
            {
                return;
            }

            if ((target == Target.AllSaved || target == Target.Others) && TNManager.IsChannelLocked(channelID))
            {
#if UNITY_EDITOR
                Debug.LogError("Can't send persistent RFCs while in a locked channel");
#endif
                return;
            }

            // Some very odd special case... sending a string[] as the only parameter
            // results in objs[] being a string[] instead, when it should be object[string[]].
            if (objs != null && objs.GetType() != typeof(object[]))
            {
                objs = new object[] { objs }
            }
            ;

            bool executeLocally = false;
            bool connected      = TNManager.isConnected;

            if (target == Target.Broadcast)
            {
                if (connected)
                {
                    BinaryWriter writer = TNManager.BeginSend(Packet.Broadcast);
                    writer.Write(TNManager.playerID);
                    writer.Write(channelID);
                    writer.Write(GetUID(uid, rfcID));
                    if (rfcID == 0)
                    {
                        writer.Write(rfcName);
                    }
                    writer.WriteArray(objs);
                    TNManager.EndSend(channelID, reliable);
                }
                else
                {
                    executeLocally = true;
                }
            }
            else if (target == Target.Admin)
            {
                if (connected)
                {
                    BinaryWriter writer = TNManager.BeginSend(Packet.BroadcastAdmin);
                    writer.Write(TNManager.playerID);
                    writer.Write(channelID);
                    writer.Write(GetUID(uid, rfcID));
                    if (rfcID == 0)
                    {
                        writer.Write(rfcName);
                    }
                    writer.WriteArray(objs);
                    TNManager.EndSend(channelID, reliable);
                }
                else
                {
                    executeLocally = true;
                }
            }
            else if (target == Target.Host && TNManager.isHosting)
            {
                // We're the host, and the packet should be going to the host -- just echo it locally
                executeLocally = true;
            }
            else
            {
                if (!connected || !reliable)
                {
                    if (target == Target.All)
                    {
                        target         = Target.Others;
                        executeLocally = true;
                    }
                    else if (target == Target.AllSaved)
                    {
                        target         = Target.OthersSaved;
                        executeLocally = true;
                    }
                }

                if (connected && TNManager.IsInChannel(channelID))
                {
                    byte         packetID = (byte)((int)Packet.ForwardToAll + (int)target);
                    BinaryWriter writer   = TNManager.BeginSend(packetID);
                    writer.Write(TNManager.playerID);
                    writer.Write(channelID);
                    writer.Write(GetUID(uid, rfcID));
                    if (rfcID == 0)
                    {
                        writer.Write(rfcName);
                    }
                    writer.WriteArray(objs);
                    TNManager.EndSend(channelID, reliable);
                }
            }

            if (executeLocally)
            {
                if (rfcID != 0)
                {
                    Execute(rfcID, objs);
                }
                else
                {
                    Execute(rfcName, objs);
                }
            }
        }

        /// <summary>
        /// Send a new RFC call to the specified target.
        /// </summary>

        void SendRFC(byte rfcID, string rfcName, string targetName, bool reliable, params object[] objs)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return;
            }
#endif
            if (mDestroyed || string.IsNullOrEmpty(targetName))
            {
                return;
            }

            if (targetName == TNManager.playerName)
            {
                if (rfcID != 0)
                {
                    Execute(rfcID, objs);
                }
                else
                {
                    Execute(rfcName, objs);
                }
            }
            else
            {
                BinaryWriter writer = TNManager.BeginSend(Packet.ForwardByName);
                writer.Write(TNManager.playerID);
                writer.Write(targetName);
                writer.Write(channelID);
                writer.Write(GetUID(uid, rfcID));
                if (rfcID == 0)
                {
                    writer.Write(rfcName);
                }
                writer.WriteArray(objs);
                TNManager.EndSend(channelID, reliable);
            }
        }

        /// <summary>
        /// Send a new remote function call to the specified player.
        /// </summary>

        void SendRFC(byte rfcID, string rfcName, int target, bool reliable, params object[] objs)
        {
            if (mDestroyed)
            {
                return;
            }

            if (TNManager.isConnected)
            {
                BinaryWriter writer = TNManager.BeginSend(Packet.ForwardToPlayer);
                writer.Write(TNManager.playerID);
                writer.Write(target);
                writer.Write(channelID);
                writer.Write(GetUID(uid, rfcID));
                if (rfcID == 0)
                {
                    writer.Write(rfcName);
                }
                writer.WriteArray(objs);
                TNManager.EndSend(channelID, reliable);
            }
            else if (target == TNManager.playerID)
            {
                if (rfcID != 0)
                {
                    Execute(rfcID, objs);
                }
                else
                {
                    Execute(rfcName, objs);
                }
            }
        }

        /// <summary>
        /// Broadcast a remote function call to all players on the network.
        /// </summary>

        void BroadcastToLAN(int port, byte rfcID, string rfcName, params object[] objs)
        {
            if (mDestroyed)
            {
                return;
            }
            BinaryWriter writer = TNManager.BeginSend(Packet.ForwardToAll);

            writer.Write(TNManager.playerID);
            writer.Write(channelID);
            writer.Write(GetUID(uid, rfcID));
            if (rfcID == 0)
            {
                writer.Write(rfcName);
            }
            writer.WriteArray(objs);
            TNManager.EndSendToLAN(port);
        }