From 1b7a8dc6c50052987021f5519cb0d3276ed1b470 Mon Sep 17 00:00:00 2001 From: Ullrich Praetz Date: Fri, 21 Jun 2024 16:32:22 +0200 Subject: [PATCH] Engine - Index: simplify - changed StoreIndex to static class --- Engine/src/ECS/Entity/Store/Extension.cs | 2 +- .../ECS/Lab/Component/IndexedComponentType.cs | 7 ++- Engine/src/ECS/Lab/Indexes/ComponentIndex.cs | 10 ++- Engine/src/ECS/Lab/StoreIndex.cs | 63 ------------------- Engine/src/ECS/Lab/Utils/StoreIndex.cs | 34 ++++++++++ Engine/src/Tests-internal/ECS/Test_Index.cs | 4 +- 6 files changed, 48 insertions(+), 72 deletions(-) delete mode 100644 Engine/src/ECS/Lab/StoreIndex.cs create mode 100644 Engine/src/ECS/Lab/Utils/StoreIndex.cs diff --git a/Engine/src/ECS/Entity/Store/Extension.cs b/Engine/src/ECS/Entity/Store/Extension.cs index 21ec768c8..422782c98 100644 --- a/Engine/src/ECS/Entity/Store/Extension.cs +++ b/Engine/src/ECS/Entity/Store/Extension.cs @@ -50,7 +50,7 @@ internal partial struct StoreExtension #endregion #region component indices - internal StoreIndex[] indexMap; // 8 - map and its component indexes created on demand + internal ComponentIndex[] indexMap; // 8 - map and its component indexes created on demand #endregion internal StoreExtension(PidType pidType) diff --git a/Engine/src/ECS/Lab/Component/IndexedComponentType.cs b/Engine/src/ECS/Lab/Component/IndexedComponentType.cs index 24d8c7f89..e1ec3e93c 100644 --- a/Engine/src/ECS/Lab/Component/IndexedComponentType.cs +++ b/Engine/src/ECS/Lab/Component/IndexedComponentType.cs @@ -22,9 +22,10 @@ private IndexedComponentType(ComponentType componentType, Type indexType) { [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2077", Justification = "TODO")] // TODO internal ComponentIndex CreateComponentIndex(EntityStore store) { - var obj = Activator.CreateInstance(indexType); - var index = (ComponentIndex)obj!; - index.store = store; + var obj = Activator.CreateInstance(indexType); + var index = (ComponentIndex)obj!; + index.store = store; + index.componentType = componentType; return index; } diff --git a/Engine/src/ECS/Lab/Indexes/ComponentIndex.cs b/Engine/src/ECS/Lab/Indexes/ComponentIndex.cs index baba8426e..516d0a51d 100644 --- a/Engine/src/ECS/Lab/Indexes/ComponentIndex.cs +++ b/Engine/src/ECS/Lab/Indexes/ComponentIndex.cs @@ -9,9 +9,11 @@ namespace Friflo.Engine.ECS.Index; internal abstract class ComponentIndex { - internal abstract int Count { get; } + internal abstract int Count { get; } + public override string ToString() => GetString(); - internal EntityStore store; // could be made readonly + internal EntityStore store; // could be made readonly + internal ComponentType componentType; // only for debugging internal abstract void Add (int id, in TComponent component) where TComponent : struct, IComponent; internal abstract void Update(int id, in TComponent component, StructHeap heap) where TComponent : struct, IComponent; @@ -20,6 +22,10 @@ internal abstract class ComponentIndex internal NotSupportedException NotSupportedException(string name) { return new NotSupportedException($"{name} not supported by {GetType().Name}"); } + + private string GetString() { + return $"{componentType.Name} - {GetType().Name} count: {Count}"; + } } internal abstract class ComponentIndex : ComponentIndex diff --git a/Engine/src/ECS/Lab/StoreIndex.cs b/Engine/src/ECS/Lab/StoreIndex.cs deleted file mode 100644 index 7ac372c52..000000000 --- a/Engine/src/ECS/Lab/StoreIndex.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved. -// See LICENSE file in the project root for full license information. - -// ReSharper disable once CheckNamespace -namespace Friflo.Engine.ECS.Index; - -internal struct StoreIndex -{ - public override string ToString() => GetString(); - - /// component index created on demand. - private ComponentIndex index; // 8 - - /// only stored for debugging - private readonly int structIndex; // 4 - - private StoreIndex(int structIndex) { - this.structIndex = structIndex; - } - - internal static ComponentIndex GetIndex(EntityStore store, int structIndex) - { - var indexMap = store.extension.indexMap; - if (indexMap != null) { - var index = indexMap[structIndex].index; - if (index != null) { - return index; - } - return indexMap[structIndex].index = CreateIndex(store, structIndex); - } - indexMap = store.extension.indexMap = CreateStoreIndexMap(); - return indexMap[structIndex].index = CreateIndex(store, structIndex); - } - - private static ComponentIndex CreateIndex(EntityStore store, int structIndex) - { - var type = EntityStoreBase.Static.EntitySchema.indexedComponentMap[structIndex]; - return type.CreateComponentIndex(store); - } - - private static StoreIndex[] CreateStoreIndexMap() - { - var schema = EntityStoreBase.Static.EntitySchema; - var storeIndexes = new StoreIndex[schema.maxStructIndex]; // could create smaller array containing no null elements - foreach (var type in schema.indexedComponents) { - storeIndexes[type.componentType.StructIndex] = new StoreIndex(type.componentType.StructIndex); - } - return storeIndexes; - } - - private string GetString() - { - var type = EntityStoreBase.Static.EntitySchema.indexedComponentMap[structIndex]; - if (type.componentType == null) { - return null; - } - var name = type.componentType.Name; - if (index == null) { - return name; - } - return $"{name} - {index.GetType().Name} count: {index.Count}"; - } -} diff --git a/Engine/src/ECS/Lab/Utils/StoreIndex.cs b/Engine/src/ECS/Lab/Utils/StoreIndex.cs new file mode 100644 index 000000000..e151841e3 --- /dev/null +++ b/Engine/src/ECS/Lab/Utils/StoreIndex.cs @@ -0,0 +1,34 @@ +// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved. +// See LICENSE file in the project root for full license information. + +// ReSharper disable once CheckNamespace +namespace Friflo.Engine.ECS.Index; + +internal static class StoreIndex +{ + internal static ComponentIndex GetIndex(EntityStore store, int structIndex) + { + var indexMap = store.extension.indexMap; + if (indexMap != null) { + var index = indexMap[structIndex]; + if (index != null) { + return index; + } + return indexMap[structIndex] = CreateIndex(store, structIndex); + } + indexMap = store.extension.indexMap = CreateStoreIndexMap(); + return indexMap[structIndex] = CreateIndex(store, structIndex); + } + + private static ComponentIndex CreateIndex(EntityStore store, int structIndex) + { + var type = EntityStoreBase.Static.EntitySchema.indexedComponentMap[structIndex]; + return type.CreateComponentIndex(store); + } + + private static ComponentIndex[] CreateStoreIndexMap() + { + var schema = EntityStoreBase.Static.EntitySchema; + return new ComponentIndex[schema.maxStructIndex]; // could create smaller array containing no null elements + } +} diff --git a/Engine/src/Tests-internal/ECS/Test_Index.cs b/Engine/src/Tests-internal/ECS/Test_Index.cs index 11a74b580..ce1427b2a 100644 --- a/Engine/src/Tests-internal/ECS/Test_Index.cs +++ b/Engine/src/Tests-internal/ECS/Test_Index.cs @@ -330,9 +330,7 @@ public static void Test_Index_StoreIndex_ToString() entity1.AddComponent(new IndexedName { name = "test" }); var indexMap = store.extension.indexMap; - AreEqual(null, indexMap[StructInfo.Index].ToString()); - - AreEqual("IndexedEntity", indexMap[StructInfo.Index].ToString()); + AreEqual(null, indexMap[StructInfo.Index]); AreEqual("IndexedName - ValueClassIndex`1 count: 1", indexMap[StructInfo.Index].ToString());