using System; using System.Runtime.CompilerServices; using static flecs_hub.flecs; namespace gaemstone.ECS; public static class ComponentExtensions { public unsafe static EntityRef InitComponent(this EntityRef entity, Type type) => (EntityRef)typeof(ComponentExtensions) .GetMethod(nameof(InitComponent), new[] { typeof(EntityRef) })! .MakeGenericMethod(type).Invoke(null, new[]{ entity })!; public unsafe static EntityRef InitComponent(this EntityRef entity) { if (typeof(T).IsPrimitive) throw new ArgumentException( "Must not be primitive"); if (typeof(T).IsValueType && RuntimeHelpers.IsReferenceOrContainsReferences()) throw new ArgumentException( "Struct component must satisfy the unmanaged constraint. " + "Consider making it a class if you need to store references."); var size = Unsafe.SizeOf(); var typeInfo = new ecs_type_info_t { size = size, alignment = size }; var componentDesc = new ecs_component_desc_t { entity = entity, type = typeInfo }; ecs_component_init(entity.World, &componentDesc); return entity.CreateLookup(typeof(T)); } }