-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMaplePacket.cs
205 lines (191 loc) · 7.46 KB
/
MaplePacket.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace MapleShark
{
public sealed class MaplePacket : ListViewItem
{
private DateTime mTimestamp;
private bool mOutbound;
private ushort mBuild = 0;
private ushort mLocale = 0;
private ushort mOpcode = 0;
private byte[] mBuffer = null;
private int mCursor = 0;
internal MaplePacket(DateTime pTimestamp, bool pOutbound, ushort pBuild, ushort pLocale, ushort pOpcode, string pName, byte[] pBuffer)
: base(new string[] {
pTimestamp.ToString(),//((pTimestamp - new DateTime(1970, 1, 1)).Ticks / 10000 / 1000).ToString("X8"),
pOutbound ? "Outbound" : "Inbound",
pBuffer.Length.ToString(),
"0x" + pOpcode.ToString("X4"),
pName })
{
mTimestamp = pTimestamp;
mOutbound = pOutbound;
mBuild = pBuild;
mOpcode = pOpcode;
mBuffer = pBuffer;
mLocale = pLocale;
}
public DateTime Timestamp { get { return mTimestamp; } }
public bool Outbound { get { return mOutbound; } }
public ushort Build { get { return mBuild; } }
public ushort Locale { get { return mLocale; } }
public ushort Opcode { get { return mOpcode; } }
public new string Name { set { SubItems[4].Text = value; } }
public byte[] InnerBuffer { get { return mBuffer; } }
public int Cursor { get { return mCursor; } }
public int Length { get { return mBuffer.Length; } }
public int Remaining { get { return mBuffer.Length - mCursor; } }
public void Rewind() { mCursor = 0; }
public bool ReadByte(out byte pValue)
{
pValue = 0;
if (mCursor + 1 > mBuffer.Length) return false;
pValue = mBuffer[mCursor++];
return true;
}
public bool ReadSByte(out sbyte pValue)
{
pValue = 0;
if (mCursor + 1 > mBuffer.Length) return false;
pValue = (sbyte)mBuffer[mCursor++];
return true;
}
public bool ReadUShort(out ushort pValue)
{
pValue = 0;
if (mCursor + 2 > mBuffer.Length) return false;
pValue = (ushort)(mBuffer[mCursor++] |
mBuffer[mCursor++] << 8);
return true;
}
public bool ReadShort(out short pValue)
{
pValue = 0;
if (mCursor + 2 > mBuffer.Length) return false;
pValue = (short)(mBuffer[mCursor++] |
mBuffer[mCursor++] << 8);
return true;
}
public bool ReadUInt(out uint pValue)
{
pValue = 0;
if (mCursor + 4 > mBuffer.Length) return false;
pValue = (uint)(mBuffer[mCursor++] |
mBuffer[mCursor++] << 8 |
mBuffer[mCursor++] << 16 |
mBuffer[mCursor++] << 24);
return true;
}
public bool ReadInt(out int pValue)
{
pValue = 0;
if (mCursor + 4 > mBuffer.Length) return false;
pValue = (int)(mBuffer[mCursor++] |
mBuffer[mCursor++] << 8 |
mBuffer[mCursor++] << 16 |
mBuffer[mCursor++] << 24);
return true;
}
public bool ReadFloat(out float pValue)
{
pValue = 0;
if (mCursor + 4 > mBuffer.Length) return false;
pValue = BitConverter.ToSingle(mBuffer, mCursor);
mCursor += 4;
return true;
}
public bool ReadULong(out ulong pValue)
{
pValue = 0;
if (mCursor + 8 > mBuffer.Length) return false;
pValue = (ulong)(mBuffer[mCursor++] |
mBuffer[mCursor++] << 8 |
mBuffer[mCursor++] << 16 |
mBuffer[mCursor++] << 24 |
mBuffer[mCursor++] << 32 |
mBuffer[mCursor++] << 40 |
mBuffer[mCursor++] << 48 |
mBuffer[mCursor++] << 56);
return true;
}
public bool ReadLong(out long pValue)
{
pValue = 0;
if (mCursor + 8 > mBuffer.Length) return false;
pValue = (long)(mBuffer[mCursor++] |
mBuffer[mCursor++] << 8 |
mBuffer[mCursor++] << 16 |
mBuffer[mCursor++] << 24 |
mBuffer[mCursor++] << 32 |
mBuffer[mCursor++] << 40 |
mBuffer[mCursor++] << 48 |
mBuffer[mCursor++] << 56);
return true;
}
public bool ReadFlippedLong(out long pValue) // 5 6 7 8 1 2 3 4
{
pValue = 0;
if (mCursor + 8 > mBuffer.Length) return false;
pValue = (long)(
mBuffer[mCursor++] << 32 |
mBuffer[mCursor++] << 40 |
mBuffer[mCursor++] << 48 |
mBuffer[mCursor++] << 56 |
mBuffer[mCursor++] |
mBuffer[mCursor++] << 8 |
mBuffer[mCursor++] << 16 |
mBuffer[mCursor++] << 24);
return true;
}
public bool ReadDouble(out double pValue)
{
pValue = 0;
if (mCursor + 8 > mBuffer.Length) return false;
pValue = BitConverter.ToDouble(mBuffer, mCursor);
mCursor += 8;
return true;
}
public bool ReadBytes(byte[] pBytes) { return ReadBytes(pBytes, 0, pBytes.Length); }
public bool ReadBytes(byte[] pBytes, int pStart, int pLength)
{
if (mCursor + pLength > mBuffer.Length) return false;
Buffer.BlockCopy(mBuffer, mCursor, pBytes, pStart, pLength);
mCursor += pLength;
return true;
}
public bool ReadPaddedString(out string pValue, int pLength)
{
pValue = "";
if (mCursor + pLength > mBuffer.Length) return false;
int length = 0;
while (length < pLength && mBuffer[mCursor + length] != 0x00) ++length;
if (length > 0) pValue = Encoding.ASCII.GetString(mBuffer, mCursor, length);
mCursor += pLength;
return true;
}
public byte[] Dump()
{
byte[] buffer = new byte[mBuffer.Length + 13];
ushort size = (ushort)(mBuffer.Length);
long ticks = mTimestamp.Ticks;
buffer[0] = (byte)ticks;
buffer[1] = (byte)(ticks >> 8);
buffer[2] = (byte)(ticks >> 16);
buffer[3] = (byte)(ticks >> 24);
buffer[4] = (byte)(ticks >> 32);
buffer[5] = (byte)(ticks >> 40);
buffer[6] = (byte)(ticks >> 48);
buffer[7] = (byte)(ticks >> 56);
buffer[8] = (byte)size;
buffer[9] = (byte)(size >> 8);
buffer[10] = (byte)mOpcode;
buffer[11] = (byte)(mOpcode >> 8);
buffer[12] = mOutbound ? (byte)1 : (byte)0;
Buffer.BlockCopy(mBuffer, 0, buffer, 13, mBuffer.Length);
return buffer;
}
}
}