namespace FoodFactory.Items; using System; using System.Collections.Generic; using FoodFactory.Math; using FoodFactory.Recipes; using SharpYaml; using SharpYaml.Serialization; public class ItemData { public string Id { get; set; } = ""; public string[] Extend { get; set; } = []; [YamlConverter(typeof(ComponentsPatcherConverter))] public Dictionary Components { get; set; } = []; public HashSet Remove { get; set; } = []; // public DataNode GetComponent(Type type) // { // if (!Components.TryGetValue(type.Name, out var value)) // { // throw new System.Exception(); // } // if (value is Dictionary thing) // { // return new DataNode(thing); // } // return new DataNode(new() { ["Value"] = value }); // } // public DataNode GetComponent() where T : struct => GetComponent(typeof(T)); } public class ComponentsPatcherConverter : YamlConverter> { private readonly Dictionary _componentsTypes = new(){ [nameof(Temperature)] = typeof(IValueProvider), // [nameof(Tags)] = typeof(IValueProvider), // [nameof(Name)] = typeof(IValueProvider), // [nameof(Mass)] = typeof(IValueProvider), // [nameof(Godot.Color)] = typeof(IValueProvider), // [nameof(BurnableTemp)] = typeof(IValueProvider), }; // public ComponentsPatcherConverter(Dictionary componentsTypes) // { // _componentsTypes = componentsTypes; // } public override Dictionary? Read(YamlReader reader) { var dict = new Dictionary(); if (reader.TokenType == YamlTokenType.StartMapping) { reader.Read(); } while (reader.TokenType != YamlTokenType.EndMapping) { var key = reader.ScalarValue; reader.Read(); var d = reader.ScalarValue; if (!_componentsTypes.TryGetValue(key, out var type)) { type = typeof(object); } if (!reader.TryGetCustomConverter(type, out var converter)){ converter = reader.GetConverter(type); } object data; data = converter.Read(reader, type); // reader.Read(); dict[key] = data; } return dict; } public override void Write(YamlWriter writer, Dictionary value) => throw new NotImplementedException(); }