Seprated many files, removed IDataCompiler with using YamlConverters. Refactored namespaces

This commit is contained in:
2026-05-28 11:42:36 -04:00
parent a76a50f38e
commit 0fa7ba766b
39 changed files with 1516 additions and 731 deletions

129
src/Blueprints/Blueprint.cs Normal file
View File

@@ -0,0 +1,129 @@
// namespace FoodFactory.Blueprints;
// using System;
// using System.Diagnostics;
// using System.Diagnostics.CodeAnalysis;
// using Arch.Core;
// using SharpYaml.Model;
// using SJK.Functional;
// using Utf8Json;
// public record Blueprint(string Name, Func<BlueprintContext, Entity> Factory)
// {
// }
// public record Blueprint(string Name, Func<BlueprintContext, Entity> Factory)
// {
// }
// public class ComponentBinderRegistry
// {
// private readonly Dictionary<ComponentType, IComponentBinder> _binder = [];
// public void Add<T>(ComponentBinder<T> componentBinder) where T : struct
// {
// _binder.Add(typeof(T), componentBinder);
// }
// public void Apply(Entity entity, World world, CompiledBlueprint itemData)
// {
// var s = world.GetSignature(entity);
// for (int i = 0; i < s.Count; i++)
// {
// if (!_binder.ContainsKey(s.Components[i]))
// {
// continue;
// }
// var binder = _binder[s.Components[i]];
// var type = s.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entity, itemData.Providers[type], context);
// }
// }
// public void Apply(Span<Entity> entities, World world, CompiledBlueprint itemData)
// {
// var signature = world.GetSignature(entities[0]);
// for (int i = 1; i < entities.Length; i++)
// {
// Debug.Assert(world.GetSignature(entities[i]) == signature);
// }
// for (int i = 0; i < signature.Count; i++)
// {
// var binder = _binder[signature.Components[i]];
// var type = signature.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entities, itemData.Providers[type], context);
// }
// }
// }
// public abstract class DataNode//(Dictionary<string, object> Data)
// {
// public abstract DataNodeType NodeType { get; }
// // public T Get<T>(string name) => (T)Data[name];
// }
// public enum DataNodeType
// {hthous stomatitis, pharyngitis, adenitis) is a
// Value,
// Sequence,
// Mapping,
// Component
// }
// public sealed class ValueNode(object value) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Value;
// public object Value { get; set; } = value;
// public Option<T> As<T>() => Value is T o?Option<T>.Some(o):Option<T>.None;
// }
// public sealed class SequenceNode(IReadOnlyList<DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Sequence;
// public IReadOnlyList<DataNode> Values { get; set; } = values;
// }
// public sealed class MappingNode(IReadOnlyDictionary<string, DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Mapping;
// public IReadOnlyDictionary<string, DataNode> Values { get; set; } = values;
// public bool TryGet<T>(string key, [NotNullWhen(true)] out T value) where T : DataNode
// {
// var result = Values.TryGetValue(key, out var node);
// value = (T)node!;
// return result;
// }
// public bool TryGet(string key, [NotNullWhen(true)] out DataNode node) => Values.TryGetValue(key, out node!);
// }
// public class DataNodeSerlicer : YamlConverter<DataNode>, IJsonFormatter<DataNode>
// {
// private Dictionary<string,string> _data = new();
// public DataNode Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
// {
// var i = 0;
// while (reader.ReadIsInObject(ref i))
// {
// var name = reader.ReadPropertyName();
// var value = reader.ReadString();
// _data.Add(name, value);
// }
// throw new NotImplementedException();
// }
// public void Serialize(ref JsonWriter writer, DataNode value, IJsonFormatterResolver formatterResolver) => throw new NotImplementedException();
// public override DataNode? Read(YamlReader reader)
// {
// // reader.pa
// throw new NotImplementedException();
// }
// public override void Write(YamlWriter writer, DataNode value)
// {
// throw new NotImplementedException();
// }
// }

View File

@@ -0,0 +1 @@
uid://cj088ogl8uqmt

View File

@@ -0,0 +1,121 @@
namespace FoodFactory.Blueprints;
using Arch.Core;
public struct BlueprintContext
{
public World World;
public BlueprintId BluePrintId;
public object PlaceHolderApi;
}
// public class ComponentBinderRegistry
// {
// private readonly Dictionary<ComponentType, IComponentBinder> _binder = [];
// public void Add<T>(ComponentBinder<T> componentBinder) where T : struct
// {
// _binder.Add(typeof(T), componentBinder);
// }
// public void Apply(Entity entity, World world, CompiledBlueprint itemData)
// {
// var s = world.GetSignature(entity);
// for (int i = 0; i < s.Count; i++)
// {
// if (!_binder.ContainsKey(s.Components[i]))
// {
// continue;
// }
// var binder = _binder[s.Components[i]];
// var type = s.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entity, itemData.Providers[type], context);
// }
// }
// public void Apply(Span<Entity> entities, World world, CompiledBlueprint itemData)
// {
// var signature = world.GetSignature(entities[0]);
// for (int i = 1; i < entities.Length; i++)
// {
// Debug.Assert(world.GetSignature(entities[i]) == signature);
// }
// for (int i = 0; i < signature.Count; i++)
// {
// var binder = _binder[signature.Components[i]];
// var type = signature.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entities, itemData.Providers[type], context);
// }
// }
// }
// public abstract class DataNode//(Dictionary<string, object> Data)
// {
// public abstract DataNodeType NodeType { get; }
// // public T Get<T>(string name) => (T)Data[name];
// }
// public enum DataNodeType
// {hthous stomatitis, pharyngitis, adenitis) is a
// Value,
// Sequence,
// Mapping,
// Component
// }
// public sealed class ValueNode(object value) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Value;
// public object Value { get; set; } = value;
// public Option<T> As<T>() => Value is T o?Option<T>.Some(o):Option<T>.None;
// }
// public sealed class SequenceNode(IReadOnlyList<DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Sequence;
// public IReadOnlyList<DataNode> Values { get; set; } = values;
// }
// public sealed class MappingNode(IReadOnlyDictionary<string, DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Mapping;
// public IReadOnlyDictionary<string, DataNode> Values { get; set; } = values;
// public bool TryGet<T>(string key, [NotNullWhen(true)] out T value) where T : DataNode
// {
// var result = Values.TryGetValue(key, out var node);
// value = (T)node!;
// return result;
// }
// public bool TryGet(string key, [NotNullWhen(true)] out DataNode node) => Values.TryGetValue(key, out node!);
// }
// public class DataNodeSerlicer : YamlConverter<DataNode>, IJsonFormatter<DataNode>
// {
// private Dictionary<string,string> _data = new();
// public DataNode Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
// {
// var i = 0;
// while (reader.ReadIsInObject(ref i))
// {
// var name = reader.ReadPropertyName();
// var value = reader.ReadString();
// _data.Add(name, value);
// }
// throw new NotImplementedException();
// }
// public void Serialize(ref JsonWriter writer, DataNode value, IJsonFormatterResolver formatterResolver) => throw new NotImplementedException();
// public override DataNode? Read(YamlReader reader)
// {
// // reader.pa
// throw new NotImplementedException();
// }
// public override void Write(YamlWriter writer, DataNode value)
// {
// throw new NotImplementedException();
// }
// }

View File

@@ -0,0 +1,14 @@
namespace FoodFactory.Blueprints;
using FoodFactory.Core;
public readonly record struct BlueprintId
{
public readonly int Id;
public CompiledBlueprint Blueprint() => _registry.GetEntry(Id);
private static readonly Registry<CompiledBlueprint> _registry = new();
public BlueprintId(CompiledBlueprint blueprint)
{
Id = _registry.GetOrCreate(blueprint);
}
}

View File

@@ -0,0 +1 @@
uid://b7kmhd6uamhg

View File

