Skip to content

Instantly share code, notes, and snippets.

@DDuarte
Created May 10, 2012 15:18
Show Gist options
  • Save DDuarte/2653856 to your computer and use it in GitHub Desktop.
Save DDuarte/2653856 to your computer and use it in GitHub Desktop.
From 402f59dd04b6f77c39edd2ada84a474697caba37 Mon Sep 17 00:00:00 2001
From: Nay <[email protected]>
Date: Thu, 10 May 2012 16:17:32 +0100
Subject: [PATCH] Remove BinaryReader from Packet and implement our own ReadX
---
WowPacketParser/Misc/Packet.cs | 49 ++++++++++++------
WowPacketParser/Misc/PacketReads.cs | 95 +++++++++++++++++++++++++++++++++++
WowPacketParser/Parsing/Handler.cs | 2 +-
3 files changed, 130 insertions(+), 16 deletions(-)
diff --git a/WowPacketParser/Misc/Packet.cs b/WowPacketParser/Misc/Packet.cs
index c870dee..5cd29a7 100644
--- a/WowPacketParser/Misc/Packet.cs
+++ b/WowPacketParser/Misc/Packet.cs
@@ -9,14 +9,13 @@ using WowPacketParser.Store.Objects;
namespace WowPacketParser.Misc
{
- public sealed partial class Packet : BinaryReader
+ public sealed partial class Packet : IDisposable
{
private static readonly bool SniffData = Settings.SQLOutput.HasAnyFlag(SQLOutputFlags.SniffData);
private static readonly bool SniffDataOpcodes = Settings.SQLOutput.HasAnyFlag(SQLOutputFlags.SniffDataOpcodes);
[SuppressMessage("Microsoft.Reliability", "CA2000", Justification = "MemoryStream is disposed in ClosePacket().")]
public Packet(byte[] input, int opcode, DateTime time, Direction direction, int number, StringBuilder writer, string fileName)
- : base(new MemoryStream(input, 0, input.Length), Encoding.UTF8)
{
Opcode = opcode;
Time = time;
@@ -26,22 +25,27 @@ namespace WowPacketParser.Misc
FileName = fileName;
Status = ParsedStatus.None;
WriteToFile = true;
+
+ _stream = new MemoryStream(input, 0, input.Length);
}
[SuppressMessage("Microsoft.Reliability", "CA2000", Justification = "MemoryStream is disposed in ClosePacket().")]
public Packet(byte[] input, int opcode, DateTime time, Direction direction, int number, string fileName)
- : base(new MemoryStream(input, 0, input.Length), Encoding.UTF8)
{
Opcode = opcode;
Time = time;
Direction = direction;
Number = number;
- Writer = new StringBuilder();
+ Writer = null;
FileName = fileName;
Status = ParsedStatus.None;
WriteToFile = true;
+
+ _stream = new MemoryStream(input, 0, input.Length);
}
+ private readonly MemoryStream _stream;
+
public int Opcode { get; private set; }
public DateTime Time { get; private set; }
public Direction Direction { get; private set; }
@@ -111,17 +115,17 @@ namespace WowPacketParser.Misc
public long Position
{
- get { return BaseStream.Position; }
+ get { return _stream.Position; }
}
public void SetPosition(long val)
{
- BaseStream.Position = val;
+ _stream.Position = val;
}
public long Length
{
- get { return BaseStream.Length; }
+ get { return _stream.Length; }
}
public bool CanRead()
@@ -131,31 +135,41 @@ namespace WowPacketParser.Misc
public void Write(string value)
{
+ if (Writer == null)
+ Writer = new StringBuilder();
+
Writer.Append(value);
}
public void Write(string format, params object[] args)
{
+ if (Writer == null)
+ Writer = new StringBuilder();
+
Writer.AppendFormat(format, args);
}
public void WriteLine()
{
+ if (Writer == null)
+ Writer = new StringBuilder();
+
Writer.AppendLine();
}
public void WriteLine(string value)
{
- Writer.AppendLine(value);
- }
+ if (Writer == null)
+ Writer = new StringBuilder();
- public void WriteLine(object value)
- {
- Writer.AppendLine(value.ToString());
+ Writer.AppendLine(value);
}
public void WriteLine(string format, params object[] args)
{
+ if (Writer == null)
+ Writer = new StringBuilder();
+
Writer.AppendLine(string.Format(format, args));
}
@@ -164,10 +178,15 @@ namespace WowPacketParser.Misc
if (Writer != null)
Writer.Clear();
- if (BaseStream != null)
- BaseStream.Close();
+ if (_stream != null)
+ _stream.Close();
- Dispose(true);
+ //Dispose(true);
+ }
+
+ public void Dispose()
+ {
+ ClosePacket();
}
}
}
diff --git a/WowPacketParser/Misc/PacketReads.cs b/WowPacketParser/Misc/PacketReads.cs
index fe39fc0..099149a 100644
--- a/WowPacketParser/Misc/PacketReads.cs
+++ b/WowPacketParser/Misc/PacketReads.cs
@@ -9,6 +9,101 @@ namespace WowPacketParser.Misc
{
public sealed partial class Packet
{
+ public byte[] ReadBytes(int count)
+ {
+ if (count < 0) throw new ArgumentOutOfRangeException("count", "ArgumentOutOfRange_NeedNonNegNum");
+
+ var result = new byte[count];
+
+ var numRead = 0;
+ do
+ {
+ var n = _stream.Read(result, numRead, count);
+ if (n == 0)
+ break;
+ numRead += n;
+ count -= n;
+ } while (count > 0);
+
+ if (numRead != result.Length)
+ {
+ // Trim array. This should happen on EOF & possibly net streams.
+ var copy = new byte[numRead];
+ Array.Copy(result, copy, numRead);
+ result = copy;
+ }
+
+ return result;
+ }
+
+ public char[] ReadChars(int count)
+ {
+ var bytes = ReadBytes(count);
+
+ var buffer = new StringBuilder();
+ foreach (var t in bytes)
+ buffer.Append(t);
+
+ return buffer.ToString().ToCharArray();
+ }
+
+ public Single ReadSingle()
+ {
+ return BitConverter.ToSingle(ReadBytes(4), 0);
+ }
+
+ public Double ReadDouble()
+ {
+ return BitConverter.ToDouble(ReadBytes(8), 0);
+ }
+
+ public Boolean ReadBoolean()
+ {
+ return ReadByte() > 0;
+ }
+
+ public Byte ReadByte()
+ {
+ return (byte) _stream.ReadByte();
+ }
+
+ public UInt16 ReadUInt16()
+ {
+ return BitConverter.ToUInt16(ReadBytes(2), 0);
+ }
+
+ public UInt32 ReadUInt32()
+ {
+ return BitConverter.ToUInt32(ReadBytes(4), 0);
+ }
+
+ public UInt64 ReadUInt64()
+ {
+ return BitConverter.ToUInt64(ReadBytes(8), 0);
+ }
+
+ public SByte ReadSByte()
+ {
+ return (sbyte) ReadBytes(1)[0]; // TODO: fix
+ }
+
+ public Int16 ReadInt16()
+ {
+ return BitConverter.ToInt16(ReadBytes(2), 0);
+ }
+
+ public Int32 ReadInt32()
+ {
+ return BitConverter.ToInt32(ReadBytes(4), 0);
+ }
+
+ public Int64 ReadInt64()
+ {
+ return BitConverter.ToInt64(ReadBytes(8), 0);
+ }
+
+ //...
+
public Guid ReadGuid()
{
var guid = new Guid(ReadUInt64());
diff --git a/WowPacketParser/Parsing/Handler.cs b/WowPacketParser/Parsing/Handler.cs
index f05dcf8..94edf44 100644
--- a/WowPacketParser/Parsing/Handler.cs
+++ b/WowPacketParser/Parsing/Handler.cs
@@ -111,7 +111,7 @@ namespace WowPacketParser.Parsing
}
catch (Exception ex)
{
- packet.WriteLine(ex.GetType());
+ packet.WriteLine(ex.GetType().ToString());
packet.WriteLine(ex.Message);
packet.WriteLine(ex.StackTrace);
--
1.7.9.msysgit.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment