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 StateEnableConverter : IEngineTypeYamlConverter { public bool Accepts(Type type) => typeof(IStateEnable).IsAssignableFrom(type); public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) { parser.Consume(); TypeContainer typeContainer = (TypeContainer)rootDeserializer(typeof(TypeContainer))!; EntityReference entityReference = (EntityReference)rootDeserializer(typeof(EntityReference))!; if (parser.Consume().Value.CompareTo(nameof(IStateEnable.Enabled)) != 0) throw new ArgumentException($"{nameof(IStateEnable)} mapping must have a {nameof(IStateEnable.Enabled)}"); bool enabled = bool.Parse(parser.Consume().Value); parser.Consume(); IStateEnable stateEnable = (IStateEnable)TypeFactory.Get(typeContainer.Type); stateEnable.Enabled = enabled; stateEnable.Assign((IEntity)TypeFactory.Get(entityReference.TypeContainer.Type)); return stateEnable; } public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) { IStateEnable stateEnable = (IStateEnable)value!; emitter.Emit(new MappingStart()); serializer(new TypeContainer(stateEnable), typeof(TypeContainer)); emitter.Emit(new Scalar(nameof(StateEnable.Entity))); serializer(new EntityReference(stateEnable.Entity), typeof(EntityReference)); emitter.Emit(new Scalar(nameof(IStateEnable.Enabled))); emitter.Emit(new Scalar(stateEnable.Enabled.ToString())); emitter.Emit(new MappingEnd()); } }