Implement KValueArray as fixed size array, not dynamic List

This commit is contained in:
BtbN
2019-04-17 19:42:15 +02:00
parent 61576fb243
commit 04e0c0d801
2 changed files with 32 additions and 11 deletions

View File

@@ -10,23 +10,43 @@ namespace eAmuseCore.KBinXML
IEnumerable<byte> ToBytes();
}
public class KValueList<T> : List<T>, IKValue where T : IKValue
public class KValueArray<T> : 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<T> AsEnumerable()
{
return values.AsEnumerable();
}
public IEnumerable<byte> ToBytes()
{
IEnumerable<byte> res = Enumerable.Empty<byte>();
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()));
}
}

View File

@@ -191,7 +191,7 @@ namespace eAmuseCore.KBinXML.XmlTypes
}
[KValue(16, "2s8", Count = 2, Size = 1)]
public class K2S8 : KValueList<S8>
public class K2S8 : KValueArray<S8>
{
public K2S8(S8 v1, S8 v2) : base(v1, v2) { }
static public K2S8 FromString(string input) => XmlTypes.ValueListTypeFromString<K2S8>(input);
@@ -199,7 +199,7 @@ namespace eAmuseCore.KBinXML.XmlTypes
}
[KValue(27, "3u8", Count = 3, Size = 1)]
public class K3U8 : KValueList<U8>
public class K3U8 : KValueArray<U8>
{
public K3U8(U8 v1, U8 v2, U8 v3) : base(v1, v2, v3) { }
static public K3U8 FromString(string input) => XmlTypes.ValueListTypeFromString<K3U8>(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);
}
}