feat: wip universe object converter added
This commit is contained in:
parent
c704173183
commit
bb934b59f3
@ -4,33 +4,33 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Syntriax.Engine.Serialization;
|
namespace Syntriax.Engine.Serialization;
|
||||||
|
|
||||||
public class EntityReferenceConverter : IEngineTypeYamlConverter
|
// public class EntityReferenceConverter : IEngineTypeYamlConverter
|
||||||
{
|
// {
|
||||||
public bool Accepts(Type type) => type == typeof(EntityReference);
|
// public bool Accepts(Type type) => type == typeof(EntityReference);
|
||||||
|
|
||||||
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
// public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
// {
|
||||||
parser.Consume<MappingStart>();
|
// parser.Consume<MappingStart>();
|
||||||
|
|
||||||
if (parser.Consume<Scalar>().Value.CompareTo(nameof(EntityReference.Id)) != 0)
|
// if (parser.Consume<Scalar>().Value.CompareTo(nameof(EntityReference.Id)) != 0)
|
||||||
throw new ArgumentException($"{nameof(EntityReference)} mapping must start with {nameof(EntityReference.Id)}");
|
// throw new ArgumentException($"{nameof(EntityReference)} mapping must start with {nameof(EntityReference.Id)}");
|
||||||
string id = parser.Consume<Scalar>().Value;
|
// string id = parser.Consume<Scalar>().Value;
|
||||||
|
|
||||||
TypeContainer typeContainer = (TypeContainer)rootDeserializer(typeof(TypeContainer))!;
|
// TypeContainer typeContainer = (TypeContainer)rootDeserializer(typeof(TypeContainer))!;
|
||||||
|
|
||||||
parser.Consume<MappingEnd>();
|
// parser.Consume<MappingEnd>();
|
||||||
|
|
||||||
return new EntityReference() { Id = id, TypeContainer = typeContainer };
|
// return new EntityReference() { Id = id, TypeContainer = typeContainer };
|
||||||
}
|
// }
|
||||||
|
|
||||||
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
// public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||||
{
|
// {
|
||||||
EntityReference? entityReference = (EntityReference)value!;
|
// EntityReference? entityReference = (EntityReference)value!;
|
||||||
|
|
||||||
emitter.Emit(new MappingStart());
|
// emitter.Emit(new MappingStart());
|
||||||
emitter.Emit(new Scalar(nameof(EntityReference.Id)));
|
// emitter.Emit(new Scalar(nameof(EntityReference.Id)));
|
||||||
emitter.Emit(new Scalar(entityReference.Id));
|
// emitter.Emit(new Scalar(entityReference.Id));
|
||||||
serializer(entityReference.TypeContainer, typeof(TypeContainer));
|
// serializer(entityReference.TypeContainer, typeof(TypeContainer));
|
||||||
emitter.Emit(new MappingEnd());
|
// emitter.Emit(new MappingEnd());
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
96
Engine.Serialization/Converters/UniverseObjectConverter.cs
Normal file
96
Engine.Serialization/Converters/UniverseObjectConverter.cs
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
using Syntriax.Engine.Core;
|
||||||
|
using YamlDotNet.Core;
|
||||||
|
using YamlDotNet.Core.Events;
|
||||||
|
using YamlDotNet.Serialization;
|
||||||
|
|
||||||
|
namespace Syntriax.Engine.Serialization;
|
||||||
|
|
||||||
|
public class UniverseObjectConverter : IEngineTypeYamlConverter
|
||||||
|
{
|
||||||
|
private const string SERIALIZED_SCALAR_NAME = "Properties";
|
||||||
|
|
||||||
|
public bool Accepts(Type type) => typeof(IUniverseObject).IsAssignableFrom(type);
|
||||||
|
|
||||||
|
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
|
{
|
||||||
|
string id;
|
||||||
|
string name;
|
||||||
|
|
||||||
|
IUniverseObject universeObject;
|
||||||
|
IStateEnable stateEnable;
|
||||||
|
IBehaviourController behaviourController;
|
||||||
|
List<IUniverseObject> children;
|
||||||
|
|
||||||
|
parser.Consume<MappingStart>();
|
||||||
|
|
||||||
|
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverseObject.Id)) != 0)
|
||||||
|
throw new();
|
||||||
|
id = parser.Consume<Scalar>().Value;
|
||||||
|
|
||||||
|
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverseObject.Name)) != 0)
|
||||||
|
throw new();
|
||||||
|
name = parser.Consume<Scalar>().Value;
|
||||||
|
|
||||||
|
if (parser.Consume<Scalar>().Value.CompareTo(SERIALIZED_SCALAR_NAME) != 0)
|
||||||
|
throw new();
|
||||||
|
SerializedClass instanceSerializedClass = (SerializedClass)rootDeserializer(typeof(SerializedClass))!;
|
||||||
|
universeObject = (IUniverseObject)instanceSerializedClass.CreateInstance();
|
||||||
|
|
||||||
|
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverseObject.StateEnable)) != 0)
|
||||||
|
throw new();
|
||||||
|
SerializedClass stateEnableSerializedClass = (SerializedClass)rootDeserializer(typeof(SerializedClass))!;
|
||||||
|
stateEnable = (IStateEnable)stateEnableSerializedClass.CreateInstance();
|
||||||
|
|
||||||
|
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverseObject.BehaviourController)) != 0)
|
||||||
|
throw new();
|
||||||
|
SerializedClass behaviourControllerSerializedClass = (SerializedClass)rootDeserializer(typeof(SerializedClass))!;
|
||||||
|
behaviourController = (IBehaviourController)behaviourControllerSerializedClass.CreateInstance();
|
||||||
|
|
||||||
|
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverseObject.Children)) != 0)
|
||||||
|
throw new();
|
||||||
|
children = (List<IUniverseObject>)rootDeserializer(typeof(List<IUniverseObject>))!;
|
||||||
|
|
||||||
|
parser.Consume<MappingEnd>();
|
||||||
|
|
||||||
|
universeObject.Id = id;
|
||||||
|
universeObject.Name = name;
|
||||||
|
|
||||||
|
stateEnable.Assign(universeObject);
|
||||||
|
universeObject.Assign(stateEnable);
|
||||||
|
|
||||||
|
behaviourController.Assign(universeObject);
|
||||||
|
universeObject.Assign(behaviourController);
|
||||||
|
|
||||||
|
foreach (IUniverseObject child in children)
|
||||||
|
universeObject.AddChild(child);
|
||||||
|
|
||||||
|
return universeObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||||
|
{
|
||||||
|
IUniverseObject universeObject = (IUniverseObject)value!;
|
||||||
|
|
||||||
|
emitter.Emit(new MappingStart());
|
||||||
|
|
||||||
|
emitter.Emit(new Scalar(nameof(IUniverseObject.Id)));
|
||||||
|
emitter.Emit(new Scalar(universeObject.Id));
|
||||||
|
|
||||||
|
emitter.Emit(new Scalar(nameof(IUniverseObject.Name)));
|
||||||
|
emitter.Emit(new Scalar(universeObject.Name));
|
||||||
|
|
||||||
|
emitter.Emit(new Scalar(SERIALIZED_SCALAR_NAME));
|
||||||
|
serializer(new SerializedClass(universeObject));
|
||||||
|
|
||||||
|
emitter.Emit(new Scalar(nameof(IUniverseObject.StateEnable)));
|
||||||
|
serializer(new SerializedClass(universeObject.StateEnable));
|
||||||
|
|
||||||
|
emitter.Emit(new Scalar(nameof(IUniverseObject.BehaviourController)));
|
||||||
|
serializer(new SerializedClass(universeObject.BehaviourController));
|
||||||
|
|
||||||
|
emitter.Emit(new Scalar(nameof(IUniverseObject.Children)));
|
||||||
|
serializer(universeObject.Children);
|
||||||
|
|
||||||
|
emitter.Emit(new MappingEnd());
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
|
using Syntriax.Engine.Core;
|
||||||
using Syntriax.Engine.Core.Factory;
|
using Syntriax.Engine.Core.Factory;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Serialization;
|
namespace Syntriax.Engine.Serialization;
|
||||||
@ -40,7 +41,11 @@ public class SerializedClass
|
|||||||
if (!isFullySerializable && !privatePropertyInfo.HasAttribute<SerializeAttribute>())
|
if (!isFullySerializable && !privatePropertyInfo.HasAttribute<SerializeAttribute>())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Private.Add(privatePropertyInfo.Name, privatePropertyInfo.GetValue(@class));
|
object? value = privatePropertyInfo.GetValue(@class);
|
||||||
|
if (value is IEntity entity)
|
||||||
|
Private.Add(privatePropertyInfo.Name, entity.Id);
|
||||||
|
else
|
||||||
|
Private.Add(privatePropertyInfo.Name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (PropertyInfo publicPropertyInfo in Utils.GetPropertyInfosIncludingBaseClasses(type, PUBLIC_BINDING_FLAGS))
|
foreach (PropertyInfo publicPropertyInfo in Utils.GetPropertyInfosIncludingBaseClasses(type, PUBLIC_BINDING_FLAGS))
|
||||||
@ -54,7 +59,11 @@ public class SerializedClass
|
|||||||
if (!isFullySerializable && !publicPropertyInfo.HasAttribute<SerializeAttribute>())
|
if (!isFullySerializable && !publicPropertyInfo.HasAttribute<SerializeAttribute>())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Public.Add(publicPropertyInfo.Name, publicPropertyInfo.GetValue(@class));
|
object? value = publicPropertyInfo.GetValue(@class);
|
||||||
|
if (value is IEntity entity)
|
||||||
|
Public.Add(publicPropertyInfo.Name, entity.Id);
|
||||||
|
else
|
||||||
|
Public.Add(publicPropertyInfo.Name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (FieldInfo privateFieldInfo in Utils.GetFieldInfosIncludingBaseClasses(type, PRIVATE_BINDING_FLAGS))
|
foreach (FieldInfo privateFieldInfo in Utils.GetFieldInfosIncludingBaseClasses(type, PRIVATE_BINDING_FLAGS))
|
||||||
@ -65,7 +74,11 @@ public class SerializedClass
|
|||||||
if (!isFullySerializable && !privateFieldInfo.HasAttribute<SerializeAttribute>())
|
if (!isFullySerializable && !privateFieldInfo.HasAttribute<SerializeAttribute>())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Private.Add(privateFieldInfo.Name, privateFieldInfo.GetValue(@class));
|
object? value = privateFieldInfo.GetValue(@class);
|
||||||
|
if (value is IEntity entity)
|
||||||
|
Private.Add(privateFieldInfo.Name, entity.Id);
|
||||||
|
else
|
||||||
|
Private.Add(privateFieldInfo.Name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (FieldInfo publicFieldInfo in Utils.GetFieldInfosIncludingBaseClasses(type, PUBLIC_BINDING_FLAGS))
|
foreach (FieldInfo publicFieldInfo in Utils.GetFieldInfosIncludingBaseClasses(type, PUBLIC_BINDING_FLAGS))
|
||||||
@ -76,7 +89,11 @@ public class SerializedClass
|
|||||||
if (!isFullySerializable && !publicFieldInfo.HasAttribute<SerializeAttribute>())
|
if (!isFullySerializable && !publicFieldInfo.HasAttribute<SerializeAttribute>())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Public.Add(publicFieldInfo.Name, publicFieldInfo.GetValue(@class));
|
object? value = publicFieldInfo.GetValue(@class);
|
||||||
|
if (value is IEntity entity)
|
||||||
|
Public.Add(publicFieldInfo.Name, entity.Id);
|
||||||
|
else
|
||||||
|
Public.Add(publicFieldInfo.Name, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ namespace Syntriax.Engine.Serialization;
|
|||||||
public static class Serializer
|
public static class Serializer
|
||||||
{
|
{
|
||||||
private static readonly ISerializer serializer = GetSerializer();
|
private static readonly ISerializer serializer = GetSerializer();
|
||||||
private static ISerializer GetSerializer(EntityFinder? entityFinder = null)
|
private static ISerializer GetSerializer()
|
||||||
{
|
{
|
||||||
SerializerBuilder serializerBuilder = new SerializerBuilder()
|
SerializerBuilder serializerBuilder = new SerializerBuilder()
|
||||||
.WithNamingConvention(PascalCaseNamingConvention.Instance)
|
.WithNamingConvention(PascalCaseNamingConvention.Instance)
|
||||||
@ -16,9 +16,6 @@ public static class Serializer
|
|||||||
foreach (IEngineTypeYamlConverter typeConverter in GetEngineYamlTypeConverters())
|
foreach (IEngineTypeYamlConverter typeConverter in GetEngineYamlTypeConverters())
|
||||||
serializerBuilder = serializerBuilder.WithTypeConverter(typeConverter);
|
serializerBuilder = serializerBuilder.WithTypeConverter(typeConverter);
|
||||||
|
|
||||||
if (entityFinder is not null)
|
|
||||||
serializerBuilder = serializerBuilder.WithTypeConverter(new EntityConverter(entityFinder.Entities));
|
|
||||||
|
|
||||||
return serializerBuilder.Build();
|
return serializerBuilder.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,10 +39,7 @@ public static class Serializer
|
|||||||
|
|
||||||
public static string Serialize(object instance)
|
public static string Serialize(object instance)
|
||||||
{
|
{
|
||||||
EntityFinder entityFinder = new();
|
return serializer.Serialize(instance);
|
||||||
entityFinder.FindEntitiesUnder(instance);
|
|
||||||
ISerializer serializer = GetSerializer(entityFinder);
|
|
||||||
return serializer.Serialize(entityFinder.Entities);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static object Deserialize(string yaml)
|
public static object Deserialize(string yaml)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user