chore: depth limit for debugging

This commit is contained in:
Syntriax 2025-04-20 23:39:16 +03:00
parent 35f6c3850e
commit a3a8fb4e84

View File

@ -11,10 +11,15 @@ namespace Syntriax.Engine.Serialization;
public class InstanceConverter : IEngineTypeYamlConverter public class InstanceConverter : IEngineTypeYamlConverter
{ {
private int depth = 0;
private int maxDepth = 5;
public bool Accepts(Type type) => !type.IsPrimitive && type != typeof(string); public bool Accepts(Type type) => !type.IsPrimitive && type != typeof(string);
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{ {
if (depth > maxDepth)
return null;
parser.Consume<MappingStart>(); parser.Consume<MappingStart>();
TypeContainer typeContainer = (TypeContainer)rootDeserializer(typeof(TypeContainer))!; 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) if (type.GetField(key, BindingFlags.Instance | BindingFlags.NonPublic) is FieldInfo fieldInfo)
{ {
depth++;
object? fieldValue = rootDeserializer(fieldInfo.FieldType); object? fieldValue = rootDeserializer(fieldInfo.FieldType);
depth--;
fieldInfo.SetValue(instance, fieldValue); fieldInfo.SetValue(instance, fieldValue);
} }
else if (type.GetProperty(key) is PropertyInfo propertyInfo) else if (type.GetProperty(key) is PropertyInfo propertyInfo)
{ {
depth++;
object? propertyValue = rootDeserializer(propertyInfo.PropertyType); object? propertyValue = rootDeserializer(propertyInfo.PropertyType);
depth--;
propertyInfo.SetValue(instance, propertyValue); propertyInfo.SetValue(instance, propertyValue);
} }
else else
@ -56,13 +65,15 @@ public class InstanceConverter : IEngineTypeYamlConverter
if (propertyInfo.GetCustomAttribute<IgnoreSerializationAttribute>() is not null) if (propertyInfo.GetCustomAttribute<IgnoreSerializationAttribute>() is not null)
continue; continue;
if ((propertyInfo.PropertyType.IsClass && propertyInfo.PropertyType != typeof(string)) || propertyInfo.PropertyType.IsAbstract)
continue;
emitter.Emit(new Scalar(propertyInfo.Name)); emitter.Emit(new Scalar(propertyInfo.Name));
object? propertyValue = propertyInfo.GetValue(value); object? propertyValue = propertyInfo.GetValue(value);
depth++;
if (depth <= maxDepth)
EmitValue(propertyValue, propertyInfo.PropertyType, emitter, serializer); EmitValue(propertyValue, propertyInfo.PropertyType, emitter, serializer);
else
emitter.Emit(new Scalar("Skipped"));
depth--;
} }
foreach (FieldInfo fieldInfo in typeData.Fields) foreach (FieldInfo fieldInfo in typeData.Fields)
@ -70,16 +81,18 @@ public class InstanceConverter : IEngineTypeYamlConverter
if (fieldInfo.GetCustomAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>() is not null) if (fieldInfo.GetCustomAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>() is not null)
continue; continue;
if (fieldInfo.GetCustomAttribute<SerializeAttribute>() is null) // if (fieldInfo.GetCustomAttribute<SerializeAttribute>() is null)
continue; // continue;
if ((fieldInfo.FieldType.IsClass && fieldInfo.FieldType != typeof(string)) || fieldInfo.FieldType.IsAbstract)
continue;
emitter.Emit(new Scalar(fieldInfo.Name)); emitter.Emit(new Scalar(fieldInfo.Name));
object? fieldValue = fieldInfo.GetValue(value); object? fieldValue = fieldInfo.GetValue(value);
depth++;
if (depth <= maxDepth)
EmitValue(fieldValue, fieldInfo.FieldType, emitter, serializer); EmitValue(fieldValue, fieldInfo.FieldType, emitter, serializer);
else
emitter.Emit(new Scalar("Skipped"));
depth--;
} }
emitter.Emit(new MappingEnd()); emitter.Emit(new MappingEnd());