@@ -0,0 +1,304 @@
namespace FoodFactory.Blueprints;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FoodFactory.Blueprints.Providers;
using FoodFactory.Blueprints.Providers.Converters;
using FoodFactory.Items;
using Godot;
using SharpYaml;
public interface IBlueprintManger
{
CompiledBlueprint GetCompiledBlueprint(string name);
// ComponentBinderRegistry BinderRegistry { get; }
}
public class BlueprintManger : IBlueprintManger
{
public readonly Dictionary<string, CompiledBlueprint> CompiledBlueprints = [];
// public ComponentBinderRegistry BinderRegistry { get; }
public BlueprintManger()
{
var root = "/home/ronnie/Documents/Godot/Projects/ChickenGameTest/mods";
var dir = DirAccess.Open(root);
var itemDatas = new List<ItemData>();
var options = new YamlSerializerOptions()
{
Converters = [
// new ComponentsPatcherConverter(
// new(){
// [nameof(Temperature)] = typeof(IValueProvider<Temperature>),
// [nameof(Tags)] = typeof(IValueProvider<Tags>),
// [nameof(Name)] = typeof(IValueProvider<Name>),
// [nameof(Mass)] = typeof(IValueProvider<Mass>),
// [nameof(Color)] = typeof(IValueProvider<Color>),
// [nameof(BurnableTemp)] = typeof(IValueProvider<BurnableTemp>),
// }
// ),
new ComponentTemperatureConverter(),
new ComponentTagsConverter(),
new ComponentNameConverter(),
]
};
foreach (var item in dir.GetFiles())
{
if (item.EndsWith(".yaml"))
{
var file = FileAccess.Open(dir.GetCurrentDir() + "/" + item, FileAccess.ModeFlags.Read);
var data = YamlSerializer.Deserialize<ItemData>(file.GetAsText(),options);
itemDatas.Add(data);
// var compiledBlueprint = new CompiledBlueprint
// {
// // Id = data.Id,
// // Signature = new Signature([typeof(BlueprintId), .. types.Where(f => data.Components.ContainsKey(f.Name)).Select(Component.GetComponentType)]),
// // Providers = new IValueProvider[data.Components.Count]
// // };
// // var i = 0;
// // foreach (var item2 in data.Components)
// // {
// // var compiler = Providers[item2.Key];
// // var compiled = compiler.CompileUnTyped(item2.Value);
// // compiledBlueprint.Providers[i] = compiled;
// // i++;
// // }
// CompiledBlueprints.Add(compiledBlueprint.Id, compiledBlueprint);
}
}
//TODO Call a Patch method thingy to adjust exsting data before loading
CompiledBlueprints = LoadBluprints([.. itemDatas]);
}
private Dictionary<string, CompiledBlueprint> LoadBluprints(ItemData[] all, Dictionary<string, CompiledBlueprint>? existing = null)
{
var completed = existing is not null ? new(existing) : new Dictionary<string,CompiledBlueprint>();
var queue = new Queue<ItemData>(all);
while (queue.Any())
{
reset:
var item = queue.Dequeue();
var sources = new List<CompiledBlueprint>();
foreach (var c in item.Extend)
{
if (!completed.ContainsKey(c))
{
queue.Enqueue(item);
goto reset;
}
sources.Add(completed[c]);
}
var providers = new Dictionary<Type, List<IValueProvider>>();//TODO Make Blupritns able to hold muiple providers in a array for cases taht build off previouse providers
foreach (var blueprint in sources)
{
foreach (var provider in blueprint.Providers)
{
if (item.Remove.Contains(provider[0].GetProviderType().Name))
{
continue;
}
if (!providers.TryGetValue(provider[0].GetProviderType(), out var providersList))
{
providers[provider[0].GetProviderType()] = providersList = new();
}
for (int i = 0; i < provider.Length; i++)
{
if (provider[i].Mode == ProviderMode.Static)
{
providersList.Clear();
providersList.Add(provider[i]);
}
}
}
}
foreach (var component in item.Components)
{
// var compiler = Providers[component.Key];
var compiledProvider = component.Value;//compiler.CompileUnTyped(component.Value);
if (!providers.TryGetValue(compiledProvider.GetProviderType(), out var providersList))
{
providers[compiledProvider.GetProviderType()] = providersList = new();
}
providersList.Add(compiledProvider);
}
var compiled = new CompiledBlueprint()
{
Id = item.Id,
// Traits = new Tags([.. item.Components.Keys]),//TODO need a way for recipies to look in each 'parent/extend' for getting recipes, either iterating each parent pool(recipe behing only in max shared, or each buckert container a copy for each parent)
Signature = [.. providers.Keys],
Providers = [.. providers.Values.Select(list => list.ToArray())],
};
/*
ex Food-> onion -> red onion
if onion id, then onion and red onion bucket have a recipe
of for each trait, iterate each bucket, starting from spefic, to top.
*/
completed.Add(item.Id, compiled);
}
return completed;
}
public CompiledBlueprint? GetCompiledBlueprint(string id)
{
if (CompiledBlueprints.TryGetValue(id, out var compiledBlueprint))
{
return compiledBlueprint;
}
return null;
}
}
// public Blueprint GetBlueprint(string name)
// {
// // throw new NotImplementedException();
// if (!Blueprints.TryGetValue(name, out var blueprint))
// {
// //TODO this is where this should look through mods/base game
// if (name == "Potato")
// {
// blueprint = new Blueprint("Potato", static ctx => ctx.World.Create(new Name("Potato_raw"),
// new Tags("potato", "raw", "vegetable", "burnable"),
// new Temperature(40, TemperatureUnit.Fahrenheit),
// ctx.BluePrintId,
// new Bacteria(BacterialSystem.Resources.Add([new BacteriaData("slamnala", 400)])),
// new BurnableTemp(new Temperature(400, TemperatureUnit.Fahrenheit))));//TODO blueprintID should not be passed, but know when blueprint is created, meaning a rework of Blueprint id register
// }
// else if (name == "Onion")
// {
// blueprint = new Blueprint("Onion", static ctx => ctx.World.Create(new Name("Onion_raw"),
// new Tags("onion", "raw", "vegetable", "burnable", "sliceable"),
// new Temperature(71, TemperatureUnit.Fahrenheit),
// ctx.BluePrintId,
// new BurnableTemp(new Temperature(400, TemperatureUnit.Fahrenheit)),
// new Mass(Weight.FromGrams(110)),
// new Carbohydrates(Fiber: 2, Starch: 3, Sugars: new(Glucose: 0, Fructose: 1.2f, Sucrose: 1, Lactose: 0))
// ));//TODO blueprintID should not be passed, but know when blueprint is created, meaning a rework of Blueprint id register
// }
// else
// {
// throw new Exception();
// }
// }
// return blueprint;
// }
// }
// public class ComponentBinderRegistry
// {
// private readonly Dictionary<ComponentType, IComponentBinder> _binder = [];
// public void Add<T>(ComponentBinder<T> componentBinder) where T : struct
// {
// _binder.Add(typeof(T), componentBinder);
// }
// public void Apply(Entity entity, World world, CompiledBlueprint itemData)
// {
// var s = world.GetSignature(entity);
// for (int i = 0; i < s.Count; i++)
// {
// if (!_binder.ContainsKey(s.Components[i]))
// {
// continue;
// }
// var binder = _binder[s.Components[i]];
// var type = s.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entity, itemData.Providers[type], context);
// }
// }
// public void Apply(Span<Entity> entities, World world, CompiledBlueprint itemData)
// {
// var signature = world.GetSignature(entities[0]);
// for (int i = 1; i < entities.Length; i++)
// {
// Debug.Assert(world.GetSignature(entities[i]) == signature);
// }
// for (int i = 0; i < signature.Count; i++)
// {
// var binder = _binder[signature.Components[i]];
// var type = signature.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entities, itemData.Providers[type], context);
// }
// }
// }
// public abstract class DataNode//(Dictionary<string, object> Data)
// {
// public abstract DataNodeType NodeType { get; }
// // public T Get<T>(string name) => (T)Data[name];
// }
// public enum DataNodeType
// {hthous stomatitis, pharyngitis, adenitis) is a
// Value,
// Sequence,
// Mapping,
// Component
// }
// public sealed class ValueNode(object value) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Value;
// public object Value { get; set; } = value;
// public Option<T> As<T>() => Value is T o?Option<T>.Some(o):Option<T>.None;
// }
// public sealed class SequenceNode(IReadOnlyList<DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Sequence;
// public IReadOnlyList<DataNode> Values { get; set; } = values;
// }
// public sealed class MappingNode(IReadOnlyDictionary<string, DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Mapping;
// public IReadOnlyDictionary<string, DataNode> Values { get; set; } = values;
// public bool TryGet<T>(string key, [NotNullWhen(true)] out T value) where T : DataNode
// {
// var result = Values.TryGetValue(key, out var node);
// value = (T)node!;
// return result;
// }
// public bool TryGet(string key, [NotNullWhen(true)] out DataNode node) => Values.TryGetValue(key, out node!);
// }
// public class DataNodeSerlicer : YamlConverter<DataNode>, IJsonFormatter<DataNode>
// {
// private Dictionary<string,string> _data = new();
// public DataNode Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
// {
// var i = 0;
// while (reader.ReadIsInObject(ref i))
// {
// var name = reader.ReadPropertyName();
// var value = reader.ReadString();
// _data.Add(name, value);
// }
// throw new NotImplementedException();
// }
// public void Serialize(ref JsonWriter writer, DataNode value, IJsonFormatterResolver formatterResolver) => throw new NotImplementedException();
// public override DataNode? Read(YamlReader reader)
// {
// // reader.pa
// throw new NotImplementedException();
// }
// public override void Write(YamlWriter writer, DataNode value)
// {
// throw new NotImplementedException();
// }
// }

