Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- namespace ArcServer
- {
- public class Packet
- {
- public MemoryStream memoryStream;
- public BinaryReader binaryReader;
- public BinaryWriter binaryWriter;
- public short Opcode { get; set; }
- /// <summary>
- /// Write a new packet without opcode
- /// </summary>
- public Packet()
- {
- memoryStream = new MemoryStream();
- memoryStream.Position = 0;
- binaryReader = new BinaryReader(memoryStream);
- binaryWriter = new BinaryWriter(memoryStream);
- }
- /// <summary>
- /// Write a new packet
- /// </summary>
- /// <param name="Opc"></param>
- public Packet(short Opc)
- {
- memoryStream = new MemoryStream();
- memoryStream.Position = 0;
- binaryReader = new BinaryReader(memoryStream);
- binaryWriter = new BinaryWriter(memoryStream);
- this.Opcode = Opc;
- WriteShort(Opc);
- }
- /// <summary>
- /// Packet class made to READ packet
- /// </summary>
- /// <param name="packetArray"></param>
- public Packet(byte[] packetArray)
- {
- memoryStream = new MemoryStream(packetArray);
- memoryStream.Position = 0;
- binaryReader = new BinaryReader(memoryStream);
- binaryWriter = new BinaryWriter(memoryStream);
- Opcode = ReadShort();
- }
- /// <summary>
- /// Release packet from memory
- /// </summary>
- public void Dispose()
- {
- memoryStream.Dispose();
- }
- public int GetSize()
- {
- return (int)binaryWriter.BaseStream.Length;
- }
- public byte[] GetData()
- {
- byte[] totBuffer = new byte[memoryStream.Length];
- long startPos = memoryStream.Position;
- memoryStream.Position = 0;
- memoryStream.Read(totBuffer, 0, (int)memoryStream.Length);
- memoryStream.Position = startPos;
- return totBuffer;
- }
- /// <summary>
- /// Write whole file into packet
- /// </summary>
- /// <param name="path"></param>
- public void WriteFile(string path)
- {
- if (!File.Exists(path))
- {
- Console.WriteLine("Cannot find " + path);
- return;
- }
- Stream file = new FileStream(path, FileMode.Open);
- byte[] buffer = new byte[file.Length];
- file.Read(buffer, 0, (int)file.Length);
- WriteBytes(buffer);
- file.Dispose();
- }
- /// <summary>
- /// Return the packet buffer including lenght
- /// </summary>
- /// <returns></returns>
- public byte[] GetPacket()
- {
- byte[] totBuffer = new byte[memoryStream.Length + 4];
- Buffer.BlockCopy(BitConverter.GetBytes((int)memoryStream.Length), 0, totBuffer, 0, 4);
- Buffer.BlockCopy(memoryStream.ToArray(), 0, totBuffer, 4, (int)memoryStream.Length);
- return totBuffer;
- }
- /// <summary>
- /// Display packet in console
- /// </summary>
- public void Display()
- {
- DisplayFromPos(0);
- }
- public void DisplayFromPos(long pos)
- {
- long oldpos = memoryStream.Position;
- memoryStream.Position = pos;
- byte[] packetdata = new byte[this.memoryStream.Length - memoryStream.Position];
- memoryStream.Read(packetdata, 0, (int)memoryStream.Length - (int)memoryStream.Position);
- Console.Write("[" + DateTime.Now.ToShortDateString() + "] " + "Packet display: " + Opcode.ToString());
- foreach (byte x in packetdata)
- {
- Console.Write(x.ToString("X2") + " ");
- }
- Console.Write("\n");
- memoryStream.Position = oldpos;
- }
- public void Padding(int count) { for (int i = 0; i < count; i++) binaryWriter.Write((byte)00); }
- public void FillPadding(string value, int count)
- {
- int padding = count - value.Length;
- foreach (char c in value) binaryWriter.Write((byte)c);
- Padding(padding);
- }
- public byte[] ReadBytes(int count)
- {
- return binaryReader.ReadBytes(count);
- }
- public void WriteUShort(ushort shorty)
- {
- binaryWriter.Write(shorty);
- }
- public void WriteUInt(uint intege)
- {
- binaryWriter.Write(intege);
- }
- public void Fill(byte data, int len)
- {
- for (int i = 0; i < len; i++)
- {
- WriteByte(data);
- }
- }
- public void WriteBytes(byte[] data)
- {
- binaryWriter.Write(data);
- }
- public void FillBytes(byte[] bytes, int count)
- {
- int topad = count - bytes.Length;
- WriteBytes(bytes);
- Padding(topad);
- }
- public void SkipBytes(int count)
- {
- this.memoryStream.Position += count;
- }
- public void setIndex(long number)
- {
- this.memoryStream.Position = number;
- }
- public ushort ReadUShort() { return binaryReader.ReadUInt16(); }
- public byte ReadByte() { return binaryReader.ReadByte(); }
- public sbyte ReadSByte() { return binaryReader.ReadSByte(); }
- public string ReadStringL()
- {
- string tempString = null;
- int size = binaryReader.ReadInt16();
- char[] stringRead = binaryReader.ReadChars(size);
- foreach (char c in stringRead) tempString += c;
- return tempString;
- }
- public string ReadString(int length)
- {
- string tempString = null;
- char[] stringRead = binaryReader.ReadChars(length);
- foreach (char c in stringRead) tempString += c;
- return tempString.Replace("\0", "");
- }
- public short ReadShort() { return binaryReader.ReadInt16(); }
- public int ReadInt() { return binaryReader.ReadInt32(); }
- public void WriteByte(byte value) { binaryWriter.Write(value); }
- public void WriteSByte(sbyte value) { binaryWriter.Write(value); }
- public void WriteInt(int value) { binaryWriter.Write(value); }
- public void WriteShort(short value) { binaryWriter.Write(value); }
- public void WriteStringL(string value)
- {
- binaryWriter.Write((short)value.Length);
- foreach (char c in value) binaryWriter.Write(c);
- }
- public void WriteString(string value) { foreach (char c in value) binaryWriter.Write(c); }
- public void WriteArray(byte[] arrayToWrite) { foreach (byte b in arrayToWrite) binaryWriter.Write(b); }
- /// <summary>
- /// Write hex string to packet e.g. ("00 01 02 A5")
- /// </summary>
- /// <param name="stringToWrite"></param>
- public void WriteHexString(string stringToWrite)
- {
- string[] tempArray = stringToWrite.Split(' ');
- foreach (string s in tempArray)
- binaryWriter.Write(byte.Parse(s, System.Globalization.NumberStyles.HexNumber));
- }
- public void WriteLong(ulong p) { binaryWriter.Write((ulong)p); }
- public string ToReadableString()
- {
- return memoryStream.ToArray().ToReadableString();
- }
- public string ToReadableByteArray()
- {
- return memoryStream.ToArray().ToReadableByteArray();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement