From 193e0f99a01da5587a5e54da810d37e3fc26cd53 Mon Sep 17 00:00:00 2001 From: Sebastiano Mandala Date: Sat, 1 Apr 2023 19:23:47 +0100 Subject: [PATCH] add IReactOnDisposeEx interface --- com.sebaslab.svelto.common | 2 +- .../Core/EnginesRoot.Engines.cs | 10 +++- com.sebaslab.svelto.ecs/Core/IEngine.cs | 10 ++++ .../DataStructures/ITypeSafeDictionary.cs | 7 +-- .../ManagedTypeSafeDictionary.cs | 14 +++--- .../TypeSafeDictionaryMethods.cs | 50 +++++++++++++++---- .../UnmanagedTypeSafeDictionary.cs | 11 ++-- 7 files changed, 78 insertions(+), 26 deletions(-) diff --git a/com.sebaslab.svelto.common b/com.sebaslab.svelto.common index 9a74e5c..114ce56 160000 --- a/com.sebaslab.svelto.common +++ b/com.sebaslab.svelto.common @@ -1 +1 @@ -Subproject commit 9a74e5c70681d4f2f5156b48cdd578dfcf6cfc4e +Subproject commit 114ce56f5efe5b5a2a8326ef55587ca668e72503 diff --git a/com.sebaslab.svelto.ecs/Core/EnginesRoot.Engines.cs b/com.sebaslab.svelto.ecs/Core/EnginesRoot.Engines.cs index 18e7fc3..e196789 100644 --- a/com.sebaslab.svelto.ecs/Core/EnginesRoot.Engines.cs +++ b/com.sebaslab.svelto.ecs/Core/EnginesRoot.Engines.cs @@ -64,6 +64,8 @@ namespace Svelto.ECS new FasterDictionary>>(); _reactiveEnginesDispose = new FasterDictionary>>(); + _reactiveEnginesDisposeEx = + new FasterDictionary>>(); _reactiveEnginesSubmission = new FasterList(); _reactiveEnginesSubmissionStarted = new FasterList(); @@ -151,6 +153,10 @@ namespace Svelto.ECS if (engine is IReactOnDispose viewEngineDispose) CheckReactEngineComponents( typeof(IReactOnDispose<>), viewEngineDispose, _reactiveEnginesDispose, type.Name); + + if (engine is IReactOnDisposeEx viewEngineDisposeEx) + CheckReactEngineComponents( + typeof(IReactOnDisposeEx<>), viewEngineDisposeEx, _reactiveEnginesDisposeEx, type.Name); if (engine is IReactOnSwap viewEngineSwap) #pragma warning disable CS0612 @@ -263,7 +269,7 @@ namespace Svelto.ECS ITypeSafeDictionary typeSafeDictionary = entityList.value; typeSafeDictionary.ExecuteEnginesDisposeCallbacks_Group( - _reactiveEnginesDispose, groups.key, + _reactiveEnginesDispose, _reactiveEnginesDisposeEx, groups.key, profiler); } catch (Exception e) @@ -300,6 +306,7 @@ namespace Svelto.ECS _reactiveEnginesAdd.Clear(); _reactiveEnginesRemove.Clear(); _reactiveEnginesDispose.Clear(); + _reactiveEnginesDisposeEx.Clear(); _reactiveEnginesSubmission.Clear(); _reactiveEnginesSubmissionStarted.Clear(); @@ -410,6 +417,7 @@ namespace Svelto.ECS readonly FasterDictionary>> _reactiveEnginesSwap; readonly FasterDictionary>> _reactiveEnginesSwapEx; readonly FasterDictionary>> _reactiveEnginesDispose; + readonly FasterDictionary>> _reactiveEnginesDisposeEx; readonly FasterList _reactiveEnginesSubmission; readonly FasterList _reactiveEnginesSubmissionStarted; diff --git a/com.sebaslab.svelto.ecs/Core/IEngine.cs b/com.sebaslab.svelto.ecs/Core/IEngine.cs index 271e26c..c622fc1 100644 --- a/com.sebaslab.svelto.ecs/Core/IEngine.cs +++ b/com.sebaslab.svelto.ecs/Core/IEngine.cs @@ -40,6 +40,10 @@ namespace Svelto.ECS.Internal public interface IReactOnSwapEx : IReactEngine { } + + public interface IReactOnDisposeEx : IReactEngine + { + } public interface IReactOnDispose : IReactEngine { @@ -132,6 +136,12 @@ namespace Svelto.ECS void MovedTo((uint start, uint end) rangeOfEntities, in EntityCollection entities, ExclusiveGroupStruct fromGroup, ExclusiveGroupStruct toGroup); } + + public interface IReactOnDisposeEx : IReactOnDisposeEx where T : struct, _IInternalEntityComponent + { + void Remove((uint start, uint end) rangeOfEntities, in EntityCollection entities, + ExclusiveGroupStruct groupID); + } /// /// Interface to mark an Engine as reacting after each entities submission phase diff --git a/com.sebaslab.svelto.ecs/DataStructures/ITypeSafeDictionary.cs b/com.sebaslab.svelto.ecs/DataStructures/ITypeSafeDictionary.cs index d93dd5a..5ad6fb5 100644 --- a/com.sebaslab.svelto.ecs/DataStructures/ITypeSafeDictionary.cs +++ b/com.sebaslab.svelto.ecs/DataStructures/ITypeSafeDictionary.cs @@ -76,12 +76,13 @@ namespace Svelto.ECS.Internal ITypeSafeDictionary toEntitiesDictionary, ExclusiveGroupStruct fromGroupId, ExclusiveGroupStruct toGroupId, in PlatformProfiler platformProfiler); void ExecuteEnginesRemoveCallbacks_Group( - FasterDictionary>> engines, + FasterDictionary>> reactiveEnginesRemove, FasterDictionary>> reactiveEnginesRemoveEx, ExclusiveGroupStruct @group, in PlatformProfiler profiler); void ExecuteEnginesDisposeCallbacks_Group - (FasterDictionary>> engines - , ExclusiveGroupStruct group, in PlatformProfiler profiler); + (FasterDictionary>> reactiveEnginesDispose, + FasterDictionary>> reactiveEnginesDisposeEx, + ExclusiveGroupStruct group, in PlatformProfiler profiler); void IncreaseCapacityBy(uint size); void EnsureCapacity(uint size); diff --git a/com.sebaslab.svelto.ecs/DataStructures/ManagedTypeSafeDictionary.cs b/com.sebaslab.svelto.ecs/DataStructures/ManagedTypeSafeDictionary.cs index 44825c5..1581585 100644 --- a/com.sebaslab.svelto.ecs/DataStructures/ManagedTypeSafeDictionary.cs +++ b/com.sebaslab.svelto.ecs/DataStructures/ManagedTypeSafeDictionary.cs @@ -263,8 +263,8 @@ namespace Svelto.ECS.Internal , in PlatformProfiler profiler) { TypeSafeDictionaryMethods.ExecuteEnginesSwapCallbacks_Group( - ref implMgd, (ITypeSafeDictionary)toDictionary, toGroup, fromGroup, this, reactiveEnginesSwap - , reactiveEnginesSwapEx, count, entityIDs, in profiler); + ref implMgd, (ITypeSafeDictionary)toDictionary, toGroup, fromGroup, reactiveEnginesSwap + , reactiveEnginesSwapEx, entityIDs, in profiler); } /// @@ -278,7 +278,7 @@ namespace Svelto.ECS.Internal , ExclusiveGroupStruct group, in PlatformProfiler profiler) { TypeSafeDictionaryMethods.ExecuteEnginesRemoveCallbacks_Group( - ref implMgd, this, reactiveEnginesRemove, reactiveEnginesRemoveEx, count, entityIDs, group + ref implMgd, reactiveEnginesRemove, reactiveEnginesRemoveEx, entityIDs, group , in profiler); } @@ -287,10 +287,12 @@ namespace Svelto.ECS.Internal /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void ExecuteEnginesDisposeCallbacks_Group - (FasterDictionary>> engines - , ExclusiveGroupStruct group, in PlatformProfiler profiler) + (FasterDictionary>> reactiveEnginesDispose, + FasterDictionary>> reactiveEnginesDisposeEx, + ExclusiveGroupStruct group, in PlatformProfiler profiler) { - TypeSafeDictionaryMethods.ExecuteEnginesDisposeCallbacks_Group(ref implMgd, engines, group, in profiler); + TypeSafeDictionaryMethods.ExecuteEnginesDisposeCallbacks_Group( + ref implMgd, reactiveEnginesDispose, reactiveEnginesDisposeEx, entityIDs, group, in profiler); } SveltoDictionary>, ManagedStrategy, diff --git a/com.sebaslab.svelto.ecs/DataStructures/TypeSafeDictionaryMethods.cs b/com.sebaslab.svelto.ecs/DataStructures/TypeSafeDictionaryMethods.cs index f00ceac..b58844c 100644 --- a/com.sebaslab.svelto.ecs/DataStructures/TypeSafeDictionaryMethods.cs +++ b/com.sebaslab.svelto.ecs/DataStructures/TypeSafeDictionaryMethods.cs @@ -101,14 +101,15 @@ namespace Svelto.ECS.Internal [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ExecuteEnginesDisposeCallbacks_Group( ref SveltoDictionary fromDictionary - , FasterDictionary>> allEngines - , ExclusiveGroupStruct inGroup, in PlatformProfiler sampler) + , FasterDictionary>> reactiveEnginesDispose + , FasterDictionary>> reactiveEnginesDisposeEx + , IEntityIDs entityids, ExclusiveGroupStruct group, in PlatformProfiler sampler) where Strategy1 : struct, IBufferStrategy> where Strategy2 : struct, IBufferStrategy where Strategy3 : struct, IBufferStrategy where TValue : struct, _IInternalEntityComponent { - if (allEngines.TryGetValue(ComponentTypeID.id, out var entityComponentsEngines) + if (reactiveEnginesDispose.TryGetValue(ComponentTypeID.id, out var entityComponentsEngines) == false) return; @@ -120,7 +121,7 @@ namespace Svelto.ECS.Internal foreach (var value in fromDictionary) { ref var entity = ref value.value; - var egid = new EGID(value.key, inGroup); + var egid = new EGID(value.key, group); var reactOnRemove = (IReactOnDispose)entityComponentsEngines[i].engine; reactOnRemove.Remove(ref entity, egid); } @@ -132,6 +133,35 @@ namespace Svelto.ECS.Internal throw; } + + + var count = fromDictionary.count; + + if (reactiveEnginesDisposeEx.TryGetValue( + ComponentTypeID.id + , out var reactiveEnginesDisposeExPerType)) + { + var enginesCount = reactiveEnginesDisposeExPerType.count; + + for (var i = 0; i < enginesCount; i++) + try + { + using (sampler.Sample(reactiveEnginesDisposeExPerType[i].name)) + { + ((IReactOnDisposeEx)reactiveEnginesDisposeExPerType[i].engine).Remove( + (0, (uint)count) + , new EntityCollection( + fromDictionary.UnsafeGetValues(out _), entityids + , (uint)count), group); + } + } + catch + { + Console.LogError("Code crashed inside Remove callback ".FastConcat(reactiveEnginesDisposeExPerType[i].name)); + + throw; + } + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -185,10 +215,9 @@ namespace Svelto.ECS.Internal [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ExecuteEnginesRemoveCallbacks_Group( ref SveltoDictionary fromDictionary - , ITypeSafeDictionary typeSafeDictionary , FasterDictionary>> reactiveenginesremove , FasterDictionary>> reactiveenginesremoveex - , int count, IEntityIDs entityids, ExclusiveGroupStruct group, in PlatformProfiler sampler) + , IEntityIDs entityids, ExclusiveGroupStruct group, in PlatformProfiler sampler) where Strategy1 : struct, IBufferStrategy> where Strategy2 : struct, IBufferStrategy where Strategy3 : struct, IBufferStrategy @@ -229,6 +258,7 @@ namespace Svelto.ECS.Internal ComponentTypeID.id , out var reactiveEnginesRemoveExPerType)) { + var count = fromDictionary.count; var enginesCount = reactiveEnginesRemoveExPerType.count; for (var i = 0; i < enginesCount; i++) @@ -239,7 +269,7 @@ namespace Svelto.ECS.Internal ((IReactOnRemoveEx)reactiveEnginesRemoveExPerType[i].engine).Remove( (0, (uint)count) , new EntityCollection( - typeSafeDictionary.GetValues(out _), entityids + fromDictionary.UnsafeGetValues(out _), entityids , (uint)count), group); } } @@ -300,10 +330,9 @@ namespace Svelto.ECS.Internal public static void ExecuteEnginesSwapCallbacks_Group( ref SveltoDictionary fromDictionary , ITypeSafeDictionary toDic, ExclusiveGroupStruct togroup, ExclusiveGroupStruct fromgroup - , ITypeSafeDictionary typeSafeDictionary , FasterDictionary>> reactiveenginesswap , FasterDictionary>> reactiveenginesswapex - , int count, IEntityIDs entityids, in PlatformProfiler sampler) + , IEntityIDs entityids, in PlatformProfiler sampler) where Strategy1 : struct, IBufferStrategy> where Strategy2 : struct, IBufferStrategy where Strategy3 : struct, IBufferStrategy @@ -346,6 +375,7 @@ namespace Svelto.ECS.Internal , out var reactiveEnginesRemoveExPerType)) { var enginesCount = reactiveEnginesRemoveExPerType.count; + var count = fromDictionary.count; for (var i = 0; i < enginesCount; i++) try @@ -355,7 +385,7 @@ namespace Svelto.ECS.Internal ((IReactOnSwapEx)reactiveEnginesRemoveExPerType[i].engine).MovedTo( (0, (uint)count) , new EntityCollection( - typeSafeDictionary.GetValues(out _), entityids + fromDictionary.UnsafeGetValues(out _), entityids , (uint)count), fromgroup, togroup); } } diff --git a/com.sebaslab.svelto.ecs/DataStructures/UnmanagedTypeSafeDictionary.cs b/com.sebaslab.svelto.ecs/DataStructures/UnmanagedTypeSafeDictionary.cs index 5359cd4..2a2ae60 100644 --- a/com.sebaslab.svelto.ecs/DataStructures/UnmanagedTypeSafeDictionary.cs +++ b/com.sebaslab.svelto.ecs/DataStructures/UnmanagedTypeSafeDictionary.cs @@ -275,8 +275,8 @@ namespace Svelto.ECS.Internal , in PlatformProfiler profiler) { TypeSafeDictionaryMethods.ExecuteEnginesSwapCallbacks_Group( - ref implUnmgd.dictionary, (ITypeSafeDictionary)toDictionary, toGroup, fromGroup, this - , reactiveEnginesSwap, reactiveEnginesSwapEx, count, entityIDs, in profiler); + ref implUnmgd.dictionary, (ITypeSafeDictionary)toDictionary, toGroup, fromGroup + , reactiveEnginesSwap, reactiveEnginesSwapEx, entityIDs, in profiler); } /// @@ -290,7 +290,7 @@ namespace Svelto.ECS.Internal , ExclusiveGroupStruct group, in PlatformProfiler profiler) { TypeSafeDictionaryMethods.ExecuteEnginesRemoveCallbacks_Group( - ref implUnmgd.dictionary, this, reactiveEnginesRemove, reactiveEnginesRemoveEx, count, entityIDs, group + ref implUnmgd.dictionary, reactiveEnginesRemove, reactiveEnginesRemoveEx, entityIDs, group , in profiler); } @@ -299,11 +299,12 @@ namespace Svelto.ECS.Internal /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void ExecuteEnginesDisposeCallbacks_Group - (FasterDictionary>> engines + ( FasterDictionary>> reactiveEnginesDispose + , FasterDictionary>> reactiveEnginesDisposeEx , ExclusiveGroupStruct group, in PlatformProfiler profiler) { TypeSafeDictionaryMethods.ExecuteEnginesDisposeCallbacks_Group( - ref implUnmgd.dictionary, engines, group, in profiler); + ref implUnmgd.dictionary, reactiveEnginesDispose, reactiveEnginesDisposeEx, entityIDs, group, in profiler); } internal SharedSveltoDictionaryNative implUnmgd;