View File

@@ -0,0 +1,48 @@
namespace FoodFactory.Blueprints;
using Arch.Core;
using FoodFactory.Blueprints.Providers;
using FoodFactory.Core.Components;
// public sealed class StaticReflectionCompiledNode<T> : IValueProvider<T> where T : struct
// {
// //TODO Make more performance by only having properties of each method, and share use a list using tuples instead.
// // private readonly Dictionary<string, object> _data;
// private readonly T _template;
// public StaticReflectionCompiledNode(Dictionary<string, object> itemData)
// {
// StructReflection.ApplyValues(ref _template, itemData);
// }
// public void Resolve(BlueprintContext context, ref T value) => value = _template;
// public ProviderMode Mode => ProviderMode.Static;
// }
// public sealed class RuntimeReflectionCompiledNode<T> : IValueProvider<T> where T : struct
// {
// //TODO Make more performance by only having properties of each method, and share use a list using tuples instead.
// private readonly Dictionary<string, object> _data;
// // private readonly T _template;
// public RuntimeReflectionCompiledNode(Dictionary<string, object> itemData)
// {
// _data = itemData;
// }
// public void Resolve(BlueprintContext context, ref T value) => StructReflection.ApplyValues(ref value, _data);
// public ProviderMode Mode => ProviderMode.Modify;
// }
public class CompiledBlueprint
{
public required string Id { get; init; }
public Tags Traits { get; set; }
public IValueProvider[][] Providers = [];
public Signature Signature;
public Entity CreateEmptyItem(BlueprintContext blueprintContext) => blueprintContext.World.Create(Signature);
}

View File

