update JIT preload
continuous-integration/drone/push Build is passing Details

pull/3/head
Evan Hemsley 2020-03-24 21:28:56 -07:00
parent 7c8154efdd
commit 0df0916347
13 changed files with 99 additions and 65 deletions

View File

@ -213,7 +213,7 @@ namespace Encompass
/// <summary>
/// Returns all Entities containing the specified Component type.
/// </summary>
protected Span<Entity> ReadEntities<TComponent>() where TComponent : struct
protected Span<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -238,7 +238,7 @@ namespace Encompass
/// <summary>
/// Returns an Entity containing the specified Component type.
/// </summary>
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -263,7 +263,7 @@ namespace Encompass
/// <summary>
/// Returns all of the Components with the specified Component Type.
/// </summary>
protected Span<TComponent> ReadComponents<TComponent>() where TComponent : struct
protected Span<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -288,7 +288,7 @@ namespace Encompass
/// <summary>
/// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned.
/// </summary>
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -313,7 +313,7 @@ namespace Encompass
/// <summary>
/// Returns true if any Component with the specified Component Type exists.
/// </summary>
protected bool SomeComponent<TComponent>() where TComponent : struct
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -335,7 +335,7 @@ namespace Encompass
}
}
private ref TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct
private ref TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -366,7 +366,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that it reads the given Component Type.
/// </exception>
protected ref readonly TComponent GetComponent<TComponent>(in Entity entity) where TComponent : struct
protected ref readonly TComponent GetComponent<TComponent>(in Entity entity) where TComponent : struct, IComponent
{
return ref GetComponentHelper<TComponent>(entity.ID);
}
@ -380,7 +380,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that it reads the given Component Type.
/// </exception>
protected ref TComponent GetComponentMutable<TComponent>(in Entity entity) where TComponent : struct
protected ref TComponent GetComponentMutable<TComponent>(in Entity entity) where TComponent : struct, IComponent
{
return ref GetComponentHelper<TComponent>(entity.ID);
}
@ -391,7 +391,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that is Reads the given Component Type.
/// </exception>
protected bool HasComponent<TComponent>(in Entity entity) where TComponent : struct
protected bool HasComponent<TComponent>(in Entity entity) where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
@ -449,7 +449,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalWriteException">
/// Thrown when the Engine does not declare that it Writes the given Component Type.
/// </exception>
protected void SetComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct
protected void SetComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct, IComponent
{
var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;
@ -489,7 +489,7 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalWriteException">
/// Thrown when the Engine does not declare that it Writes the given Component Type.
/// </exception>
protected void AddComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct
protected void AddComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct, IComponent
{
if (!EntityCreatedThisFrame(entity.ID))
{
@ -597,7 +597,7 @@ namespace Encompass
/// Destroys an arbitrary Entity containing a Component of the specified Type.
/// Entity destruction takes place after all the Engines have been processed by World Update.
/// </summary>
protected void DestroyWith<TComponent>() where TComponent : struct
protected void DestroyWith<TComponent>() where TComponent : struct, IComponent
{
Destroy(ReadEntity<TComponent>());
}
@ -606,7 +606,7 @@ namespace Encompass
/// Destroys all Entities containing a Component of the specified Type.
/// Entity destruction takes place after all the Engines have been processed by World Update.
/// </summary>
protected void DestroyAllWith<TComponent>() where TComponent : struct
protected void DestroyAllWith<TComponent>() where TComponent : struct, IComponent
{
foreach (var entity in ReadEntities<TComponent>())
{
@ -619,7 +619,7 @@ namespace Encompass
/// Note that the Engine must Read the Component type that is being removed.
/// If a Component with the specified type does not exist on the Entity, returns false and does not mutate the Entity.
/// </summary>
protected void RemoveComponent<TComponent>(in Entity entity) where TComponent : struct
protected void RemoveComponent<TComponent>(in Entity entity) where TComponent : struct, IComponent
{
var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;

View File

@ -0,0 +1,4 @@
namespace Encompass
{
public interface IComponent { }
}

View File

@ -18,37 +18,37 @@ namespace Encompass
_componentManager = componentManager;
}
protected Span<Entity> ReadEntities<TComponent>() where TComponent : struct
protected Span<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
{
return _componentManager.GetExistingEntities<TComponent>();
}
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
{
return ref _componentManager.ExistingSingularEntity<TComponent>();
}
protected Span<TComponent> ReadComponents<TComponent>() where TComponent : struct
protected Span<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
{
return _componentManager.GetComponentsByType<TComponent>();
}
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
{
return ref _componentManager.ExistingSingular<TComponent>();
}
protected ref readonly TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct
protected ref readonly TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return ref _componentManager.GetComponentByEntityAndType<TComponent>(entity.ID);
}
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return _componentManager.EntityHasComponentOfType<TComponent>(entity.ID);
}
protected bool SomeComponent<TComponent>() where TComponent : struct
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
{
return _componentManager.SomeExistingComponent<TComponent>();
}

View File

@ -5,7 +5,7 @@ namespace Encompass
/// <summary>
/// OrdereredRenderer provides a structure for the common pattern of wishing to draw a specific DrawComponent at a specific layer.
/// </summary>
public abstract class OrderedRenderer<TComponent> : Renderer where TComponent : struct, IDrawableComponent
public abstract class OrderedRenderer<TComponent> : Renderer where TComponent : struct, IComponent, IDrawableComponent
{
public abstract void Render(Entity entity, in TComponent drawComponent);

View File

@ -35,15 +35,14 @@ namespace Encompass
}
}
// we can't reflect invoke on Span returns right now... tragic
public override void Update(double dt)
{
foreach (var type in _componentTypes)
{
CallGenericMethod(type, "ReadComponent", null);
//CallGenericMethod(type, "ReadComponents", null);
CallGenericWrappedMethod(type, "ReadComponentsWrapper", null);
CallGenericMethod(type, "ReadEntity", null);
//CallGenericMethod(type, "ReadEntities", null);
CallGenericWrappedMethod(type, "ReadEntitiesWrapper", null);
CallGenericMethod(type, "GetComponent", new object[] { Entity });
CallGenericMethod(type, "HasComponent", 1, new object[] { Entity });
CallGenericMethod(type, "SomeComponent", null);
@ -59,7 +58,7 @@ namespace Encompass
CallGenericMethod(type, "SendMessage", 2, new object[] { Activator.CreateInstance(type), 1 });
CallGenericMethod(type, "ReadMessage", null);
//CallGenericMethod(type, "ReadMessages", null);
CallGenericWrappedMethod(type, "ReadMessagesWrapper", null);
CallGenericMethod(type, "SomeMessage", null);
if (typeof(IHasEntity).IsAssignableFrom(type))
{
@ -70,6 +69,23 @@ namespace Encompass
}
}
// we can't reflect invoke on Span returns right now... so we have non-return wrapper methods
protected void ReadComponentsWrapper<TComponent>() where TComponent : struct, IComponent
{
ReadComponents<TComponent>();
}
protected void ReadMessagesWrapper<TMessage>() where TMessage : struct, IMessage
{
ReadMessages<TMessage>();
}
protected void ReadEntitiesWrapper<TComponent>() where TComponent : struct, IComponent
{
ReadEntities<TComponent>();
}
// trying to use PrepareMethod because we can't reflect invoke methods that return a span...
private void CallGenericMethod(Type type, string methodName, object[] parameters)
{
@ -79,6 +95,13 @@ namespace Encompass
// RuntimeHelpers.PrepareMethod(genericReadComponentMethod.MethodHandle);
}
private void CallGenericWrappedMethod(Type type, string methodName, object[] parameters)
{
var readComponentMethod = typeof(UberEngine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
genericReadComponentMethod.Invoke(this, parameters);
}
private void CallGenericMethod(Type type, string methodName, Type[] types, object[] parameters)
{
var readComponentMethod = typeof(Engine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);

View File

@ -183,7 +183,7 @@ namespace Encompass
/// <summary>
/// Adds the specified OrderedRenderer to the World.
/// </summary>
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IDrawableComponent
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IComponent, IDrawableComponent
{
RegisterComponentType<TComponent>();
renderer.AssignEntityManager(_entityManager);
@ -354,7 +354,7 @@ namespace Encompass
throw new EngineWriteConflictException(errorString);
}
PreloadJIT(_componentTypesToPreload, _messageTypes);
PreloadJIT(_messageTypes);
var engineOrder = new List<Engine>();
@ -388,7 +388,7 @@ namespace Encompass
/// 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)
private void PreloadJIT(IEnumerable<Type> messageTypes)
{
var dummyTimeManager = new TimeManager();
var dummyMessageManager = new MessageManager(dummyTimeManager);
@ -398,9 +398,30 @@ namespace Encompass
var dummyEntityManager = new EntityManager(dummyComponentManager, _entityCapacity);
var dummyRenderManager = new RenderManager(dummyEntityManager, dummyDrawLayerManager);
// doing reflection to grab all component types, because not all writes need to be declared
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
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);
}
if (componentType.GetInterface("IDrawableComponent") != null)
{
var drawLayerManagerRegisterMethod = typeof(DrawLayerManager).GetMethod("RegisterOrderedDrawable");
var drawLayerManagerRegisterGenericMethod = drawLayerManagerRegisterMethod.MakeGenericMethod(componentType);
drawLayerManagerRegisterGenericMethod.Invoke(dummyDrawLayerManager, null);
}
}
}
var prepEngineOrder = new List<Engine>();
var uberEngine = new UberEngine(componentTypes, messageTypes);
var uberEngine = new UberEngine(_componentTypesToPreload, messageTypes);
uberEngine.AssignEntityManager(dummyEntityManager);
uberEngine.AssignComponentManager(dummyComponentManager);
@ -408,24 +429,10 @@ namespace Encompass
uberEngine.AssignTimeManager(dummyTimeManager);
uberEngine.AssignTrackingManager(dummyTrackingManager);
var uberRenderer = new UberRenderer(componentTypes);
var uberRenderer = new UberRenderer(_componentTypesToPreload);
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(

View File

@ -8,7 +8,7 @@ namespace Tests
{
public class ComponentTests
{
struct MockComponent
struct MockComponent : IComponent
{
public int myInt;
}

View File

@ -10,7 +10,7 @@ using Encompass.Exceptions;
namespace Tests
{
struct MockComponent
struct MockComponent : IComponent
{
public int myInt;
}
@ -618,7 +618,7 @@ namespace Tests
world.Update(0.01f);
}
struct DestroyerComponent { }
struct DestroyerComponent : IComponent { }
[Reads(typeof(DestroyerComponent))]
class DestroyerEngine : Engine
@ -964,7 +964,7 @@ namespace Tests
entity.Should().BeEquivalentTo(readEntity);
}
struct MockComponentB
struct MockComponentB : IComponent
{
private int value;
@ -1407,9 +1407,9 @@ namespace Tests
public class QueryTests
{
struct MockComponentB { }
struct MockComponentC { }
struct MockComponentD { }
struct MockComponentB : IComponent { }
struct MockComponentC : IComponent { }
struct MockComponentD : IComponent { }
[Reads(typeof(MockComponent), typeof(MockComponentB))]
[Writes(typeof(MockComponentB))]
@ -1863,7 +1863,7 @@ namespace Tests
_components.Should().NotBeEmpty();
}
struct MockTimerComponent
struct MockTimerComponent : IComponent
{
public double Timer { get; }

View File

@ -5,7 +5,7 @@ namespace Tests
{
public static class GeneralRendererTest
{
struct AComponent { }
struct AComponent : IComponent { }
public class SingletonRead
{

View File

@ -9,11 +9,11 @@ namespace Tests
{
public class OrderedRendererTest
{
struct AComponent { }
struct BComponent { }
struct CComponent { }
struct AComponent : IComponent { }
struct BComponent : IComponent { }
struct CComponent : IComponent { }
struct TestDrawComponent : IDrawableComponent
struct TestDrawComponent : IComponent, IDrawableComponent
{
public int Layer { get; set; }
}

View File

@ -5,7 +5,7 @@ namespace Tests
{
public class SpawnerTest
{
struct TestComponent { }
struct TestComponent : IComponent { }
struct SpawnMessageA : IMessage { }
static Entity resultEntity;

View File

@ -147,7 +147,7 @@ namespace Tests
public Entity entity;
}
struct AComponent
struct AComponent : IComponent
{
public int myInt;
}
@ -216,7 +216,7 @@ namespace Tests
public Entity entity;
}
struct AComponent
struct AComponent : IComponent
{
public int myInt;
}
@ -407,8 +407,8 @@ namespace Tests
{
static List<Engine> order = new List<Engine>();
struct AComponent { }
struct BComponent { }
struct AComponent : IComponent { }
struct BComponent : IComponent { }
struct AMessage : IMessage { }
struct BMessage : IMessage { }

View File

@ -11,8 +11,8 @@ namespace Tests
{
public class WorldTest
{
struct TestComponent { }
struct TestDrawComponent : IDrawableComponent
struct TestComponent : IComponent { }
struct TestDrawComponent : IComponent, IDrawableComponent
{
public int Layer { get; set; }
}