diff --git a/src/Filter.cs b/src/Filter.cs index a3b48ac..2fd2f19 100644 --- a/src/Filter.cs +++ b/src/Filter.cs @@ -1,8 +1,8 @@ -using MoonTools.ECS.Collections; +using System; +using MoonTools.ECS.Collections; namespace MoonTools.ECS; -// TODO: do we want to get fancy with queries beyond Include and Exclude? public class Filter { private World World; @@ -10,6 +10,8 @@ public class Filter internal IndexableSet EntitySet = new IndexableSet(); + private bool IsDisposed; + public ReverseSpanEnumerator Entities => EntitySet.GetEnumerator(); public bool Empty => EntitySet.Count == 0; @@ -91,4 +93,31 @@ public class Filter public bool MoveNext() => LinearCongruentialEnumerator.MoveNext(); public Entity Current => Filter.NthEntity(LinearCongruentialEnumerator.Current); } + + protected virtual void Dispose(bool disposing) + { + if (!IsDisposed) + { + if (disposing) + { + EntitySet.Dispose(); + } + + IsDisposed = true; + } + } + + // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources + // ~Filter() + // { + // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + // Dispose(disposing: false); + // } + + public void Dispose() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); + } } diff --git a/src/IdAssigner.cs b/src/IdAssigner.cs index 5cb4143..a31e73b 100644 --- a/src/IdAssigner.cs +++ b/src/IdAssigner.cs @@ -1,12 +1,15 @@ +using System; using MoonTools.ECS.Collections; namespace MoonTools.ECS; -internal class IdAssigner +internal class IdAssigner : IDisposable { uint Next; NativeArray AvailableIds = new NativeArray(); + private bool IsDisposed; + public uint Assign() { if (AvailableIds.TryPop(out var id)) @@ -29,4 +32,31 @@ internal class IdAssigner AvailableIds.CopyTo(other.AvailableIds); other.Next = Next; } + + protected virtual void Dispose(bool disposing) + { + if (!IsDisposed) + { + if (disposing) + { + AvailableIds.Dispose(); + } + + IsDisposed = true; + } + } + + // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources + // ~IdAssigner() + // { + // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + // Dispose(disposing: false); + // } + + public void Dispose() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); + } } diff --git a/src/World.cs b/src/World.cs index 3ede9ef..4653a34 100644 --- a/src/World.cs +++ b/src/World.cs @@ -466,7 +466,6 @@ namespace MoonTools.ECS { if (disposing) { - // TODO: dispose managed state (managed objects) foreach (var componentStorage in ComponentIndex.Values) { componentStorage.Dispose(); @@ -476,10 +475,31 @@ namespace MoonTools.ECS { relationStorage.Dispose(); } + + foreach (var messageStorage in MessageIndex.Values) + { + messageStorage.Dispose(); + } + + foreach (var typeSet in EntityComponentIndex.Values) + { + typeSet.Dispose(); + } + + foreach (var typeSet in EntityRelationIndex.Values) + { + typeSet.Dispose(); + } + + foreach (var filter in FilterIndex.Values) + { + filter.Dispose(); + } + + EntityIdAssigner.Dispose(); + TypeIdAssigner.Dispose(); } - // TODO: free unmanaged resources (unmanaged objects) and override finalizer - // TODO: set large fields to null IsDisposed = true; } }