@@ -0,0 +1,162 @@
namespace FoodFactory.Blueprints.Providers.Converters;
using System;
using FoodFactory.Core.Components;
using SharpYaml.Serialization;
public sealed class ComponentTagsConverter : ItemComponentsConverter<Tags>
{
public override IValueProvider<Tags> Read(YamlReader reader)
{
if (reader.TokenType == YamlTokenType.StartSequence)
{
if (reader.GetConverter(typeof(string[])) is not YamlConverter<string[]> converter)
{
throw new Exception();
}
return new StaticCompiledNode<Tags>(new Tags(converter.Read(reader)));
}
if (reader.TokenType == YamlTokenType.StartMapping)
{
string[] with = [];
string[] exclude = [];
reader.Read();
while (reader.TokenType != YamlTokenType.EndMapping)
{
var key = reader.ScalarValue;
reader.Read();
if (key == "With")
{
if (reader.GetConverter(typeof(string[])) is not YamlConverter<string[]> converter)
{
throw new Exception();
}
with = converter.Read(reader);
}
if (key == "Except")
{
if (reader.GetConverter(typeof(string[])) is not YamlConverter<string[]> converter)
{
throw new Exception();
}
exclude = converter.Read(reader);
}
reader.Read();
}
return new FactoryCompiledNode<Tags>((in factory, ref tags) => tags.With(with).WithOut(exclude));
}
throw new NotSupportedException();
}
public override void Write(YamlWriter writer, IValueProvider<Tags> value) => throw new NotImplementedException();
}
// public class ComponentBinderRegistry
// {
// private readonly Dictionary<ComponentType, IComponentBinder> _binder = [];
// public void Add<T>(ComponentBinder<T> componentBinder) where T : struct
// {
// _binder.Add(typeof(T), componentBinder);
// }
// public void Apply(Entity entity, World world, CompiledBlueprint itemData)
// {
// var s = world.GetSignature(entity);
// for (int i = 0; i < s.Count; i++)
// {
// if (!_binder.ContainsKey(s.Components[i]))
// {
// continue;
// }
// var binder = _binder[s.Components[i]];
// var type = s.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entity, itemData.Providers[type], context);
// }
// }
// public void Apply(Span<Entity> entities, World world, CompiledBlueprint itemData)
// {
// var signature = world.GetSignature(entities[0]);
// for (int i = 1; i < entities.Length; i++)
// {
// Debug.Assert(world.GetSignature(entities[i]) == signature);
// }
// for (int i = 0; i < signature.Count; i++)
// {
// var binder = _binder[signature.Components[i]];
// var type = signature.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entities, itemData.Providers[type], context);
// }
// }
// }
// public abstract class DataNode//(Dictionary<string, object> Data)
// {
// public abstract DataNodeType NodeType { get; }
// // public T Get<T>(string name) => (T)Data[name];
// }
// public enum DataNodeType
// {hthous stomatitis, pharyngitis, adenitis) is a
// Value,
// Sequence,
// Mapping,
// Component
// }
// public sealed class ValueNode(object value) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Value;
// public object Value { get; set; } = value;
// public Option<T> As<T>() => Value is T o?Option<T>.Some(o):Option<T>.None;
// }
// public sealed class SequenceNode(IReadOnlyList<DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Sequence;
// public IReadOnlyList<DataNode> Values { get; set; } = values;
// }
// public sealed class MappingNode(IReadOnlyDictionary<string, DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Mapping;
// public IReadOnlyDictionary<string, DataNode> Values { get; set; } = values;
// public bool TryGet<T>(string key, [NotNullWhen(true)] out T value) where T : DataNode
// {
// var result = Values.TryGetValue(key, out var node);
// value = (T)node!;
// return result;
// }
// public bool TryGet(string key, [NotNullWhen(true)] out DataNode node) => Values.TryGetValue(key, out node!);
// }
// public class DataNodeSerlicer : YamlConverter<DataNode>, IJsonFormatter<DataNode>
// {
// private Dictionary<string,string> _data = new();
// public DataNode Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
// {
// var i = 0;
// while (reader.ReadIsInObject(ref i))
// {
// var name = reader.ReadPropertyName();
// var value = reader.ReadString();
// _data.Add(name, value);
// }
// throw new NotImplementedException();
// }
// public void Serialize(ref JsonWriter writer, DataNode value, IJsonFormatterResolver formatterResolver) => throw new NotImplementedException();
// public override DataNode? Read(YamlReader reader)
// {
// // reader.pa
// throw new NotImplementedException();
// }
// public override void Write(YamlWriter writer, DataNode value)
// {
// throw new NotImplementedException();
// }
// }

View File

