Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to disable AsDisposable functionality for stashes #218

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Core/Stash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ public sealed class Stash<T> : 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<T> components;

#if !MORPEH_DISABLE_COMPONENT_DISPOSE
internal ComponentDispose componentDispose;
#endif

[UnityEngine.Scripting.Preserve]
static Stash() {
Expand Down Expand Up @@ -153,9 +157,11 @@ public Stash(int capacity = -1) {
internal Stash(Stash<T> other) {
this.typeId = other.typeId;
this.offset = other.offset;

this.components = new IntHashMap<T>(other.components);
#if !MORPEH_DISABLE_COMPONENT_DISPOSE
this.componentDispose = other.componentDispose;
#endif

RegisterStash(this);
RegisterTypedStash(this);
Expand Down Expand Up @@ -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;
Expand All @@ -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]);
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -401,19 +414,23 @@ 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;

UnregisterTypedStash(this);
UnregisterStash(this);

#if !MORPEH_DISABLE_COMPONENT_DISPOSE
this.componentDispose = null;
#endif
this.IsDisposed = true;
}
}
Expand Down
2 changes: 2 additions & 0 deletions Core/StashExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Scellecs.Morpeh {
using System;
using Collections;
public static class StashExtensions {
#if !MORPEH_DISABLE_COMPONENT_DISPOSE
public static Stash<T> AsDisposable<T>(this Stash<T> stash) where T : struct, IComponent, IDisposable {
#if MORPEH_DEBUG
if (stash == null || stash.components == null) {
Expand All @@ -25,6 +26,7 @@ public static Stash<T> AsDisposable<T>(this Stash<T> stash) where T : struct, IC

return stash;
}
#endif

public static Stash<T> Clone<T>(this Stash<T> stash) where T : unmanaged, IComponent {
#if MORPEH_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down