Rename Identifier to Id

wip/bindgen
copygirl 1 year ago
parent 26b9ad3a6a
commit 230d1abfe3
  1. 2
      src/gaemstone.ECS/Entity.cs
  2. 30
      src/gaemstone.ECS/EntityBase.cs
  3. 28
      src/gaemstone.ECS/EntityBuilder.cs
  4. 2
      src/gaemstone.ECS/EntityPath.cs
  5. 26
      src/gaemstone.ECS/EntityRef.cs
  6. 6
      src/gaemstone.ECS/EntityType.cs
  7. 30
      src/gaemstone.ECS/Id.cs
  8. 55
      src/gaemstone.ECS/IdRef.cs
  9. 55
      src/gaemstone.ECS/IdentifierRef.cs
  10. 4
      src/gaemstone.ECS/Iterator.cs
  11. 10
      src/gaemstone.ECS/Term.cs

@ -27,6 +27,6 @@ public readonly struct Entity
public static bool operator !=(Entity left, Entity right) => !left.Equals(right);
public static implicit operator ecs_entity_t(Entity e) => e.Value;
public static implicit operator Identifier(Entity e) => new(e.Value.Data);
public static implicit operator Id(Entity e) => new(e.Value.Data);
public static implicit operator ecs_id_t(Entity e) => e.Value.Data;
}

@ -5,36 +5,36 @@ public abstract class EntityBase<TReturn>
public abstract World World { get; }
public abstract TReturn Add(Identifier id);
public abstract TReturn Remove(Identifier id);
public abstract bool Has(Identifier id);
public abstract TReturn Add(Id id);
public abstract TReturn Remove(Id id);
public abstract bool Has(Id id);
public TReturn Add(string symbol) => Add(World.LookupBySymbolOrThrow(symbol));
public TReturn Add<T>() => Add(World.LookupByTypeOrThrow(typeof(T)));
public TReturn Add(Entity relation, Entity target) => Add(Identifier.Pair(relation, target));
public TReturn Add(Entity relation, Entity target) => Add(Id.Pair(relation, target));
public TReturn Add<TRelation>(Entity target) => Add(World.LookupByTypeOrThrow<TRelation>(), target);
public TReturn Add<TRelation, TTarget>() => Add(World.LookupByTypeOrThrow<TRelation>(), World.LookupByTypeOrThrow<TTarget>());
public TReturn Remove(string symbol) => Remove(World.LookupBySymbolOrThrow(symbol));
public TReturn Remove<T>() => Remove(World.LookupByTypeOrThrow(typeof(T)));
public TReturn Remove(Entity relation, Entity target) => Remove(Identifier.Pair(relation, target));
public TReturn Remove(Entity relation, Entity target) => Remove(Id.Pair(relation, target));
public TReturn Remove<TRelation>(Entity target) => Remove(World.LookupByTypeOrThrow<TRelation>(), target);
public TReturn Remove<TRelation, TTarget>() => Remove(World.LookupByTypeOrThrow<TRelation>(), World.LookupByTypeOrThrow<TTarget>());
public bool Has(string symbol) => Has(World.LookupBySymbolOrThrow(symbol));
public bool Has<T>() => Has(World.LookupByTypeOrThrow(typeof(T)));
public bool Has(Entity relation, Entity target) => Has(Identifier.Pair(relation, target));
public bool Has(Entity relation, Entity target) => Has(Id.Pair(relation, target));
public bool Has<TRelation>(Entity target) => Has(World.LookupByTypeOrThrow<TRelation>(), target);
public bool Has<TRelation, TTarget>() => Has(World.LookupByTypeOrThrow<TRelation>(), World.LookupByTypeOrThrow<TTarget>());
public abstract T? GetOrNull<T>(Identifier id) where T : unmanaged;
public abstract T? GetOrNull<T>(Identifier id, T _ = null!) where T : class;
public abstract T GetOrThrow<T>(Identifier id);
public abstract ref T GetMut<T>(Identifier id) where T : unmanaged;
public abstract ref T GetRefOrNull<T>(Identifier id) where T : unmanaged;
public abstract ref T GetRefOrThrow<T>(Identifier id) where T : unmanaged;
public abstract void Modified<T>(Identifier id);
public abstract T? GetOrNull<T>(Id id) where T : unmanaged;
public abstract T? GetOrNull<T>(Id id, T _ = null!) where T : class;
public abstract T GetOrThrow<T>(Id id);
public abstract ref T GetMut<T>(Id id) where T : unmanaged;
public abstract ref T GetRefOrNull<T>(Id id) where T : unmanaged;
public abstract ref T GetRefOrThrow<T>(Id id) where T : unmanaged;
public abstract void Modified<T>(Id id);
public T? GetOrNull<T>() where T : unmanaged => GetOrNull<T>(World.LookupByTypeOrThrow<T>());
public T? GetOrNull<T>(T _ = null!) where T : class => GetOrNull<T>(World.LookupByTypeOrThrow<T>());
@ -45,8 +45,8 @@ public abstract class EntityBase<TReturn>
public void Modified<T>() => Modified<T>(World.LookupByTypeOrThrow<T>());
public abstract TReturn Set<T>(Identifier id, in T value) where T : unmanaged;
public abstract TReturn Set<T>(Identifier id, T obj) where T : class;
public abstract TReturn Set<T>(Id id, in T value) where T : unmanaged;
public abstract TReturn Set<T>(Id id, T obj) where T : class;
public TReturn Set<T>(in T value) where T : unmanaged => Set(World.LookupByTypeOrThrow<T>(), value);
public TReturn Set<T>(T obj) where T : class => Set(World.LookupByTypeOrThrow<T>(), obj);