@@ -0,0 +1,263 @@
namespace FoodFactory.Blueprints.Providers.Converters;
using System;
using FoodFactory.Math;
using SharpYaml.Serialization;
// public interface IComponentBinder
// {
// ComponentType ComponentType { get; }
// void ApplyUntyped(
// Entity existing,
// IValueProvider data,
// BlueprintContext context);
// void ApplyUntyped(
// Span<Entity> existing,
// IValueProvider data,
// BlueprintContext context);
// }
// public class ComponentBinder<T> : IComponentBinder where T : struct
// {
// public ComponentType ComponentType => Component<T>.ComponentType;
// public void ApplyUntyped(
// Entity existing,
// IValueProvider data,
// BlueprintContext context)
// {
// if (data is IValueProvider<T> dataT)
// {
// ref var component = ref context.World.Get<T>(existing);
// Apply(ref component, dataT, context);
// return;
// }
// throw new System.Exception();
// }
// public void ApplyUntyped(Span<Entity> existing, IValueProvider data, BlueprintContext context)
// {
// if (data is not IValueProvider<T> dataT)
// {
// throw new System.Exception();
// }
// for (int i = 0; i < existing.Length; i++)
// {
// Apply(ref existing[i].Get<T>(), dataT, context);
// }
// }
// public void Apply(
// ref T existing,
// IValueProvider<T> data,
// BlueprintContext context) => data.Resolve(context, ref existing);
// }
// public class DefaultBinder<T> : ComponentBinder<T> where T : struct
// {
// public override void Apply(ref T existing, IValueProvider<T> data, BlueprintContext context);
// }
// public class TemperatureBinder : ComponentBinder<Temperature>
// {
// public override void Apply(ref Temperature existing, IValueProvider<Temperature> data, BlueprintContext context) => data.Resolve(context, ref existing);
// }
public sealed class ComponentTemperatureConverter : ItemComponentsConverter<Temperature>//, IDataCompiler<Temperature>
{
// public IValueProvider<Temperature> Compile(object data)
// {
// if (data is IValueProvider<Temperature> t)
// {
// return t;
// }
// if (data is string says)
// {
// if (says == "ambient")
// {
// return new AmbientTemperatureProvider();
// }
// }
// if (data is float f)
// {
// return new StaticCompiledNode<Temperature>(new Temperature(f));
// }
// if (data is double d)
// {
// return new StaticCompiledNode<Temperature>(new Temperature(d));
// }
// if (data is Dictionary<string, object> dict)
// {
// return new StaticReflectionCompiledNode<Temperature>(dict);
// }
// throw new NotSupportedException();
// }
public override IValueProvider<Temperature>? Read(YamlReader reader)
{
if (reader.TokenType == YamlTokenType.Scalar)
{
var value = reader.ScalarValue;
reader.Read();
if (value == "ambient")
{
return new AmbientTemperatureProvider();
}
if (float.TryParse(value, out var f))
{
return new StaticCompiledNode<Temperature>(new Temperature(f));
}
if (double.TryParse(value, out var d))
{
return new StaticCompiledNode<Temperature>(new Temperature(f));
}
throw new NotImplementedException();
}
if (reader.TokenType == YamlTokenType.StartMapping)
{
reader.Read();
IValueProvider<Temperature>? provider = null;
while (reader.TokenType != YamlTokenType.EndMapping)
{
var key = reader.ScalarValue;
reader.Read();
if (key == nameof(Fahrenheit))
{
provider = new StaticCompiledNode<Temperature>(new Fahrenheit(float.Parse(reader.ScalarValue)));
}
if (key == nameof(Celsius))
{
provider = new StaticCompiledNode<Temperature>(new Celsius(float.Parse(reader.ScalarValue)));
}
if (key == nameof(Kelvin))
{
provider = new StaticCompiledNode<Temperature>(new Kelvin(float.Parse(reader.ScalarValue)));
}
reader.Read();
}
return provider??throw new NotSupportedException();
}
throw new NotImplementedException();
}
public override void Write(YamlWriter writer, IValueProvider<Temperature> value) => throw new NotImplementedException();
public sealed class AmbientTemperatureProvider : IValueProvider<Temperature>
{
public ProviderMode Mode => ProviderMode.Static;
public void Resolve(BlueprintContext context, ref Temperature value)
{
value.Kelvin = (float)context.PlaceHolderApi;
}
}
}
// public class ComponentBinderRegistry
// {
// private readonly Dictionary<ComponentType, IComponentBinder> _binder = [];
// public void Add<T>(ComponentBinder<T> componentBinder) where T : struct
// {
// _binder.Add(typeof(T), componentBinder);
// }
// public void Apply(Entity entity, World world, CompiledBlueprint itemData)
// {
// var s = world.GetSignature(entity);
// for (int i = 0; i < s.Count; i++)
// {
// if (!_binder.ContainsKey(s.Components[i]))
// {
// continue;
// }
// var binder = _binder[s.Components[i]];
// var type = s.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entity, itemData.Providers[type], context);
// }
// }
// public void Apply(Span<Entity> entities, World world, CompiledBlueprint itemData)
// {
// var signature = world.GetSignature(entities[0]);
// for (int i = 1; i < entities.Length; i++)
// {
// Debug.Assert(world.GetSignature(entities[i]) == signature);
// }
// for (int i = 0; i < signature.Count; i++)
// {
// var binder = _binder[signature.Components[i]];
// var type = signature.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entities, itemData.Providers[type], context);
// }
// }
// }
// public abstract class DataNode//(Dictionary<string, object> Data)
// {
// public abstract DataNodeType NodeType { get; }
// // public T Get<T>(string name) => (T)Data[name];
// }
// public enum DataNodeType
// {hthous stomatitis, pharyngitis, adenitis) is a
// Value,
// Sequence,
// Mapping,
// Component
// }
// public sealed class ValueNode(object value) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Value;
// public object Value { get; set; } = value;
// public Option<T> As<T>() => Value is T o?Option<T>.Some(o):Option<T>.None;
// }
// public sealed class SequenceNode(IReadOnlyList<DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Sequence;
// public IReadOnlyList<DataNode> Values { get; set; } = values;
// }
// public sealed class MappingNode(IReadOnlyDictionary<string, DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Mapping;
// public IReadOnlyDictionary<string, DataNode> Values { get; set; } = values;
// public bool TryGet<T>(string key, [NotNullWhen(true)] out T value) where T : DataNode
// {
// var result = Values.TryGetValue(key, out var node);
// value = (T)node!;
// return result;
// }
// public bool TryGet(string key, [NotNullWhen(true)] out DataNode node) => Values.TryGetValue(key, out node!);
// }
// public class DataNodeSerlicer : YamlConverter<DataNode>, IJsonFormatter<DataNode>
// {
// private Dictionary<string,string> _data = new();
// public DataNode Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
// {
// var i = 0;
// while (reader.ReadIsInObject(ref i))
// {
// var name = reader.ReadPropertyName();
// var value = reader.ReadString();
// _data.Add(name, value);
// }
// throw new NotImplementedException();
// }
// public void Serialize(ref JsonWriter writer, DataNode value, IJsonFormatterResolver formatterResolver) => throw new NotImplementedException();
// public override DataNode? Read(YamlReader reader)
// {
// // reader.pa
// throw new NotImplementedException();
// }
// public override void Write(YamlWriter writer, DataNode value)
// {
// throw new NotImplementedException();
// }
// }

