-
Notifications
You must be signed in to change notification settings - Fork 16
/
MainForm.cs
280 lines (252 loc) · 9.78 KB
/
MainForm.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using EOLib.Config;
using EOLib.IO.Services;
using EOLib.Logger;
using EOLib.Net;
using EOLib.Net.PacketProcessing;
namespace PacketDecoder
{
public partial class MainForm : Form
{
private enum DataTypes
{
None = -1,
PacketFamily,
PacketAction,
Byte,
Char,
Short,
Three,
Int,
BreakString,
EndString,
FixedString,
}
private DataTypes m_type;
private readonly IPacketProcessActions _packetProcessActions;
private readonly IPacketEncoderRepository _packetEncoderRepository;
private int m_packetOffset, m_dataLength;
private bool m_suppressEvent;
public MainForm()
{
InitializeComponent();
cmbOutputFmt_SelectedIndexChanged(null, null);
_packetEncoderRepository = new PacketEncoderRepository();
_packetProcessActions = new PacketProcessActions(new SequenceRepository(),
_packetEncoderRepository,
new PacketEncoderService(new NumberEncoderService()),
new PacketSequenceService(),
new LoggerProvider(new LoggerFactory(new ConfigurationRepository())));
}
private void cmbOutputFmt_SelectedIndexChanged(object sender, EventArgs e)
{
m_type = (DataTypes) cmbOutputFmt.SelectedIndex;
if (m_type == DataTypes.FixedString)
{
txtLength.Enabled = true;
txtLength.Text = "";
}
else
{
txtLength.Enabled = false;
switch (m_type)
{
case DataTypes.BreakString:
case DataTypes.EndString:
case DataTypes.None:
txtLength.Text = "";
break;
case DataTypes.PacketFamily:
txtLength.Text = "1";
break;
case DataTypes.PacketAction:
txtLength.Text = "1";
break;
case DataTypes.Byte:
txtLength.Text = "1";
break;
case DataTypes.Char:
txtLength.Text = "1";
break;
case DataTypes.Short:
txtLength.Text = "2";
break;
case DataTypes.Three:
txtLength.Text = "3";
break;
case DataTypes.Int:
txtLength.Text = "4";
break;
}
}
_checkRequiredInputs();
}
private void intTextValidate(object sender, EventArgs e)
{
if (m_suppressEvent) return;
TextBox txt = sender as TextBox;
if (txt == null)
return;
int param;
if (!int.TryParse(txt.Text, out param))
{
if (txt == txtLength)
m_dataLength = 0;
return;
}
if (txt == txtDMulti)
{
_packetProcessActions.SetEncodeMultiples((byte)param, _packetEncoderRepository.SendMultiplier);
if (param < 6 || param > 12)
{
txtDMulti.BackColor = Color.FromArgb(255, 255, 128, 128);
}
else
{
txtDMulti.BackColor = Color.White;
}
}
else if (txt == txtEMulti)
{
_packetProcessActions.SetEncodeMultiples(_packetEncoderRepository.ReceiveMultiplier, (byte)param);
if (param < 6 || param > 12)
{
txtEMulti.BackColor = Color.FromArgb(255, 255, 128, 128);
}
else
{
txtEMulti.BackColor = Color.White;
}
}
else if (txt == txtOffset)
{
m_packetOffset = param;
if (param >= txtInputData.TextLength)
param = txtInputData.TextLength - 1;
}
else if (txt == txtLength)
m_dataLength = param;
_checkRequiredInputs();
}
private void _checkRequiredInputs()
{
txtOutput.Text = "";
if (txtDMulti.TextLength == 0 || txtEMulti.TextLength == 0 || txtOffset.TextLength == 0)
return;
if (txtLength.TextLength == 0 && m_type == DataTypes.FixedString)
return;
string inputData = txtInputData.Text;
if (inputData.Length == 0)
return;
//input data is copied from wireshark. colon delimited.
string bytes = inputData.Replace(":", "");
string len = bytes.Substring(0, 4);//first 2 bytes are the length!
bytes = bytes.Substring(4);
byte[] data = new byte[bytes.Length / 2];
byte[] lenDat = new byte[2];
for (int i = 0; i < len.Length; i += 2)
{
lenDat[i/2] = Convert.ToByte(len.Substring(i, 2), 16);
}
lblPacketLength.Text = "Packet Length: " + OldPacket.DecodeNumber(lenDat).ToString();
for (int i = 0; i < bytes.Length; i += 2)
{
data[i/2] = Convert.ToByte(bytes.Substring(i, 2), 16);
}
var pkt = _packetProcessActions.DecodeData(data);
pkt.Seek(m_packetOffset, SeekOrigin.Begin);
lblFamily.Text = pkt.Family.ToString();
lblAction.Text = pkt.Action.ToString();
string decoded = "";
for (int i = 0; i < pkt.Length; i++)
{
decoded += $"{pkt.RawData[i].ToString("D3")} ";
}
txtDecoded.Text = decoded;
switch ((DataTypes) cmbOutputFmt.SelectedIndex)
{
case DataTypes.None:
txtOutput.Text = pkt.ReadEndString();
break;
case DataTypes.PacketFamily:
txtOutput.Text = ((PacketFamily) pkt.PeekByte()).ToString();
break;
case DataTypes.PacketAction:
txtOutput.Text = ((PacketAction)pkt.PeekByte()).ToString();
break;
case DataTypes.Byte:
txtOutput.Text = pkt.PeekByte().ToString();
break;
case DataTypes.Char:
txtOutput.Text = pkt.PeekChar().ToString();
break;
case DataTypes.Short:
txtOutput.Text = pkt.PeekShort().ToString();
break;
case DataTypes.Three:
txtOutput.Text = pkt.PeekThree().ToString();
break;
case DataTypes.Int:
txtOutput.Text = pkt.PeekInt().ToString();
break;
case DataTypes.BreakString:
txtOutput.Text = pkt.PeekBreakString();
break;
case DataTypes.EndString:
txtOutput.Text = pkt.PeekEndString();
break;
case DataTypes.FixedString:
txtOutput.Text = pkt.PeekString(m_dataLength);
break;
default:
throw new ArgumentOutOfRangeException();
}
int selLen;
if (m_dataLength > 0) selLen = m_dataLength;
else
switch (m_type)
{
case DataTypes.EndString:
selLen = 3*(pkt.Length - pkt.ReadPosition) - 1;
break;
case DataTypes.BreakString:
int oldPos = pkt.ReadPosition;
while (pkt.ReadByte() != 255) ;
selLen = pkt.ReadPosition - oldPos;
pkt.Seek(oldPos, SeekOrigin.Begin);
break;
default:
selLen = 0;
break;
}
txtDecoded.Select(4 * m_packetOffset, 4 * selLen - 1);
if (m_type == DataTypes.EndString || m_type == DataTypes.BreakString)
{
m_suppressEvent = true;
txtLength.Text = selLen.ToString();
m_suppressEvent = false;
}
}
private void btnImportMultis_Click(object sender, EventArgs e)
{
string inp = Microsoft.VisualBasic.Interaction.InputBox("Paste the raw, colon-delimited packet data here: ", "Enter packet data");
if (inp.Length == 0)
return;
inp = inp.Replace(":", "");
if (inp.Length%2 != 0) return;
inp = inp.Substring(4);
byte[] data = new byte[inp.Length / 2];
for (int i = 0; i < inp.Length; i += 2)
data[i/2] = Convert.ToByte(inp.Substring(i, 2), 16);
//no need to decrypt since it's init data
OldPacket pkt = new OldPacket(data);
pkt.Skip(3);
txtDMulti.Text = pkt.GetByte().ToString();
txtEMulti.Text = pkt.GetByte().ToString();
_packetProcessActions.SetEncodeMultiples(pkt.Get()[5], pkt.Get()[6]);
}
}
}