From a3a8fb4e8428c1f211e1794f6916fab109142c89 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sun, 20 Apr 2025 23:39:16 +0300 Subject: [PATCH] chore: depth limit for debugging --- .../Converters/InstanceConverter.cs | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Engine.Serialization/Converters/InstanceConverter.cs b/Engine.Serialization/Converters/InstanceConverter.cs index faa34e2..d7ae718 100644 --- a/Engine.Serialization/Converters/InstanceConverter.cs +++ b/Engine.Serialization/Converters/InstanceConverter.cs @@ -11,10 +11,15 @@ namespace Syntriax.Engine.Serialization; public class InstanceConverter : IEngineTypeYamlConverter { + private int depth = 0; + private int maxDepth = 5; + public bool Accepts(Type type) => !type.IsPrimitive && type != typeof(string); public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) { + if (depth > maxDepth) + return null; parser.Consume(); TypeContainer typeContainer = (TypeContainer)rootDeserializer(typeof(TypeContainer))!; @@ -27,12 +32,16 @@ public class InstanceConverter : IEngineTypeYamlConverter if (type.GetField(key, BindingFlags.Instance | BindingFlags.NonPublic) is FieldInfo fieldInfo) { + depth++; object? fieldValue = rootDeserializer(fieldInfo.FieldType); + depth--; fieldInfo.SetValue(instance, fieldValue); } else if (type.GetProperty(key) is PropertyInfo propertyInfo) { + depth++; object? propertyValue = rootDeserializer(propertyInfo.PropertyType); + depth--; propertyInfo.SetValue(instance, propertyValue); } else @@ -56,13 +65,15 @@ public class InstanceConverter : IEngineTypeYamlConverter if (propertyInfo.GetCustomAttribute() is not null) continue; - if ((propertyInfo.PropertyType.IsClass && propertyInfo.PropertyType != typeof(string)) || propertyInfo.PropertyType.IsAbstract) - continue; - emitter.Emit(new Scalar(propertyInfo.Name)); object? propertyValue = propertyInfo.GetValue(value); - EmitValue(propertyValue, propertyInfo.PropertyType, emitter, serializer); + depth++; + if (depth <= maxDepth) + EmitValue(propertyValue, propertyInfo.PropertyType, emitter, serializer); + else + emitter.Emit(new Scalar("Skipped")); + depth--; } foreach (FieldInfo fieldInfo in typeData.Fields) @@ -70,16 +81,18 @@ public class InstanceConverter : IEngineTypeYamlConverter if (fieldInfo.GetCustomAttribute() is not null) continue; - if (fieldInfo.GetCustomAttribute() is null) - continue; - - if ((fieldInfo.FieldType.IsClass && fieldInfo.FieldType != typeof(string)) || fieldInfo.FieldType.IsAbstract) - continue; + // if (fieldInfo.GetCustomAttribute() is null) + // continue; emitter.Emit(new Scalar(fieldInfo.Name)); object? fieldValue = fieldInfo.GetValue(value); - EmitValue(fieldValue, fieldInfo.FieldType, emitter, serializer); + depth++; + if (depth <= maxDepth) + EmitValue(fieldValue, fieldInfo.FieldType, emitter, serializer); + else + emitter.Emit(new Scalar("Skipped")); + depth--; } emitter.Emit(new MappingEnd());