fix IDrawableComponent throwing if non auto property used for Layer #9

Merged
cosmonaut merged 1 commits from LayerPropertyFix into master 2020-07-16 07:10:21 +00:00
3 changed files with 30 additions and 5 deletions

View File

@ -26,7 +26,8 @@ namespace Encompass
foreach (var type in _componentTypes)
{
var instanceParam = new object[] { Entity, Activator.CreateInstance(type) };
var instance = Activator.CreateInstance(type);
var instanceParam = new object[] { Entity, instance };
var setComponentMethod = typeof(Engine).GetMethod("SetComponent", BindingFlags.NonPublic | BindingFlags.Instance);
var genericSetComponentMethod = setComponentMethod.MakeGenericMethod(type);
genericSetComponentMethod.Invoke(this, instanceParam);

View File

@ -175,8 +175,8 @@ namespace Encompass
/// <param name="layer">The draw layer to register.</param>
public void RegisterDrawLayer(int layer)
{
if (_rendererRegistered)
{
if (_rendererRegistered)
{
throw new IllegalDrawLayerException("Cannot register a draw layer after a Renderer has been registered.");
}
_drawLayerManager.RegisterDrawLayer(layer);
@ -418,6 +418,11 @@ namespace Encompass
if (componentType.GetInterface("IDrawableComponent") != null)
{
// register draw layer using property value
var instance = Activator.CreateInstance(componentType);
var layerPropertyInfo = componentType.GetProperty("Layer");
dummyDrawLayerManager.RegisterDrawLayer((int)layerPropertyInfo.GetValue(instance));
var drawLayerManagerRegisterMethod = typeof(DrawLayerManager).GetMethod("RegisterOrderedDrawable");
var drawLayerManagerRegisterGenericMethod = drawLayerManagerRegisterMethod.MakeGenericMethod(componentType);
drawLayerManagerRegisterGenericMethod.Invoke(dummyDrawLayerManager, null);

View File

@ -563,15 +563,24 @@ namespace Tests
{
struct AComponent : IComponent, IDrawableComponent
{
public int Layer { get; set; }
public int Layer { get; }
}
struct BComponent : IComponent, IDrawableComponent
{
public int Layer { get => 3; }
}
class ARenderer : OrderedRenderer<AComponent>
{
public override void Render(Entity entity, in AComponent drawComponent) { }
}
class BRenderer : OrderedRenderer<BComponent>
{
public override void Render(Entity entity, in BComponent drawComponent) { }
}
[Test]
public void DrawLayerRegisterAfterOrderedRendererRegisterThrows()
{
@ -590,6 +599,16 @@ namespace Tests
Assert.DoesNotThrow(() => worldBuilder.RegisterDrawLayer(1));
Assert.DoesNotThrow(() => worldBuilder.AddOrderedRenderer(new ARenderer()));
}
[Test]
public void DrawLayerWithProperty()
{
var worldBuilder = new WorldBuilder();
var rendererB = worldBuilder.AddOrderedRenderer(new BRenderer());
Assert.DoesNotThrow(() => worldBuilder.Build());
}
}
}
}