@ -37,7 +37,7 @@ public class EntityBuilder
public bool UseLowId { get; set; }
/// <summary> Ids to add to the new or existing entity. </summary>
private readonly HashSet<Identifier> _toAdd = new();
private readonly HashSet<Id> _toAdd = new();
private Entity _parent = Entity.None;
/// <summary> String expression with components to add. </summary>
@ -49,7 +49,7 @@ public class EntityBuilder
public EntityBuilder(World world, EntityPath? path = null)
{ World = world; Path = path; }
public override EntityBuilder Add(Identifier id)
public override EntityBuilder Add(Id id)
{
// If adding a ChildOf relation, store the parent separately.
if (id.AsPair(World) is (EntityRef relation, EntityRef target) &&
@ -61,25 +61,25 @@ public class EntityBuilder
return this;
}
public override EntityBuilder Remove(Identifier id)
public override EntityBuilder Remove(Id id)
=> throw new NotSupportedException();
public override bool Has(Identifier id)
public override bool Has(Id id)
=> !id.IsWildcard ? _toAdd.Contains(id)
: throw new NotSupportedException(); // TODO: Support wildcard.
public override T? GetOrNull<T>(Identifier id) => throw new NotSupportedException();
public override T? GetOrNull<T>(Identifier id, T _ = null!) where T : class => throw new NotSupportedException();
public override T GetOrThrow<T>(Identifier id) => throw new NotSupportedException();
public override ref T GetMut<T>(Identifier id) => throw new NotSupportedException();
public override ref T GetRefOrNull<T>(Identifier id) => throw new NotSupportedException();
public override ref T GetRefOrThrow<T>(Identifier id) => throw new NotSupportedException();
public override void Modified<T>(Identifier id) => throw new NotImplementedException();
public override T? GetOrNull<T>(Id id) => throw new NotSupportedException();
public override T? GetOrNull<T>(Id id, T _ = null!) where T : class => throw new NotSupportedException();
public override T GetOrThrow<T>(Id id) => throw new NotSupportedException();
public override ref T GetMut<T>(Id id) => throw new NotSupportedException();
public override ref T GetRefOrNull<T>(Id id) => throw new NotSupportedException();
public override ref T GetRefOrThrow<T>(Id id) => throw new NotSupportedException();
public override void Modified<T>(Id id) => throw new NotImplementedException();
public override EntityBuilder Set<T>(Identifier id, in T value)
public override EntityBuilder Set<T>(Id id, in T value)
// "in" can't be used with lambdas, so we make a local copy.
{ var copy = value; _toSet.Add(e => e.Set(id, copy)); return this; }
public override EntityBuilder Set<T>(Identifier id, T obj)
public override EntityBuilder Set<T>(Id id, T obj)
{ _toSet.Add(e => e.Set(id, obj)); return this; }
public unsafe EntityRef Build()
@ -104,7 +104,7 @@ public class EntityBuilder
};
var add = desc.add; var index = 0;
if (parent.IsSome) add[index++] = Identifier.Pair(World.ChildOf, parent);
if (parent.IsSome) add[index++] = ECS.Id.Pair(World.ChildOf, parent);
foreach (var id in _toAdd) add[index++] = id;
var entityId = ecs_entity_init(World, &desc);

