Guest User

Untitled

a guest
Apr 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.54 KB | None | 0 0
  1. public unsafe class DataPacket
  2.     {
  3.         private byte[] buffer;
  4.         private Action __event;
  5.         public Action Event { get { return __event; } }
  6.  
  7.         public DataPacket(int Size)
  8.         {
  9.             buffer = new byte[Size];
  10.         }
  11.         public DataPacket(int Size, Action _event)
  12.         {
  13.             buffer = new byte[Size];
  14.             __event = _event;
  15.         }
  16.  
  17.         public DataPacket(byte[] _buffer)
  18.         {
  19.             buffer = _buffer;
  20.         }
  21.         public DataPacket(byte[] _buffer, Action _event)
  22.         {
  23.             buffer = _buffer;
  24.             __event = _event;
  25.         }
  26.  
  27.         public byte* CreatePointer()
  28.         {
  29.             try
  30.             {
  31.                 byte[] buffer_copy = new byte[buffer.Length];
  32.                 Buffer.BlockCopy(buffer, 0, buffer_copy, 0, buffer.Length);
  33.  
  34.                 fixed (byte* pointer = buffer_copy)
  35.                     return pointer;
  36.             }
  37.             catch
  38.             {
  39.                 throw new MemoryPointException("Could not create the pointer of the buffer.");
  40.             }
  41.         }
  42.  
  43.         public byte Read(int Offset)
  44.         {
  45.             return buffer[Offset];
  46.         }
  47.         public ushort Read(int Offset)
  48.         {
  49.             return BitConverter.ToUInt16(buffer, Offset);
  50.         }
  51.         public uint Read(int Offset)
  52.         {
  53.             return BitConverter.ToUInt32(buffer, Offset);
  54.         }
  55.         public ulong Read(int Offset)
  56.         {
  57.             return BitConverter.ToUInt64(buffer, Offset);
  58.         }
  59.  
  60.         public void Write(int Offset, byte Value)
  61.         {
  62.             try
  63.             {
  64.                 fixed (byte* ptr = buffer)
  65.                     *((byte*)(ptr + Offset)) = Value;
  66.             }
  67.             catch
  68.             {
  69.                 throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
  70.             }
  71.         }
  72.         public void Write(int Offset, ushort Value)
  73.         {
  74.             try
  75.             {
  76.                 fixed (byte* ptr = buffer)
  77.                     *((ushort*)(ptr + Offset)) = Value;
  78.             }
  79.             catch
  80.             {
  81.                 throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
  82.             }
  83.         }
  84.         public void Write(int Offset, uint Value)
  85.         {
  86.             try
  87.             {
  88.                 fixed (byte* ptr = buffer)
  89.                     *((uint*)(ptr + Offset)) = Value;
  90.             }
  91.             catch
  92.             {
  93.                 throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
  94.             }
  95.         }
  96.         public void Write(int Offset, ulong Value)
  97.         {
  98.             try
  99.             {
  100.                 fixed (byte* ptr = buffer)
  101.                     *((ulong*)(ptr + Offset)) = Value;
  102.             }
  103.             catch
  104.             {
  105.                 throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
  106.             }
  107.         }
  108.         public void Write(byte* pointer, int Offset, byte Value)
  109.         {
  110.             try
  111.             {
  112.                 *((byte*)(pointer + Offset)) = Value;
  113.             }
  114.             catch
  115.             {
  116.                 throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
  117.             }
  118.         }
  119.         public void Write(byte* pointer, int Offset, ushort Value)
  120.         {
  121.             try
  122.             {
  123.                 *((ushort*)(pointer + Offset)) = Value;
  124.             }
  125.             catch
  126.             {
  127.                 throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
  128.             }
  129.         }
  130.         public void Write(byte* pointer, int Offset, uint Value)
  131.         {
  132.             try
  133.             {
  134.                 *((uint*)(pointer + Offset)) = Value;
  135.             }
  136.             catch
  137.             {
  138.                 throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
  139.             }
  140.         }
  141.         public void Write(byte* pointer, int Offset, ulong Value)
  142.         {
  143.             try
  144.             {
  145.                 *((ulong*)(pointer + Offset)) = Value;
  146.             }
  147.             catch
  148.             {
  149.                 throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
  150.             }
  151.         }
  152.     }
Add Comment
Please, Sign In to add comment