feat: behaviour & behaviour controller converters added

This commit is contained in:
Syntriax 2025-04-25 21:23:31 +03:00
parent f92f36442c
commit b002dd469a
3 changed files with 151 additions and 3 deletions

View File

@ -0,0 +1,76 @@
using Syntriax.Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Syntriax.Engine.Serialization;
public class BehaviourControllerConverter : IEngineTypeYamlConverter
{
private const string SERIALIZED_SCALAR_NAME = "Properties";
private const string BEHAVIOURS_SCALAR_NAME = "Behaviours";
public bool Accepts(Type type) => typeof(IBehaviourController).IsAssignableFrom(type);
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
string id;
IBehaviourController behaviourController;
IStateEnable stateEnable;
List<IBehaviour> behaviours;
parser.Consume<MappingStart>();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IBehaviourController.Id)) != 0)
throw new();
id = parser.Consume<Scalar>().Value;
if (parser.Consume<Scalar>().Value.CompareTo(SERIALIZED_SCALAR_NAME) != 0)
throw new();
SerializedClass instanceSerializedClass = (SerializedClass)rootDeserializer(typeof(SerializedClass))!;
behaviourController = (IBehaviourController)instanceSerializedClass.CreateInstance();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IBehaviourController.StateEnable)) != 0)
throw new();
stateEnable = (IStateEnable)rootDeserializer(typeof(IStateEnable))!;
if (parser.Consume<Scalar>().Value.CompareTo(BEHAVIOURS_SCALAR_NAME) != 0)
throw new();
behaviours = (List<IBehaviour>)rootDeserializer(typeof(List<IBehaviour>))!;
parser.Consume<MappingEnd>();
behaviourController.Id = id;
stateEnable.Assign(behaviourController);
behaviourController.Assign(stateEnable);
foreach (IBehaviour behaviour in behaviours)
behaviourController.AddBehaviour(behaviour);
return behaviourController;
}
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
IBehaviourController behaviourController = (IBehaviourController)value!;
emitter.Emit(new MappingStart());
emitter.Emit(new Scalar(nameof(IBehaviourController.Id)));
emitter.Emit(new Scalar(behaviourController.Id));
emitter.Emit(new Scalar(SERIALIZED_SCALAR_NAME));
serializer(new SerializedClass(behaviourController));
emitter.Emit(new Scalar(nameof(IBehaviourController.StateEnable)));
serializer(behaviourController.StateEnable);
emitter.Emit(new Scalar(BEHAVIOURS_SCALAR_NAME));
serializer(behaviourController.GetBehaviours<IBehaviour>());
emitter.Emit(new MappingEnd());
}
}

View File

@ -0,0 +1,73 @@
using Syntriax.Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Syntriax.Engine.Serialization;
public class BehaviourConverter : IEngineTypeYamlConverter
{
private const string SERIALIZED_SCALAR_NAME = "Properties";
public bool Accepts(Type type) => typeof(IBehaviour).IsAssignableFrom(type);
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
string id;
int priority;
IBehaviour behaviour;
IStateEnable stateEnable;
parser.Consume<MappingStart>();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IBehaviour.Id)) != 0)
throw new();
id = parser.Consume<Scalar>().Value;
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IBehaviour.Priority)) != 0)
throw new();
priority = int.Parse(parser.Consume<Scalar>().Value);
if (parser.Consume<Scalar>().Value.CompareTo(SERIALIZED_SCALAR_NAME) != 0)
throw new();
SerializedClass instanceSerializedClass = (SerializedClass)rootDeserializer(typeof(SerializedClass))!;
behaviour = (IBehaviour)instanceSerializedClass.CreateInstance();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IBehaviour.StateEnable)) != 0)
throw new();
stateEnable = (IStateEnable)rootDeserializer(typeof(IStateEnable))!;
parser.Consume<MappingEnd>();
behaviour.Id = id;
behaviour.Priority = priority;
stateEnable.Assign(behaviour);
behaviour.Assign(stateEnable);
return behaviour;
}
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
IBehaviour behaviour = (IBehaviour)value!;
emitter.Emit(new MappingStart());
emitter.Emit(new Scalar(nameof(IBehaviour.Id)));
emitter.Emit(new Scalar(behaviour.Id));
emitter.Emit(new Scalar(nameof(IBehaviour.Priority)));
emitter.Emit(new Scalar(behaviour.Priority.ToString()));
emitter.Emit(new Scalar(SERIALIZED_SCALAR_NAME));
serializer(new SerializedClass(behaviour));
emitter.Emit(new Scalar(nameof(IBehaviour.StateEnable)));
serializer(behaviour.StateEnable);
emitter.Emit(new MappingEnd());
}
}

View File

@ -43,8 +43,7 @@ public class UniverseObjectConverter : IEngineTypeYamlConverter
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverseObject.BehaviourController)) != 0)
throw new();
SerializedClass behaviourControllerSerializedClass = (SerializedClass)rootDeserializer(typeof(SerializedClass))!;
behaviourController = (IBehaviourController)behaviourControllerSerializedClass.CreateInstance();
behaviourController = (IBehaviourController)rootDeserializer(typeof(IBehaviourController))!;
if (parser.Consume<Scalar>().Value.CompareTo(nameof(IUniverseObject.Children)) != 0)
throw new();
@ -86,7 +85,7 @@ public class UniverseObjectConverter : IEngineTypeYamlConverter
serializer(universeObject.StateEnable);
emitter.Emit(new Scalar(nameof(IUniverseObject.BehaviourController)));
serializer(new SerializedClass(universeObject.BehaviourController));
serializer(universeObject.BehaviourController);
emitter.Emit(new Scalar(nameof(IUniverseObject.Children)));
serializer(universeObject.Children);