From 8b9a778bfd99630b4b89e99ccbab9a280bb5675f Mon Sep 17 00:00:00 2001
From: Evan Hemsley <evan@moonside.games>
Date: Thu, 1 Aug 2019 17:05:36 -0700
Subject: [PATCH] fix bug when doing pending and existing read

---
 encompass-cs/ComponentMessageManager.cs | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/encompass-cs/ComponentMessageManager.cs b/encompass-cs/ComponentMessageManager.cs
index 2ecefdd..432c369 100644
--- a/encompass-cs/ComponentMessageManager.cs
+++ b/encompass-cs/ComponentMessageManager.cs
@@ -69,7 +69,7 @@ namespace Encompass
 
         internal void AddExistingComponentMessage<TComponent>(ComponentMessage<TComponent> componentMessage) where TComponent : struct, IComponent
         {
-            RegisterExistingOrPendingComponentMessage(componentMessage.componentID, componentMessage.component);
+            RegisterExistingOrPendingComponentMessage(componentMessage.entity, componentMessage.componentID, componentMessage.component);
 
             if (!componentMessageTypeToExistingComponentIDs.ContainsKey(typeof(TComponent)))
             {
@@ -92,7 +92,7 @@ namespace Encompass
 
         internal void AddPendingComponentMessage<TComponent>(PendingComponentMessage<TComponent> pendingComponentMessage) where TComponent : struct, IComponent
         {
-            RegisterExistingOrPendingComponentMessage(pendingComponentMessage.componentID, pendingComponentMessage.component);
+            RegisterExistingOrPendingComponentMessage(pendingComponentMessage.entity, pendingComponentMessage.componentID, pendingComponentMessage.component);
 
             if (!componentMessageTypeToPendingComponentIDs.ContainsKey(typeof(TComponent)))
             {
@@ -113,14 +113,26 @@ namespace Encompass
             entityToTypeToPendingComponentIDs[pendingComponentMessage.entity][typeof(TComponent)].Add(pendingComponentMessage.componentID);
         }
 
-        private void RegisterExistingOrPendingComponentMessage<TComponent>(Guid componentID, TComponent component) where TComponent: struct, IComponent
+        private void RegisterExistingOrPendingComponentMessage<TComponent>(Entity entity, Guid componentID, TComponent component) where TComponent: struct, IComponent
         {
             componentIDToComponent[componentID] = component;
+
             if (!componentMessageTypeToComponentIDs.ContainsKey(typeof(TComponent)))
             {
                 componentMessageTypeToComponentIDs.Add(typeof(TComponent), new HashSet<Guid>());
             }
             componentMessageTypeToComponentIDs[typeof(TComponent)].Add(componentID);
+
+            if (!entityToTypeToComponentIDs.ContainsKey(entity))
+            {
+                entityToTypeToComponentIDs.Add(entity, new Dictionary<Type, HashSet<Guid>>());
+            }
+            if (!entityToTypeToComponentIDs[entity].ContainsKey(typeof(TComponent)))
+            {
+                entityToTypeToComponentIDs[entity].Add(typeof(TComponent), new HashSet<Guid>());
+            }
+
+            entityToTypeToComponentIDs[entity][typeof(TComponent)].Add(componentID);
         }
 
         // general component reads by type