using Filter.Handlers.IRC; using Filter.Handlers.Login; using Filter.Handlers.Manager; using System; using System.IO; using System.Text; namespace Filter.Networking.Instances { internal class Packet : IDisposable { private MemoryStream PacketStream; private BinaryWriter PacketWriter; private BinaryReader PacketReader; public ushort OPCode; public byte Header; public byte Type; public int Remaining { get { return Convert.ToInt32(this.PacketStream.Length - this.PacketStream.Position); } } public Packet(byte[] Buffer) { this.PacketStream = new MemoryStream(Buffer); this.PacketReader = new BinaryReader(this.PacketStream); } public Packet(byte TransferHeader, byte TransferType) { this.PacketStream = new MemoryStream(); this.PacketWriter = new BinaryWriter(this.PacketStream); this.Header = TransferHeader; this.Type = TransferType; this.OPCode = Convert.ToUInt16((this.Header << 10) + (this.Type & 1023)); this.WriteUInt16(this.OPCode); } public Packet(IRC1TypeServer Type) : this(1, Convert.ToByte(Type)) { } public Packet(Login2TypeServer Type) : this(2, Convert.ToByte(Type)) { } public Packet(Login3TypeClient Type) : this(3, Convert.ToByte(Type)) { } public Packet(Login3TypeServer Type) : this(3, Convert.ToByte(Type)) { } public Packet(Manager2TypeServer Type) : this(2, Convert.ToByte(Type)) { } public Packet(Manager3TypeClient Type) : this(3, Convert.ToByte(Type)) { } public Packet(Manager3TypeServer Type) : this(3, Convert.ToByte(Type)) { } public Packet(Manager4TypeClient Type) : this(4, Convert.ToByte(Type)) { } public Packet(Manager5TypeClient Type) : this(5, Convert.ToByte(Type)) { } public Packet(Manager5TypeServer Type) : this(5, Convert.ToByte(Type)) { } public Packet(Manager8TypeClient Type) : this(8, Convert.ToByte(Type)) { } public Packet(Manager8TypeServer Type) : this(8, Convert.ToByte(Type)) { } public Packet(Manager9TypeServer Type) : this(9, Convert.ToByte(Type)) { } public Packet(Manager21TypeClient Type) : this(21, Convert.ToByte(Type)) { } public Packet(Manager22TypeServer Type) : this(22, Convert.ToByte(Type)) { } public Packet(Manager25TypeServer Type) : this(25, Convert.ToByte(Type)) { } public Packet(Manager29TypeClient Type) : this(29, Convert.ToByte(Type)) { } public Packet(Manager38TypeClient Type) : this(38, Convert.ToByte(Type)) { } public void Dispose() { if (this.PacketStream.CanRead || this.PacketStream.CanWrite) { this.PacketStream.Close(); } if (this.PacketWriter != null) { this.PacketWriter.Close(); } if (this.PacketReader != null) { this.PacketReader.Close(); } if (this.PacketStream != null) { this.PacketStream.Dispose(); } if (this.PacketWriter != null) { this.PacketWriter.Dispose(); } if (this.PacketReader != null) { this.PacketReader.Dispose(); } } ~Packet() { this.Dispose(); } public bool ReadByte(out byte Value) { if (this.Remaining < 1) { Value = 0; return false; } Value = this.PacketReader.ReadByte(); return true; } public bool ReadBytes(byte[] Buffer) { if (this.Remaining < (int)Buffer.Length) { return false; } this.PacketStream.Read(Buffer, 0, (int)Buffer.Length); return true; } public bool ReadBytes(byte[] Buffer, int Length) { if (this.Remaining < Length) { return false; } Buffer = this.PacketReader.ReadBytes(Length); return true; } public bool ReadInt16(out short Value) { if (this.Remaining < 2) { Value = 0; return false; } Value = this.PacketReader.ReadInt16(); return true; } public bool ReadString(out string Value) { byte num; if (!this.ReadByte(out num)) { Value = string.Empty; return false; } if (this.Remaining < num) { Value = string.Empty; return false; } return this.ReadString(out Value, (int)num); } public bool ReadString(out string Value, int ReadLength) { if (this.Remaining < ReadLength) { Value = string.Empty; return false; } byte[] numArray = new byte[ReadLength]; if (!this.ReadBytes(numArray)) { Value = string.Empty; return false; } int readLength = 0; if (numArray[ReadLength - 1] == 0) { while (numArray[readLength] != 0 && readLength < ReadLength) { readLength++; } } else { readLength = ReadLength; } if (readLength <= 0) { Value = string.Empty; return false; } Value = Encoding.ASCII.GetString(numArray, 0, readLength); return true; } public bool ReadUInt16(out ushort Value) { if (this.Remaining < 2) { Value = 0; return false; } Value = this.PacketReader.ReadUInt16(); return true; } public bool SetHeaderAndType() { bool flag; this.ReadUInt16(out this.OPCode); try { this.Header = Convert.ToByte(this.OPCode >> 10); this.Type = Convert.ToByte(this.OPCode & 1023); flag = true; } catch { flag = false; } return flag; } public void ToArray(FiestaCrypto Crypto, out byte[] PacketBuffer) { byte[] num; byte[] array = this.PacketStream.ToArray(); Crypto.Crypt(array, 0, (int)array.Length); if ((int)array.Length > 255) { num = new byte[(int)array.Length + 3]; Buffer.BlockCopy(array, 0, num, 3, (int)array.Length); Buffer.BlockCopy(BitConverter.GetBytes(Convert.ToUInt16((int)array.Length)), 0, num, 1, 2); } else { num = new byte[(int)array.Length + 1]; Buffer.BlockCopy(array, 0, num, 1, (int)array.Length); num[0] = Convert.ToByte((int)array.Length); } PacketBuffer = num; } public void ToArray(out byte[] PacketBuffer) { byte[] num; byte[] array = this.PacketStream.ToArray(); if ((int)array.Length > 255) { num = new byte[(int)array.Length + 3]; Buffer.BlockCopy(array, 0, num, 3, (int)array.Length); Buffer.BlockCopy(BitConverter.GetBytes(Convert.ToUInt16((int)array.Length)), 0, num, 1, 2); } else { num = new byte[(int)array.Length + 1]; Buffer.BlockCopy(array, 0, num, 1, (int)array.Length); num[0] = Convert.ToByte((int)array.Length); } PacketBuffer = num; } public void WriteByte(byte Value) { this.PacketWriter.Write(Value); } public void WriteBytes(byte[] Value) { this.PacketWriter.Write(Value); } public void WriteInt16(short Value) { this.PacketWriter.Write(Value); } public void WriteString(string Value) { this.PacketWriter.Write(Encoding.ASCII.GetBytes(Value)); } public void WriteString(string Value, bool WriteLength) { this.WriteByte(Convert.ToByte(Value.Length)); this.WriteBytes(Encoding.ASCII.GetBytes(Value)); } public void WriteString(string Value, int FillLength) { byte[] bytes = Encoding.ASCII.GetBytes(Value); this.WriteBytes(bytes); for (int i = 0; i < FillLength - (int)bytes.Length; i++) { this.WriteByte(0); } } public void WriteUInt16(ushort Value) { this.PacketWriter.Write(Value); } } }