Small improvements.
This commit is contained in:
parent
ab372b3eec
commit
7e1492c664
@ -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;
|
||||||
|
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
@ -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,54 +120,67 @@ public static class VkvConvert
|
|||||||
{
|
{
|
||||||
if (node is null) return null;
|
if (node is null) return null;
|
||||||
|
|
||||||
object? instance = Activator.CreateInstance(outputType);
|
if (node is VkvSingleNode single)
|
||||||
if (instance is null) return null;
|
|
||||||
|
|
||||||
IEnumerable<FieldInfo> validFields = from field in outputType.GetFields()
|
|
||||||
let isPublic = field.IsPublic
|
|
||||||
let isStatic = field.IsStatic
|
|
||||||
let isIgnored = field.CustomAttributes.Any(x =>
|
|
||||||
x.AttributeType == typeof(VkvIgnoreAttribute))
|
|
||||||
let isConst = field.IsLiteral
|
|
||||||
where isPublic && !isStatic && !isIgnored && !isConst
|
|
||||||
select field;
|
|
||||||
|
|
||||||
IEnumerable<PropertyInfo> validProperties;
|
|
||||||
if (options.serializeProperties)
|
|
||||||
{
|
{
|
||||||
validProperties = from prop in outputType.GetProperties()
|
object? value = single.value;
|
||||||
let canSet = prop.SetMethod is not null
|
if (value is null) return null;
|
||||||
let isPublic = canSet && prop.SetMethod!.IsPublic
|
else if (value is string str)
|
||||||
let isStatic = canSet && prop.SetMethod!.IsStatic
|
|
||||||
let isIgnored = prop.CustomAttributes.Any(x =>
|
|
||||||
x.AttributeType == typeof(VkvIgnoreAttribute))
|
|
||||||
where canSet && isPublic && !isStatic && !isIgnored
|
|
||||||
select prop;
|
|
||||||
}
|
|
||||||
else validProperties = Array.Empty<PropertyInfo>();
|
|
||||||
|
|
||||||
foreach (FieldInfo field in validFields)
|
|
||||||
{
|
|
||||||
// TODO: check if the node tree has that field.
|
|
||||||
|
|
||||||
Type castType = field.FieldType;
|
|
||||||
if (TypeParsers.CanParse(instance))
|
|
||||||
{
|
{
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
foreach (PropertyInfo prop in validProperties)
|
else if (node is VkvTreeNode tree)
|
||||||
{
|
{
|
||||||
// TODO: check if the node tree has that field.
|
object? instance = Activator.CreateInstance(outputType);
|
||||||
|
if (instance is null) return null;
|
||||||
|
|
||||||
Type castType = prop.PropertyType;
|
IEnumerable<FieldInfo> validFields = from field in outputType.GetFields()
|
||||||
if (TypeParsers.CanParse(instance))
|
let isPublic = field.IsPublic
|
||||||
|
let isStatic = field.IsStatic
|
||||||
|
let isIgnored = field.CustomAttributes.Any(x =>
|
||||||
|
x.AttributeType == typeof(VkvIgnoreAttribute))
|
||||||
|
let isConst = field.IsLiteral
|
||||||
|
where isPublic && !isStatic && !isIgnored && !isConst
|
||||||
|
select field;
|
||||||
|
|
||||||
|
IEnumerable<PropertyInfo> validProperties;
|
||||||
|
if (options.serializeProperties)
|
||||||
{
|
{
|
||||||
|
validProperties = from prop in outputType.GetProperties()
|
||||||
|
let canSet = prop.SetMethod is not null
|
||||||
|
let isPublic = canSet && prop.SetMethod!.IsPublic
|
||||||
|
let isStatic = canSet && prop.SetMethod!.IsStatic
|
||||||
|
let isIgnored = prop.CustomAttributes.Any(x =>
|
||||||
|
x.AttributeType == typeof(VkvIgnoreAttribute))
|
||||||
|
where canSet && isPublic && !isStatic && !isIgnored
|
||||||
|
select prop;
|
||||||
}
|
}
|
||||||
}
|
else validProperties = Array.Empty<PropertyInfo>();
|
||||||
|
|
||||||
return null;
|
foreach (FieldInfo field in validFields)
|
||||||
|
{
|
||||||
|
// TODO: check if the node tree has that field.
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// parsables
|
||||||
|
// enums
|
||||||
|
// casting
|
||||||
|
// sub-conversion
|
||||||
|
}
|
||||||
|
foreach (PropertyInfo prop in validProperties)
|
||||||
|
{
|
||||||
|
// TODO: check if the node tree has that field.
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else throw new VkvSerializationException("Unknown VKV node type.");
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -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],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user