Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public unsafe class DataPacket
- {
- private byte[] buffer;
- private Action __event;
- public Action Event { get { return __event; } }
- public DataPacket(int Size)
- {
- buffer = new byte[Size];
- }
- public DataPacket(int Size, Action _event)
- {
- buffer = new byte[Size];
- __event = _event;
- }
- public DataPacket(byte[] _buffer)
- {
- buffer = _buffer;
- }
- public DataPacket(byte[] _buffer, Action _event)
- {
- buffer = _buffer;
- __event = _event;
- }
- public byte* CreatePointer()
- {
- try
- {
- byte[] buffer_copy = new byte[buffer.Length];
- Buffer.BlockCopy(buffer, 0, buffer_copy, 0, buffer.Length);
- fixed (byte* pointer = buffer_copy)
- return pointer;
- }
- catch
- {
- throw new MemoryPointException("Could not create the pointer of the buffer.");
- }
- }
- public byte Read(int Offset)
- {
- return buffer[Offset];
- }
- public ushort Read(int Offset)
- {
- return BitConverter.ToUInt16(buffer, Offset);
- }
- public uint Read(int Offset)
- {
- return BitConverter.ToUInt32(buffer, Offset);
- }
- public ulong Read(int Offset)
- {
- return BitConverter.ToUInt64(buffer, Offset);
- }
- public void Write(int Offset, byte Value)
- {
- try
- {
- fixed (byte* ptr = buffer)
- *((byte*)(ptr + Offset)) = Value;
- }
- catch
- {
- throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
- }
- }
- public void Write(int Offset, ushort Value)
- {
- try
- {
- fixed (byte* ptr = buffer)
- *((ushort*)(ptr + Offset)) = Value;
- }
- catch
- {
- throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
- }
- }
- public void Write(int Offset, uint Value)
- {
- try
- {
- fixed (byte* ptr = buffer)
- *((uint*)(ptr + Offset)) = Value;
- }
- catch
- {
- throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
- }
- }
- public void Write(int Offset, ulong Value)
- {
- try
- {
- fixed (byte* ptr = buffer)
- *((ulong*)(ptr + Offset)) = Value;
- }
- catch
- {
- throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
- }
- }
- public void Write(byte* pointer, int Offset, byte Value)
- {
- try
- {
- *((byte*)(pointer + Offset)) = Value;
- }
- catch
- {
- throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
- }
- }
- public void Write(byte* pointer, int Offset, ushort Value)
- {
- try
- {
- *((ushort*)(pointer + Offset)) = Value;
- }
- catch
- {
- throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
- }
- }
- public void Write(byte* pointer, int Offset, uint Value)
- {
- try
- {
- *((uint*)(pointer + Offset)) = Value;
- }
- catch
- {
- throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
- }
- }
- public void Write(byte* pointer, int Offset, ulong Value)
- {
- try
- {
- *((ulong*)(pointer + Offset)) = Value;
- }
- catch
- {
- throw new DataWritingException("Could not write the data into the buffer. Make sure the offset is correct.");
- }
- }
- }
Add Comment
Please, Sign In to add comment