context: using System;
  using System.IO;
  using TerrariaApi.Server;
  using System.Diagnostics;
  using System.Threading.Tasks;
  namespace Terraria
  {
  	public class MessageBuffer
  	{
  		public const int readBufferMax = 65535;
  		public const int writeBufferMax = 65535;
  		public bool broadcast;
  		public byte[] readBuffer = new byte[65535];
  		public byte[] writeBuffer = new byte[65535];
  		public bool writeLocked;
  		public int messageLength;
  		public int totalData;
  		public int whoAmI;
  		public int spamCount;
  		public int maxSpam;
  		public bool checkBytes;
  
  		public BinaryReader binaryReader;
  		public BinaryWriter binaryWriter;
  
  		public MessageBuffer()
  		{
  			binaryReader = new BinaryReader(new MemoryStream(readBuffer));
  			binaryWriter = new BinaryWriter(new MemoryStream(writeBuffer));
  		}
  
  		~MessageBuffer()
  		{
  			binaryReader.Dispose();
  			binaryWriter.Dispose();
  		}
  
  		public void Reset()
  		{
  			this.readBuffer = new byte[65535];
  			this.writeBuffer = new byte[65535];
  			binaryReader.Dispose();
  			binaryWriter.Dispose();
  			binaryReader = new BinaryReader(new MemoryStream(readBuffer));
  			binaryWriter = new BinaryWriter(new MemoryStream(writeBuffer));
  
  			this.writeLocked = false;
  			this.messageLength = 0;
  			this.totalData = 0;
  			this.spamCount = 0;
  			this.broadcast = false;
  			this.checkBytes = false;
  		}
  		public void GetData(int start, int length)
  		{
  			if (this.whoAmI < 256)
  			{
  				Netplay.serverSock[this.whoAmI].timeOut = 0;
  			}
  			else
  			{
  				Netplay.clientSock.timeOut = 0;
  			}
  			int num = start + 1;
  			byte b = this.readBuffer[start];
  			if (ServerApi.Hooks.InvokeNetGetData(ref b, this, ref num, ref length))
  			{
  				return;
  			}
  			Main.rxMsg++;
  			Main.rxData += length;
  			Main.rxMsgType[(int)b]++;
  			Main.rxDataType[(int)b] += length;
  			if (Main.netMode == 1 && Netplay.clientSock.statusMax > 0)
  			{
  				Netplay.clientSock.statusCount++;
  			}
  			if (Main.verboseNetplay)
  			{
  				for (int i = start; i < start + length; i++)
  				{
  				}
  				for (int j = start; j < start + length; j++)
  				{
  					byte arg_CD_0 = this.readBuffer[j];
  				}
  			}
  			if (Main.netMode == 2 && b != 38 && Netplay.serverSock[this.whoAmI].state == -1)
  			{
  				NetMessage.SendData(2, this.whoAmI, -1, Lang.mp[1], 0, 0f, 0f, 0f, 0);
  				return;
  			}
  			if (Main.netMode == 2 && Netplay.serverSock[this.whoAmI].state < 10 && b > 12 && b != 16 && b != 42 && b != 50 && b != 38 && b != 68)
  			{
  				NetMessage.BootPlayer(this.whoAmI, Lang.mp[2]);
  			}
  			binaryReader.BaseStream.Position = (long)num;
  			switch (b)
  			{
  			case 1:
  			{
  				if (Main.netMode != 2)
  				{
  					return;
  				}
  				if (Main.dedServ && Netplay.CheckBan(Netplay.serverSock[this.whoAmI].tcpClient.Client.RemoteEndPoint.ToString()))
  				{
  					NetMessage.SendData(2, this.whoAmI, -1, Lang.mp[3], 0, 0f, 0f, 0f, 0);
  					return;
  				}
  				if (Netplay.serverSock[this.whoAmI].state != 0)
  				{
  					return;
  				}
  				string a = binaryReader.ReadString();
  				if (!(a == "Terraria" + Main.curRelease))
  				{
  					NetMessage.SendData(2, this.whoAmI, -1, Lang.mp[4], 0, 0f, 0f, 0f, 0);
  					return;
  				}
  				if (string.IsNullOrEmpty(Netplay.password))
  				{
  					Netplay.serverSock[this.whoAmI].state = 1;
  					NetMessage.SendData(3, this.whoAmI, -1, "", 0, 0f, 0f, 0f, 0);
  					return;
  				}
  				Netplay.serverSock[this.whoAmI].state = -1;
  				NetMessage.SendData(37, this.whoAmI, -1, "", 0, 0f, 0f, 0f, 0);
  				return;
  			}
  			case 2:
  				if (Main.netMode != 1)
  				{
  					return;
  				}
  				Main.statusText = binaryReader.ReadString();
  				return;
  			case 3:
  			{
  				if (Main.netMode != 1)
  				{
  					return;
  				}
  				if (Netplay.clientSock.state == 1)
  				{
  					Netplay.clientSock.state = 2;
  				}
  				int num2 = (int)binaryReader.ReadByte();
  				if (num2 != Main.myPlayer)
  				{
  					Main.player[num2] = (Player)Main.player[Main.myPlayer].Clone();
  					Main.player[Main.myPlayer] = new Player();
  					Main.player[num2].whoAmi = num2;
  					Main.myPlayer = num2;
  				}
  				Player player = Main.player[num2];
  				NetMessage.SendData(4, -1, -1, player.name, num2, 0f, 0f, 0f, 0);
  				NetMessage.SendData(68, -1, -1, "", num2, 0f, 0f, 0f, 0);
  				NetMessage.SendData(16, -1, -1, "", num2, 0f, 0f, 0f, 0);
  				NetMessage.SendData(42, -1, -1, "", num2, 0f, 0f, 0f, 0);
  				NetMessage.SendData(50, -1, -1, "", num2, 0f, 0f, 0f, 0);
  				for (int k = 0; k < 59; k++)
  				{
  					NetMessage.SendData(5, -1, -1, player.inventory[k].name, num2, (float)k, (float)player.inventory[k].prefix, 0f, 0);
  				}
  				for (int l = 0; l < 16; l++)
  				{
  					NetMessage.SendData(5, -1, -1, player.armor[l].name, num2, (float)(59 + l), (float)player.armor[l].prefix, 0f, 0);
  				}
  				for (int m = 0; m < 8; m++)
  				{
  					NetMessage.SendData(5, -1, -1, player.dye[m].name, num2, (float)(75 + m), (float)player.dye[m].prefix, 0f, 0);
  				}
  				NetMessage.SendData(6, -1, -1, "", 0, 0f, 0f, 0f, 0);
  				if (Netplay.clientSock.state == 2)
  				{
  					Netplay.clientSock.state = 3;
  					return;
  				}
  				return;
  			}
  			case 4:
  			{
  				int num3 = (int)binaryReader.ReadByte();
  				if (Main.netMode == 2)
  				{
  					num3 = this.whoAmI;
  				}
  				if (num3 == Main.myPlayer && !Main.ServerSideCharacter)
  				{
  					return;
  				}
  				Player player2 = Main.player[num3];
  				player2.whoAmi = num3;
  				if (binaryReader.ReadByte() == 0)
  				{
  					player2.male = true;
  				}
  				else
  				{
  					player2.male = false;
  				}
  				player2.hair = (int)binaryReader.ReadByte();
  				if (player2.hair >= 123)
  				{
  					player2.hair = 0;
  				}
  				player2.name = binaryReader.ReadString().Trim().Trim();
  				player2.hairDye = binaryReader.ReadByte();
  				player2.hideVisual = binaryReader.ReadByte();
  				player2.hairColor = binaryReader.ReadRGB();
  				player2.skinColor = binaryReader.ReadRGB();
  				player2.eyeColor = binaryReader.ReadRGB();
  				player2.shirtColor = binaryReader.ReadRGB();
  				player2.underShirtColor = binaryReader.ReadRGB();
  				player2.pantsColor = binaryReader.ReadRGB();
  				player2.shoeColor = binaryReader.ReadRGB();
  				player2.difficulty = binaryReader.ReadByte();
  				if (Main.netMode != 2)
  				{
  					return;
  				}
  				bool flag = false;
  				if (Netplay.serverSock[this.whoAmI].state < 10)
  				{
  					for (int n = 0; n < 255; n++)
  					{
  						if (n != num3 && player2.name == Main.player[n].name && Netplay.serverSock[n].active)
  						{
  							flag = true;
  						}
  					}
  				}
  				if (flag)
  				{
  					if (!ServerApi.Hooks.InvokeNetNameCollision(num3, player2.name))
  					{
  						NetMessage.SendData(2, this.whoAmI, -1, player2.name + " " + Lang.mp[5], 0, 0f, 0f, 0f, 0);
  						return;
  					}
  				}
  				if (player2.name.Length > Player.nameLen)
  				{
  					NetMessage.SendData(2, this.whoAmI, -1, "Name is too long.", 0, 0f, 0f, 0f, 0);
  					return;
  				}
  				if (player2.name == "")
  				{
  					NetMessage.SendData(2, this.whoAmI, -1, "Empty name.", 0, 0f, 0f, 0f, 0);
  					return;
  				}
  				Netplay.serverSock[this.whoAmI].oldName = player2.name;
  				Netplay.serverSock[this.whoAmI].name = player2.name;
  				NetMessage.SendData(4, -1, this.whoAmI, player2.name, num3, 0f, 0f, 0f, 0);
  				return;
  			}
  			case 5:
  			{
  				int num4 = (int)binaryReader.ReadByte();
  				if (Main.netMode == 2)
  				{
  					num4 = this.whoAmI;
  				}
  				if (num4 == Main.myPlayer && !Main.ServerSideCharacter)
  				{
  					return;
  				}
  				Player player3 = Main.player[num4];
  				lock (player3)
  				{
  					int num5 = (int)binaryReader.ReadByte();
  					int stack = (int)binaryReader.ReadInt16();
  					int num6 = (int)binaryReader.ReadByte();
  					int type = (int)binaryReader.ReadInt16();
  					if (num5 < 59)
  					{
  						player3.inventory[num5] = new Item();
  						player3.inventory[num5].netDefaults(type);
  						player3.inventory[num5].stack = stack;
  						player3.inventory[num5].Prefix(num6);
  						if (num4 == Main.myPlayer && num5 == 58)
  						{
  							Main.mouseItem = player3.inventory[num5].Clone();
  						}
  					}
  					else if (num5 >= 75 && num5 <= 82)
  					{
  						int num7 = num5 - 58 - 17;
  						player3.dye[num7] = new Item();
  						player3.dye[num7].netDefaults(type);
  						player3.dye[num7].stack = stack;
  						player3.dye[num7].Prefix(num6);
  					}
  					else
  					{
  						int num8 = num5 - 58 - 1;
  						player3.armor[num8] = new Item();
  						player3.armor[num8].netDefaults(type);
  						player3.armor[num8].stack = stack;
  						player3.armor[num8].Prefix(num6);
  					}
  					if (Main.netMode == 2 && num4 == this.whoAmI)
  					{
  						NetMessage.SendData(5, -1, this.whoAmI, "", num4, (float)num5, (float)num6, 0f, 0);
  					}
  					return;
  				}
  				break;
  			}
  			case 6:
  				break;
  			case 7:
  			{
  				if (Main.netMode != 1)
  				{
  					return;
  				}
  				Main.time = (double)binaryReader.ReadInt32();
  				BitsByte bitsByte = binaryReader.ReadByte();
  				Main.dayTime = bitsByte[0];
  				Main.bloodMoon = bitsByte[1];
  				Main.eclipse = bitsByte[2];
  				Main.moonPhase = (int)binaryReader.ReadByte();
  				Main.maxTilesX = (int)binaryReader.ReadInt16();
  				Main.maxTilesY = (int)binaryReader.ReadInt16();
  				Main.spawnTileX = (int)binaryReader.ReadInt16();
  				Main.spawnTileY = (int)binaryReader.ReadInt16();
  				Main.worldSurface = (double)binaryReader.ReadInt16();
  				Main.rockLayer = (double)binaryReader.ReadInt16();
  				Main.worldID = binaryReader.ReadInt32();
  				Main.worldName = binaryReader.ReadString();
  				Main.moonType = (int)binaryReader.ReadByte();
  				WorldGen.setBG(0, (int)binaryReader.ReadByte());
  				WorldGen.setBG(1, (int)binaryReader.ReadByte());
  				WorldGen.setBG(2, (int)binaryReader.ReadByte());
  				WorldGen.setBG(3, (int)binaryReader.ReadByte());
  				WorldGen.setBG(4, (int)binaryReader.ReadByte());
  				WorldGen.setBG(5, (int)binaryReader.ReadByte());
  				WorldGen.setBG(6, (int)binaryReader.ReadByte());
  				WorldGen.setBG(7, (int)binaryReader.ReadByte());
  				Main.iceBackStyle = (int)binaryReader.ReadByte();
  				Main.jungleBackStyle = (int)binaryReader.ReadByte();
  				Main.hellBackStyle = (int)binaryReader.ReadByte();
  				Main.windSpeedSet = binaryReader.ReadSingle();
  				Main.numClouds = (int)binaryReader.ReadByte();
  				for (int num9 = 0; num9 < 3; num9++)
  				{
  					Main.treeX[num9] = binaryReader.ReadInt32();
  				}
  				for (int num10 = 0; num10 < 4; num10++)
  				{
  					Main.treeStyle[num10] = (int)binaryReader.ReadByte();
  				}
  				for (int num11 = 0; num11 < 3; num11++)
  				{
  					Main.caveBackX[num11] = binaryReader.ReadInt32();
  				}
  				for (int num12 = 0; num12 < 4; num12++)
  				{
  					Main.caveBackStyle[num12] = (int)binaryReader.ReadByte();
  				}
  				Main.maxRaining = binaryReader.ReadSingle();
  				Main.raining = (Main.maxRaining > 0f);
  				BitsByte bitsByte2 = binaryReader.ReadByte();
  				WorldGen.shadowOrbSmashed = bitsByte2[0];
  				NPC.downedBoss1 = bitsByte2[1];
  				NPC.downedBoss2 = bitsByte2[2];
  				NPC.downedBoss3 = bitsByte2[3];
  				Main.hardMode = bitsByte2[4];
  				NPC.downedClown = bitsByte2[5];
  				Main.ServerSideCharacter = bitsByte2[6];
  				NPC.downedPlantBoss = bitsByte2[7];
  				BitsByte bitsByte3 = binaryReader.ReadByte();
  				NPC.downedMechBoss1 = bitsByte3[0];
  				NPC.downedMechBoss2 = bitsByte3[1];
  				NPC.downedMechBoss3 = bitsByte3[2];
  				NPC.downedMechBossAny = bitsByte3[3];
  				Main.cloudBGActive = (float)(bitsByte3[4] ? 1 : 0);
  				WorldGen.crimson = bitsByte3[5];
  				Main.pumpkinMoon = bitsByte3[6];
  				Main.snowMoon = bitsByte3[7];
  				if (Netplay.clientSock.state == 3)
  				{
  					Netplay.clientSock.state = 4;
  					return;
  				}
  				return;
  			}
  			case 8:
  			{
  				if (Main.netMode != 2)
  				{
  					return;
  				}
  				int num13 = binaryReader.ReadInt32();
  				int num14 = binaryReader.ReadInt32();
  				bool flag3 = true;
  				if (num13 == -1 || num14 == -1)
  				{
  					flag3 = false;
  				}
  				else if (num13 < 10 || num13 > Main.maxTilesX - 10)
  				{
  					flag3 = false;
  				}
  				else if (num14 < 10 || num14 > Main.maxTilesY - 10)
  				{
  					flag3 = false;
  				}
  				int num15 = Netplay.GetSectionX(Main.spawnTileX) - 2;
  				int num16 = Netplay.GetSectionY(Main.spawnTileY) - 1;
  				int num17 = num15 + 5;
  				int num18 = num16 + 3;
  				if (num15 < 0)
  				{
  					num15 = 0;
  				}
  				if (num17 >= Main.maxSectionsX)
  				{
  					num17 = Main.maxSectionsX - 1;
  				}
  				if (num16 < 0)
  				{
  					num16 = 0;
  				}
  				if (num18 >= Main.maxSectionsY)
  				{
  					num18 = Main.maxSectionsY - 1;
  				}
  				int num19 = (num17 - num15) * (num18 - num16);
  				int num20 = -1;
  				int num21 = -1;
  				if (flag3)
  				{
  					num13 = Netplay.GetSectionX(num13) - 2;
  					num14 = Netplay.GetSectionY(num14) - 1;
  					num20 = num13 + 5;
  					num21 = num14 + 3;
  					if (num13 < 0)
  					{
  						num13 = 0;
  					}
  					if (num20 >= Main.maxSectionsX)
  					{
  						num20 = Main.maxSectionsX - 1;
  					}
  					if (num14 < 0)
  					{
  						num14 = 0;
  					}
  					if (num21 >= Main.maxSectionsY)
  					{
  						num21 = Main.maxSectionsY - 1;
  					}
  					for (int num22 = num13; num22 < num20; num22++)
  					{
  						for (int num23 = num14; num23 < num21; num23++)
  						{
  							if (num22 < num15 || num22 >= num17 || num23 < num16 || num23 >= num18)
  							{
  								num19++;
  							}
  						}
  					}
  				}
  				if (Netplay.serverSock[this.whoAmI].state == 2)
  				{
  					Netplay.serverSock[this.whoAmI].state = 3;
  				}
  				NetMessage.SendData(9, this.whoAmI, -1, Lang.inter[44], num19, 0f, 0f, 0f, 0);
  				Netplay.serverSock[this.whoAmI].statusText2 = "is receiving tile data";
  				Netplay.serverSock[this.whoAmI].statusMax += num19;
  				for (int num24 = num15; num24 < num17; num24++)
  				{
  					for (int num25 = num16; num25 < num18; num25++)
  					{
  						NetMessage.SendSection(this.whoAmI, num24, num25, false);
  					}
  				}
  				if (flag3)
  				{
  					for (int num26 = num13; num26 < num20; num26++)
  					{
  						for (int num27 = num14; num27 < num21; num27++)
  						{
  							NetMessage.SendSection(this.whoAmI, num26, num27, true);
  						}
  					}
  					NetMessage.SendData(11, this.whoAmI, -1, "", num13, (float)num14, (float)(num20 - 1), (float)(num21 - 1), 0);
  				}
  				NetMessage.SendData(11, this.whoAmI, -1, "", num15, (float)num16, (float)(num17 - 1), (float)(num18 - 1), 0);
  				for (int num28 = 0; num28 < 400; num28++)
  				{
  					if (Main.item[num28].active)
  					{
  						NetMessage.SendData(21, this.whoAmI, -1, "", num28, 0f, 0f, 0f, 0);
  						NetMessage.SendData(22, this.whoAmI, -1, "", num28, 0f, 0f, 0f, 0);
  					}
  				}
  				for (int num29 = 0; num29 < 200; num29++)
  				{
  					if (Main.npc[num29].active)
  					{
  						NetMessage.SendData(23, this.whoAmI, -1, "", num29, 0f, 0f, 0f, 0);
  					}
  				}
  				for (int num30 = 0; num30 < 1000; num30++)
  				{
  					if (Main.projectile[num30].active && (Main.projPet[Main.projectile[num30].type] || Main.projectile[num30].netImportant))
  					{
  						NetMessage.SendData(27, this.whoAmI, -1, "", num30, 0f, 0f, 0f, 0);
  					}
  				}
  				NetMessage.SendData(49, this.whoAmI, -1, "", 0, 0f, 0f, 0f, 0);
  				NetMessage.SendData(57, this.whoAmI, -1, "", 0, 0f, 0f, 0f, 0);
  				NetMessage.SendData(7, this.whoAmI, -1, "", 0, 0f, 0f, 0f, 0);
  				return;
  			}
  			case 9:
  				if (Main.netMode != 1)
  				{
  					return;
  				}
  				Netplay.clientSock.statusMax += binaryReader.ReadInt32();
  				Netplay.clientSock.statusText = binaryReader.ReadString();
  				return;
  			case 10:
  				if (Main.netMode != 1)
  				{
  					return;
  				}
  				NetMessage.DecompressTileBlock(this.readBuffer, num, length, true);
  				return;
  			case 11:
  				if (Main.netMode != 1)
  				{
  					return;
  				}
  				WorldGen.SectionTileFrame((int)binaryReader.ReadInt16(), (int)binaryReader.ReadInt16(), (int)binaryReader.ReadInt16(), (int)binaryReader.ReadInt16());
  				return;
  			case 12:
  			{
  				int num31 = (int)binaryReader.ReadByte();
  				if (Main.netMode == 2)
  				{
  					num31 = this.whoAmI;
  				}
  				Player player4 = Main.player[num31];
  				player4.SpawnX = (int)binaryReader.ReadInt16();
  				player4.SpawnY = (int)binaryReader.ReadInt16();
  				player4.Spawn();
  				if (Main.netMode != 2 || Netplay.serverSock[this.whoAmI].state < 3)
  				{
  					return;
  				}
  				if (Netplay.serverSock[this.whoAmI].state == 3)
  				{
  					Netplay.serverSock[this.whoAmI].state = 10;
  					NetMessage.greetPlayer(this.whoAmI);
  					NetMessage.buffer[this.whoAmI].broadcast = true;
  					Task.Factory.StartNew(() => NetMessage.syncJoin(this.whoAmI)).LogExceptions();
  					NetMessage.SendData(12, -1, this.whoAmI, "", this.whoAmI, 0f, 0f, 0f, 0);
  					return;
  				}
  				NetMessage.SendData(12, -1, this.whoAmI, "", this.whoAmI, 0f, 0f, 0f, 0);
  				return;
  			}
  			case 13:
  			{
  				int num32 = (int)binaryReader.ReadByte();
  				if (num32 == Main.myPlayer && !Main.ServerSideCharacter)
  				{
  					return;
  				}
  				if (Main.netMode == 2)
  				{
  					num32 = this.whoAmI;
  				}
  				Player player5 = Main.player[num32];
  				BitsByte bitsByte4 = binaryReader.ReadByte();
  				player5.controlUp = bitsByte4[0];
  				player5.controlDown = bitsByte4[1];
  				player5.controlLeft = bitsByte4[2];
  				player5.controlRight = bitsByte4[3];
  				player5.controlJump = bitsByte4[4];
  				player5.controlUseItem = bitsByte4[5];
  				player5.direction = (bitsByte4[6] ? 1 : -1);
  				BitsByte bitsByte5 = binaryReader.ReadByte();
  				if (bitsByte5[0])
  				{
  					player5.pulley = true;
  					player5.pulleyDir = (byte)(bitsByte5[1] ? 2 : 1);
  				}
  				else
  				{
  					player5.pulley = false;
  				}
  				player5.selectedItem = (int)binaryReader.ReadByte();
  				player5.position = binaryReader.ReadVector2();
  				if (bitsByte5[2])
  				{
  					player5.velocity = binaryReader.ReadVector2();
  				}
  				if (Main.netMode == 2 && Netplay.serverSock[this.whoAmI].state == 10)
  				{
  					NetMessage.SendData(13, -1, this.whoAmI, "", num32, 0f, 0f, 0f, 0);
  					return;
  				}
  				return;
  			}
  			case 14:
  			{
  				if (Main.netMode != 1)
  				{
  					return;
  				}
  				int num33 = (int)binaryReader.ReadByte();
  				int num34 = (int)binaryReader.ReadByte();
  				if (num34 == 1)
  				{
  					if (!Main.player[num33].active)
  					{
  						Main.player[num33] = new Player();
  					}
  					Main.player[num33].active = true;
  					return;
  				}
  				Main.player[num33].active = false;
  				return;
  			}
  			case 15:
  			case 67:
  				return;
  			case 16:
  			{
  				int num35 = (int)binaryReader.ReadByte();
  				if (num35 == Main.myPlayer && !Main.ServerSideCharacter)
  				{
  					return;
  				}
  				if (Main.netMode == 2)
  				{
  					num35 = this.whoAmI;
  				}
  				Player player6 = Main.player[num35];
  				player6.statLife = (int)binaryReader.ReadInt16();
  				player6.statLifeMax = (int)binaryReader.ReadInt16();
  
gt: 				if (player6.statLifeMax < 100)