encompass-cs/encompass-cs/WorldBuilder.cs

471 lines
20 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using Encompass.Exceptions;
using MoonTools.Core.Graph;
using MoonTools.Core.Graph.Extensions;
namespace Encompass
{
/// <summary>
/// WorldBuilder is used to construct a World from Engines, Renderers, and an initial state of Entities, Components, and Messages.
/// </summary>
/// <remarks>
/// WorldBuilder enforces certain rules about Engine structure. It is forbidden to have messages create cycles between Engines,
/// and no Component may be written by more than one Engine.
/// The WorldBuilder uses Engines and their Message read/emit information to determine a valid ordering of the Engines, which is given to the World.
/// </remarks>
public class WorldBuilder
{
private readonly int entityCapacity;
private readonly List<Engine> engines = new List<Engine>();
private readonly DirectedGraph<Engine, Unit> engineGraph = GraphBuilder.DirectedGraph<Engine>();
private readonly ComponentStore startingExistingComponentStore;
private readonly ComponentStore startingUpToDateComponentStore;
private readonly ComponentManager componentManager;
private readonly EntityManager entityManager;
private readonly MessageManager messageManager;
private readonly TimeManager timeManager;
private readonly DrawLayerManager drawLayerManager;
private readonly RenderManager renderManager;
private readonly TrackingManager trackingManager;
private readonly Dictionary<Type, HashSet<Engine>> typeToReaders = new Dictionary<Type, HashSet<Engine>>();
private readonly HashSet<Engine> senders = new HashSet<Engine>();
private readonly HashSet<Type> componentTypesToRegister = new HashSet<Type>();
private readonly HashSet<Type> messageTypes = new HashSet<Type>();
private readonly Dictionary<Type, int> typeToIndex = new Dictionary<Type, int>();
public WorldBuilder(int entityCapacity = 32768)
{
this.entityCapacity = entityCapacity;
drawLayerManager = new DrawLayerManager(typeToIndex);
timeManager = new TimeManager();
trackingManager = new TrackingManager();
componentManager = new ComponentManager(drawLayerManager, typeToIndex);
messageManager = new MessageManager(timeManager);
entityManager = new EntityManager(componentManager, entityCapacity);
renderManager = new RenderManager(entityManager, drawLayerManager);
startingExistingComponentStore = new ComponentStore(typeToIndex);
startingUpToDateComponentStore = new ComponentStore(typeToIndex);
}
/// <summary>
/// Creates and returns a new empty Entity.
/// </summary>
public Entity CreateEntity()
{
return entityManager.CreateEntity();
}
/// <summary>
/// Specifies that the given Message should be sent immediately on the first World Update.
/// </summary>
public void SendMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
{
messageManager.AddMessage(message);
}
/// <summary>
/// Specifies that the given Message should be sent after the specified number of seconds after the first World Update.
/// </summary>
public void SendMessage<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
{
messageManager.AddMessage<TMessage>(message, time);
}
/// <summary>
/// Sets Component data for the specified Component Type on the specified Entity.
/// </summary>
public void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
RegisterComponentType<TComponent>();
componentTypesToRegister.Add(typeof(TComponent));
startingExistingComponentStore.Set(entity.ID, component);
startingUpToDateComponentStore.Set(entity.ID, component);
if (component is IDrawableComponent drawableComponent)
{
componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer);
drawLayerManager.RegisterOrderedDrawable<TComponent>();
}
}
internal void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
{
if (!typeToIndex.ContainsKey(typeof(TComponent)))
{
typeToIndex.Add(typeof(TComponent), typeToIndex.Count);
componentManager.RegisterComponentType<TComponent>();
startingExistingComponentStore.RegisterComponentType<TComponent>();
startingUpToDateComponentStore.RegisterComponentType<TComponent>();
}
}
internal void RegisterMessageTypes(IEnumerable<Type> types)
{
messageTypes.UnionWith(types);
}
internal void AddComponentTypeToRegister(Type componentType)
{
componentTypesToRegister.Add(componentType);
}
/// <summary>
/// Adds the specified Engine to the World.
/// </summary>
/// <param name="engine">An instance of an Engine.</param>
public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine
{
engine.AssignEntityManager(entityManager);
engine.AssignComponentManager(componentManager);
engine.AssignMessageManager(messageManager);
engine.AssignTimeManager(timeManager);
engine.AssignTrackingManager(trackingManager);
engines.Add(engine);
engineGraph.AddNode(engine);
var messageReceiveTypes = engine.receiveTypes;
var messageSendTypes = engine.sendTypes;
RegisterMessageTypes(engine.receiveTypes.Union(engine.sendTypes));
foreach (var writeImmediateType in engine.writeImmediateTypes.Intersect(engine.readImmediateTypes))
{
throw new EngineSelfCycleException("Engine {0} both writes and reads immediate Component {1}", engine.GetType().Name, writeImmediateType.Name);
}
foreach (var messageType in messageReceiveTypes.Intersect(messageSendTypes))
{
throw new EngineSelfCycleException("Engine {0} both receives and sends Message {1}", engine.GetType().Name, messageType.Name);
}
if (messageSendTypes.Count > 0 || engine.writeImmediateTypes.Count > 0)
{
senders.Add(engine);
}
foreach (var componentType in engine.queryWithTypes.Union(engine.queryWithoutTypes))
{
trackingManager.RegisterComponentTypeToEngine(componentType, engine);
if (engine.readImmediateTypes.Contains(componentType))
{
trackingManager.RegisterImmediateComponentTypeToEngine(componentType, engine);
}
}
foreach (var componentType in engine.readTypes.Union(engine.writeTypes).Union(engine.readImmediateTypes))
{
AddComponentTypeToRegister(componentType);
}
foreach (var receiveType in engine.receiveTypes.Union(engine.readImmediateTypes))
{
if (!typeToReaders.ContainsKey(receiveType))
{
typeToReaders.Add(receiveType, new HashSet<Engine>());
}
typeToReaders[receiveType].Add(engine);
}
return engine;
}
/// <summary>
/// Registers a draw layer. This must be done before any Renderers are registered.
/// </summary>
/// <param name="layer">The draw layer to register.</param>
public void RegisterDrawLayer(int layer)
{
drawLayerManager.RegisterDrawLayer(layer);
}
/// <summary>
/// Adds the specified OrderedRenderer to the World.
/// </summary>
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IComponent, IDrawableComponent
{
renderer.AssignEntityManager(entityManager);
renderer.AssignComponentManager(componentManager);
renderManager.RegisterOrderedRenderer<TComponent>(renderer.InternalRender);
return renderer;
}
/// <summary>
/// Adds the specified GeneralRenderer to the World at the specified layer.
/// Higher layer numbers draw on top of lower layer numbers.
/// </summary>
/// <param name="renderer">An instance of a GeneralRenderer.</param>
/// <param name="layer">The layer at which the GeneralRenderer should render. Higher numbers draw over lower numbers.</param>
public TRenderer AddGeneralRenderer<TRenderer>(TRenderer renderer, int layer) where TRenderer : GeneralRenderer
{
renderer.AssignEntityManager(entityManager);
renderer.AssignComponentManager(componentManager);
renderManager.RegisterGeneralRendererWithLayer(renderer, layer);
return renderer;
}
private void BuildEngineGraph()
{
foreach (var senderEngine in senders)
{
foreach (var messageType in senderEngine.sendTypes.Union(senderEngine.writeImmediateTypes))
{
if (typeToReaders.ContainsKey(messageType))
{
foreach (var readerEngine in typeToReaders[messageType])
{
if (senderEngine != readerEngine)
{
if (!engineGraph.Exists(senderEngine, readerEngine))
{
engineGraph.AddEdge(senderEngine, readerEngine);
}
}
}
}
}
}
}
/// <summary>
/// Builds the World out of the state specified on the WorldBuilder.
/// Validates and constructs an ordering of the given Engines.
/// </summary>
/// <returns>An instance of World.</returns>
public World Build()
{
BuildEngineGraph();
if (engineGraph.Cyclic())
{
var cycles = engineGraph.SimpleCycles();
var errorString = "Cycle(s) found in Engines: ";
foreach (var cycle in cycles)
{
var reversed = cycle.Reverse();
errorString += "\n" +
string.Join(" -> ", reversed.Select((engine) => engine.GetType().Name)) +
" -> " +
reversed.First().GetType().Name;
}
throw new EngineCycleException(errorString);
}
var writtenComponentTypesWithoutPriority = new HashSet<Type>();
var writtenComponentTypesWithPriority = new HashSet<Type>();
var duplicateWritesWithoutPriority = new List<Type>();
var duplicateWritesWithSamePriority = new List<Type>();
var writePriorities = new Dictionary<Type, HashSet<int>>();
var writeMessageToEngines = new Dictionary<Type, List<Engine>>();
foreach (var engine in engines)
{
if (engine.GetType().GetCustomAttribute<IgnoresTimeDilation>() != null)
{
engine.usesTimeDilation = false;
}
var defaultWritePriorityAttribute = engine.GetType().GetCustomAttribute<DefaultWritePriority>(false);
foreach (var writeType in engine.writeTypes)
{
int? priority = null;
if (engine.writePriorities.ContainsKey(writeType))
{
priority = engine.writePriorities[writeType];
}
else if (defaultWritePriorityAttribute != null)
{
priority = defaultWritePriorityAttribute.writePriority;
}
if (priority.HasValue)
{
writtenComponentTypesWithPriority.Add(writeType);
if (!writePriorities.ContainsKey(writeType))
{
writePriorities[writeType] = new HashSet<int>();
}
if (writePriorities[writeType].Contains(priority.Value))
{
duplicateWritesWithSamePriority.Add(writeType);
}
else if (writtenComponentTypesWithoutPriority.Contains(writeType))
{
duplicateWritesWithoutPriority.Add(writeType);
}
else
{
writePriorities[writeType].Add(priority.Value);
}
}
else
{
if (writtenComponentTypesWithoutPriority.Contains(writeType) || writtenComponentTypesWithPriority.Contains(writeType))
{
duplicateWritesWithoutPriority.Add(writeType);
}
else
{
writtenComponentTypesWithoutPriority.Add(writeType);
}
}
if (!writeMessageToEngines.ContainsKey(writeType))
{
writeMessageToEngines[writeType] = new List<Engine>();
}
writeMessageToEngines[writeType].Add(engine);
}
}
if (duplicateWritesWithoutPriority.Count > 0)
{
var errorString = "Multiple Engines write the same Component without declaring priority: ";
foreach (var componentType in duplicateWritesWithoutPriority)
{
errorString += "\n" +
componentType.Name + " written by: " +
string.Join(", ", writeMessageToEngines[componentType].Select((engine) => engine.GetType().Name));
}
errorString += "\nTo resolve the conflict, add priority arguments to the Writes declarations or use a DefaultWritePriority attribute.";
throw new EngineWriteConflictException(errorString);
}
if (duplicateWritesWithSamePriority.Count > 0)
{
var errorString = "Multiple Engines write the same Component with the same priority: ";
foreach (var componentType in duplicateWritesWithSamePriority)
{
errorString += "\n" +
componentType.Name + " written by: " +
string.Join(", ", writeMessageToEngines[componentType].Select(engine => engine.GetType().Name));
}
errorString += "\nTo resolve the conflict, add priority arguments to the Writes declarations or use a DefaultWritePriority attribute.";
throw new EngineWriteConflictException(errorString);
}
var engineOrder = new List<Engine>();
foreach (var registeredComponentType in componentTypesToRegister)
{
var method = typeof(WorldBuilder).GetMethod("RegisterComponentType", BindingFlags.NonPublic | BindingFlags.Instance);
var generic = method.MakeGenericMethod(registeredComponentType);
generic.Invoke(this, null);
}
PreloadJIT(componentTypesToRegister, messageTypes);
foreach (var engine in engineGraph.TopologicalSort())
{
engineOrder.Add(engine);
engine.BuildEntityQuery();
}
var world = new World(
engineOrder,
entityManager,
componentManager,
trackingManager,
messageManager,
timeManager,
renderManager
);
componentManager.SetExistingComponentStore(startingExistingComponentStore);
componentManager.SetUpToDateComponentStore(startingUpToDateComponentStore);
trackingManager.InitializeTracking(entityManager.EntityIDs);
return world;
}
/// <summary>
/// This is necessary because Encompass heavily uses generic methods with value types,
/// so the first time any code path runs the JIT gets smashed. This method warms up the runtime.
/// It does so by grabbing all component and message types known to the WorldBuilder and
/// executing every possible generic method that could be executed with those types.
/// </summary>
private void PreloadJIT(IEnumerable<Type> componentTypes, IEnumerable<Type> messageTypes)
{
var dummyTimeManager = new TimeManager();
var dummyMessageManager = new MessageManager(dummyTimeManager);
var dummyDrawLayerManager = new DrawLayerManager(typeToIndex);
var dummyTrackingManager = new TrackingManager();
var dummyComponentManager = new ComponentManager(dummyDrawLayerManager, typeToIndex);
var dummyEntityManager = new EntityManager(dummyComponentManager, entityCapacity);
var dummyRenderManager = new RenderManager(dummyEntityManager, dummyDrawLayerManager);
var prepEngineOrder = new List<Engine>();
var uberEngine = new UberEngine(componentTypes, messageTypes);
uberEngine.AssignEntityManager(dummyEntityManager);
uberEngine.AssignComponentManager(dummyComponentManager);
uberEngine.AssignMessageManager(dummyMessageManager);
uberEngine.AssignTimeManager(dummyTimeManager);
uberEngine.AssignTrackingManager(dummyTrackingManager);
var uberRenderer = new UberRenderer(componentTypes);
uberRenderer.AssignComponentManager(dummyComponentManager);
uberRenderer.AssignEntityManager(dummyEntityManager);
foreach (var type in componentTypes)
{
var componentManagerRegisterMethod = typeof(ComponentManager).GetMethod("RegisterComponentType");
var componentManagerRegisterGenericMethod = componentManagerRegisterMethod.MakeGenericMethod(type);
componentManagerRegisterGenericMethod.Invoke(dummyComponentManager, null);
if (type.GetInterface("IDrawableComponent") != null)
{
var drawLayerManagerRegisterMethod = typeof(DrawLayerManager).GetMethod("RegisterOrderedDrawable");
var drawLayerManagerRegisterGenericMethod = drawLayerManagerRegisterMethod.MakeGenericMethod(type);
drawLayerManagerRegisterGenericMethod.Invoke(dummyDrawLayerManager, null);
}
}
prepEngineOrder.Add(uberEngine);
var dummyWorld = new World(
prepEngineOrder,
dummyEntityManager,
dummyComponentManager,
dummyTrackingManager,
dummyMessageManager,
dummyTimeManager,
dummyRenderManager
);
uberEngine.Write();
dummyComponentManager.WriteComponents();
dummyWorld.Update(1);
uberEngine.Write();
dummyComponentManager.WriteComponents();
uberRenderer.SetEntity(uberEngine.Entity);
uberRenderer.Render();
}
}
}