register and preload component types via reflection in worldbuilder
continuous-integration/drone/push Build is passing Details

pull/2/head
Evan Hemsley 2020-03-18 13:49:37 -07:00
parent 4a962bac3c
commit 1c3c6c6b69
1 changed files with 16 additions and 21 deletions

View File

@ -36,7 +36,7 @@ namespace Encompass
private readonly HashSet<Engine> senders = new HashSet<Engine>();
private readonly HashSet<Type> componentTypesToRegister = new HashSet<Type>();
private readonly HashSet<Type> componentTypesToPreload = new HashSet<Type>();
private readonly HashSet<Type> messageTypes = new HashSet<Type>();
@ -87,8 +87,6 @@ namespace Encompass
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);
@ -104,6 +102,7 @@ namespace Encompass
if (!typeToIndex.ContainsKey(typeof(TComponent)))
{
typeToIndex.Add(typeof(TComponent), typeToIndex.Count);
componentTypesToPreload.Add(typeof(TComponent));
componentManager.RegisterComponentType<TComponent>();
startingExistingComponentStore.RegisterComponentType<TComponent>();
startingUpToDateComponentStore.RegisterComponentType<TComponent>();
@ -115,11 +114,6 @@ namespace Encompass
messageTypes.UnionWith(types);
}
internal void AddComponentTypeToRegister(Type componentType)
{
componentTypesToRegister.Add(componentType);
}
/// <summary>
/// Adds the specified Engine to the World.
/// </summary>
@ -164,11 +158,6 @@ namespace Encompass
}
}
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))
@ -197,7 +186,6 @@ namespace Encompass
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IComponent, IDrawableComponent
{
RegisterComponentType<TComponent>();
componentTypesToRegister.Add(typeof(TComponent));
renderer.AssignEntityManager(entityManager);
renderer.AssignComponentManager(componentManager);
renderManager.RegisterOrderedRenderer<TComponent>(renderer.InternalRender);
@ -366,16 +354,23 @@ namespace Encompass
throw new EngineWriteConflictException(errorString);
}
var engineOrder = new List<Engine>();
foreach (var registeredComponentType in componentTypesToRegister)
// doing reflection to grab all component types, because not all writes need to be declared
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
var method = typeof(WorldBuilder).GetMethod("RegisterComponentType", BindingFlags.NonPublic | BindingFlags.Instance);
var generic = method.MakeGenericMethod(registeredComponentType);
generic.Invoke(this, null);
foreach (var componentType in assembly.GetTypes())
{
if (typeof(IComponent).IsAssignableFrom(componentType) && componentType.IsValueType && !componentType.IsEnum && !componentType.IsPrimitive)
{
var method = typeof(WorldBuilder).GetMethod("RegisterComponentType", BindingFlags.NonPublic | BindingFlags.Instance);
var generic = method.MakeGenericMethod(componentType);
generic.Invoke(this, null);
}
}
}
PreloadJIT(componentTypesToRegister, messageTypes);
PreloadJIT(componentTypesToPreload, messageTypes);
var engineOrder = new List<Engine>();
foreach (var engine in engineGraph.TopologicalSort())
{