Guest User

Untitled

a guest
Jun 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. using System.Text;
  5.  
  6. namespace xBot
  7. {
  8.     /// <summary>
  9.     /// A class designed to hold the packet's data and read throughout it.
  10.     /// </summary>
  11.     public class PacketReader
  12.     {
  13.         private PacketReader() { } // Disable default constructor.
  14.         public PacketReader(byte[] data, bool encrypted)
  15.         {
  16.             packetData = data;
  17.             Pointer = 6; // Skip header.
  18.             Encrypted = encrypted;
  19.         }
  20.  
  21.         private byte[] packetData { get; set; }
  22.         public bool Encrypted { get; set; }
  23.         public int Pointer { get; set; }
  24.         public ushort Opcode { get { return BitConverter.ToUInt16(packetData, 2); } }
  25.         public int Length { get { return packetData.Length - 6; } }
  26.  
  27.         public T Read<T>()
  28.         {
  29.             TypeCode type = Type.GetTypeCode(typeof(T));
  30.             int size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(T));
  31.             byte[] tmp = new byte[size]; // Holds the data to be converted.
  32.             Buffer.BlockCopy(packetData, Pointer, tmp, 0, size);
  33.             //Array.Copy(packetData, Pointer, tmp, 0, size);
  34.             object returnValue = null; // The converted value (of the T type)
  35.  
  36.             switch (type)
  37.             {
  38.                 case TypeCode.Byte:
  39.                     returnValue = tmp[0];
  40.                     break;
  41.                 case TypeCode.UInt16:
  42.                     returnValue = BitConverter.ToUInt16(tmp, 0);
  43.                     break;
  44.                 case TypeCode.UInt32:
  45.                     returnValue = BitConverter.ToUInt32(tmp, 0);
  46.                     break;
  47.                 case TypeCode.UInt64:
  48.                     returnValue = BitConverter.ToUInt64(tmp, 0);
  49.                     break;
  50.                 case TypeCode.Single:
  51.                     returnValue = BitConverter.ToSingle(tmp, 0);
  52.                     break;
  53.                 default:
  54.                     throw new Exception("Unsupported datatype.");
  55.             }
  56.             Pointer += size;
  57.             return (T)Convert.ChangeType(returnValue, type);
  58.         }
  59.         public byte[] Read(int length)
  60.         {
  61.             byte[] tmp = new byte[length];
  62.             Array.ConstrainedCopy(packetData, Pointer, tmp, 0, length);
  63.             Pointer += length;
  64.             return tmp;
  65.         }
  66.         public string ReadString(Encoding encoding)
  67.         {
  68.             ushort length = Read<ushort>();
  69.             string value = null;
  70.  
  71.             if (length == 0)
  72.                 return "";
  73.             if (encoding == Encoding.Unicode) // Unicode string
  74.             {
  75.                 value = encoding.GetString(packetData, Pointer, length * 2);
  76.                 Pointer += length * 2;
  77.             }
  78.             else if (encoding == Encoding.ASCII)// ASCII string
  79.             {
  80.                 value = encoding.GetString(packetData, Pointer, length);
  81.                 Pointer += length;
  82.             }
  83.             return value;
  84.         }
  85.         public void SkipBytes(int amount)
  86.         {
  87.             Pointer += amount;            
  88.         }
  89.         public PacketWriter ToPacketWriter()
  90.         {
  91.             int temp = Pointer;
  92.             Pointer = 6;
  93.             PacketWriter writer = new PacketWriter(Opcode, Encrypted);
  94.             writer.AppendArray(Read(Length));
  95.             Pointer = temp;
  96.             return writer;
  97.         }
  98.     }
  99. }
Add Comment
Please, Sign In to add comment