diff --git a/Core/Stash.cs b/Core/Stash.cs index 06b1fb57..03a0ffd3 100644 --- a/Core/Stash.cs +++ b/Core/Stash.cs @@ -93,12 +93,16 @@ public sealed class Stash : Stash where T : struct, IComponent { // ReSharper disable once StaticMemberInGenericType internal static IntStack typedStashesFreeIds; +#if !MORPEH_DISABLE_COMPONENT_DISPOSE internal delegate void ComponentDispose(ref T component); +#endif internal T empty; internal IntHashMap components; +#if !MORPEH_DISABLE_COMPONENT_DISPOSE internal ComponentDispose componentDispose; +#endif [UnityEngine.Scripting.Preserve] static Stash() { @@ -153,9 +157,11 @@ public Stash(int capacity = -1) { internal Stash(Stash other) { this.typeId = other.typeId; this.offset = other.offset; - + this.components = new IntHashMap(other.components); +#if !MORPEH_DISABLE_COMPONENT_DISPOSE this.componentDispose = other.componentDispose; +#endif RegisterStash(this); RegisterTypedStash(this); @@ -292,7 +298,9 @@ public override bool Remove(Entity entity) { if (this.components.Remove(entity.entityId.id, out var lastValue)) { entity.RemoveTransfer(this.typeId, this.offset); +#if !MORPEH_DISABLE_COMPONENT_DISPOSE this.componentDispose?.Invoke(ref lastValue); +#endif return true; } return false; @@ -302,6 +310,7 @@ public override bool Remove(Entity entity) { public override void RemoveAll() { world.ThreadSafetyCheck(); +#if !MORPEH_DISABLE_COMPONENT_DISPOSE if (this.componentDispose != null) { foreach (var index in this.components) { this.componentDispose.Invoke(ref this.components.data[index]); @@ -310,7 +319,9 @@ public override void RemoveAll() { this.world.GetEntity(entityId).RemoveTransfer(this.typeId, this.offset); } } - else { + else +#endif + { foreach (var index in this.components) { var entityId = this.components.GetKeyByIndex(index); this.world.GetEntity(entityId).RemoveTransfer(this.typeId, this.offset); @@ -323,7 +334,9 @@ public override void RemoveAll() { [MethodImpl(MethodImplOptions.AggressiveInlining)] internal override bool Clean(Entity entity) { if (this.components.Remove(entity.entityId.id, out var lastValue)) { +#if !MORPEH_DISABLE_COMPONENT_DISPOSE this.componentDispose?.Invoke(ref lastValue); +#endif return true; } return false; @@ -401,11 +414,13 @@ public override void Dispose() { world.ThreadSafetyCheck(); +#if !MORPEH_DISABLE_COMPONENT_DISPOSE if (this.componentDispose != null) { foreach (var componentId in this.components) { this.componentDispose.Invoke(ref this.components.data[componentId]); } } +#endif this.components.Clear(); this.components = null; @@ -413,7 +428,9 @@ public override void Dispose() { UnregisterTypedStash(this); UnregisterStash(this); +#if !MORPEH_DISABLE_COMPONENT_DISPOSE this.componentDispose = null; +#endif this.IsDisposed = true; } } diff --git a/Core/StashExtensions.cs b/Core/StashExtensions.cs index a4377a39..19d35459 100644 --- a/Core/StashExtensions.cs +++ b/Core/StashExtensions.cs @@ -9,6 +9,7 @@ namespace Scellecs.Morpeh { using System; using Collections; public static class StashExtensions { +#if !MORPEH_DISABLE_COMPONENT_DISPOSE public static Stash AsDisposable(this Stash stash) where T : struct, IComponent, IDisposable { #if MORPEH_DEBUG if (stash == null || stash.components == null) { @@ -25,6 +26,7 @@ public static Stash AsDisposable(this Stash stash) where T : struct, IC return stash; } +#endif public static Stash Clone(this Stash stash) where T : unmanaged, IComponent { #if MORPEH_DEBUG diff --git a/README.md b/README.md index 357dcbad..3c5c1811 100644 --- a/README.md +++ b/README.md @@ -838,7 +838,7 @@ Can be set by user: * `MORPEH_DISABLE_SET_ICONS` Define for disabling set icons in Project Window. * `MORPEH_DISABLE_AUTOINITIALIZATION` Define for disable default world creation and creating Morpeh Runner GameObject. * `MORPEH_DISABLE_COMPILATION_REPORT` Define for disable compilation report in Editor Console. - +* `MORPEH_DISABLE_COMPONENT_DISPOSE` Define to disable component disposing feature. Will be set by framework: * `MORPEH_BURST` Determine if Burst is enabled, and framework has enabled Native API.