@ -170,7 +170,7 @@ public class EntityPath
fixed (byte* ptr = part.AsSpan())
if (skipLookup || (parent = new(ecs_lookup_child(world, parent, ptr))).IsNone) {
var desc = new ecs_entity_desc_t { name = ptr, sep = CStringExtensions.ETX };
if (parent.IsSome) desc.add[0] = Identifier.Pair(world.ChildOf, parent);
if (parent.IsSome) desc.add[0] = Id.Pair(world.ChildOf, parent);
parent = new(ecs_entity_init(world, &desc));
skipLookup = true;
}

@ -74,24 +74,24 @@ public unsafe class EntityRef
}
public override EntityRef Add(Identifier id) { ecs_add_id(World, this, id); return this; }
public override EntityRef Remove(Identifier id) { ecs_remove_id(World, this, id); return this; }
public override bool Has(Identifier id) => ecs_has_id(World, this, id);
public override EntityRef Add(Id id) { ecs_add_id(World, this, id); return this; }
public override EntityRef Remove(Id id) { ecs_remove_id(World, this, id); return this; }
public override bool Has(Id id) => ecs_has_id(World, this, id);
public override T? GetOrNull<T>(Identifier id)
public override T? GetOrNull<T>(Id id)
{
var ptr = ecs_get_id(World, this, id);
return (ptr != null) ? Unsafe.Read<T>(ptr) : null;
}
public override T? GetOrNull<T>(Identifier id, T _ = null!)
public override T? GetOrNull<T>(Id id, T _ = null!)
where T : class
{
var ptr = ecs_get_id(World, this, id);
return (ptr != null) ? (T)((GCHandle)Unsafe.Read<nint>(ptr)).Target! : null;
}
public override T GetOrThrow<T>(Identifier id)
public override T GetOrThrow<T>(Id id)
{
var ptr = ecs_get_id(World, this, id);
if (ptr == null) throw new Exception($"Component {typeof(T)} not found on {this}");
@ -99,14 +99,14 @@ public unsafe class EntityRef
: (T)((GCHandle)Unsafe.Read<nint>(ptr)).Target!;
}
public override ref T GetRefOrNull<T>(Identifier id)
public override ref T GetRefOrNull<T>(Id id)
{
var @ref = ecs_ref_init_id(World, this, id);
var ptr = ecs_ref_get_id(World, &@ref, id);
return ref (ptr != null) ? ref Unsafe.AsRef<T>(ptr) : ref Unsafe.NullRef<T>();
}
public override ref T GetRefOrThrow<T>(Identifier id)
public override ref T GetRefOrThrow<T>(Id id)
{
ref var ptr = ref GetRefOrNull<T>(id);
if (Unsafe.IsNullRef(ref ptr)) throw new Exception(
@ -114,17 +114,17 @@ public unsafe class EntityRef
return ref ptr;
}
public override ref T GetMut<T>(Identifier id)
public override ref T GetMut<T>(Id id)
{
var ptr = ecs_get_mut_id(World, this, id);
// NOTE: Value is added if it doesn't exist on the entity.
return ref Unsafe.AsRef<T>(ptr);
}
public override void Modified<T>(Identifier id)
public override void Modified<T>(Id id)
=> ecs_modified_id(World, this, id);
public override EntityRef Set<T>(Identifier id, in T value)
public override EntityRef Set<T>(Id id, in T value)
{
var size = (ulong)Unsafe.SizeOf<T>();
fixed (T* ptr = &value)
@ -133,7 +133,7 @@ public unsafe class EntityRef
return this;
}
public override EntityRef Set<T>(Identifier id, T obj) where T : class
public override EntityRef Set<T>(Id id, T obj) where T : class
{
var handle = (nint)GCHandle.Alloc(obj);
// FIXME: Previous handle needs to be freed.
@ -163,6 +163,6 @@ public unsafe class EntityRef
public static implicit operator Entity(EntityRef? e) => e?.Entity ?? default;
public static implicit operator ecs_entity_t(EntityRef? e) => e?.Entity.Value ?? default;
public static implicit operator Identifier(EntityRef? e) => new(e?.Entity.Value.Data ?? default);
public static implicit operator Id(EntityRef? e) => new(e?.Entity.Value.Data ?? default);
public static implicit operator ecs_id_t(EntityRef? e) => e?.Entity.Value.Data ?? default;
}

@ -6,7 +6,7 @@ using static flecs_hub.flecs;
namespace gaemstone.ECS;
public unsafe readonly struct EntityType
: IReadOnlyList<IdentifierRef>
: IReadOnlyList<IdRef>
{
public World World { get; }
public ecs_type_t* Handle { get; }
@ -19,7 +19,7 @@ public unsafe readonly struct EntityType
// IReadOnlyList implementation
public int Count => Handle->count;
public IdentifierRef this[int index] => new(World, new(Handle->array[index]));
public IEnumerator<IdentifierRef> GetEnumerator() { for (var i = 0; i < Count; i++) yield return this[i]; }
public IdRef this[int index] => new(World, new(Handle->array[index]));
public IEnumerator<IdRef> GetEnumerator() { for (var i = 0; i < Count; i++) yield return this[i]; }
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

@ -3,49 +3,49 @@ using static flecs_hub.flecs;
namespace gaemstone.ECS;
public readonly struct Identifier
: IEquatable<Identifier>
public readonly struct Id
: IEquatable<Id>
{
public readonly ecs_id_t Value;
public bool IsPair => ecs_id_is_pair(this);
public bool IsWildcard => ecs_id_is_wildcard(this);
public IdentifierFlags Flags => (IdentifierFlags)(Value & ECS_ID_FLAGS_MASK);
public IdFlags Flags => (IdFlags)(Value & ECS_ID_FLAGS_MASK);
public Entity RelationUnsafe => new(new() { Data = (Value & ECS_COMPONENT_MASK) >> 32 });
public Entity TargetUnsafe => new(new() { Data = Value & ECS_ENTITY_MASK });
public Identifier(ecs_id_t value) => Value = value;
public Id(ecs_id_t value) => Value = value;
public static Identifier Combine(IdentifierFlags flags, Identifier id)
public static Id Combine(IdFlags flags, Id id)
=> new((ulong)flags | id.Value);
public static Identifier Pair(Entity relation, Entity target)
=> Combine(IdentifierFlags.Pair, new(
public static Id Pair(Entity relation, Entity target)
=> Combine(IdFlags.Pair, new(
((relation.Value.Data << 32) & ECS_COMPONENT_MASK) |
( target.Value.Data & ECS_ENTITY_MASK )));
public EntityRef? AsEntity(World world)
=> new IdentifierRef(world, this).AsEntity();
=> new IdRef(world, this).AsEntity();
public (EntityRef Relation, EntityRef Target)? AsPair(World world)
=> new IdentifierRef(world, this).AsPair();
=> new IdRef(world, this).AsPair();
public bool Equals(Identifier other) => Value.Data == other.Value.Data;
public override bool Equals(object? obj) => (obj is Identifier other) && Equals(other);
public bool Equals(Id other) => Value.Data == other.Value.Data;
public override bool Equals(object? obj) => (obj is Id other) && Equals(other);
public override int GetHashCode() => Value.Data.GetHashCode();
public override string? ToString()
=> (Flags != default) ? $"Identifier(0x{Value.Data:X}, Flags={Flags})"
: $"Identifier(0x{Value.Data:X})";
public static bool operator ==(Identifier left, Identifier right) => left.Equals(right);
public static bool operator !=(Identifier left, Identifier right) => !left.Equals(right);
public static bool operator ==(Id left, Id right) => left.Equals(right);
public static bool operator !=(Id left, Id right) => !left.Equals(right);
public static implicit operator ecs_id_t(Identifier i) => i.Value;
public static implicit operator ecs_id_t(Id i) => i.Value;
}
[Flags]
public enum IdentifierFlags : ulong
public enum IdFlags : ulong
{
Pair = 1ul << 63,
Override = 1ul << 62,

@ -0,0 +1,55 @@
using System;
using gaemstone.Utility;
using static flecs_hub.flecs;
namespace gaemstone.ECS;
public unsafe class IdRef
: IEquatable<IdRef>
{
public World World { get; }
public Id Id { get; }
public IdFlags Flags => Id.Flags;
public bool IsPair => Id.IsPair;
public bool IsWildcard => Id.IsWildcard;
public bool IsValid => ecs_id_is_valid(World, this);
public bool IsInUse => ecs_id_in_use(World, this);
public int Count => ecs_count_id(World, this);
public IdRef(World world, Id id)
{ World = world; Id = id; }
public static IdRef Combine(IdFlags flags, IdRef id)
=> new(id.World, Id.Combine(flags, id));
public static IdRef Pair(World world, Entity relation, Entity target)
=> new(world, Id.Pair(relation, target));
public static IdRef Pair(EntityRef relation, EntityRef target)
=> Pair(relation.World, relation, target);
public static IdRef Pair(EntityRef relation, Entity target)
=> Pair(relation.World, relation, target);
public static IdRef Pair(Entity relation, EntityRef target)
=> Pair(target.World, relation, target);
public static IdRef Pair<TRelation>(EntityRef target)
=> Pair(target.World.LookupByTypeOrThrow<TRelation>(), target);
public EntityRef? AsEntity()
=> (Flags == default) ? World.LookupAlive(new Entity(new() { Data = Id })) : null;
public (EntityRef Relation, EntityRef Target)? AsPair()
=> IsPair && (World.LookupAlive(Id.RelationUnsafe) is EntityRef relation) &&
(World.LookupAlive(Id.TargetUnsafe ) is EntityRef target )
? (relation, target) : null;
public bool Equals(IdRef? other) => (other is not null) && (World == other.World) && (Id == other.Id);
public override bool Equals(object? obj) => Equals(obj as IdRef);
public override int GetHashCode() => HashCode.Combine(World, Id);
public override string? ToString() => ecs_id_str(World, this).FlecsToStringAndFree()!;
public static bool operator ==(IdRef? left, IdRef? right) => ReferenceEquals(left, right) || (left?.Equals(right) ?? false);
public static bool operator !=(IdRef? left, IdRef? right) => !(left == right);
public static implicit operator Id(IdRef i) => i.Id;
public static implicit operator ecs_id_t(IdRef i) => i.Id.Value;
}

@ -1,55 +0,0 @@
using System;
using gaemstone.Utility;
using static flecs_hub.flecs;
namespace gaemstone.ECS;
public unsafe class IdentifierRef
: IEquatable<IdentifierRef>
{
public World World { get; }
public Identifier Id { get; }
public IdentifierFlags Flags => Id.Flags;
public bool IsPair => Id.IsPair;
public bool IsWildcard => Id.IsWildcard;
public bool IsValid => ecs_id_is_valid(World, this);
public bool IsInUse => ecs_id_in_use(World, this);
public int Count => ecs_count_id(World, this);
public IdentifierRef(World world, Identifier id)
{ World = world; Id = id; }
public static IdentifierRef Combine(IdentifierFlags flags, IdentifierRef id)
=> new(id.World, Identifier.Combine(flags, id));
public static IdentifierRef Pair(World world, Entity relation, Entity target)
=> new(world, Identifier.Pair(relation, target));
public static IdentifierRef Pair(EntityRef relation, EntityRef target)
=> Pair(relation.World, relation, target);
public static IdentifierRef Pair(EntityRef relation, Entity target)
=> Pair(relation.World, relation, target);
public static IdentifierRef Pair(Entity relation, EntityRef target)
=> Pair(target.World, relation, target);
public static IdentifierRef Pair<TRelation>(EntityRef target)
=> Pair(target.World.LookupByTypeOrThrow<TRelation>(), target);
public EntityRef? AsEntity()
=> (Flags == default) ? World.LookupAlive(new Entity(new() { Data = Id })) : null;
public (EntityRef Relation, EntityRef Target)? AsPair()
=> IsPair && (World.LookupAlive(Id.RelationUnsafe) is EntityRef relation) &&
(World.LookupAlive(Id.TargetUnsafe ) is EntityRef target )
? (relation, target) : null;
public bool Equals(IdentifierRef? other) => (other is not null) && (World == other.World) && (Id == other.Id);
public override bool Equals(object? obj) => Equals(obj as IdentifierRef);
public override int GetHashCode() => HashCode.Combine(World, Id);
public override string? ToString() => ecs_id_str(World, this).FlecsToStringAndFree()!;
public static bool operator ==(IdentifierRef? left, IdentifierRef? right) => ReferenceEquals(left, right) || (left?.Equals(right) ?? false);
public static bool operator !=(IdentifierRef? left, IdentifierRef? right) => !(left == right);
public static implicit operator Identifier(IdentifierRef i) => i.Id;
public static implicit operator ecs_id_t(IdentifierRef i) => i.Id.Value;
}

@ -71,7 +71,7 @@ public unsafe class Iterator
public EntityRef Entity(int index)
=> new(World, new(Value.entities[index]));
public IdentifierRef FieldId(int index)
public IdRef FieldId(int index)
{
fixed (ecs_iter_t* ptr = &Value)
return new(World, new(ecs_field_id(ptr, index)));
@ -104,7 +104,7 @@ public unsafe class Iterator
// The id might be "(Identifier, Name)", but its data type "Identifier".
public bool FieldIs<T>(int index)
=> FieldIs(index, World.LookupByType<T>());
public bool FieldIs(int index, Identifier id)
public bool FieldIs(int index, Id id)
{
fixed (ecs_iter_t* ptr = &Value)
return ecs_field_id(ptr, index) == id.Value;

@ -6,23 +6,23 @@ namespace gaemstone.ECS;
public class Term
{
public Identifier Id { get; set; }
public Id Id { get; set; }
public TermId? Source { get; set; }
public TermId? Relation { get; set; }
public TermId? Target { get; set; }
public TermInOutKind InOut { get; set; }
public TermOperKind Oper { get; set; }
public IdentifierFlags Flags { get; set; }
public IdFlags Flags { get; set; }
public Term() { }
public Term(Identifier id) => Id = id;
public Term(Id id) => Id = id;
public Term(TermId relation, TermId target)
{ Relation = relation; Target = target; }
public static implicit operator Term(EntityRef entity) => new(entity);
public static implicit operator Term(Entity entity) => new(entity);
public static implicit operator Term(IdentifierRef id) => new(id);
public static implicit operator Term(Identifier id) => new(id);
public static implicit operator Term(IdRef id) => new(id);
public static implicit operator Term(Id id) => new(id);
public Term None { get { InOut = TermInOutKind.None; return this; } }
public Term In { get { InOut = TermInOutKind.In; return this; } }

Loading…
Cancel
Save