Added VDF Generator

This commit is contained in:
2026-05-31 11:06:50 +02:00
parent ec7031d9fd
commit ab391186f2
13 changed files with 720 additions and 17 deletions

View File

@@ -0,0 +1,53 @@
using EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue.Models;
using System.Text;
namespace EonaCat.FirstLight.SaveTransfer.VdfGenerator.KeyValue;
/// <summary>
/// Provides functionality to serialize keyvalue groups into a text-based format.
/// </summary>
public class KeyValueSerializer
{
/// <summary>
/// Serializes the given keyvalue group into a text format.
/// </summary>
/// <param name="group">The keyvalue group to serialize. Cannot be null.</param>
/// <returns>A string representing the serialized form of the keyvalue group.</returns>
public static string Serialize(KeyValueGroup group)
{
var sb = new StringBuilder();
SerializeGroup(group, sb, 0);
return sb.ToString();
}
/// <summary>
/// Serializes the specified key-value group and its child elements into a formatted string representation, appending the result to the provided StringBuilder.
/// </summary>
/// <param name="group">The key-value group to serialize. Cannot be null.</param>
/// <param name="sb">The StringBuilder to which the serialized output is appended. Cannot be null.</param>
/// <param name="indent">The indentation level to apply to the serialized output.</param>
private static void SerializeGroup(KeyValueGroup group, StringBuilder sb, int indent)
{
const char padChar = '\t';
var pad = new string(padChar, indent);
sb.Append($"{pad}\"{group.Key}\"\n");
sb.Append($"{pad}{{\n");
foreach (var node in group.Nodes)
{
switch (node)
{
case Models.KeyValuePair pair:
sb.Append($"{pad}{padChar}\"{pair.Key}\"{padChar}{padChar}\"{pair.Value}\"\n");
break;
case KeyValueGroup childGroup:
SerializeGroup(childGroup, sb, indent + 1);
break;
}
}
sb.Append($"{pad}}}\n");
}
}