fix bug where multiple messages between same engines caused build crash

pull/5/head
thatcosmonaut 2019-11-13 13:05:31 -08:00
parent 1172866c9f
commit 0920b9a8f7
2 changed files with 48 additions and 2 deletions

View File

@ -187,13 +187,15 @@ namespace Encompass
{ {
foreach (var senderEngine in senders) foreach (var senderEngine in senders)
{ {
foreach (var messageType in senderEngine.sendTypes.Where((type) => type.GetInterfaces().Contains(typeof(IMessage)))) foreach (var messageType in senderEngine.sendTypes)
{ {
if (typeToReaders.ContainsKey(messageType)) if (typeToReaders.ContainsKey(messageType))
{ {
foreach (var readerEngine in typeToReaders[messageType]) foreach (var readerEngine in typeToReaders[messageType])
{ {
if (senderEngine != readerEngine) if (senderEngine != readerEngine)
{
if (!engineGraph.Exists(senderEngine, readerEngine))
{ {
engineGraph.AddEdge(senderEngine, readerEngine); engineGraph.AddEdge(senderEngine, readerEngine);
} }
@ -202,6 +204,7 @@ namespace Encompass
} }
} }
} }
}
/// <summary> /// <summary>
/// Builds the World out of the state specified on the WorldBuilder. /// Builds the World out of the state specified on the WorldBuilder.

View File

@ -518,5 +518,48 @@ namespace Tests
resultMessages.First().Should().BeOfType<AMessage>(); resultMessages.First().Should().BeOfType<AMessage>();
} }
} }
public class MultipleMessagesBetweenEngines
{
static List<Engine> order = new List<Engine>();
struct AMessage : IMessage { }
struct BMessage : IMessage { }
[Sends(typeof(AMessage), typeof(BMessage))]
class AEngine : Engine
{
public override void Update(double dt)
{
order.Add(this);
}
}
[Receives(typeof(AMessage), typeof(BMessage))]
class BEngine : Engine
{
public override void Update(double dt)
{
order.Add(this);
}
}
[Test]
public void WorldBuilderDoesNotThrowError()
{
var worldBuilder = new WorldBuilder();
var engineA = worldBuilder.AddEngine(new AEngine());
var engineB = worldBuilder.AddEngine(new BEngine());
Assert.DoesNotThrow(() => worldBuilder.Build());
var world = worldBuilder.Build();
world.Update(0.01f);
Assert.That(order.IndexOf(engineA), Is.LessThan(order.IndexOf(engineB)));
}
}
} }
} }