Files
2026-05-31 11:06:50 +02:00

55 lines
2.1 KiB
C#

using EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue.Models;
namespace EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue;
/// <summary>
/// Provides static methods for deserializing text representations of key-value groups into KvGroup objects.
/// </summary>
public static class KeyValueDeserializer
{
/// <summary>
/// Deserializes the specified text into a KvGroup object.
/// </summary>
/// <param name="text">The input string containing the serialized representation of a KvGroup. Cannot be null.</param>
/// <returns>A KvGroup object that represents the data contained in the input text.</returns>
public static KeyValueGroup Deserialize(string text)
{
var tokenizer = new KeyValueTokenizer(text);
return ParseGroup(tokenizer);
}
/// <summary>
/// Parses a group from the provided tokenizer, including any nested groups or key-value pairs.
/// </summary>
/// <param name="t">The tokenizer used to read group names, symbols, and key-value pairs from the input stream. Must not be null and must be positioned at the start of a group.</param>
/// <param name="k">The optional name of the group to parse. If null, the group name is read from the tokenizer.</param>
/// <returns>A KvGroup representing the parsed group, including all nested groups and key-value pairs.</returns>
private static KeyValueGroup ParseGroup(KeyValueTokenizer t, string? k = null)
{
// Expect group name
var groupName = k ?? t.ReadString();
var group = new KeyValueGroup(groupName);
t.ReadSymbol('{');
while (!t.PeekSymbol('}'))
{
var key = t.ReadString();
if (t.PeekSymbol('{'))
{
// Nested group
group.Nodes.Add(ParseGroup(t, key));
}
else
{
// Key-value pair
var value = t.ReadString();
group.Nodes.Add(new Models.KeyValuePair(key, value));
}
}
t.ReadSymbol('}');
return group;
}
}