From e731f9003009201c72da7c47f225dbd10ccf8f86 Mon Sep 17 00:00:00 2001 From: BtbN Date: Wed, 17 Apr 2019 01:23:09 +0200 Subject: [PATCH] Fix array generation logic, add IP4 --- eAmuseCore/KBinXML/KBinXML.cs | 2 +- eAmuseCore/KBinXML/XmlTypes.cs | 42 ++++++++++++++++++++++++++++++---- eAmuseTest/Program.cs | 2 +- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/eAmuseCore/KBinXML/KBinXML.cs b/eAmuseCore/KBinXML/KBinXML.cs index ead32d5..fb108be 100644 --- a/eAmuseCore/KBinXML/KBinXML.cs +++ b/eAmuseCore/KBinXML/KBinXML.cs @@ -243,7 +243,7 @@ namespace eAmuseCore.KBinXML } else { - node.SetValue(XmlTypes.XmlTypes.MakeNodeFromBytes(nodeType, data)); + node.SetValue(XmlTypes.XmlTypes.MakeNodeFromBytes(nodeType, arrCount, data)); } } diff --git a/eAmuseCore/KBinXML/XmlTypes.cs b/eAmuseCore/KBinXML/XmlTypes.cs index 45fd3b3..2dc2de7 100644 --- a/eAmuseCore/KBinXML/XmlTypes.cs +++ b/eAmuseCore/KBinXML/XmlTypes.cs @@ -2,7 +2,9 @@ using System.Linq; using System.Collections.Generic; using System.Reflection; +using System.Net; using System.Text; +using System.Collections; namespace eAmuseCore.KBinXML.XmlTypes { @@ -112,6 +114,18 @@ namespace eAmuseCore.KBinXML.XmlTypes } } + [KValue(12, "ip4", Count = 1, Size = 4)] + public class IP4 : KValue + { + public IP4(IPAddress value) => Value = value; + + public override string ToString() => Value.ToString(); + + static public IP4 FromString(string input) => new IP4(IPAddress.Parse(input)); + + static public IP4 FromBytes(IEnumerable input) => new IP4(new IPAddress(input.Take(4).ToArray())); + } + public static class XmlTypes { public const int NodeStartType = 1; @@ -138,6 +152,7 @@ namespace eAmuseCore.KBinXML.XmlTypes typeof(U64), typeof(Bin), typeof(Str), + typeof(IP4), }; foreach (Type type in types) @@ -156,11 +171,30 @@ namespace eAmuseCore.KBinXML.XmlTypes return lookupMap[type]; } - public static object MakeNodeFromBytes(byte type, IEnumerable data) + public static object MakeNodeFromBytes(byte type, int count, IEnumerable data) { - return GetByType(type) - .GetMethod("FromBytes", BindingFlags.Static | BindingFlags.Public) - .Invoke(null, new object[] { data }); + Type valType = GetByType(type); + MethodInfo fromBytes = valType.GetMethod("FromBytes", BindingFlags.Static | BindingFlags.Public); + + if (count <= 1) + { + return fromBytes.Invoke(null, new object[] { data }); + } + else + { + Type listType = typeof(KValueList<>).MakeGenericType(valType); + IList list = (IList)Activator.CreateInstance(listType); + var attr = KValueAttribute.GetAttrByType(type); + int size = attr.Size * attr.Count; + + for (int i = 0; i < count; i++) + { + list.Add(fromBytes.Invoke(null, new object[] { data })); + data = data.Skip(size); + } + + return list; + } } } } diff --git a/eAmuseTest/Program.cs b/eAmuseTest/Program.cs index 4f7da9e..2fab9e0 100644 --- a/eAmuseTest/Program.cs +++ b/eAmuseTest/Program.cs @@ -46,7 +46,7 @@ namespace eAmuseTest else if (compress != "none") throw new ArgumentException("Unsupported compression algorithm"); - KBinXML kbinxml = new KBinXML(rawData); + //KBinXML kbinxml = new KBinXML(rawData); KBinXML testDoc = new KBinXML(ExtractResource("eAmuseTest.testcases_out.kbin")); }