From 428b375d88c72be31fd03dc91762727de7cfecff Mon Sep 17 00:00:00 2001
From: cosmonaut <evan@moonside.games>
Date: Fri, 25 Mar 2022 12:32:35 -0700
Subject: [PATCH] DebugSystem API

---
 src/ComponentDepot.cs   | 29 +++++++++++++++++++++++++++++
 src/ComponentStorage.cs | 10 +++++++++-
 src/DebugSystem.cs      | 26 ++++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 src/DebugSystem.cs

diff --git a/src/ComponentDepot.cs b/src/ComponentDepot.cs
index 02afb20..beb96de 100644
--- a/src/ComponentDepot.cs
+++ b/src/ComponentDepot.cs
@@ -205,4 +205,33 @@ internal class ComponentDepot
 
 		filterSignatureToEntityIDs[filterSignature].Add(entityID);
 	}
+
+	// debug use only!
+
+	public IEnumerable<object> Debug_GetAllComponents(int entityID)
+	{
+		foreach (var (type, storage) in storages)
+		{
+			if (storage.Has(entityID))
+			{
+				yield return storage.Debug_Get(entityID);
+			}
+		}
+	}
+
+	public ReadOnlySpan<Entity> Debug_GetEntities(Type componentType)
+	{
+		return Lookup(componentType).AllEntities();
+	}
+
+	public IEnumerable<Type> Debug_SearchComponentType(string typeString)
+	{
+		foreach (var type in storages.Keys)
+		{
+			if (type.ToString().ToLower().Contains(typeString.ToLower()))
+			{
+				yield return type;
+			}
+		}
+	}
 }
diff --git a/src/ComponentStorage.cs b/src/ComponentStorage.cs
index 54ca698..7844c6c 100644
--- a/src/ComponentStorage.cs
+++ b/src/ComponentStorage.cs
@@ -4,6 +4,9 @@ internal abstract class ComponentStorage
 {
 	public abstract bool Has(int entityID);
 	public abstract void Remove(int entityID);
+	public abstract ReadOnlySpan<Entity> AllEntities();
+
+	public abstract object Debug_Get(int entityID);
 }
 
 internal class ComponentStorage<TComponent> : ComponentStorage where TComponent : struct
@@ -29,6 +32,11 @@ internal class ComponentStorage<TComponent> : ComponentStorage where TComponent
 		return ref components[entityIDToStorageIndex[entityID]];
 	}
 
+	public override object Debug_Get(int entityID)
+	{
+		return components[entityIDToStorageIndex[entityID]];
+	}
+
 	public ref readonly TComponent Get()
 	{
 		#if DEBUG
@@ -89,7 +97,7 @@ internal class ComponentStorage<TComponent> : ComponentStorage where TComponent
 		entityIDToStorageIndex.Clear();
 	}
 
-	public ReadOnlySpan<Entity> AllEntities()
+	public override ReadOnlySpan<Entity> AllEntities()
 	{
 		return new ReadOnlySpan<Entity>(storageIndexToEntities, 0, nextID);
 	}
diff --git a/src/DebugSystem.cs b/src/DebugSystem.cs
new file mode 100644
index 0000000..0697800
--- /dev/null
+++ b/src/DebugSystem.cs
@@ -0,0 +1,26 @@
+// NOTE: these methods are very inefficient
+// this class should only be used in debugging contexts!!
+namespace MoonTools.ECS
+{
+	public abstract class DebugSystem : System
+	{
+		protected DebugSystem(World world) : base(world)
+		{
+		}
+
+		protected IEnumerable<object> Debug_GetAllComponents(Entity entity)
+		{
+			return ComponentDepot.Debug_GetAllComponents(entity.ID);
+		}
+
+		protected ReadOnlySpan<Entity> Debug_GetEntities(Type componentType)
+		{
+			return ComponentDepot.Debug_GetEntities(componentType);
+		}
+
+		protected IEnumerable<Type> Debug_SearchComponentType(string typeString)
+		{
+			return ComponentDepot.Debug_SearchComponentType(typeString);
+		}
+	}
+}