Files
FoodFactory/src/Items/ItemData.cs

84 lines
2.6 KiB
C#

namespace FoodFactory.Items;
using System;
using System.Collections.Generic;
using FoodFactory.Blueprints.Providers;
using FoodFactory.Core.Components;
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<string, IValueProvider> Components { get; set; } = [];
public HashSet<string> Remove { get; set; } = [];
// public DataNode GetComponent(Type type)
// {
// if (!Components.TryGetValue(type.Name, out var value))
// {
// throw new System.Exception();
// }
// if (value is Dictionary<string, object> thing)
// {
// return new DataNode(thing);
// }
// return new DataNode(new() { ["Value"] = value });
// }
// public DataNode GetComponent<T>() where T : struct => GetComponent(typeof(T));
}
public class ComponentsPatcherConverter : YamlConverter<Dictionary<string, IValueProvider>>
{
private readonly Dictionary<string, Type> _componentsTypes = new(){
[nameof(Temperature)] = typeof(IValueProvider<Temperature>),
[nameof(Tags)] = typeof(IValueProvider<Tags>),
[nameof(Name)] = typeof(IValueProvider<Name>),
// [nameof(Mass)] = typeof(IValueProvider<Mass>),
// [nameof(Godot.Color)] = typeof(IValueProvider<Godot.Color>),
// [nameof(BurnableTemp)] = typeof(IValueProvider<BurnableTemp>),
};
// public ComponentsPatcherConverter(Dictionary<string, Type> componentsTypes)
// {
// _componentsTypes = componentsTypes;
// }
public override Dictionary<string, IValueProvider>? Read(YamlReader reader)
{
var dict = new Dictionary<string,IValueProvider>();
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 = converter.Read(reader, type);
if (data is not IValueProvider)
{
continue;
}
// reader.Read();
dict[key] = (IValueProvider)data;
}
return dict;
}
public override void Write(YamlWriter writer, Dictionary<string, IValueProvider> value) => throw new NotImplementedException();
}