From 04e0c0d8013b526ef9d80ec87487d4c39f3d4660 Mon Sep 17 00:00:00 2001 From: BtbN Date: Wed, 17 Apr 2019 19:42:15 +0200 Subject: [PATCH] Implement KValueArray as fixed size array, not dynamic List --- eAmuseCore/KBinXML/TypeHelpers.cs | 30 +++++++++++++++++++++++++----- eAmuseCore/KBinXML/XmlTypes.cs | 13 +++++++------ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/eAmuseCore/KBinXML/TypeHelpers.cs b/eAmuseCore/KBinXML/TypeHelpers.cs index 950bb32..7655904 100644 --- a/eAmuseCore/KBinXML/TypeHelpers.cs +++ b/eAmuseCore/KBinXML/TypeHelpers.cs @@ -10,23 +10,43 @@ namespace eAmuseCore.KBinXML IEnumerable ToBytes(); } - public class KValueList : List, IKValue where T : IKValue + public class KValueArray : IKValue where T : IKValue { - public KValueList() : base() { } + private T[] values; - public KValueList(params T[] values) : base(values) { } + public KValueArray(params T[] values) + { + this.values = values; + } + + public T this[int idx] + { + get + { + return values[idx]; + } + set + { + values[idx] = value; + } + } + + public IEnumerable AsEnumerable() + { + return values.AsEnumerable(); + } public IEnumerable ToBytes() { IEnumerable res = Enumerable.Empty(); - foreach (IKValue val in this) + foreach (IKValue val in values) res = res.Concat(val.ToBytes()); return res; } public override string ToString() { - return string.Join(" ", this.Select(v => v.ToString())); + return string.Join(" ", values.Select(v => v.ToString())); } } diff --git a/eAmuseCore/KBinXML/XmlTypes.cs b/eAmuseCore/KBinXML/XmlTypes.cs index e7d30c8..a83fa20 100644 --- a/eAmuseCore/KBinXML/XmlTypes.cs +++ b/eAmuseCore/KBinXML/XmlTypes.cs @@ -191,7 +191,7 @@ namespace eAmuseCore.KBinXML.XmlTypes } [KValue(16, "2s8", Count = 2, Size = 1)] - public class K2S8 : KValueList + public class K2S8 : KValueArray { public K2S8(S8 v1, S8 v2) : base(v1, v2) { } static public K2S8 FromString(string input) => XmlTypes.ValueListTypeFromString(input); @@ -199,7 +199,7 @@ namespace eAmuseCore.KBinXML.XmlTypes } [KValue(27, "3u8", Count = 3, Size = 1)] - public class K3U8 : KValueList + public class K3U8 : KValueArray { public K3U8(U8 v1, U8 v2, U8 v3) : base(v1, v2, v3) { } static public K3U8 FromString(string input) => XmlTypes.ValueListTypeFromString(input); @@ -248,17 +248,18 @@ namespace eAmuseCore.KBinXML.XmlTypes } else { - Type listType = typeof(KValueList<>).MakeGenericType(valType); - IList list = (IList)Activator.CreateInstance(listType); + Type listType = typeof(KValueArray<>).MakeGenericType(valType); int size = attrs.Size * attrs.Count; + object[] p = new object[count]; + for (int i = 0; i < count; i++) { - list.Add(fromBytes.Invoke(null, new object[] { data })); + p[i] = fromBytes.Invoke(null, new object[] { data }); data = data.Skip(size); } - return list; + return Activator.CreateInstance(listType, p); } }