fix crash where overwriting an existing component on the same frame as an entity was being destroyed would cause the component to not be destroyed

pull/5/head
Evan Hemsley 2019-08-22 20:07:41 -07:00
parent f26cd48afd
commit 5bcde8bb96
2 changed files with 46 additions and 2 deletions

View File

@ -105,7 +105,7 @@ namespace Encompass
var (entity, type) = keyValuePair.Key; var (entity, type) = keyValuePair.Key;
var (componentID, component) = keyValuePair.Value; var (componentID, component) = keyValuePair.Value;
if (!entityIDToComponentTypeToComponentID.ContainsKey(entity.ID)) { continue; } if (!componentIDsMarkedForWrite.Contains(componentID) || !entityIDToComponentTypeToComponentID.ContainsKey(entity.ID)) { continue; }
if (entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type)) if (entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type))
{ {
@ -218,7 +218,7 @@ namespace Encompass
{ {
foreach (var componentID in componentsMarkedForRemoval) foreach (var componentID in componentsMarkedForRemoval)
{ {
if (componentIDsMarkedForWrite.Contains(componentID)) if (componentIDsMarkedForWrite.Contains(componentID) && !IDToComponent.ContainsKey(componentID))
{ {
componentIDsMarkedForWrite.Remove(componentID); componentIDsMarkedForWrite.Remove(componentID);
} }

View File

@ -847,5 +847,49 @@ namespace Tests
Assert.DoesNotThrow(() => world.Update(0.01)); Assert.DoesNotThrow(() => world.Update(0.01));
} }
struct DestroyComponentMessage : IMessage { public Entity entity; }
[Reads(typeof(MockComponent))]
[Writes(typeof(MockComponent))]
class AddComponentEngine : Engine
{
public override void Update(double dt)
{
foreach (var (componentID, component) in ReadComponents<MockComponent>())
{
SetComponent(componentID, new MockComponent {});
}
}
}
[Receives(typeof(DestroyComponentMessage))]
class DestroyEntityEngine : Engine
{
public override void Update(double dt)
{
foreach (var message in ReadMessages<DestroyComponentMessage>())
{
Destroy(message.entity.ID);
}
}
}
[Test]
public void EngineSetComponentAndDestroyEntitySameFrame()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new AddComponentEngine());
worldBuilder.AddEngine(new DestroyEntityEngine());
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent {});
worldBuilder.SendMessage(new DestroyComponentMessage { entity = entity });
var world = worldBuilder.Build();
world.Update(0.01);
Assert.DoesNotThrow(() => world.Update(0.01));
}
} }
} }