forked from Simonlamb1979/CreatureScriptsParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParsers.cs
292 lines (243 loc) · 11.8 KB
/
Parsers.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
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading.Tasks;
using static CreatureScriptsParser.Packets;
namespace CreatureScriptsParser
{
public class Parsers
{
private MainForm mainForm;
public Parsers(MainForm mainForm) { this.mainForm = mainForm; }
private void ChangeButtonsState(bool state)
{
mainForm.toolStripButton_ImportSniff.Enabled = state;
mainForm.toolStripTextBox_CreatureGuid.Enabled = state;
mainForm.toolStripButton_Search.Enabled = state;
mainForm.Update();
}
public List<object> GetPacketsFromSniffFile(string fileName, bool createDataFile)
{
ChangeButtonsState(false);
mainForm.toolStripStatusLabel_FileStatus.Text = "Current status: Reading all lines...";
mainForm.Update();
string[] lines = File.ReadAllLines(fileName);
List<object> packetsList = GetDataFromPackets(lines, GetPacketIndexesFromLines(lines));
if (createDataFile)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (FileStream fileStream = new FileStream(fileName.Replace("_parsed.txt", "_packets.dat"), FileMode.OpenOrCreate))
{
binaryFormatter.Serialize(fileStream, packetsList);
}
}
ChangeButtonsState(true);
return packetsList;
}
public List<object> GetPacketsFromDataFile(string fileName)
{
ChangeButtonsState(false);
mainForm.toolStripStatusLabel_FileStatus.Text = "Current status: Getting packets from data file...";
BinaryFormatter binaryFormatter = new BinaryFormatter();
List<object> packetsList;
using (FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate))
{
packetsList = (List<object>)binaryFormatter.Deserialize(fileStream);
}
ChangeButtonsState(true);
return packetsList;
}
private SynchronizedCollection<Packet> GetPacketIndexesFromLines(string[] lines)
{
mainForm.toolStripStatusLabel_FileStatus.Text = "Current status: Getting packet indexes...";
mainForm.Update();
SynchronizedCollection<Packet> packetIndexesDictionary = new SynchronizedCollection<Packet>();
Parallel.For(0, lines.Length, index =>
{
foreach (var packetName in Enum.GetNames(typeof(Packet.PacketTypes)))
{
if (lines[index].Contains(packetName))
{
long i = index;
Enum.TryParse(packetName, out Packet.PacketTypes packetType);
switch (packetType)
{
case Packet.PacketTypes.SMSG_UPDATE_OBJECT:
{
Dictionary<long, long> indexesDictionary = new Dictionary<long, long>();
bool hasDestroyObjects = false;
do
{
i++;
if (!hasDestroyObjects && lines[i].Contains("DestroyedObjCount") && !lines[i].Contains("DestroyedObjCount : 0"))
{
hasDestroyObjects = true;
}
if (lines[i].Contains("UpdateType:") || ((lines[i].Contains("DestroyedObjCount") || lines[i].Contains("DataSize")) && hasDestroyObjects) || lines[i] == "")
{
if (indexesDictionary.Count() == 0)
{
indexesDictionary.Add(i, 0);
}
else if (indexesDictionary.Last().Value == 0)
{
indexesDictionary[indexesDictionary.Last().Key] = i;
if (lines[i] != "" && !lines[i].Contains("DataSize"))
{
indexesDictionary.Add(i, 0);
}
}
else
{
indexesDictionary.Add(i, 0);
}
}
}
while (lines[i] != "");
packetIndexesDictionary.Add(new Packet(packetType, LineGetters.GetTimeSpanFromLine(lines[index]), LineGetters.GetPacketNumberFromLine(lines[index]), indexesDictionary));
break;
}
case Packet.PacketTypes.SMSG_AURA_UPDATE:
{
Dictionary<long, long> indexesDictionary = new Dictionary<long, long>();
do
{
i++;
if (lines[i].Contains("Slot") || lines[i] == "")
{
if (indexesDictionary.Count() == 0)
{
indexesDictionary.Add(i, 0);
}
else if (indexesDictionary.Last().Value == 0)
{
indexesDictionary[indexesDictionary.Last().Key] = i - 1;
if (lines[i] != "")
{
indexesDictionary.Add(i, 0);
}
}
}
}
while (lines[i] != "");
packetIndexesDictionary.Add(new Packet(packetType, LineGetters.GetTimeSpanFromLine(lines[index]), LineGetters.GetPacketNumberFromLine(lines[index]), indexesDictionary));
break;
}
default:
{
do
{
i++;
}
while (lines[i] != "");
packetIndexesDictionary.Add(new Packet(packetType, LineGetters.GetTimeSpanFromLine(lines[index]), LineGetters.GetPacketNumberFromLine(lines[index]), index, i));
break;
}
}
break;
}
}
});
return packetIndexesDictionary;
}
private List<object> GetDataFromPackets(string[] lines, SynchronizedCollection<Packet> packets)
{
mainForm.toolStripStatusLabel_FileStatus.Text = "Current status: Getting data from packets...";
mainForm.Update();
List<object> packetsList = new List<object>();
foreach(Packet packet in packets)
{
switch (packet.type)
{
case Packet.PacketTypes.SMSG_UPDATE_OBJECT:
{
foreach(var updatePacket in UpdateObjectPacket.ParseObjectUpdatePacket(lines, packet))
{
packetsList.Add(updatePacket);
}
break;
}
case Packet.PacketTypes.SMSG_SPELL_START:
case Packet.PacketTypes.SMSG_SPELL_GO:
{
SpellStartPacket spellPacket = SpellStartPacket.ParseSpellStartPacket(lines, packet);
if (spellPacket.guid != "")
{
packetsList.Add(spellPacket);
}
break;
}
case Packet.PacketTypes.SMSG_ON_MONSTER_MOVE:
{
MonsterMovePacket monsterMovePacket = MonsterMovePacket.ParseMovementPacket(lines, packet);
if (monsterMovePacket.guid != "" && (monsterMovePacket.waypoints.Count() != 0 || monsterMovePacket.HasJump() ||
monsterMovePacket.HasOrientation() || monsterMovePacket.hasFacingToPlayer))
{
packetsList.Add(monsterMovePacket);
}
break;
}
case Packet.PacketTypes.SMSG_PLAY_ONE_SHOT_ANIM_KIT:
{
PlayOneShotAnimKit playOneShotAnimKitPacket = PlayOneShotAnimKit.ParsePlayOneShotAnimKitPacket(lines, packet);
if (playOneShotAnimKitPacket.guid != "")
{
packetsList.Add(playOneShotAnimKitPacket);
}
break;
}
case Packet.PacketTypes.SMSG_CHAT:
{
ChatPacket chatPacket = ChatPacket.ParseChatPacket(lines, packet);
if (chatPacket.guid != "")
{
packetsList.Add(chatPacket);
}
break;
}
case Packet.PacketTypes.SMSG_EMOTE:
{
EmotePacket emotePacket = EmotePacket.ParseEmotePacket(lines, packet);
if (emotePacket.guid != "")
{
packetsList.Add(emotePacket);
}
break;
}
case Packet.PacketTypes.SMSG_AURA_UPDATE:
{
foreach(var auraPacket in AuraUpdatePacket.ParseAuraUpdatePacket(lines, packet))
{
packetsList.Add(auraPacket);
}
break;
}
case Packet.PacketTypes.SMSG_SET_AI_ANIM_KIT:
{
SetAiAnimKit setAiAnimKitPacket = SetAiAnimKit.ParseSetAiAnimKitPacket(lines, packet);
if (setAiAnimKitPacket.guid != "")
{
packetsList.Add(setAiAnimKitPacket);
}
break;
}
case Packet.PacketTypes.SMSG_PLAY_SPELL_VISUAL_KIT:
{
PlaySpellVisualKit playSpellVisualKitPacket = PlaySpellVisualKit.ParsePlaySpellVisualKitPacket(lines, packet);
if (playSpellVisualKitPacket.guid != "")
{
packetsList.Add(playSpellVisualKitPacket);
}
break;
}
default:
break;
}
}
SpellStartPacket.FilterSpellPackets(packetsList);
return packetsList;
}
}
}