chore: removed unused classes
This commit is contained in:
parent
3a0942ff46
commit
791349686b
@ -1,133 +0,0 @@
|
|||||||
using System.Collections;
|
|
||||||
using System.Reflection;
|
|
||||||
using Syntriax.Engine.Core;
|
|
||||||
using Syntriax.Engine.Core.Factory;
|
|
||||||
|
|
||||||
using YamlDotNet.Core;
|
|
||||||
using YamlDotNet.Core.Events;
|
|
||||||
using YamlDotNet.Serialization;
|
|
||||||
|
|
||||||
namespace Syntriax.Engine.Serialization;
|
|
||||||
|
|
||||||
public class EntityConverter(IReadOnlyDictionary<string, IEntity> entities) : IYamlTypeConverter
|
|
||||||
{
|
|
||||||
private readonly IReadOnlyDictionary<string, IEntity> Entities = entities;
|
|
||||||
private readonly HashSet<IEntity> entitiesVisited = [];
|
|
||||||
|
|
||||||
public bool Accepts(Type type) => typeof(IEntity).IsAssignableFrom(type);
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
if (type.HasAttribute<IgnoreSerializationAttribute>())
|
|
||||||
{
|
|
||||||
emitter.Emit(new Scalar(""));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
IEntity entity = (IEntity)value!;
|
|
||||||
if (!entitiesVisited.Add(entity))
|
|
||||||
{
|
|
||||||
emitter.Emit(new Scalar(entity.Id));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
emitter.Emit(new MappingStart());
|
|
||||||
serializer(new TypeContainer(value), typeof(TypeContainer));
|
|
||||||
|
|
||||||
TypeData typeData = Utils.GetTypeData(type);
|
|
||||||
|
|
||||||
foreach (PropertyInfo propertyInfo in typeData.Properties)
|
|
||||||
{
|
|
||||||
if (propertyInfo.HasAttribute<IgnoreSerializationAttribute>())
|
|
||||||
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.HasAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (fieldInfo.HasAttribute<IgnoreSerializationAttribute>())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// if (!fieldInfo.HasAttribute<SerializeAttribute>())
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value is IEntity entity)
|
|
||||||
{
|
|
||||||
emitter.Emit(new Scalar(entity.Id));
|
|
||||||
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)
|
|
||||||
if (value is IEntity sequenceEntity)
|
|
||||||
emitter.Emit(new Scalar(sequenceEntity.Id));
|
|
||||||
else
|
|
||||||
serializer(item);
|
|
||||||
emitter.Emit(new SequenceEnd());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,60 +0,0 @@
|
|||||||
using System.Collections;
|
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
using Syntriax.Engine.Core;
|
|
||||||
|
|
||||||
namespace Syntriax.Engine.Serialization;
|
|
||||||
|
|
||||||
public class EntityFinder
|
|
||||||
{
|
|
||||||
private readonly Dictionary<string, IEntity> _entities = [];
|
|
||||||
|
|
||||||
public IReadOnlyDictionary<string, IEntity> Entities => _entities;
|
|
||||||
|
|
||||||
public void FindEntitiesUnder(object @object)
|
|
||||||
{
|
|
||||||
TypeData typeData = Utils.GetTypeData(@object.GetType());
|
|
||||||
|
|
||||||
if (@object is not IEntity entity)
|
|
||||||
{
|
|
||||||
if (@object is IEnumerable enumerable && @object.GetType() != typeof(string))
|
|
||||||
foreach (object? listObject in enumerable)
|
|
||||||
FindEntitiesUnder(listObject);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Entities.ContainsKey(entity.Id))
|
|
||||||
return;
|
|
||||||
|
|
||||||
_entities.Add(entity.Id, entity);
|
|
||||||
|
|
||||||
foreach (PropertyInfo propertyInfo in typeData.Properties)
|
|
||||||
{
|
|
||||||
if (propertyInfo.PropertyType?.IsPrimitive ?? true || propertyInfo.PropertyType == typeof(string))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (propertyInfo.HasAttribute<IgnoreSerializationAttribute>())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (propertyInfo.GetValue(@object) is object propertyObject)
|
|
||||||
FindEntitiesUnder(propertyObject);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (FieldInfo fieldInfo in typeData.Fields)
|
|
||||||
{
|
|
||||||
if (fieldInfo.FieldType?.IsPrimitive ?? true || fieldInfo.FieldType == typeof(string))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (fieldInfo.HasAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// if (!fieldInfo.HasAttribute<SerializeAttribute>())
|
|
||||||
// continue;
|
|
||||||
|
|
||||||
if (fieldInfo.GetValue(@object) is object fieldObject)
|
|
||||||
FindEntitiesUnder(fieldObject);
|
|
||||||
}
|
|
||||||
|
|
||||||
FindEntitiesUnder(entity);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
using Syntriax.Engine.Core;
|
|
||||||
|
|
||||||
namespace Syntriax.Engine.Serialization;
|
|
||||||
|
|
||||||
public class EntityReference
|
|
||||||
{
|
|
||||||
public string Id { get; set; } = string.Empty;
|
|
||||||
public TypeContainer TypeContainer { get; set; } = new();
|
|
||||||
|
|
||||||
public EntityReference() { }
|
|
||||||
public EntityReference(IEntity entity) { TypeContainer = new(entity); Id = entity.Id; }
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
using YamlDotNet.Core;
|
|
||||||
using YamlDotNet.Core.Events;
|
|
||||||
using YamlDotNet.Serialization;
|
|
||||||
|
|
||||||
namespace Syntriax.Engine.Serialization;
|
|
||||||
|
|
||||||
// public class EntityReferenceConverter : IEngineTypeYamlConverter
|
|
||||||
// {
|
|
||||||
// public bool Accepts(Type type) => type == typeof(EntityReference);
|
|
||||||
|
|
||||||
// public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
|
||||||
// {
|
|
||||||
// parser.Consume<MappingStart>();
|
|
||||||
|
|
||||||
// if (parser.Consume<Scalar>().Value.CompareTo(nameof(EntityReference.Id)) != 0)
|
|
||||||
// throw new ArgumentException($"{nameof(EntityReference)} mapping must start with {nameof(EntityReference.Id)}");
|
|
||||||
// string id = parser.Consume<Scalar>().Value;
|
|
||||||
|
|
||||||
// TypeContainer typeContainer = (TypeContainer)rootDeserializer(typeof(TypeContainer))!;
|
|
||||||
|
|
||||||
// parser.Consume<MappingEnd>();
|
|
||||||
|
|
||||||
// return new EntityReference() { Id = id, TypeContainer = typeContainer };
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
|
||||||
// {
|
|
||||||
// EntityReference? entityReference = (EntityReference)value!;
|
|
||||||
|
|
||||||
// emitter.Emit(new MappingStart());
|
|
||||||
// emitter.Emit(new Scalar(nameof(EntityReference.Id)));
|
|
||||||
// emitter.Emit(new Scalar(entityReference.Id));
|
|
||||||
// serializer(entityReference.TypeContainer, typeof(TypeContainer));
|
|
||||||
// emitter.Emit(new MappingEnd());
|
|
||||||
// }
|
|
||||||
// }
|
|
@ -1,10 +0,0 @@
|
|||||||
namespace Syntriax.Engine.Serialization;
|
|
||||||
|
|
||||||
public class TypeContainer
|
|
||||||
{
|
|
||||||
public string Type { get; set; } = string.Empty;
|
|
||||||
|
|
||||||
public TypeContainer() { }
|
|
||||||
public TypeContainer(Type type) { Type = type.FullName ?? string.Empty; }
|
|
||||||
public TypeContainer(object? value) { Type = value?.GetType().FullName ?? string.Empty; }
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
using YamlDotNet.Core;
|
|
||||||
using YamlDotNet.Core.Events;
|
|
||||||
using YamlDotNet.Serialization;
|
|
||||||
|
|
||||||
namespace Syntriax.Engine.Serialization;
|
|
||||||
|
|
||||||
public class TypeContainerConverter : IEngineTypeYamlConverter
|
|
||||||
{
|
|
||||||
public bool Accepts(Type type) => type == typeof(TypeContainer);
|
|
||||||
|
|
||||||
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
|
||||||
{
|
|
||||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(TypeContainer.Type)) != 0)
|
|
||||||
throw new ArgumentException($"{nameof(TypeContainer)} mapping must start with {nameof(TypeContainer.Type)}");
|
|
||||||
string typeFullName = parser.Consume<Scalar>().Value;
|
|
||||||
|
|
||||||
return new TypeContainer() { Type = typeFullName };
|
|
||||||
}
|
|
||||||
|
|
||||||
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
|
||||||
{
|
|
||||||
TypeContainer? typeContainer = (TypeContainer)value!;
|
|
||||||
|
|
||||||
emitter.Emit(new Scalar(nameof(TypeContainer.Type)));
|
|
||||||
emitter.Emit(new Scalar(typeContainer.Type));
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user