diff --git a/src/Rev2/Compatibility/DebugSystem.cs b/src/Rev2/Compatibility/DebugSystem.cs index 8a477fe..6cd1690 100644 --- a/src/Rev2/Compatibility/DebugSystem.cs +++ b/src/Rev2/Compatibility/DebugSystem.cs @@ -10,7 +10,7 @@ public abstract class DebugSystem : System { protected DebugSystem(World world) : base(world) { } - protected World.ComponentEnumerator Debug_GetAllComponents(EntityId entity) => World.Debug_GetAllComponents(entity); + protected World.ComponentTypeEnumerator Debug_GetAllComponentTypes(EntityId entity) => World.Debug_GetAllComponentTypes(entity); protected Filter.EntityEnumerator Debug_GetEntities(Type componentType) => World.Debug_GetEntities(componentType); protected IEnumerable Debug_SearchComponentType(string typeString) => World.Debug_SearchComponentType(typeString); } diff --git a/src/Rev2/World.cs b/src/Rev2/World.cs index e3fbdb1..d6f00d9 100644 --- a/src/Rev2/World.cs +++ b/src/Rev2/World.cs @@ -3,10 +3,6 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using MoonTools.ECS.Collections; -#if DEBUG -using System.Reflection; -#endif - namespace MoonTools.ECS.Rev2; public class World : IDisposable @@ -122,39 +118,35 @@ public class World : IDisposable return typeId; } - private void TryRegisterComponentId() where T : unmanaged + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private TypeId GetComponentId() where T : unmanaged { var typeId = GetTypeId(); if (!SingleTypeFilters.ContainsKey(typeId)) { SingleTypeFilters.Add(typeId, FilterBuilder.Include().Build()); } + + return typeId; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private TypeId GetComponentId() where T : unmanaged + private RelationStorage RegisterRelationType(TypeId typeId) { - return TypeToId[typeof(T)]; - } - - private void RegisterRelationType(TypeId typeId) - { - RelationIndex.Add(typeId, new RelationStorage(ElementSizes[typeId])); - } - - private void TryRegisterRelationType() where T : unmanaged - { - var typeId = GetTypeId(); - if (!RelationIndex.ContainsKey(typeId)) - { - RegisterRelationType(typeId); - } + var relationStorage = new RelationStorage(ElementSizes[typeId]); + RelationIndex.Add(typeId, relationStorage); + return relationStorage; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private RelationStorage GetRelationStorage() where T : unmanaged { - return RelationIndex[TypeToId[typeof(T)]]; + var typeId = GetTypeId(); + if (RelationIndex.TryGetValue(typeId, out var relationStorage)) + { + return relationStorage; + } + + return RegisterRelationType(typeId); } // Messages @@ -262,7 +254,6 @@ public class World : IDisposable public unsafe void Set(in EntityId entityId, in T component) where T : unmanaged { - TryRegisterComponentId(); var componentId = GetComponentId(); if (Has(entityId)) @@ -322,6 +313,11 @@ public class World : IDisposable { Archetype? nextArchetype; + if (!Has(entityId)) + { + return; + } + var componentId = GetComponentId(); var (archetype, row) = EntityIndex[entityId]; @@ -352,7 +348,6 @@ public class World : IDisposable public void Relate(in EntityId entityA, in EntityId entityB, in T relation) where T : unmanaged { - TryRegisterRelationType(); var relationStorage = GetRelationStorage(); relationStorage.Set(entityA, entityB, relation); EntityRelationIndex[entityA].Add(TypeToId[typeof(T)]); @@ -600,9 +595,9 @@ public class World : IDisposable // NOTE: these methods are very inefficient // they should only be used in debugging contexts!! #if DEBUG - public ComponentEnumerator Debug_GetAllComponents(EntityId entity) + public ComponentTypeEnumerator Debug_GetAllComponentTypes(EntityId entity) { - return new ComponentEnumerator(this, EntityIndex[entity]); + return new ComponentTypeEnumerator(this, EntityIndex[entity]); } public Filter.EntityEnumerator Debug_GetEntities(Type componentType) @@ -622,15 +617,15 @@ public class World : IDisposable } } - public ref struct ComponentEnumerator + public ref struct ComponentTypeEnumerator { private World World; private Record Record; private int ComponentIndex; - public ComponentEnumerator GetEnumerator() => this; + public ComponentTypeEnumerator GetEnumerator() => this; - internal ComponentEnumerator( + internal ComponentTypeEnumerator( World world, Record record ) @@ -646,14 +641,7 @@ public class World : IDisposable return ComponentIndex < Record.Archetype.ComponentColumns.Length; } - public unsafe object Current - { - get - { - var elt = Record.Archetype.ComponentColumns[ComponentIndex].Get(Record.Row); - return Pointer.Box(elt, World.IdToType[Record.Archetype.Signature[ComponentIndex]]); - } - } + public unsafe Type Current => World.IdToType[Record.Archetype.Signature[ComponentIndex]]; } #endif