Simplify CreateComponent

wip/source-generators
copygirl 1 year ago
parent 477f7deb23
commit 3d9097b41b
  1. 26
      src/gaemstone/ECS/Component.cs

@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;
using gaemstone.Utility;
using static flecs_hub.flecs;
@ -23,25 +22,12 @@ public static class ComponentExtensions
var typeInfo = default(ecs_type_info_t);
if (type.IsPrimitive) throw new ArgumentException(
"Must not be primitive", nameof(type));
else if (type.IsEnum) {
var wrapper = TypeWrapper.For(type);
typeInfo.size = wrapper.Size;
typeInfo.alignment = wrapper.Size;
} else if (type.IsValueType) {
var wrapper = TypeWrapper.For(type);
if (!wrapper.IsUnmanaged) throw new Exception(
"Struct component must satisfy the unmanaged constraint. " +
"Consider making it a class if you need to store references.");
var structLayout = type.StructLayoutAttribute;
if (structLayout == null || structLayout.Value == LayoutKind.Auto) throw new Exception(
"Struct component must have a sequential or explicit StructLayout. " +
"This is to ensure that the struct fields are not reorganized.");
typeInfo.size = wrapper.Size;
typeInfo.alignment = structLayout.Pack;
} else {
typeInfo.size = sizeof(nint);
typeInfo.alignment = sizeof(nint);
}
var wrapper = TypeWrapper.For(type);
if (type.IsValueType && !wrapper.IsUnmanaged) throw new Exception(
"Struct component must satisfy the unmanaged constraint. " +
"Consider making it a class if you need to store references.");
typeInfo.size = wrapper.Size;
typeInfo.alignment = wrapper.Size;
var desc = new ecs_component_desc_t { entity = entity, type = typeInfo };
ecs_component_init(entity.Universe, &desc);

Loading…
Cancel
Save