From 856811687e46a6d43adebdc4267f570cebe06041 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Tue, 9 May 2023 18:01:12 -0400 Subject: [PATCH] Renamed VDF stuff to VKV as it should be and moved to subfolder. --- .../Valve/ObjectModels/IVdfConvertible.cs | 6 -- .../ObjectModels/VdfSerializationException.cs | 8 -- SrcMod/Shell/Valve/VdfNode.cs | 6 -- SrcMod/Shell/Valve/VdfSingleNode.cs | 11 --- .../Valve/Vkv/ObjectModels/IVkvConvertible.cs | 8 ++ .../ObjectModels/VkvIgnoreAttribute.cs} | 4 +- .../ObjectModels/VkvSerializationException.cs | 8 ++ .../{VdfConvert.cs => Vkv/VkvConvert.cs} | 90 +++++++++---------- SrcMod/Shell/Valve/Vkv/VkvNode.cs | 3 + .../{VdfOptions.cs => Vkv/VkvOptions.cs} | 8 +- .../VkvSerializer.cs} | 22 ++--- SrcMod/Shell/Valve/Vkv/VkvSingleNode.cs | 11 +++ .../{VdfTreeNode.cs => Vkv/VkvTreeNode.cs} | 18 ++-- 13 files changed, 101 insertions(+), 102 deletions(-) delete mode 100644 SrcMod/Shell/Valve/ObjectModels/IVdfConvertible.cs delete mode 100644 SrcMod/Shell/Valve/ObjectModels/VdfSerializationException.cs delete mode 100644 SrcMod/Shell/Valve/VdfNode.cs delete mode 100644 SrcMod/Shell/Valve/VdfSingleNode.cs create mode 100644 SrcMod/Shell/Valve/Vkv/ObjectModels/IVkvConvertible.cs rename SrcMod/Shell/Valve/{ObjectModels/VdfIgnoreAttribute.cs => Vkv/ObjectModels/VkvIgnoreAttribute.cs} (53%) create mode 100644 SrcMod/Shell/Valve/Vkv/ObjectModels/VkvSerializationException.cs rename SrcMod/Shell/Valve/{VdfConvert.cs => Vkv/VkvConvert.cs} (79%) create mode 100644 SrcMod/Shell/Valve/Vkv/VkvNode.cs rename SrcMod/Shell/Valve/{VdfOptions.cs => Vkv/VkvOptions.cs} (74%) rename SrcMod/Shell/Valve/{VdfSerializer.cs => Vkv/VkvSerializer.cs} (59%) create mode 100644 SrcMod/Shell/Valve/Vkv/VkvSingleNode.cs rename SrcMod/Shell/Valve/{VdfTreeNode.cs => Vkv/VkvTreeNode.cs} (62%) diff --git a/SrcMod/Shell/Valve/ObjectModels/IVdfConvertible.cs b/SrcMod/Shell/Valve/ObjectModels/IVdfConvertible.cs deleted file mode 100644 index a522adf..0000000 --- a/SrcMod/Shell/Valve/ObjectModels/IVdfConvertible.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace SrcMod.Shell.Valve.ObjectModels; - -public interface IVdfConvertible -{ - public VdfNode ToNodeTree(); -} \ No newline at end of file diff --git a/SrcMod/Shell/Valve/ObjectModels/VdfSerializationException.cs b/SrcMod/Shell/Valve/ObjectModels/VdfSerializationException.cs deleted file mode 100644 index 10379a7..0000000 --- a/SrcMod/Shell/Valve/ObjectModels/VdfSerializationException.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace SrcMod.Shell.Valve.ObjectModels; - -public class VdfSerializationException : Exception -{ - public VdfSerializationException() : base() { } - public VdfSerializationException(string message) : base(message) { } - public VdfSerializationException(string message, Exception inner) : base(message, inner) { } -} diff --git a/SrcMod/Shell/Valve/VdfNode.cs b/SrcMod/Shell/Valve/VdfNode.cs deleted file mode 100644 index 36f3326..0000000 --- a/SrcMod/Shell/Valve/VdfNode.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace SrcMod.Shell.Valve; - -public abstract class VdfNode -{ - -} diff --git a/SrcMod/Shell/Valve/VdfSingleNode.cs b/SrcMod/Shell/Valve/VdfSingleNode.cs deleted file mode 100644 index 429025f..0000000 --- a/SrcMod/Shell/Valve/VdfSingleNode.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace SrcMod.Shell.Valve; - -public class VdfSingleNode : VdfNode -{ - public object? value; - - public VdfSingleNode(object? value = null) : base() - { - this.value = value; - } -} diff --git a/SrcMod/Shell/Valve/Vkv/ObjectModels/IVkvConvertible.cs b/SrcMod/Shell/Valve/Vkv/ObjectModels/IVkvConvertible.cs new file mode 100644 index 0000000..8bc6364 --- /dev/null +++ b/SrcMod/Shell/Valve/Vkv/ObjectModels/IVkvConvertible.cs @@ -0,0 +1,8 @@ +using SrcMod.Shell.Valve.Vkv; + +namespace SrcMod.Shell.Valve.Vkv.ObjectModels; + +public interface IVkvConvertible +{ + public VkvNode ToNodeTree(); +} \ No newline at end of file diff --git a/SrcMod/Shell/Valve/ObjectModels/VdfIgnoreAttribute.cs b/SrcMod/Shell/Valve/Vkv/ObjectModels/VkvIgnoreAttribute.cs similarity index 53% rename from SrcMod/Shell/Valve/ObjectModels/VdfIgnoreAttribute.cs rename to SrcMod/Shell/Valve/Vkv/ObjectModels/VkvIgnoreAttribute.cs index 26ecb15..7fab5fa 100644 --- a/SrcMod/Shell/Valve/ObjectModels/VdfIgnoreAttribute.cs +++ b/SrcMod/Shell/Valve/Vkv/ObjectModels/VkvIgnoreAttribute.cs @@ -1,4 +1,4 @@ -namespace SrcMod.Shell.Valve.ObjectModels; +namespace SrcMod.Shell.Valve.Vkv.ObjectModels; [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)] -public class VdfIgnoreAttribute : Attribute { } +public class VkvIgnoreAttribute : Attribute { } diff --git a/SrcMod/Shell/Valve/Vkv/ObjectModels/VkvSerializationException.cs b/SrcMod/Shell/Valve/Vkv/ObjectModels/VkvSerializationException.cs new file mode 100644 index 0000000..2995971 --- /dev/null +++ b/SrcMod/Shell/Valve/Vkv/ObjectModels/VkvSerializationException.cs @@ -0,0 +1,8 @@ +namespace SrcMod.Shell.Valve.Vkv.ObjectModels; + +public class VkvSerializationException : Exception +{ + public VkvSerializationException() : base() { } + public VkvSerializationException(string message) : base(message) { } + public VkvSerializationException(string message, Exception inner) : base(message, inner) { } +} diff --git a/SrcMod/Shell/Valve/VdfConvert.cs b/SrcMod/Shell/Valve/Vkv/VkvConvert.cs similarity index 79% rename from SrcMod/Shell/Valve/VdfConvert.cs rename to SrcMod/Shell/Valve/Vkv/VkvConvert.cs index b3b2a8a..76007b0 100644 --- a/SrcMod/Shell/Valve/VdfConvert.cs +++ b/SrcMod/Shell/Valve/Vkv/VkvConvert.cs @@ -1,8 +1,8 @@ -using SrcMod.Shell.Valve.ObjectModels; +using SrcMod.Shell.Valve.Vkv.ObjectModels; -namespace SrcMod.Shell.Valve; +namespace SrcMod.Shell.Valve.Vkv; -public static class VdfConvert +public static class VkvConvert { private static readonly Dictionary p_escapeCodes = new() { @@ -20,12 +20,12 @@ public static class VdfConvert }; #region DeserializeNode - public static VdfNode? DeserializeNode(StreamReader reader) => - DeserializeNode(reader, VdfOptions.Default, out _, null); - public static VdfNode? DeserializeNode(StreamReader reader, VdfOptions options) => + public static VkvNode? DeserializeNode(StreamReader reader) => + DeserializeNode(reader, VkvOptions.Default, out _, null); + public static VkvNode? DeserializeNode(StreamReader reader, VkvOptions options) => DeserializeNode(reader, options, out _, null); - private static VdfNode? DeserializeNode(StreamReader reader, VdfOptions options, out string name, + private static VkvNode? DeserializeNode(StreamReader reader, VkvOptions options, out string name, string? first) { string? header = first ?? (reader.ReadLine()?.Trim()); @@ -36,31 +36,31 @@ public static class VdfConvert } string[] parts = SplitContent(header, options); - if (parts.Length > 2) throw new VdfSerializationException("Too many values in node."); + if (parts.Length > 2) throw new VkvSerializationException("Too many values in node."); - VdfNode node; + VkvNode node; name = DeserializeString(parts[0], options); if (parts.Length == 2) { object value = DeserializeObject(DeserializeString(parts[1], options)); - node = new VdfSingleNode(value); + node = new VkvSingleNode(value); } else { string? next = reader.ReadLine()?.Trim(); - if (next is null) throw new VdfSerializationException("Expected starting '{', found end-of-file."); - else if (next != "{") throw new VdfSerializationException($"Expected starting '{{', found \"{next}\"."); - VdfTreeNode tree = new(); + if (next is null) throw new VkvSerializationException("Expected starting '{', found end-of-file."); + else if (next != "{") throw new VkvSerializationException($"Expected starting '{{', found \"{next}\"."); + VkvTreeNode tree = new(); string? current; while ((current = reader.ReadLine()?.Trim()) is not null) { if (current == "}") break; - VdfNode? output = DeserializeNode(reader, options, out string subName, current); - if (output is null) throw new VdfSerializationException("Error deserializing sub-node."); + VkvNode? output = DeserializeNode(reader, options, out string subName, current); + if (output is null) throw new VkvSerializationException("Error deserializing sub-node."); tree[subName] = output; } - if (current is null) throw new VdfSerializationException("Reached end-of-file while deserializing group."); + if (current is null) throw new VkvSerializationException("Reached end-of-file while deserializing group."); node = tree; } @@ -69,12 +69,12 @@ public static class VdfConvert private static object DeserializeObject(string content) => TypeParsers.ParseAll(content); - private static string DeserializeString(string content, VdfOptions options) + private static string DeserializeString(string content, VkvOptions options) { if (options.useQuotes) { if (!content.StartsWith('\"') || !content.EndsWith('\"')) - throw new VdfSerializationException("No quotes found around content."); + throw new VkvSerializationException("No quotes found around content."); content = content[1..^1]; } if (options.useEscapeCodes) @@ -85,7 +85,7 @@ public static class VdfConvert return content; } - private static string[] SplitContent(string content, VdfOptions options) + private static string[] SplitContent(string content, VkvOptions options) { content = content.Replace('\t', ' '); if (options.useQuotes) @@ -105,7 +105,7 @@ public static class VdfConvert } else current += c; } - if (inQuote) throw new VdfSerializationException("Reached end-of-line while inside quotations."); + if (inQuote) throw new VkvSerializationException("Reached end-of-line while inside quotations."); if (!string.IsNullOrEmpty(current)) values.Add(current); return values.ToArray(); } @@ -114,7 +114,7 @@ public static class VdfConvert #endregion #region FromNodeTree - public static object? FromNodeTree(Type outputType, VdfNode? node, VdfOptions options) + public static object? FromNodeTree(Type outputType, VkvNode? node, VkvOptions options) { if (node is null) return null; @@ -125,7 +125,7 @@ public static class VdfConvert let isPublic = field.IsPublic let isStatic = field.IsStatic let isIgnored = field.CustomAttributes.Any(x => - x.AttributeType == typeof(VdfIgnoreAttribute)) + x.AttributeType == typeof(VkvIgnoreAttribute)) let isConst = field.IsLiteral where isPublic && !isStatic && !isIgnored && !isConst select field; @@ -138,7 +138,7 @@ public static class VdfConvert let isPublic = canSet && prop.SetMethod!.IsPublic let isStatic = canSet && prop.SetMethod!.IsStatic let isIgnored = prop.CustomAttributes.Any(x => - x.AttributeType == typeof(VdfIgnoreAttribute)) + x.AttributeType == typeof(VkvIgnoreAttribute)) where canSet && isPublic && !isStatic && !isIgnored select prop; } @@ -151,7 +151,7 @@ public static class VdfConvert Type castType = field.FieldType; if (TypeParsers.CanParse(instance)) { - + } } foreach (PropertyInfo prop in validProperties) @@ -170,22 +170,22 @@ public static class VdfConvert #endregion #region SerializeNode - public static void SerializeNode(StreamWriter writer, VdfNode? node, string name, - VdfOptions options) => SerializeNode(writer, node, name, options, 0); - public static void SerializeNode(StreamWriter writer, VdfNode? node, string name) => - SerializeNode(writer, node, name, VdfOptions.Default, 0); + public static void SerializeNode(StreamWriter writer, VkvNode? node, string name, + VkvOptions options) => SerializeNode(writer, node, name, options, 0); + public static void SerializeNode(StreamWriter writer, VkvNode? node, string name) => + SerializeNode(writer, node, name, VkvOptions.Default, 0); - private static void SerializeNode(StreamWriter writer, VdfNode? node, string name, - VdfOptions options, int indentLevel) + private static void SerializeNode(StreamWriter writer, VkvNode? node, string name, + VkvOptions options, int indentLevel) { if (node is null) return; - else if (node is VdfSingleNode single) SerializeSingleNode(writer, single, name, options, indentLevel); - else if (node is VdfTreeNode tree) SerializeTreeNode(writer, tree, name, options, indentLevel); + else if (node is VkvSingleNode single) SerializeSingleNode(writer, single, name, options, indentLevel); + else if (node is VkvTreeNode tree) SerializeTreeNode(writer, tree, name, options, indentLevel); else throw new("Unknown node type."); } - private static void SerializeSingleNode(StreamWriter writer, VdfSingleNode node, string name, - VdfOptions options, int indentLevel) + private static void SerializeSingleNode(StreamWriter writer, VkvSingleNode node, string name, + VkvOptions options, int indentLevel) { string? serializedValue = SerializeObject(node.value); if (serializedValue is null) return; @@ -197,8 +197,8 @@ public static class VdfConvert serializedValue = SerializeString(serializedValue, options); writer.WriteLine(serializedValue); } - private static void SerializeTreeNode(StreamWriter writer, VdfTreeNode node, string name, - VdfOptions options, int indentLevel) + private static void SerializeTreeNode(StreamWriter writer, VkvTreeNode node, string name, + VkvOptions options, int indentLevel) { if (node.SubNodeCount <= 0) return; @@ -206,7 +206,7 @@ public static class VdfConvert writer.WriteLine(SerializeString(name, options)); writer.WriteLine(new string(' ', indentLevel) + '{'); - foreach (KeyValuePair subNode in node) + foreach (KeyValuePair subNode in node) SerializeNode(writer, subNode.Value, subNode.Key, options, indentLevel + options.indentSize); writer.WriteLine(new string(' ', indentLevel) + '}'); @@ -218,7 +218,7 @@ public static class VdfConvert return obj.ToString() ?? string.Empty; } - private static string SerializeString(string content, VdfOptions options) + private static string SerializeString(string content, VkvOptions options) { if (options.useEscapeCodes) { @@ -232,18 +232,18 @@ public static class VdfConvert #endregion #region ToNodeTree - public static VdfNode? ToNodeTree(object? obj) => ToNodeTree(obj, VdfOptions.Default); - public static VdfNode? ToNodeTree(object? obj, VdfOptions options) + public static VkvNode? ToNodeTree(object? obj) => ToNodeTree(obj, VkvOptions.Default); + public static VkvNode? ToNodeTree(object? obj, VkvOptions options) { if (obj is null) return null; Type type = obj.GetType(); - if (type.IsPrimitive || TypeParsers.CanParse(obj)) return new VdfSingleNode(obj); + if (type.IsPrimitive || TypeParsers.CanParse(obj)) return new VkvSingleNode(obj); else if (type.IsPointer) throw new("Cannot serialize a pointer."); - VdfTreeNode tree = new(); + VkvTreeNode tree = new(); - if (obj is IVdfConvertible vdf) return vdf.ToNodeTree(); + if (obj is IVkvConvertible vdf) return vdf.ToNodeTree(); else if (obj is IDictionary dictionary) { object[] keys = new object[dictionary.Count], @@ -271,7 +271,7 @@ public static class VdfConvert let isPublic = field.IsPublic let isStatic = field.IsStatic let isIgnored = field.CustomAttributes.Any(x => - x.AttributeType == typeof(VdfIgnoreAttribute)) + x.AttributeType == typeof(VkvIgnoreAttribute)) let isConst = field.IsLiteral where isPublic && !isStatic && !isIgnored && !isConst select field; @@ -284,7 +284,7 @@ public static class VdfConvert let isPublic = canGet && prop.GetMethod!.IsPublic let isStatic = canGet && prop.GetMethod!.IsStatic let isIgnored = prop.CustomAttributes.Any(x => - x.AttributeType == typeof(VdfIgnoreAttribute)) + x.AttributeType == typeof(VkvIgnoreAttribute)) where canGet && isPublic && !isStatic && !isIgnored select prop; } diff --git a/SrcMod/Shell/Valve/Vkv/VkvNode.cs b/SrcMod/Shell/Valve/Vkv/VkvNode.cs new file mode 100644 index 0000000..2f7a9da --- /dev/null +++ b/SrcMod/Shell/Valve/Vkv/VkvNode.cs @@ -0,0 +1,3 @@ +namespace SrcMod.Shell.Valve.Vkv; + +public abstract class VkvNode { } diff --git a/SrcMod/Shell/Valve/VdfOptions.cs b/SrcMod/Shell/Valve/Vkv/VkvOptions.cs similarity index 74% rename from SrcMod/Shell/Valve/VdfOptions.cs rename to SrcMod/Shell/Valve/Vkv/VkvOptions.cs index 6e5e03d..de8ce23 100644 --- a/SrcMod/Shell/Valve/VdfOptions.cs +++ b/SrcMod/Shell/Valve/Vkv/VkvOptions.cs @@ -1,8 +1,8 @@ -namespace SrcMod.Shell.Valve; +namespace SrcMod.Shell.Valve.Vkv; -public record class VdfOptions +public record class VkvOptions { - public static VdfOptions Default => new(); + public static VkvOptions Default => new(); public bool closeWhenFinished; public int indentSize; @@ -11,7 +11,7 @@ public record class VdfOptions public bool useEscapeCodes; public bool useQuotes; - public VdfOptions() + public VkvOptions() { closeWhenFinished = true; indentSize = 4; diff --git a/SrcMod/Shell/Valve/VdfSerializer.cs b/SrcMod/Shell/Valve/Vkv/VkvSerializer.cs similarity index 59% rename from SrcMod/Shell/Valve/VdfSerializer.cs rename to SrcMod/Shell/Valve/Vkv/VkvSerializer.cs index e59ac67..53e25c4 100644 --- a/SrcMod/Shell/Valve/VdfSerializer.cs +++ b/SrcMod/Shell/Valve/Vkv/VkvSerializer.cs @@ -1,22 +1,22 @@ -namespace SrcMod.Shell.Valve; +namespace SrcMod.Shell.Valve.Vkv; -public class VdfSerializer +public class VkvSerializer { - public VdfOptions Options => p_options; + public VkvOptions Options => p_options; - private readonly VdfOptions p_options; + private readonly VkvOptions p_options; - public VdfSerializer() : this(VdfOptions.Default) { } - public VdfSerializer(VdfOptions options) + public VkvSerializer() : this(VkvOptions.Default) { } + public VkvSerializer(VkvOptions options) { p_options = options; } - public VdfNode? Deserialize(Stream stream) + public VkvNode? Deserialize(Stream stream) { long pos = stream.Position; StreamReader reader = new(stream, leaveOpen: !p_options.closeWhenFinished); - VdfNode? result = VdfConvert.DeserializeNode(reader, p_options); + VkvNode? result = VkvConvert.DeserializeNode(reader, p_options); reader.Close(); if (!p_options.closeWhenFinished && p_options.resetStreamPosition) stream.Seek(pos, SeekOrigin.Begin); @@ -25,14 +25,14 @@ public class VdfSerializer public void Serialize(Stream stream, object? value, string parentNodeName) { - VdfNode? nodeTree = VdfConvert.ToNodeTree(value, p_options); + VkvNode? nodeTree = VkvConvert.ToNodeTree(value, p_options); Serialize(stream, nodeTree, parentNodeName); } - public void Serialize(Stream stream, VdfNode? parentNode, string parentNodeName) + public void Serialize(Stream stream, VkvNode? parentNode, string parentNodeName) { long pos = stream.Position; StreamWriter writer = new(stream, leaveOpen: !p_options.closeWhenFinished); - VdfConvert.SerializeNode(writer, parentNode, parentNodeName, p_options); + VkvConvert.SerializeNode(writer, parentNode, parentNodeName, p_options); writer.Close(); if (!p_options.closeWhenFinished && p_options.resetStreamPosition) stream.Seek(pos, SeekOrigin.Begin); diff --git a/SrcMod/Shell/Valve/Vkv/VkvSingleNode.cs b/SrcMod/Shell/Valve/Vkv/VkvSingleNode.cs new file mode 100644 index 0000000..7285ed2 --- /dev/null +++ b/SrcMod/Shell/Valve/Vkv/VkvSingleNode.cs @@ -0,0 +1,11 @@ +namespace SrcMod.Shell.Valve.Vkv; + +public class VkvSingleNode : VkvNode +{ + public object? value; + + public VkvSingleNode(object? value = null) : base() + { + this.value = value; + } +} diff --git a/SrcMod/Shell/Valve/VdfTreeNode.cs b/SrcMod/Shell/Valve/Vkv/VkvTreeNode.cs similarity index 62% rename from SrcMod/Shell/Valve/VdfTreeNode.cs rename to SrcMod/Shell/Valve/Vkv/VkvTreeNode.cs index 610b231..bdb9cde 100644 --- a/SrcMod/Shell/Valve/VdfTreeNode.cs +++ b/SrcMod/Shell/Valve/Vkv/VkvTreeNode.cs @@ -1,21 +1,21 @@ -namespace SrcMod.Shell.Valve; +namespace SrcMod.Shell.Valve.Vkv; -public class VdfTreeNode : VdfNode, IEnumerable> +public class VkvTreeNode : VkvNode, IEnumerable> { public int SubNodeCount => p_subNodes.Count; - private readonly Dictionary p_subNodes; + private readonly Dictionary p_subNodes; - public VdfTreeNode(Dictionary? subNodes = null) : base() + public VkvTreeNode(Dictionary? subNodes = null) : base() { p_subNodes = subNodes ?? new(); } - public VdfNode? this[string key] + public VkvNode? this[string key] { get { - if (p_subNodes.TryGetValue(key, out VdfNode? value)) return value; + if (p_subNodes.TryGetValue(key, out VkvNode? value)) return value; else return null; } set @@ -24,7 +24,7 @@ public class VdfTreeNode : VdfNode, IEnumerable> else p_subNodes.Add(key, value); } } - public VdfNode? this[int index] + public VkvNode? this[int index] { get { @@ -38,8 +38,8 @@ public class VdfTreeNode : VdfNode, IEnumerable> } } - public void Add(string key, VdfNode? value) => this[key] = value; + public void Add(string key, VkvNode? value) => this[key] = value; IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - public IEnumerator> GetEnumerator() => p_subNodes.GetEnumerator(); + public IEnumerator> GetEnumerator() => p_subNodes.GetEnumerator(); }