Skip to content

Instantly share code, notes, and snippets.

@Shauren
Created December 14, 2018 17:30
Show Gist options
  • Save Shauren/6362385b029bb1dd3e356e9c6ab8775a to your computer and use it in GitHub Desktop.
Save Shauren/6362385b029bb1dd3e356e9c6ab8775a to your computer and use it in GitHub Desktop.
diff --git a/WowPacketParser/Enums/UpdateFields.cs b/WowPacketParser/Enums/UpdateFields.cs
index a394691c1..7be6b5633 100644
--- a/WowPacketParser/Enums/UpdateFields.cs
+++ b/WowPacketParser/Enums/UpdateFields.cs
@@ -1,4 +1,5 @@
using WowPacketParser.Parsing;
+using WowPacketParser.Parsing.Parsers;
namespace WowPacketParser.Enums
{
@@ -176,7 +177,7 @@ namespace WowPacketParser.Enums
UNIT_FIELD_DISPLAY_SCALE,
UNIT_FIELD_EFFECTIVE_LEVEL,
UNIT_FIELD_END,
- [UpdateField(UpdateFieldType.Custom)]
+ [UpdateField(typeof(UpdateFieldCustomParsers.FactionTemplate))]
UNIT_FIELD_FACTIONTEMPLATE,
[UpdateField(UpdateFieldType.Uint)]
UNIT_FIELD_FLAGS,
diff --git a/WowPacketParser/Enums/Version/UpdateFields.cs b/WowPacketParser/Enums/Version/UpdateFields.cs
index 8881497de..16c27205b 100644
--- a/WowPacketParser/Enums/Version/UpdateFields.cs
+++ b/WowPacketParser/Enums/Version/UpdateFields.cs
@@ -5,6 +5,7 @@ using System.Linq;
using System.Reflection;
using WowPacketParser.Misc;
using WowPacketParser.Parsing;
+using static WowPacketParser.Parsing.Parsers.UpdateFieldCustomParsers;
namespace WowPacketParser.Enums.Version
{
@@ -14,6 +15,7 @@ namespace WowPacketParser.Enums.Version
public string Name;
public int Size;
public UpdateFieldType Format;
+ public ICustomUpdateFieldParser CustomParser;
}
public static class UpdateFields
@@ -81,14 +83,21 @@ namespace WowPacketParser.Enums.Version
for (int i = 0; i < vValues.Length; ++i)
{
- var format = enumType.GetMember(vNames[i])
+ var attr = enumType.GetMember(vNames[i])
.SelectMany(member => member.GetCustomAttributes(typeof(UpdateFieldAttribute), false))
- .Where(attribute => ((UpdateFieldAttribute)attribute).Version <= ClientVersion.VersionDefiningBuild)
- .OrderByDescending(attribute => ((UpdateFieldAttribute)attribute).Version)
- .Select(attribute => ((UpdateFieldAttribute)attribute).UFAttribute)
- .DefaultIfEmpty(UpdateFieldType.Default).First();
+ .Cast<UpdateFieldAttribute>()
+ .Where(attribute => attribute.Version <= ClientVersion.VersionDefiningBuild)
+ .OrderByDescending(attribute => attribute.Version)
+ .FirstOrDefault();
- result.Add((int)vValues.GetValue(i), new UpdateFieldInfo() { Value = (int)vValues.GetValue(i), Name = vNames[i], Size = 0, Format = format });
+ result.Add((int)vValues.GetValue(i), new UpdateFieldInfo()
+ {
+ Value = (int)vValues.GetValue(i),
+ Name = vNames[i],
+ Size = 0,
+ Format = attr != null ? attr.UFAttribute : UpdateFieldType.Default,
+ CustomParser = attr != null && attr.CustomParser != null ? Activator.CreateInstance(attr.CustomParser) as ICustomUpdateFieldParser : null
+ });
namesResult.Add(vNames[i], (int)vValues.GetValue(i));
}
diff --git a/WowPacketParser/Parsing/Parsers/UpdateFieldCustomParsers.cs b/WowPacketParser/Parsing/Parsers/UpdateFieldCustomParsers.cs
new file mode 100644
index 000000000..a73b3d8fe
--- /dev/null
+++ b/WowPacketParser/Parsing/Parsers/UpdateFieldCustomParsers.cs
@@ -0,0 +1,23 @@
+using System.Collections.Generic;
+using WowPacketParser.Enums;
+using WowPacketParser.Misc;
+
+namespace WowPacketParser.Parsing.Parsers
+{
+ public static class UpdateFieldCustomParsers
+ {
+ public interface ICustomUpdateFieldParser
+ {
+ void Handle(Packet packet, List<UpdateField> values, object index);
+ }
+
+ public class FactionTemplate : ICustomUpdateFieldParser
+ {
+ public void Handle(Packet packet, List<UpdateField> values, object index)
+ {
+ packet.AddValue(UnitField.UNIT_FIELD_FACTIONTEMPLATE.ToString(),
+ values[0].Int32Value + $" ({ StoreGetters.GetName(StoreNameType.Faction, values[0].Int32Value, false) })", index);
+ }
+ }
+ }
+}
diff --git a/WowPacketParser/Parsing/Parsers/UpdateHandler.cs b/WowPacketParser/Parsing/Parsers/UpdateHandler.cs
index a3f734209..31391225e 100644
--- a/WowPacketParser/Parsing/Parsers/UpdateHandler.cs
+++ b/WowPacketParser/Parsing/Parsers/UpdateHandler.cs
@@ -416,9 +416,7 @@ namespace WowPacketParser.Parsing.Parsers
}
case UpdateFieldType.Custom:
{
- // TODO: add custom handling
- if (key == UnitField.UNIT_FIELD_FACTIONTEMPLATE.ToString())
- packet.AddValue(key, value + $" ({ StoreGetters.GetName(StoreNameType.Faction, fieldData[0].Int32Value, false) })", index);
+ fieldInfo.CustomParser.Handle(packet, fieldData, index);
break;
}
default:
diff --git a/WowPacketParser/Parsing/UpdateFieldAttributes.cs b/WowPacketParser/Parsing/UpdateFieldAttributes.cs
index f72b59a87..c909a5207 100644
--- a/WowPacketParser/Parsing/UpdateFieldAttributes.cs
+++ b/WowPacketParser/Parsing/UpdateFieldAttributes.cs
@@ -29,8 +29,15 @@ namespace WowPacketParser.Parsing
UFAttribute = attrib;
Version = fromVersion;
}
+ public UpdateFieldAttribute(Type customParser)
+ {
+ UFAttribute = UpdateFieldType.Custom;
+ Version = ClientVersionBuild.Zero;
+ CustomParser = customParser;
+ }
public UpdateFieldType UFAttribute { get; private set; }
public ClientVersionBuild Version { get; private set; }
+ public Type CustomParser { get; private set; }
}
-}
\ No newline at end of file
+}
diff --git a/WowPacketParser/WowPacketParser.csproj b/WowPacketParser/WowPacketParser.csproj
index 16def3607..b4185c6de 100644
--- a/WowPacketParser/WowPacketParser.csproj
+++ b/WowPacketParser/WowPacketParser.csproj
@@ -537,6 +537,7 @@
<Compile Include="Parsing\Parsers\TaxiHandler.cs" />
<Compile Include="Parsing\Parsers\TicketHandler.cs" />
<Compile Include="Parsing\Parsers\TradeHandler.cs" />
+ <Compile Include="Parsing\Parsers\UpdateFieldCustomParsers.cs" />
<Compile Include="Parsing\Parsers\UpdateHandler.cs" />
<Compile Include="Parsing\Parsers\VoiceChatHandler.cs" />
<Compile Include="Parsing\Parsers\VoidStorageHandler.cs" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment