Added DataBased Compiled/Provider system
This commit is contained in:
79
src/Items/ItemData.cs
Normal file
79
src/Items/ItemData.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
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<string, object> 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, object>>
|
||||
{
|
||||
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, object>? Read(YamlReader reader)
|
||||
{
|
||||
var dict = new Dictionary<string,object>();
|
||||
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<string, object> value) => throw new NotImplementedException();
|
||||
}
|
||||
Reference in New Issue
Block a user