View File

@@ -0,0 +1,131 @@
namespace FoodFactory.Blueprints.Providers.Converters;
using System;
using SharpYaml.Serialization;
public sealed class ComponentNameConverter : ItemComponentsConverter<Name>
{
public override IValueProvider<Name> Read(YamlReader reader)
{
if (reader.TokenType == YamlTokenType.Scalar)
{
var key = reader.GetScalarValue();
reader.Read();
return new StaticCompiledNode<Name>(new(key));
}
return base.Read(reader);
}
public override void Write(YamlWriter writer, IValueProvider<Name> value) => throw new NotImplementedException();
}
// public class ComponentBinderRegistry
// {
// private readonly Dictionary<ComponentType, IComponentBinder> _binder = [];
// public void Add<T>(ComponentBinder<T> componentBinder) where T : struct
// {
// _binder.Add(typeof(T), componentBinder);
// }
// public void Apply(Entity entity, World world, CompiledBlueprint itemData)
// {
// var s = world.GetSignature(entity);
// for (int i = 0; i < s.Count; i++)
// {
// if (!_binder.ContainsKey(s.Components[i]))
// {
// continue;
// }
// var binder = _binder[s.Components[i]];
// var type = s.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entity, itemData.Providers[type], context);
// }
// }
// public void Apply(Span<Entity> entities, World world, CompiledBlueprint itemData)
// {
// var signature = world.GetSignature(entities[0]);
// for (int i = 1; i < entities.Length; i++)
// {
// Debug.Assert(world.GetSignature(entities[i]) == signature);
// }
// for (int i = 0; i < signature.Count; i++)
// {
// var binder = _binder[signature.Components[i]];
// var type = signature.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entities, itemData.Providers[type], context);
// }
// }
// }
// public abstract class DataNode//(Dictionary<string, object> Data)
// {
// public abstract DataNodeType NodeType { get; }
// // public T Get<T>(string name) => (T)Data[name];
// }
// public enum DataNodeType
// {hthous stomatitis, pharyngitis, adenitis) is a
// Value,
// Sequence,
// Mapping,
// Component
// }
// public sealed class ValueNode(object value) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Value;
// public object Value { get; set; } = value;
// public Option<T> As<T>() => Value is T o?Option<T>.Some(o):Option<T>.None;
// }
// public sealed class SequenceNode(IReadOnlyList<DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Sequence;
// public IReadOnlyList<DataNode> Values { get; set; } = values;
// }
// public sealed class MappingNode(IReadOnlyDictionary<string, DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Mapping;
// public IReadOnlyDictionary<string, DataNode> Values { get; set; } = values;
// public bool TryGet<T>(string key, [NotNullWhen(true)] out T value) where T : DataNode
// {
// var result = Values.TryGetValue(key, out var node);
// value = (T)node!;
// return result;
// }
// public bool TryGet(string key, [NotNullWhen(true)] out DataNode node) => Values.TryGetValue(key, out node!);
// }
// public class DataNodeSerlicer : YamlConverter<DataNode>, IJsonFormatter<DataNode>
// {
// private Dictionary<string,string> _data = new();
// public DataNode Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
// {
// var i = 0;
// while (reader.ReadIsInObject(ref i))
// {
// var name = reader.ReadPropertyName();
// var value = reader.ReadString();
// _data.Add(name, value);
// }
// throw new NotImplementedException();
// }
// public void Serialize(ref JsonWriter writer, DataNode value, IJsonFormatterResolver formatterResolver) => throw new NotImplementedException();
// public override DataNode? Read(YamlReader reader)
// {
// // reader.pa
// throw new NotImplementedException();
// }
// public override void Write(YamlWriter writer, DataNode value)
// {
// throw new NotImplementedException();
// }
// }

View File

