From 558c6cb360c79f28ab0afbf475d20bbcfcdcc8a8 Mon Sep 17 00:00:00 2001 From: BtbN Date: Sun, 21 Apr 2019 02:42:28 +0200 Subject: [PATCH] Avoid using Count() --- eAmuseCore/KBinXML/KBinXML.cs | 12 ++++++------ eAmuseCore/KBinXML/XmlTypes.cs | 8 ++++++-- eAmuseTest/Program.cs | 4 +++- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/eAmuseCore/KBinXML/KBinXML.cs b/eAmuseCore/KBinXML/KBinXML.cs index 0af414b..4d4b0b4 100644 --- a/eAmuseCore/KBinXML/KBinXML.cs +++ b/eAmuseCore/KBinXML/KBinXML.cs @@ -187,14 +187,14 @@ namespace eAmuseCore.KBinXML dataList.Realign(); } - private IEnumerable GetNodeData(XElement node, byte nodeType, XmlType xmlType, int count) + private byte[] GetNodeData(XElement node, byte nodeType, XmlType xmlType, int count) { if (nodeType == XmlType.StrType) { if (count != 1) throw new FormatException("String value cannot have a count != 1."); - return BinEncoding.GetBytes(node.Value).Concat(new byte[] { 0 }); + return BinEncoding.GetBytes(node.Value).Concat(new byte[] { 0 }).ToArray(); } else if (nodeType == XmlType.BinType) { @@ -210,7 +210,7 @@ namespace eAmuseCore.KBinXML } else { - IEnumerable parts = node.Value.Split(' ').AsEnumerable(); + IEnumerable parts = node.Value.Split(' '); if (parts.Count() != count * xmlType.Count) throw new ArgumentException("Node value does not have required amount of fields.", "node"); @@ -223,7 +223,7 @@ namespace eAmuseCore.KBinXML parts = parts.Skip(xmlType.Count); } - return res; + return res.ToArray(); } } @@ -262,11 +262,11 @@ namespace eAmuseCore.KBinXML if (nodeType != XmlType.VoidType) { - IEnumerable data = GetNodeData(node, nodeType, xmlType, count); + byte[] data = GetNodeData(node, nodeType, xmlType, count); if (isArray || xmlType.Count < 0) { - dataList.AddU32((uint)data.Count()); + dataList.AddU32((uint)data.Length); dataList.AddRangeAligned(data); } else diff --git a/eAmuseCore/KBinXML/XmlTypes.cs b/eAmuseCore/KBinXML/XmlTypes.cs index 0990517..d0ac3d1 100644 --- a/eAmuseCore/KBinXML/XmlTypes.cs +++ b/eAmuseCore/KBinXML/XmlTypes.cs @@ -91,8 +91,11 @@ namespace eAmuseCore.KBinXML public string KToString(IEnumerable input) { - if (input.Take(Size).Count() != Size) + input = input.Take(Size); + ICollection col = input as ICollection; + if (col != null && col.Count != Size) throw new ArgumentException("input does not provide enough data", "input"); + return Converter.KToString(input); } @@ -117,7 +120,8 @@ namespace eAmuseCore.KBinXML KBinToString toString = b => { IEnumerable data = b.Take(size); - if (data.Count() != size) + ICollection col = data as ICollection; + if (col != null && col.Count != size) throw new ArgumentException("input does not provide enough data for all elements", "b"); string[] res = new string[count]; diff --git a/eAmuseTest/Program.cs b/eAmuseTest/Program.cs index 7195274..186d1fe 100644 --- a/eAmuseTest/Program.cs +++ b/eAmuseTest/Program.cs @@ -28,7 +28,9 @@ namespace eAmuseTest else if (compress != "none") throw new ArgumentException("Unsupported compression algorithm"); - KBinXML kbinxml = new KBinXML(rawData); + Console.WriteLine("GO"); + + KBinXML kbinxml = new KBinXML(rawData.ToArray()); Console.WriteLine(kbinxml); }