Example #1
0
        public static bool SendData(int msgType, int remoteClient = -1, int ignoreClient = -1, string text = "", int number = 0, float number2 = 0f, float number3 = 0f, float number4 = 0f, int number5 = 0)
        {
            #if Full_API
            if (Main.netMode == 0)
            {
                return(false);
            }

            int bufferId = 256;
            if (Main.netMode == 2 && remoteClient >= 0)
            {
                bufferId = remoteClient;
            }

            lock (NetMessage.buffer [bufferId])
            {
                var writer = NetMessage.buffer[bufferId].writer;
                if (writer == null)
                {
                    NetMessage.buffer[bufferId].ResetWriter();
                    writer = NetMessage.buffer[bufferId].writer;
                }

                writer.BaseStream.Position = 0;
                var position = writer.BaseStream.Position;
                writer.BaseStream.Position += 2;
                writer.Write((byte)msgType);

                var ctx = new HookContext()
                {
                    Sender = HookContext.ConsoleSender
                };
                var args = new HookArgs.SendNetMessage()
                {
                    MsgType      = msgType,
                    BufferId     = bufferId,
                    RemoteClient = remoteClient,
                    IgnoreClient = ignoreClient,
                    Text         = text,
                    Number       = number,
                    Number2      = number2,
                    Number3      = number3,
                    Number4      = number4,
                    Number5      = number5
                };
                HookPoints.SendNetMessage.Invoke(ref ctx, ref args);

                if (ctx.Result != HookResult.DEFAULT)
                {
                    var endOfMessage = (int)writer.BaseStream.Position;
                    writer.BaseStream.Position = position;
                    writer.Write((short)endOfMessage);
                    writer.BaseStream.Position = (long)endOfMessage;

                    if (remoteClient == -1)
                    {
                        switch ((Packet)msgType)
                        {
                        case Packet.CHEST:
                        case Packet.CHEST_NAME_UPDATE:
                            for (int i = 0; i < 256; i++)
                            {
                                if (i != ignoreClient && NetMessage.buffer[i].broadcast && Netplay.Clients[i].IsConnected())
                                {
                                    try
                                    {
                                        NetMessage.buffer[i].spamCount++;
                                        Main.txMsg++;
                                        Main.txData += endOfMessage;
                                        Main.txMsgType[msgType]++;
                                        Main.txDataType[msgType] += endOfMessage;
                                        Netplay.Clients[i].Socket.AsyncSend(NetMessage.buffer[bufferId].writeBuffer, 0, endOfMessage, new SocketSendCallback(Netplay.Clients[i].ServerWriteCallBack), null);
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                            break;

                        case Packet.TILE_SQUARE:
                            for (int i = 0; i < 256; i++)
                            {
                                if (i != ignoreClient && NetMessage.buffer[i].broadcast && Netplay.Clients[i].IsConnected() && Netplay.Clients[i].SectionRange(number, (int)number2, (int)number3))
                                {
                                    try
                                    {
                                        NetMessage.buffer[i].spamCount++;
                                        Main.txMsg++;
                                        Main.txData += endOfMessage;
                                        Main.txMsgType[msgType]++;
                                        Main.txDataType[msgType] += endOfMessage;
                                        Netplay.Clients[i].Socket.AsyncSend(NetMessage.buffer[bufferId].writeBuffer, 0, endOfMessage, new SocketSendCallback(Netplay.Clients[i].ServerWriteCallBack), null);
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                            break;

                        case Packet.NPC_INFO:
                            NPC npcInfo = Main.npc[number];
                            for (int i = 0; i < 256; i++)
                            {
                                if (i != ignoreClient && NetMessage.buffer[i].broadcast && Netplay.Clients[i].IsConnected())
                                {
                                    var sendData = false;
                                    if (npcInfo.boss || npcInfo.netAlways || npcInfo.townNPC || !npcInfo.active)
                                    {
                                        sendData = true;
                                    }
                                    else if (npcInfo.netSkip <= 0)
                                    {
                                        Rectangle rect  = Main.player[i].getRect();
                                        Rectangle rect2 = npcInfo.getRect();
                                        rect2.X      -= 2500;
                                        rect2.Y      -= 2500;
                                        rect2.Width  += 5000;
                                        rect2.Height += 5000;
                                        if (rect.Intersects(rect2))
                                        {
                                            sendData = true;
                                        }
                                    }
                                    else
                                    {
                                        sendData = true;
                                    }

                                    if (sendData)
                                    {
                                        try
                                        {
                                            NetMessage.buffer[i].spamCount++;
                                            Main.txMsg++;
                                            Main.txData += endOfMessage;
                                            Main.txMsgType[msgType]++;
                                            Main.txDataType[msgType] += endOfMessage;
                                            Netplay.Clients[i].Socket.AsyncSend(NetMessage.buffer[bufferId].writeBuffer, 0, endOfMessage, new SocketSendCallback(Netplay.Clients[i].ServerWriteCallBack), null);
                                        }
                                        catch
                                        {
                                        }
                                    }
                                }
                            }
                            npcInfo.netSkip++;
                            if (npcInfo.netSkip > 4)
                            {
                                npcInfo.netSkip = 0;
                            }
                            break;

                        case Packet.DAMAGE_NPC:
                            NPC npcDmg = Main.npc[number];
                            for (int i = 0; i < 256; i++)
                            {
                                if (i != ignoreClient && NetMessage.buffer[i].broadcast && Netplay.Clients[i].IsConnected())
                                {
                                    var sendData = false;
                                    if (npcDmg.life <= 0)
                                    {
                                        sendData = true;
                                    }
                                    else
                                    {
                                        Rectangle rect3 = Main.player[i].getRect();
                                        Rectangle rect4 = npcDmg.getRect();
                                        rect4.X      -= 3000;
                                        rect4.Y      -= 3000;
                                        rect4.Width  += 6000;
                                        rect4.Height += 6000;
                                        if (rect3.Intersects(rect4))
                                        {
                                            sendData = true;
                                        }
                                    }

                                    if (sendData)
                                    {
                                        try
                                        {
                                            NetMessage.buffer[i].spamCount++;
                                            Main.txMsg++;
                                            Main.txData += endOfMessage;
                                            Main.txMsgType[msgType]++;
                                            Main.txDataType[msgType] += endOfMessage;
                                            Netplay.Clients[i].Socket.AsyncSend(NetMessage.buffer[bufferId].writeBuffer, 0, endOfMessage, new SocketSendCallback(Netplay.Clients[i].ServerWriteCallBack), null);
                                        }
                                        catch
                                        {
                                        }
                                    }
                                }
                            }
                            break;

                        case Packet.PLAYER_STATE_UPDATE:
                            for (int i = 0; i < 256; i++)
                            {
                                if (i != ignoreClient && NetMessage.buffer[i].broadcast && Netplay.Clients[i].IsConnected())
                                {
                                    try
                                    {
                                        NetMessage.buffer[i].spamCount++;
                                        Main.txMsg++;
                                        Main.txData += endOfMessage;
                                        Main.txMsgType[msgType]++;
                                        Main.txDataType[msgType] += endOfMessage;
                                        Netplay.Clients[i].Socket.AsyncSend(NetMessage.buffer[bufferId].writeBuffer, 0, endOfMessage, new SocketSendCallback(Netplay.Clients[i].ServerWriteCallBack), null);
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                            Main.player[number].netSkip++;
                            if (Main.player[number].netSkip > 2)
                            {
                                Main.player[number].netSkip = 0;
                            }
                            break;

                        case Packet.PROJECTILE:
                            Projectile projectile2 = Main.projectile[number];
                            for (int i = 0; i < 256; i++)
                            {
                                if (i != ignoreClient && NetMessage.buffer[i].broadcast && Netplay.Clients[i].IsConnected())
                                {
                                    var sendData = false;
                                    if (projectile2.type == 12 || Main.projPet[projectile2.type] || projectile2.aiStyle == 11 || projectile2.netImportant)
                                    {
                                        sendData = true;
                                    }
                                    else
                                    {
                                        Rectangle rect5 = Main.player[i].getRect();
                                        Rectangle rect6 = projectile2.getRect();
                                        //Is this some sort of in range check? o.O
                                        rect6.X      -= 5000;
                                        rect6.Y      -= 5000;
                                        rect6.Width  += 10000;
                                        rect6.Height += 10000;
                                        if (rect5.Intersects(rect6))
                                        {
                                            sendData = true;
                                        }
                                    }

                                    if (sendData)
                                    {
                                        try
                                        {
                                            NetMessage.buffer[i].spamCount++;
                                            Main.txMsg++;
                                            Main.txData += endOfMessage;
                                            Main.txMsgType[msgType]++;
                                            Main.txDataType[msgType] += endOfMessage;
                                            Netplay.Clients[i].Socket.AsyncSend(NetMessage.buffer[bufferId].writeBuffer, 0, endOfMessage, new SocketSendCallback(Netplay.Clients[i].ServerWriteCallBack), null);
                                        }
                                        catch
                                        {
                                        }
                                    }
                                }
                            }
                            break;

                        default:
                            for (int i = 0; i < 256; i++)
                            {
                                if (i != ignoreClient && (NetMessage.buffer[i].broadcast || (Netplay.Clients[i].State >= 3 && msgType == 10)) && Netplay.Clients[i].IsConnected())
                                {
                                    try
                                    {
                                        NetMessage.buffer[i].spamCount++;
                                        Main.txMsg++;
                                        Main.txData += endOfMessage;
                                        Main.txMsgType[msgType]++;
                                        Main.txDataType[msgType] += endOfMessage;
                                        Netplay.Clients[i].Socket.AsyncSend(NetMessage.buffer[bufferId].writeBuffer, 0, endOfMessage, new SocketSendCallback(Netplay.Clients[i].ServerWriteCallBack), null);
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                            break;
                        }
                    }
                    else
                    {
                        if (Netplay.Clients[remoteClient].IsConnected())
                        {
                            try
                            {
                                NetMessage.buffer[remoteClient].spamCount++;
                                Main.txMsg++;
                                Main.txData += endOfMessage;
                                Main.txMsgType[msgType]++;
                                Main.txDataType[msgType] += endOfMessage;
                                Netplay.Clients[remoteClient].Socket.AsyncSend(NetMessage.buffer[bufferId].writeBuffer, 0, endOfMessage, new SocketSendCallback(Netplay.Clients[remoteClient].ServerWriteCallBack), null);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                else
                {
                    return(true); //Continue with vanilla code
                }
            }
            #endif
            return(false);
        }
Example #2
0
        void OnNetMessageSend(ref HookContext ctx, ref HookArgs.SendNetMessage args)
        {
            if (Terraria.Main.ServerSideCharacter)
            {
                switch ((Packet)args.MsgType)
                {
                case Packet.WORLD_DATA:
                    ctx.SetResult(HookResult.IGNORE);

                    var writer = NetMessage.buffer[args.BufferId].writer;

                    writer.Write((int)Main.time);

                    byte value;

                    value = 0;
                    if (Main.dayTime)
                    {
                        value += 1;
                    }
                    if (Main.bloodMoon)
                    {
                        value += 2;
                    }
                    if (Main.bloodMoon)
                    {
                        value += 4;
                    }
                    writer.Write(value);

                    writer.Write((byte)Main.moonPhase);
                    writer.Write((short)Main.maxTilesX);
                    writer.Write((short)Main.maxTilesY);
                    writer.Write((short)Main.spawnTileX);
                    writer.Write((short)Main.spawnTileY);
                    writer.Write((short)Main.worldSurface);
                    writer.Write((short)Main.rockLayer);
                    writer.Write(Main.worldID);
                    writer.Write(Main.worldName);
                    writer.Write((byte)Main.moonType);
                    writer.Write((byte)WorldGen.treeBG);
                    writer.Write((byte)WorldGen.corruptBG);
                    writer.Write((byte)WorldGen.jungleBG);
                    writer.Write((byte)WorldGen.snowBG);
                    writer.Write((byte)WorldGen.hallowBG);
                    writer.Write((byte)WorldGen.crimsonBG);
                    writer.Write((byte)WorldGen.desertBG);
                    writer.Write((byte)WorldGen.oceanBG);
                    writer.Write((byte)Main.iceBackStyle);
                    writer.Write((byte)Main.jungleBackStyle);
                    writer.Write((byte)Main.hellBackStyle);
                    writer.Write(Main.windSpeedSet);
                    writer.Write((byte)Main.numClouds);

                    for (int k = 0; k < 3; k++)
                    {
                        writer.Write(Main.treeX[k]);
                    }
                    for (int l = 0; l < 4; l++)
                    {
                        writer.Write((byte)Main.treeStyle[l]);
                    }
                    for (int m = 0; m < 3; m++)
                    {
                        writer.Write(Main.caveBackX[m]);
                    }
                    for (int n = 0; n < 4; n++)
                    {
                        writer.Write((byte)Main.caveBackStyle[n]);
                    }

                    if (!Main.raining)
                    {
                        Main.maxRaining = 0;
                    }
                    writer.Write(Main.maxRaining);

                    value = 0;
                    if (WorldGen.shadowOrbSmashed)
                    {
                        value += 1;
                    }
                    if (NPC.downedBoss1)
                    {
                        value += 2;
                    }
                    if (NPC.downedBoss2)
                    {
                        value += 4;
                    }
                    if (NPC.downedBoss3)
                    {
                        value += 8;
                    }
                    if (Main.hardMode)
                    {
                        value += 16;
                    }
                    if (NPC.downedClown)
                    {
                        value += 32;
                    }
                    if (Main.ServerSideCharacter)
                    {
                        value += 64;
                    }
                    if (NPC.downedPlantBoss)
                    {
                        value += 128;
                    }
                    writer.Write(value);

                    value = 0;
                    if (NPC.downedMechBoss1)
                    {
                        value += 1;
                    }
                    if (NPC.downedMechBoss2)
                    {
                        value += 2;
                    }
                    if (NPC.downedMechBoss3)
                    {
                        value += 4;
                    }
                    if (NPC.downedMechBossAny)
                    {
                        value += 8;
                    }
                    if (Main.cloudBGActive >= 1)
                    {
                        value += 16;
                    }
                    if (WorldGen.crimson)
                    {
                        value += 32;
                    }
                    if (Main.pumpkinMoon)
                    {
                        value += 64;
                    }
                    if (Main.snowMoon)
                    {
                        value += 128;
                    }
                    writer.Write(value);

                    value = 0;
                    if (Main.expertMode)
                    {
                        value += 1;
                    }
                    if (Main.fastForwardTime)
                    {
                        value += 2;
                    }
                    if (Main.slimeRain)
                    {
                        value += 4;
                    }
                    if (NPC.downedSlimeKing)
                    {
                        value += 8;
                    }
                    if (NPC.downedQueenBee)
                    {
                        value += 16;
                    }
                    if (NPC.downedFishron)
                    {
                        value += 32;
                    }
                    if (NPC.downedMartians)
                    {
                        value += 64;
                    }
                    if (NPC.downedAncientCultist)
                    {
                        value += 128;
                    }
                    writer.Write(value);

                    value = 0;
                    if (NPC.downedMoonlord)
                    {
                        value += 1;
                    }
                    if (NPC.downedHalloweenKing)
                    {
                        value += 2;
                    }
                    if (NPC.downedHalloweenTree)
                    {
                        value += 4;
                    }
                    if (NPC.downedChristmasIceQueen)
                    {
                        value += 8;
                    }
                    if (NPC.downedChristmasSantank)
                    {
                        value += 16;
                    }
                    if (NPC.downedChristmasTree)
                    {
                        value += 32;
                    }
                    if (NPC.downedGolemBoss)
                    {
                        value += 64;
                    }
                    writer.Write(value);

                    writer.Write((sbyte)Main.invasionType);

                    /*if (SocialAPI.Network != null)
                     *  writer.Write(SocialAPI.Network.GetLobbyId());
                     * else*/
                    writer.Write(0);

                    break;
                }
            }
        }