Small starts on the serializer.
This commit is contained in:
parent
864c39be7f
commit
4d64b0bae6
30
SrcMod/Shell/Valve/KeyValueConvert.cs
Normal file
30
SrcMod/Shell/Valve/KeyValueConvert.cs
Normal file
@ -0,0 +1,30 @@
|
||||
namespace SrcMod.Shell.Valve;
|
||||
|
||||
public static class KeyValueConvert
|
||||
{
|
||||
private static readonly Dictionary<string, string> p_escapeCodes = new()
|
||||
{
|
||||
{ "\'", "\\\'" },
|
||||
{ "\"", "\\\"" },
|
||||
{ "\\", "\\\\" },
|
||||
{ "\0", "\\\0" },
|
||||
{ "\a", "\\\a" },
|
||||
{ "\b", "\\\b" },
|
||||
{ "\f", "\\\f" },
|
||||
{ "\n", "\\\n" },
|
||||
{ "\r", "\\\r" },
|
||||
{ "\t", "\\\t" },
|
||||
{ "\v", "\\\v" }
|
||||
};
|
||||
|
||||
public static string SerializeName(string content, KeyValueSerializer.Options? options = null)
|
||||
{
|
||||
options ??= KeyValueSerializer.Options.Default;
|
||||
if (options.useNameQuotes) content = $"\"{content}\"";
|
||||
if (options.useEscapeCodes)
|
||||
foreach (KeyValuePair<string, string> escapeCode in p_escapeCodes)
|
||||
content = content.Replace(escapeCode.Key, escapeCode.Value);
|
||||
|
||||
return content;
|
||||
}
|
||||
}
|
||||
18
SrcMod/Shell/Valve/KeyValueNode.cs
Normal file
18
SrcMod/Shell/Valve/KeyValueNode.cs
Normal file
@ -0,0 +1,18 @@
|
||||
namespace SrcMod.Shell.Valve;
|
||||
|
||||
public class KeyValueNode
|
||||
{
|
||||
public int SubNodeCount => p_subNodes.Count;
|
||||
|
||||
public object value;
|
||||
|
||||
private Dictionary<string, KeyValueNode> p_subNodes;
|
||||
|
||||
internal KeyValueNode()
|
||||
{
|
||||
value = new();
|
||||
p_subNodes = new();
|
||||
}
|
||||
|
||||
public KeyValueNode this[string name] => p_subNodes[name];
|
||||
}
|
||||
41
SrcMod/Shell/Valve/KeyValueSerializer.cs
Normal file
41
SrcMod/Shell/Valve/KeyValueSerializer.cs
Normal file
@ -0,0 +1,41 @@
|
||||
namespace SrcMod.Shell.Valve;
|
||||
|
||||
public class KeyValueSerializer
|
||||
{
|
||||
public int? IndentSize => p_options.indentSize;
|
||||
public bool UseEscapeCodes => p_options.useEscapeCodes;
|
||||
public bool UseNameQuotes => p_options.useNameQuotes;
|
||||
public bool UseValueQuotes => p_options.useValueQuotes;
|
||||
|
||||
private readonly Options p_options;
|
||||
|
||||
public KeyValueSerializer() : this(Options.Default) { }
|
||||
public KeyValueSerializer(Options options)
|
||||
{
|
||||
p_options = options;
|
||||
}
|
||||
|
||||
public StringBuilder Serialize(StringBuilder builder, KeyValueNode parentNode, string? parentNodeName = null)
|
||||
{
|
||||
if (parentNodeName is not null) builder.AppendLine(KeyValueConvert.SerializeName(parentNodeName, p_options));
|
||||
return builder;
|
||||
}
|
||||
|
||||
public record class Options
|
||||
{
|
||||
public static Options Default => new();
|
||||
|
||||
public int? indentSize;
|
||||
public bool useEscapeCodes;
|
||||
public bool useNameQuotes;
|
||||
public bool useValueQuotes;
|
||||
|
||||
public Options()
|
||||
{
|
||||
indentSize = null;
|
||||
useEscapeCodes = false;
|
||||
useNameQuotes = true;
|
||||
useValueQuotes = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user