Added a no exception mode for the VKV serializers.

This commit is contained in:
That-One-Nerd 2023-05-16 16:10:32 -04:00
parent 82afefd3e7
commit 7cad137f39
3 changed files with 109 additions and 75 deletions

View File

@ -1,6 +1,4 @@
using Valve.Vkv.ObjectModels; namespace Valve.Vkv;
namespace Valve.Vkv;
public static class VkvConvert public static class VkvConvert
{ {
@ -21,9 +19,19 @@ public static class VkvConvert
#region DeserializeNode #region DeserializeNode
public static VkvNode? DeserializeNode(StreamReader reader) => public static VkvNode? DeserializeNode(StreamReader reader) =>
DeserializeNode(reader, VkvOptions.Default, out _, null); DeserializeNode(reader, VkvOptions.Default);
public static VkvNode? DeserializeNode(StreamReader reader, VkvOptions options) => public static VkvNode? DeserializeNode(StreamReader reader, VkvOptions options)
DeserializeNode(reader, options, out _, null); {
try
{
return DeserializeNode(reader, options, out _, null);
}
catch
{
if (!options.noExceptions) throw;
return null;
}
}
private static VkvNode? DeserializeNode(StreamReader reader, VkvOptions options, out string name, private static VkvNode? DeserializeNode(StreamReader reader, VkvOptions options, out string name,
string? first) string? first)
@ -113,6 +121,8 @@ public static class VkvConvert
#region FromNodeTree #region FromNodeTree
public static T? FromNodeTree<T>(VkvNode? node, VkvOptions options) => (T?)FromNodeTree(typeof(T), node, options); public static T? FromNodeTree<T>(VkvNode? node, VkvOptions options) => (T?)FromNodeTree(typeof(T), node, options);
public static object? FromNodeTree(Type outputType, VkvNode? node, VkvOptions options) public static object? FromNodeTree(Type outputType, VkvNode? node, VkvOptions options)
{
try
{ {
if (node is null) return null; if (node is null) return null;
@ -120,6 +130,12 @@ public static class VkvConvert
else if (node is VkvTreeNode tree) return FromTreeNode(outputType, tree, options); else if (node is VkvTreeNode tree) return FromTreeNode(outputType, tree, options);
else throw new VkvSerializationException("Unknown VKV node type."); else throw new VkvSerializationException("Unknown VKV node type.");
} }
catch
{
if (!options.noExceptions) throw;
return null;
}
}
private static object? FromSingleNode(Type outputType, VkvSingleNode node) private static object? FromSingleNode(Type outputType, VkvSingleNode node)
{ {
@ -267,9 +283,19 @@ public static class VkvConvert
#region SerializeNode #region SerializeNode
public static void SerializeNode(StreamWriter writer, VkvNode? node, string name, public static void SerializeNode(StreamWriter writer, VkvNode? node, string name,
VkvOptions options) => SerializeNode(writer, node, name, options, 0); VkvOptions options)
{
try
{
SerializeNode(writer, node, name, options, 0);
}
catch
{
if (!options.noExceptions) throw;
}
}
public static void SerializeNode(StreamWriter writer, VkvNode? node, string name) => public static void SerializeNode(StreamWriter writer, VkvNode? node, string name) =>
SerializeNode(writer, node, name, VkvOptions.Default, 0); SerializeNode(writer, node, name, VkvOptions.Default);
private static void SerializeNode(StreamWriter writer, VkvNode? node, string name, private static void SerializeNode(StreamWriter writer, VkvNode? node, string name,
VkvOptions options, int indentLevel) VkvOptions options, int indentLevel)
@ -342,6 +368,8 @@ public static class VkvConvert
#region ToNodeTree #region ToNodeTree
public static VkvNode? ToNodeTree(object? obj) => ToNodeTree(obj, VkvOptions.Default); public static VkvNode? ToNodeTree(object? obj) => ToNodeTree(obj, VkvOptions.Default);
public static VkvNode? ToNodeTree(object? obj, VkvOptions options) public static VkvNode? ToNodeTree(object? obj, VkvOptions options)
{
try
{ {
if (obj is null) return null; if (obj is null) return null;
Type type = obj.GetType(); Type type = obj.GetType();
@ -411,5 +439,11 @@ public static class VkvConvert
return tree; return tree;
} }
catch
{
if (!options.noExceptions) throw;
return null;
}
}
#endregion #endregion
} }

View File

@ -6,6 +6,7 @@ public record class VkvOptions
public bool closeWhenFinished; public bool closeWhenFinished;
public int indentSize; public int indentSize;
public bool noExceptions;
public bool resetStreamPosition; public bool resetStreamPosition;
public bool serializeProperties; public bool serializeProperties;
public SpacingMode spacing; public SpacingMode spacing;
@ -16,6 +17,7 @@ public record class VkvOptions
{ {
closeWhenFinished = true; closeWhenFinished = true;
indentSize = 4; indentSize = 4;
noExceptions = false;
resetStreamPosition = false; resetStreamPosition = false;
serializeProperties = true; serializeProperties = true;
spacing = SpacingMode.DoubleTab; spacing = SpacingMode.DoubleTab;

View File

@ -1,6 +1,4 @@
using System.Reflection.PortableExecutable; namespace Valve.Vkv;
namespace Valve.Vkv;
public class VkvSerializer public class VkvSerializer
{ {