Array and list support for VKV.
This commit is contained in:
parent
ab0453e0ab
commit
c1a90cfc99
@ -1,4 +1,5 @@
|
|||||||
using Valve.Vkv.ObjectModels;
|
using System;
|
||||||
|
using Valve.Vkv.ObjectModels;
|
||||||
|
|
||||||
using ValveParsers = Valve.Miscellaneous.TypeParsers;
|
using ValveParsers = Valve.Miscellaneous.TypeParsers;
|
||||||
|
|
||||||
@ -45,7 +46,7 @@ public static class VkvConvert
|
|||||||
name = DeserializeString(parts[0], options);
|
name = DeserializeString(parts[0], options);
|
||||||
if (parts.Length == 2)
|
if (parts.Length == 2)
|
||||||
{
|
{
|
||||||
object value = DeserializeObject(DeserializeString(parts[1], options));
|
string value = DeserializeString(parts[1], options);
|
||||||
node = new VkvSingleNode(value);
|
node = new VkvSingleNode(value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -69,8 +70,6 @@ public static class VkvConvert
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static object DeserializeObject(string content) =>
|
|
||||||
ValveParsers.ParseAll(content);
|
|
||||||
private static string DeserializeString(string content, VkvOptions options)
|
private static string DeserializeString(string content, VkvOptions options)
|
||||||
{
|
{
|
||||||
if (options.useQuotes)
|
if (options.useQuotes)
|
||||||
@ -120,9 +119,14 @@ public static class VkvConvert
|
|||||||
{
|
{
|
||||||
if (node is null) return null;
|
if (node is null) return null;
|
||||||
|
|
||||||
if (node is VkvSingleNode single)
|
if (node is VkvSingleNode single) return FromSingleNode(outputType, single);
|
||||||
|
else if (node is VkvTreeNode tree) return FromTreeNode(outputType, tree, options);
|
||||||
|
else throw new VkvSerializationException("Unknown VKV node type.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object? FromSingleNode(Type outputType, VkvSingleNode node)
|
||||||
{
|
{
|
||||||
object? value = single.value;
|
object? value = node.value;
|
||||||
if (value is null) return null;
|
if (value is null) return null;
|
||||||
else if (value is string str)
|
else if (value is string str)
|
||||||
{
|
{
|
||||||
@ -135,8 +139,15 @@ public static class VkvConvert
|
|||||||
}
|
}
|
||||||
return Convert.ChangeType(value, outputType);
|
return Convert.ChangeType(value, outputType);
|
||||||
}
|
}
|
||||||
else if (node is VkvTreeNode tree)
|
|
||||||
|
private static object? FromTreeNode(Type outputType, VkvTreeNode node, VkvOptions options)
|
||||||
{
|
{
|
||||||
|
if (outputType.IsArray)
|
||||||
|
return FromTreeNodeArray(outputType, node, options);
|
||||||
|
|
||||||
|
else if (outputType.GetInterface("IList") is not null)
|
||||||
|
return FromTreeNodeList(outputType, node, options);
|
||||||
|
|
||||||
object? instance = Activator.CreateInstance(outputType);
|
object? instance = Activator.CreateInstance(outputType);
|
||||||
if (instance is null) return null;
|
if (instance is null) return null;
|
||||||
|
|
||||||
@ -167,7 +178,7 @@ public static class VkvConvert
|
|||||||
{
|
{
|
||||||
string name = field.Name;
|
string name = field.Name;
|
||||||
|
|
||||||
VkvNode? subNode = tree[name];
|
VkvNode? subNode = node[name];
|
||||||
if (subNode is null) continue;
|
if (subNode is null) continue;
|
||||||
|
|
||||||
object? result = FromNodeTree(field.FieldType, subNode, options);
|
object? result = FromNodeTree(field.FieldType, subNode, options);
|
||||||
@ -178,7 +189,7 @@ public static class VkvConvert
|
|||||||
{
|
{
|
||||||
string name = prop.Name;
|
string name = prop.Name;
|
||||||
|
|
||||||
VkvNode? subNode = tree[name];
|
VkvNode? subNode = node[name];
|
||||||
if (subNode is null) continue;
|
if (subNode is null) continue;
|
||||||
|
|
||||||
object? result = FromNodeTree(prop.PropertyType, subNode, options);
|
object? result = FromNodeTree(prop.PropertyType, subNode, options);
|
||||||
@ -188,7 +199,41 @@ public static class VkvConvert
|
|||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
else throw new VkvSerializationException("Unknown VKV node type.");
|
|
||||||
|
private static object? FromTreeNodeArray(Type outputType, VkvTreeNode node, VkvOptions options)
|
||||||
|
{
|
||||||
|
Type elementType = outputType.GetElementType()!;
|
||||||
|
Array array = Array.CreateInstance(elementType, node.SubNodeCount);
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
foreach (KeyValuePair<string, VkvNode?> subNode in node)
|
||||||
|
{
|
||||||
|
string indexStr = index.ToString();
|
||||||
|
if (subNode.Key != indexStr) throw new VkvSerializationException($"Cannot convert node tree to array.");
|
||||||
|
array.SetValue(FromNodeTree(elementType, subNode.Value, options), index);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object? FromTreeNodeList(Type outputType, VkvTreeNode node, VkvOptions options)
|
||||||
|
{
|
||||||
|
IList? instance = (IList?)Activator.CreateInstance(outputType);
|
||||||
|
if (instance is null) return null;
|
||||||
|
|
||||||
|
// There is no guarentee that the first type argument corresponds to the element type,
|
||||||
|
// but as far as I know there isn't a better way.
|
||||||
|
Type elementType = outputType.IsGenericType ? outputType.GetGenericArguments().First() : typeof(object);
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
foreach (KeyValuePair<string, VkvNode?> subNode in node)
|
||||||
|
{
|
||||||
|
string indexStr = index.ToString();
|
||||||
|
if (subNode.Key != indexStr) throw new VkvSerializationException($"Cannot convert node tree to array.");
|
||||||
|
instance.Add(FromNodeTree(elementType, subNode.Value, options));
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user