Skip to content

Commit 1521cb2

Browse files
committed
Optimize Packet.AddValue packet field path formatting
1 parent 3d5544a commit 1521cb2

3 files changed

Lines changed: 28 additions & 19 deletions

File tree

WowPacketParser/Misc/Extensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics.Contracts;
55
using System.Globalization;
66
using System.Linq;
7+
using System.Text;
78
using System.Threading;
89
using WowPacketParser.Enums;
910
using WowPacketParser.Proto;
@@ -176,6 +177,29 @@ public static IEnumerable<T> Flatten<T>(this IEnumerable<T> values)
176177
}
177178
}
178179

180+
public static StringBuilder AppendObjectPropertyPath(this StringBuilder builder, object[] keys)
181+
{
182+
foreach (var value in keys)
183+
{
184+
if (value == null)
185+
continue;
186+
187+
switch (value)
188+
{
189+
case string str:
190+
builder.Append('(').Append(str).Append(") ");
191+
break;
192+
case object[] nested:
193+
builder.AppendObjectPropertyPath(nested);
194+
break;
195+
default:
196+
builder.Append('[').Append(value).Append("] ");
197+
break;
198+
}
199+
}
200+
return builder;
201+
}
202+
179203
public static string GetExtension(this FileCompression value)
180204
{
181205
var attributes = (FileCompressionAttribute[])value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(FileCompressionAttribute), false);

WowPacketParser/Misc/Packet.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
using System.Diagnostics.CodeAnalysis;
44
using System.IO;
55
using System.Net;
6-
using System.Runtime.InteropServices;
76
using System.Text;
87
using WowPacketParser.Enums;
98
using WowPacketParser.Enums.Version;
10-
using WowPacketParser.Parsing.Parsers;
119
using WowPacketParser.Proto;
1210
using WowPacketParser.Store;
1311
using WowPacketParser.Store.Objects;
@@ -248,7 +246,7 @@ public T AddValue<T>(string name, T obj, params object[] indexes)
248246
return obj;
249247

250248
Writer ??= new StringBuilder();
251-
Writer.AppendLine($"{GetIndexString(indexes)}{name}: {obj}");
249+
Writer.AppendObjectPropertyPath(indexes).Append(name).Append(": ").Append(obj).AppendLine();
252250

253251
return obj;
254252
}
@@ -259,7 +257,7 @@ public void AddLine(string value, params object[] indexes)
259257
return;
260258

261259
Writer ??= new StringBuilder();
262-
Writer.AppendLine($"{GetIndexString(indexes)}{value}");
260+
Writer.AppendObjectPropertyPath(indexes).Append(value).AppendLine();
263261
}
264262
}
265263
}

WowPacketParser/Misc/PacketReads.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,8 @@ public byte[] ReadBytesString(string name, int length, params object[] indexes)
455455
public byte[] ReadBytesTable(string name, int length, params object[] indexes)
456456
{
457457
var val = ReadBytes(length);
458-
AddValue(name, Utilities.ByteArrayToHexTable(val, true,
459-
GetIndexString(indexes).Length + name.Length + ": ".Length),
460-
indexes);
458+
AddLine(name, indexes);
459+
Write(Utilities.ByteArrayToHexTable(val, true));
461460
return val;
462461
}
463462

@@ -760,17 +759,5 @@ private static string FormatInteger(IFormattable value, string name)
760759
return $"{value} (0x{value:X4}) ({name})";
761760
return value + " (" + name + ")";
762761
}
763-
764-
private static string GetIndexString(params object[] values)
765-
{
766-
var list = values.Flatten();
767-
768-
return list.Where(value => value != null)
769-
.Aggregate(string.Empty, (current, value) =>
770-
{
771-
var s = value is string ? "()" : "[]";
772-
return current + (s[0] + value.ToString() + s[1] + ' ');
773-
});
774-
}
775762
}
776763
}

0 commit comments

Comments
 (0)