using System; using System.Linq; using Engine.Serialization.DTOs; using YamlDotNet.Core; using YamlDotNet.Core.Events; using YamlDotNet.Serialization; namespace Engine.Serialization; internal class StateEnableYamlConverter : IYamlTypeConverter { public bool Accepts(Type type) => type == typeof(StateEnableDTO); public object ReadYaml(IParser parser, Type type) { if (parser.Current is not MappingStart) throw new InvalidOperationException("Expected MappingStart"); parser.MoveNext(); var stateEnable = new StateEnableDTO(); while (parser.Current != null && parser.Current is not MappingEnd) { var propertyName = ((Scalar)parser.Current).Value; parser.MoveNext(); switch (propertyName) { case nameof(StateEnableDTO.ClassName): stateEnable.ClassName = ((Scalar)parser.Current).Value; break; case nameof(StateEnableDTO.Enabled): stateEnable.Enabled = bool.Parse(((Scalar)parser.Current).Value); break; } parser.MoveNext(); } return stateEnable; } public void WriteYaml(IEmitter emitter, object? value, Type type) { var stateEnable = (StateEnableDTO)(value ?? throw new Exception()); emitter.Emit(new MappingStart()); emitter.Emit(new Scalar(nameof(StateEnableDTO.ClassName))); emitter.Emit(new Scalar(stateEnable.ClassName)); emitter.Emit(new Scalar(nameof(StateEnableDTO.Enabled))); emitter.Emit(new Scalar(stateEnable.Enabled.ToString())); emitter.Emit(new MappingEnd()); } }