forked from zh3305/MapleShark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacketReader.cs
164 lines (145 loc) · 3.47 KB
/
PacketReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.IO;
using System.Text;
namespace MapleLib.PacketLib
{
/// <summary>
/// Class to handle reading data from a packet
/// </summary>
public class PacketReader
{
protected MemoryStream _buffer;
/// <summary>
/// The main reader tool
/// </summary>
private readonly BinaryReader _binReader;
/// <summary>
/// Amount of data left in the reader
/// </summary>
public int Length
{
get { return (int)_buffer.Length; }
}
/// <summary>
/// Creates a new instance of PacketReader
/// </summary>
/// <param name="arrayOfBytes">Starting byte array</param>
public PacketReader(byte[] arrayOfBytes)
{
_buffer = new MemoryStream(arrayOfBytes, false);
_binReader = new BinaryReader(_buffer, Encoding.Default);
}
/// <summary>
/// Restart reading from the point specified.
/// </summary>
/// <param name="length">The point of the packet to start reading from.</param>
public void Reset(int length)
{
_buffer.Seek(length, SeekOrigin.Begin);
}
public void Skip(int length)
{
_buffer.Position += length;
}
public int Position {
get {
return (int)_buffer.Position;
}
}
public int Remaining {
get {
return Length - (int)_buffer.Position;
}
}
/// <summary>
/// Reads an unsigned byte from the stream
/// </summary>
/// <returns> an unsigned byte from the stream</returns>
public byte ReadByte()
{
return _binReader.ReadByte();
}
/// <summary>
/// Reads a byte array from the stream
/// </summary>
/// <param name="length">Amount of bytes</param>
/// <returns>A byte array</returns>
public byte[] ReadBytes(int count)
{
return _binReader.ReadBytes(count);
}
/// <summary>
/// Reads a bool from the stream
/// </summary>
/// <returns>A bool</returns>
public bool ReadBool()
{
return _binReader.ReadBoolean();
}
/// <summary>
/// Reads a signed short from the stream
/// </summary>
/// <returns>A signed short</returns>
public short ReadShort()
{
return _binReader.ReadInt16();
}
/// <summary>
/// Reads an unsigned short from the stream
/// </summary>
/// <returns>A signed short</returns>
public ushort ReadUShort() {
return _binReader.ReadUInt16();
}
/// <summary>
/// Reads a signed int from the stream
/// </summary>
/// <returns>A signed int</returns>
public int ReadInt()
{
return _binReader.ReadInt32();
}
/// <summary>
/// Reads an unsigned int from the stream
/// </summary>
/// <returns>A signed int</returns>
public uint ReadUInt() {
return _binReader.ReadUInt32();
}
/// <summary>
/// Reads a signed long from the stream
/// </summary>
/// <returns>A signed long</returns>
public long ReadLong()
{
return _binReader.ReadInt64();
}
/// <summary>
/// Reads an unsigned long from the stream
/// </summary>
/// <returns>A signed long</returns>
public ulong ReadULong() {
return _binReader.ReadUInt64();
}
/// <summary>
/// Reads an ASCII string from the stream
/// </summary>
/// <param name="length">Amount of bytes</param>
/// <returns>An ASCII string</returns>
public string ReadString(int length)
{
return Encoding.Default.GetString(ReadBytes(length));
}
/// <summary>
/// Reads a maple string from the stream
/// </summary>
/// <returns>A maple string</returns>
public string ReadMapleString()
{
return ReadString(ReadShort());
}
public byte[] ToArray()
{ return _buffer.ToArray();
}
}
}