112 lines
3.6 KiB
C#
112 lines
3.6 KiB
C#
using System.Collections;
|
|
using System.Reflection;
|
|
|
|
using Syntriax.Engine.Core.Factory;
|
|
|
|
using YamlDotNet.Core;
|
|
using YamlDotNet.Core.Events;
|
|
using YamlDotNet.Serialization;
|
|
|
|
namespace Syntriax.Engine.Serialization;
|
|
|
|
public class InstanceConverter : IEngineTypeYamlConverter
|
|
{
|
|
public bool Accepts(Type type) => !type.IsPrimitive && type != typeof(string);
|
|
|
|
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
|
{
|
|
parser.Consume<MappingStart>();
|
|
|
|
TypeContainer typeContainer = (TypeContainer)rootDeserializer(typeof(TypeContainer))!;
|
|
|
|
object instance = TypeFactory.Get(typeContainer.Type);
|
|
|
|
while (!parser.TryConsume<MappingEnd>(out _))
|
|
{
|
|
string key = parser.Consume<Scalar>().Value;
|
|
|
|
if (type.GetField(key, BindingFlags.Instance | BindingFlags.NonPublic) is FieldInfo fieldInfo)
|
|
{
|
|
object? fieldValue = rootDeserializer(fieldInfo.FieldType);
|
|
fieldInfo.SetValue(instance, fieldValue);
|
|
}
|
|
else if (type.GetProperty(key) is PropertyInfo propertyInfo)
|
|
{
|
|
object? propertyValue = rootDeserializer(propertyInfo.PropertyType);
|
|
propertyInfo.SetValue(instance, propertyValue);
|
|
}
|
|
else
|
|
parser.SkipThisAndNestedEvents();
|
|
}
|
|
|
|
parser.TryConsume<MappingEnd>(out _);
|
|
|
|
return instance;
|
|
}
|
|
|
|
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
|
{
|
|
emitter.Emit(new MappingStart());
|
|
serializer(new TypeContainer(value), typeof(TypeContainer));
|
|
|
|
TypeData typeData = Utils.GetTypeData(type);
|
|
|
|
foreach (PropertyInfo propertyInfo in typeData.Properties)
|
|
{
|
|
if (propertyInfo.GetCustomAttribute<IgnoreSerializationAttribute>() 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);
|
|
}
|
|
|
|
foreach (FieldInfo fieldInfo in typeData.Fields)
|
|
{
|
|
if (fieldInfo.GetCustomAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>() is not null)
|
|
continue;
|
|
|
|
if (fieldInfo.GetCustomAttribute<SerializeAttribute>() is null)
|
|
continue;
|
|
|
|
if ((fieldInfo.FieldType.IsClass && fieldInfo.FieldType != typeof(string)) || fieldInfo.FieldType.IsAbstract)
|
|
continue;
|
|
|
|
emitter.Emit(new Scalar(fieldInfo.Name));
|
|
object? fieldValue = fieldInfo.GetValue(value);
|
|
|
|
EmitValue(fieldValue, fieldInfo.FieldType, emitter, serializer);
|
|
}
|
|
|
|
emitter.Emit(new MappingEnd());
|
|
}
|
|
|
|
private static void EmitValue(object? value, Type declaredType, IEmitter emitter, ObjectSerializer serializer)
|
|
{
|
|
if (value is null)
|
|
{
|
|
emitter.Emit(new Scalar(""));
|
|
return;
|
|
}
|
|
|
|
bool isSequence = Utils.IsEnumerable(declaredType);
|
|
|
|
if (!isSequence)
|
|
{
|
|
serializer(value);
|
|
return;
|
|
}
|
|
|
|
IEnumerable sequence = (IEnumerable)value;
|
|
|
|
emitter.Emit(new SequenceStart(null, null, false, SequenceStyle.Block));
|
|
foreach (object? item in sequence)
|
|
serializer(item);
|
|
emitter.Emit(new SequenceEnd());
|
|
}
|
|
}
|