Small improvements.

This commit is contained in:
That_One_Nerd 2023-05-10 07:59:15 -04:00
parent ab372b3eec
commit 7e1492c664
3 changed files with 66 additions and 44 deletions

View File

@ -4,7 +4,7 @@ public class Shell
{ {
public const string Author = "That_One_Nerd"; public const string Author = "That_One_Nerd";
public const string Name = "SrcMod"; public const string Name = "SrcMod";
public const string Version = "Beta 0.4.0"; public const string Version = "Beta 0.5.0";
public readonly string? ShellDirectory; public readonly string? ShellDirectory;

View File

@ -1,10 +1,17 @@
namespace Valve.Miscellaneous; namespace Valve.Miscellaneous;
internal static class TypeParsers public static class TypeParsers
{ {
public static bool CanParse(object? obj) => obj is not null && obj is sbyte or byte or short or ushort or int public static bool CanParse(object? obj) => obj is not null && obj is sbyte or byte or short or ushort or int
or uint or long or ulong or Int128 or UInt128 or nint or nuint or Half or float or double or decimal or uint or long or ulong or Int128 or UInt128 or nint or nuint or Half or float or double or decimal
or char or DateOnly or DateTime or DateTimeOffset or Guid or TimeOnly or TimeSpan; or char or DateOnly or DateTime or DateTimeOffset or Guid or TimeOnly or TimeSpan;
public static bool CanParse(Type type) => type == typeof(sbyte) || type == typeof(byte) || type == typeof(short)
|| type == typeof(ushort) || type == typeof(int) || type == typeof(uint) || type == typeof(long)
|| type == typeof(ulong) || type == typeof(Int128) || type == typeof(UInt128) || type == typeof(nint)
|| type == typeof(nuint) || type == typeof(Half) || type == typeof(float) || type == typeof(double)
|| type == typeof(decimal) || type == typeof(char) || type == typeof(DateOnly) || type == typeof(DateTime)
|| type == typeof(DateTimeOffset) || type == typeof(Guid) || type == typeof(TimeOnly)
|| type == typeof(TimeSpan);
public static object ParseAll(string msg) public static object ParseAll(string msg)
{ {
if (TryParse(msg, out sbyte int8)) return int8; if (TryParse(msg, out sbyte int8)) return int8;

View File

@ -1,5 +1,7 @@
using Valve.Vkv.ObjectModels; using Valve.Vkv.ObjectModels;
using ValveParsers = Valve.Miscellaneous.TypeParsers;
namespace Valve.Vkv; namespace Valve.Vkv;
public static class VkvConvert public static class VkvConvert
@ -68,7 +70,7 @@ public static class VkvConvert
} }
private static object DeserializeObject(string content) => private static object DeserializeObject(string content) =>
TypeParsers.ParseAll(content); ValveParsers.ParseAll(content);
private static string DeserializeString(string content, VkvOptions options) private static string DeserializeString(string content, VkvOptions options)
{ {
if (options.useQuotes) if (options.useQuotes)
@ -118,6 +120,23 @@ public static class VkvConvert
{ {
if (node is null) return null; if (node is null) return null;
if (node is VkvSingleNode single)
{
object? value = single.value;
if (value is null) return null;
else if (value is string str)
{
value = ValveParsers.ParseAll(str);
if (value is string still && outputType.IsEnum)
{
if (Enum.TryParse(outputType, still, true, out object? res) && res is not null)
value = res;
}
}
return Convert.ChangeType(value, outputType);
}
else if (node is VkvTreeNode tree)
{
object? instance = Activator.CreateInstance(outputType); object? instance = Activator.CreateInstance(outputType);
if (instance is null) return null; if (instance is null) return null;
@ -148,25 +167,21 @@ public static class VkvConvert
{ {
// TODO: check if the node tree has that field. // TODO: check if the node tree has that field.
Type castType = field.FieldType; // TODO:
if (TypeParsers.CanParse(instance)) // parsables
{ // enums
// casting
} // sub-conversion
} }
foreach (PropertyInfo prop in validProperties) foreach (PropertyInfo prop in validProperties)
{ {
// TODO: check if the node tree has that field. // TODO: check if the node tree has that field.
Type castType = prop.PropertyType;
if (TypeParsers.CanParse(instance))
{
}
} }
return null; return null;
} }
else throw new VkvSerializationException("Unknown VKV node type.");
}
#endregion #endregion
#region SerializeNode #region SerializeNode
@ -238,12 +253,12 @@ public static class VkvConvert
if (obj is null) return null; if (obj is null) return null;
Type type = obj.GetType(); Type type = obj.GetType();
if (type.IsPrimitive || TypeParsers.CanParse(obj)) return new VkvSingleNode(obj); if (type.IsPrimitive || ValveParsers.CanParse(obj)) return new VkvSingleNode(obj);
else if (type.IsPointer) throw new("Cannot serialize a pointer."); else if (type.IsPointer) throw new("Cannot serialize a pointer.");
VkvTreeNode tree = new(); VkvTreeNode tree = new();
if (obj is IVkvConvertible vdf) return vdf.ToNodeTree(); if (obj is IVkvConvertible vkv) return vkv.ToNodeTree();
else if (obj is IDictionary dictionary) else if (obj is IDictionary dictionary)
{ {
object[] keys = new object[dictionary.Count], object[] keys = new object[dictionary.Count],