From bb520424ac9aea61da76ad0f0ad77862bde7e338 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Sun, 7 May 2023 21:23:04 -0400 Subject: [PATCH] More tree construction progress. Small. --- SrcMod/Shell/Valve/IVdfSerializable.cs | 6 ++++++ SrcMod/Shell/Valve/VdfConvert.cs | 25 ++++++++++++++++++++++-- SrcMod/Shell/Valve/VdfIgnoreAttribute.cs | 4 ++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 SrcMod/Shell/Valve/IVdfSerializable.cs create mode 100644 SrcMod/Shell/Valve/VdfIgnoreAttribute.cs diff --git a/SrcMod/Shell/Valve/IVdfSerializable.cs b/SrcMod/Shell/Valve/IVdfSerializable.cs new file mode 100644 index 0000000..4170e6e --- /dev/null +++ b/SrcMod/Shell/Valve/IVdfSerializable.cs @@ -0,0 +1,6 @@ +namespace SrcMod.Shell.Valve; + +public interface IVdfSerializable +{ + public VdfNode ToNodeTree(); +} \ No newline at end of file diff --git a/SrcMod/Shell/Valve/VdfConvert.cs b/SrcMod/Shell/Valve/VdfConvert.cs index 94f7427..ff69a19 100644 --- a/SrcMod/Shell/Valve/VdfConvert.cs +++ b/SrcMod/Shell/Valve/VdfConvert.cs @@ -33,7 +33,8 @@ public static class VdfConvert VdfTreeNode tree = new(); - if (obj is IDictionary dictionary) + if (obj is IVdfSerializable vdf) return vdf.ToNodeTree(); + else if (obj is IDictionary dictionary) { object[] keys = new object[dictionary.Count], values = new object[dictionary.Count]; @@ -45,7 +46,7 @@ public static class VdfConvert } return tree; } - else if (obj is IEnumerable enumerable) + else if (obj is ICollection enumerable) { int index = 0; foreach (object item in enumerable) @@ -57,6 +58,26 @@ public static class VdfConvert } // TODO: serialize object + IEnumerable validFields = from field in type.GetFields() + let isPublic = field.IsPublic + let isStatic = field.IsStatic + let isIgnored = field.CustomAttributes.Any(x => + x.AttributeType == typeof(VdfIgnoreAttribute)) + let isConst = field.IsLiteral + where isPublic && !isStatic && !isIgnored && !isConst + select field; + + IEnumerable validProperties = from prop in type.GetProperties() + let canGet = prop.GetMethod is not null + let isPublic = canGet && prop.GetMethod!.IsPublic + let isStatic = canGet && prop.GetMethod!.IsStatic + let isIgnored = prop.CustomAttributes.Any(x => + x.AttributeType == typeof(VdfIgnoreAttribute)) + where canGet && isPublic && !isStatic && !isIgnored + select prop; + + foreach (FieldInfo field in validFields) Write($"field: {field.Name}"); + foreach (PropertyInfo prop in validProperties) Write($"prop: {prop.Name}"); return tree; } diff --git a/SrcMod/Shell/Valve/VdfIgnoreAttribute.cs b/SrcMod/Shell/Valve/VdfIgnoreAttribute.cs new file mode 100644 index 0000000..10e04e5 --- /dev/null +++ b/SrcMod/Shell/Valve/VdfIgnoreAttribute.cs @@ -0,0 +1,4 @@ +namespace SrcMod.Shell.Valve; + +[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)] +public class VdfIgnoreAttribute : Attribute { }