-
Notifications
You must be signed in to change notification settings - Fork 4
/
PacketLog.cs
278 lines (241 loc) · 6.88 KB
/
PacketLog.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
using System;
using System.Collections;
using System.Collections.Generic;
namespace PacketLogConverter
{
public delegate void PacketLogUpdate(PacketLog logManager);
/// <summary>
/// Holds all the log data and all information about it
/// </summary>
public class PacketLog : IEnumerable<Packet>, IIndexedContainer<Packet>
{
private bool m_isDirty = true;
private string m_streamName = string.Empty;
private int m_unknownPacketsCount;
private float m_version;
private bool m_reinitRequired;
private bool m_ignoreVersionChanges;
private bool m_subversionReinit = false;
private readonly List<Packet> m_packets = new List<Packet>();
public event PacketLogUpdate OnPacketsChanged;
private bool m_logSelected;
public bool LogSelected
{
get { return m_logSelected; }
set { m_logSelected = value; }
}
public bool IsDirty
{
get { return m_isDirty; }
set { m_isDirty = value; }
}
public string StreamName
{
get { return m_streamName; }
set { m_streamName = value; }
}
/// <summary>
/// Gets the <see cref="T:Packet"/> at the specified index.
/// </summary>
/// <value></value>
public Packet this[int index]
{
get { return m_packets[index]; }
}
/// <summary>
/// Adds the packet.
/// </summary>
/// <param name="pak">The pak.</param>
public void AddPacket(Packet pak)
{
if (pak == null)
throw new ArgumentNullException("pak");
m_packets.Add(pak);
FirePacketLogsChangedEvent();
}
/// <summary>
/// Inserts the packet.
/// </summary>
/// <param name="position">The position.</param>
/// <param name="pak">The pak.</param>
public void InsertPacket(int position, Packet pak)
{
if (pak == null)
throw new ArgumentNullException("pak");
m_packets.Insert(position, pak);
FirePacketLogsChangedEvent();
}
/// <summary>
/// Adds the range.
/// </summary>
/// <param name="collection">The collection.</param>
public void AddRange(ICollection<Packet> collection)
{
m_packets.AddRange(collection);
FirePacketLogsChangedEvent();
}
public List<Packet> GetRange(int startIndex, int endIndex)
{
List<Packet> ret = m_packets.GetRange(startIndex, endIndex - startIndex); // need there + 1 ?
return ret;
}
/// <summary>
/// Gets the count of objects contained in the container.
/// </summary>
/// <value>The count.</value>
public int Count
{
get { return m_packets.Count; }
}
/// <summary>
/// Gets the unknown packets count.
/// </summary>
/// <value>The unknown packets count.</value>
public int UnknownPacketsCount
{
get { return m_unknownPacketsCount; }
}
/// <summary>
/// Gets or sets the version.
/// </summary>
/// <value>The version.</value>
public float Version
{
get { return m_version; }
set
{
if (IgnoreVersionChanges)
return;
if (m_version != value)
m_reinitRequired = true;
IsDirty = m_version != value;
m_version = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether version changes should be ignored.
/// </summary>
/// <value>
/// <c>true</c> if version changes ignored; otherwise, <c>false</c>.
/// </value>
public bool IgnoreVersionChanges
{
get { return m_ignoreVersionChanges; }
set
{
IsDirty = m_ignoreVersionChanges != value;
m_ignoreVersionChanges = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether [reinit required].
/// </summary>
/// <value><c>true</c> if [reinit required]; otherwise, <c>false</c>.</value>
public bool ReinitRequired
{
get { return m_reinitRequired; }
set { m_reinitRequired = value; }
}
public bool SubversionReinit
{
get { return m_subversionReinit; }
set { m_subversionReinit = value; }
}
/// <summary>
/// Loads the packet parsers based on current log version.
/// </summary>
/// <param name="manager">The log manager initializing logs.</param>
/// <param name="maxRepeat">The max repeat.</param>
/// <param name="callback">The callback for UI updates.</param>
public void Init(LogManager manager, int maxRepeat, ProgressCallback callback)
{
if (maxRepeat < 0)
{
Log.Info("Log info keep changing, giving up version auto detection.");
return;
}
ReinitRequired = false;
int workTotal = m_packets.Count;
int workDone = 0;
for (int i = 0; i < m_packets.Count; i++)
{
if (callback != null && (workDone++ & 0xFFF) == 0)
callback(workDone, workTotal);
Packet packet = (Packet)m_packets[i];
// LogWriters.Logger.Say("maxRepeat:{0} IsDirty:{1} ver:{2} reinit:{3} [{4}]:{5}", maxRepeat, IsDirty, Version, ReinitRequired, i, packet.GetType().ToString());
if (packet.AllowClassChange)
packet = PacketManager.ChangePacketClass(packet, Version);
packet.InitLog(this);
m_packets[i] = packet;
if (ReinitRequired)
{
Init(manager, maxRepeat - 1, callback);
return; // version changed by the packets, start again...
}
}
m_unknownPacketsCount = 0;
foreach (Packet packet in m_packets)
{
// if (callback != null && (workDone++ & 0xFFF) == 0)
// callback(workDone, workTotal);
try
{
packet.InitException = null;
packet.Initialized = false;
packet.Position = 0;
packet.Init();
packet.Initialized = true;
}
catch (OutOfMemoryException e)
{
packet.InitException = e;
packet.Initialized = false;
Log.Info(string.Format("{0}: {1}", e.Message, packet.GetType().ToString()));
}
catch (Exception e)
{
packet.InitException = e;
packet.Initialized = false;
}
packet.PositionAfterInit = packet.Position;
if (packet.GetType() == typeof(Packet))
++m_unknownPacketsCount;
}
}
/// <summary>
/// Fires the packet logs changed event.
/// </summary>
private void FirePacketLogsChangedEvent()
{
PacketLogUpdate evnt = OnPacketsChanged;
if (evnt != null)
{
evnt(this);
}
}
#region IEnumerable Members
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
/// </returns>
public IEnumerator<Packet> GetEnumerator()
{
return m_packets.GetEnumerator();
}
///<summary>
///Returns an enumerator that iterates through a collection.
///</summary>
///
///<returns>
///An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
///</returns>
///<filterpriority>2</filterpriority>
IEnumerator IEnumerable.GetEnumerator()
{
return m_packets.GetEnumerator();
}
#endregion
}
}