@@ -0,0 +1,133 @@
namespace FoodFactory.Blueprints.Providers.Converters;
using System;
using SharpYaml.Serialization;
public abstract class ItemComponentsConverter<T> : YamlConverter<IValueProvider<T>> where T : struct
{
public override IValueProvider<T> Read(YamlReader reader)
{
var converter = reader.GetConverter(typeof(T));
var value = converter.Read(reader, typeof(T));
if (value is not T t)
{
throw new Exception();
}
return new StaticCompiledNode<T>(t);
}
public override void Write(YamlWriter writer, IValueProvider<T> value) => throw new NotSupportedException();
}
// public class ComponentBinderRegistry
// {
// private readonly Dictionary<ComponentType, IComponentBinder> _binder = [];
// public void Add<T>(ComponentBinder<T> componentBinder) where T : struct
// {
// _binder.Add(typeof(T), componentBinder);
// }
// public void Apply(Entity entity, World world, CompiledBlueprint itemData)
// {
// var s = world.GetSignature(entity);
// for (int i = 0; i < s.Count; i++)
// {
// if (!_binder.ContainsKey(s.Components[i]))
// {
// continue;
// }
// var binder = _binder[s.Components[i]];
// var type = s.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entity, itemData.Providers[type], context);
// }
// }
// public void Apply(Span<Entity> entities, World world, CompiledBlueprint itemData)
// {
// var signature = world.GetSignature(entities[0]);
// for (int i = 1; i < entities.Length; i++)
// {
// Debug.Assert(world.GetSignature(entities[i]) == signature);
// }
// for (int i = 0; i < signature.Count; i++)
// {
// var binder = _binder[signature.Components[i]];
// var type = signature.Components[i].Type;
// var context = new BlueprintContext(){World = world};
// binder.ApplyUntyped(entities, itemData.Providers[type], context);
// }
// }
// }
// public abstract class DataNode//(Dictionary<string, object> Data)
// {
// public abstract DataNodeType NodeType { get; }
// // public T Get<T>(string name) => (T)Data[name];
// }
// public enum DataNodeType
// {hthous stomatitis, pharyngitis, adenitis) is a
// Value,
// Sequence,
// Mapping,
// Component
// }
// public sealed class ValueNode(object value) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Value;
// public object Value { get; set; } = value;
// public Option<T> As<T>() => Value is T o?Option<T>.Some(o):Option<T>.None;
// }
// public sealed class SequenceNode(IReadOnlyList<DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Sequence;
// public IReadOnlyList<DataNode> Values { get; set; } = values;
// }
// public sealed class MappingNode(IReadOnlyDictionary<string, DataNode> values) : DataNode
// {
// public override DataNodeType NodeType => DataNodeType.Mapping;
// public IReadOnlyDictionary<string, DataNode> Values { get; set; } = values;
// public bool TryGet<T>(string key, [NotNullWhen(true)] out T value) where T : DataNode
// {
// var result = Values.TryGetValue(key, out var node);
// value = (T)node!;
// return result;
// }
// public bool TryGet(string key, [NotNullWhen(true)] out DataNode node) => Values.TryGetValue(key, out node!);
// }
// public class DataNodeSerlicer : YamlConverter<DataNode>, IJsonFormatter<DataNode>
// {
// private Dictionary<string,string> _data = new();
// public DataNode Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
// {
// var i = 0;
// while (reader.ReadIsInObject(ref i))
// {
// var name = reader.ReadPropertyName();
// var value = reader.ReadString();
// _data.Add(name, value);
// }
// throw new NotImplementedException();
// }
// public void Serialize(ref JsonWriter writer, DataNode value, IJsonFormatterResolver formatterResolver) => throw new NotImplementedException();
// public override DataNode? Read(YamlReader reader)
// {
// // reader.pa
// throw new NotImplementedException();
// }
// public override void Write(YamlWriter writer, DataNode value)
// {
// throw new NotImplementedException();
// }
// }

View File

@@ -0,0 +1,21 @@
namespace FoodFactory.Blueprints.Providers;
using System;
using Arch.Core;
using Arch.Core.Extensions;
public sealed class DefaultValueProvider : IValueProvider
{
private readonly object _object;
public DefaultValueProvider(Type type, object @object)
{
Type = type;
_object = @object;
}
public ProviderMode Mode => ProviderMode.Static;
public Type Type { get; }
public Type GetProviderType() => Type;
public void Resolve(BlueprintContext context, Entity entity) => entity.Set(_object);
}

View File

@@ -0,0 +1,16 @@
namespace FoodFactory.Blueprints.Providers;
public sealed class FactoryCompiledNode<T> : IValueProvider<T>
{
public delegate void ResolveFunc(in BlueprintContext context, ref T value);
private readonly ResolveFunc _factory;
public ProviderMode Mode => ProviderMode.Modify;
public FactoryCompiledNode(ResolveFunc factory)
{
_factory = factory;
}
public void Resolve(BlueprintContext context, ref T value) => _factory(context, ref value);
}

View File

@@ -0,0 +1,22 @@
namespace FoodFactory.Blueprints.Providers;
using System;
using Arch.Core;
using Arch.Core.Extensions;
public interface IValueProvider
{
Type GetProviderType();
void Resolve(BlueprintContext context, Entity entity);
ProviderMode Mode { get; }
}
public interface IValueProvider<T> : IValueProvider
{
Type IValueProvider.GetProviderType() => typeof(T);
void Resolve(BlueprintContext context, ref T value);
void IValueProvider.Resolve(BlueprintContext context, Entity entity) => Resolve(context, ref entity.Get<T>());
}
public enum ProviderMode
{
Static,
Modify
}

View File

@@ -0,0 +1,14 @@
namespace FoodFactory.Blueprints.Providers;
public sealed class StaticCompiledNode<T> : IValueProvider<T>
{
public StaticCompiledNode(T value)
{
Value = value;
}
public T Value { get; }
public void Resolve(BlueprintContext context, ref T value) => value = Value;
public ProviderMode Mode => ProviderMode.Static;
}