BREAKING CHANGE: moved yaml serialization from Engine.Serialization to Engine.Integration
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
|
||||
using Engine.Core;
|
||||
using Engine.Core.Serialization;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class BehaviourConverter : EngineTypeYamlSerializerBase<IBehaviour>
|
||||
{
|
||||
public override IBehaviour? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
bool isTrackingController = ProgressionTracker.Progression.ApproximatelyEquals(0f);
|
||||
ProgressionTracker.Set(isTrackingController ? .25f : ProgressionTracker.Progression, $"Reading behaviour information");
|
||||
|
||||
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))!;
|
||||
ProgressionTracker.Set(isTrackingController ? .5f : ProgressionTracker.Progression, $"Creating {instanceSerializedClass.Type}");
|
||||
behaviour = (IBehaviour)instanceSerializedClass.CreateInstance(EntityRegistry);
|
||||
|
||||
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);
|
||||
|
||||
ProgressionTracker.Set(isTrackingController ? 1f : ProgressionTracker.Progression, $"Created {instanceSerializedClass.Type}");
|
||||
return behaviour;
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
IBehaviour behaviour = (IBehaviour)value!;
|
||||
|
||||
bool isTrackingController = ProgressionTracker.Progression.ApproximatelyEquals(0f);
|
||||
ProgressionTracker.Set(isTrackingController ? .25f : ProgressionTracker.Progression, $"Serializing behaviour");
|
||||
|
||||
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);
|
||||
|
||||
ProgressionTracker.Set(isTrackingController ? 1f : ProgressionTracker.Progression, $"Serialized behaviour");
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user