2019-12-17 03:55:27 +00:00
|
|
|
|
using System;
|
2019-12-17 02:51:45 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-12-17 03:55:27 +00:00
|
|
|
|
using System.Linq;
|
2019-12-16 22:51:12 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace Encompass
|
|
|
|
|
{
|
|
|
|
|
internal class UberEngine : Engine
|
|
|
|
|
{
|
2019-12-17 02:51:45 +00:00
|
|
|
|
private IEnumerable<Type> _componentTypes;
|
2019-12-17 03:55:27 +00:00
|
|
|
|
private IEnumerable<Type> _messageTypes;
|
2019-12-24 02:45:49 +00:00
|
|
|
|
public Entity Entity { get; private set; }
|
2019-12-17 02:51:45 +00:00
|
|
|
|
|
2019-12-24 02:45:49 +00:00
|
|
|
|
public UberEngine(IEnumerable<Type> componentTypes, IEnumerable<Type> messageTypes)
|
2019-12-17 02:51:45 +00:00
|
|
|
|
{
|
|
|
|
|
_componentTypes = componentTypes;
|
2019-12-17 03:55:27 +00:00
|
|
|
|
_messageTypes = messageTypes;
|
2019-12-17 02:51:45 +00:00
|
|
|
|
readTypes.UnionWith(componentTypes);
|
|
|
|
|
writeTypes.UnionWith(componentTypes);
|
2019-12-17 03:55:27 +00:00
|
|
|
|
sendTypes.UnionWith(messageTypes);
|
|
|
|
|
receiveTypes.UnionWith(messageTypes);
|
2019-12-17 02:51:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Write()
|
2019-12-16 22:51:12 +00:00
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
|
Entity = CreateEntity();
|
|
|
|
|
|
2019-12-17 02:51:45 +00:00
|
|
|
|
foreach (var type in _componentTypes)
|
2019-12-16 22:51:12 +00:00
|
|
|
|
{
|
2019-12-24 02:45:49 +00:00
|
|
|
|
var instanceParam = new object[] { Entity, Activator.CreateInstance(type) };
|
2019-12-17 02:51:45 +00:00
|
|
|
|
var setComponentMethod = typeof(Engine).GetMethod("SetComponent", BindingFlags.NonPublic | BindingFlags.Instance);
|
|
|
|
|
var genericSetComponentMethod = setComponentMethod.MakeGenericMethod(type);
|
|
|
|
|
genericSetComponentMethod.Invoke(this, instanceParam);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-16 22:51:12 +00:00
|
|
|
|
|
2019-12-17 02:51:45 +00:00
|
|
|
|
public override void Update(double dt)
|
|
|
|
|
{
|
|
|
|
|
foreach (var type in _componentTypes)
|
|
|
|
|
{
|
|
|
|
|
CallGenericMethod(type, "ReadComponent", null);
|
|
|
|
|
CallGenericMethod(type, "ReadComponentIncludingEntity", null);
|
|
|
|
|
CallGenericMethod(type, "ReadComponents", null);
|
|
|
|
|
CallGenericMethod(type, "ReadComponentsIncludingEntity", null);
|
2019-12-17 04:16:46 +00:00
|
|
|
|
CallGenericMethod(type, "ReadEntity", null);
|
|
|
|
|
CallGenericMethod(type, "ReadEntities", null);
|
2019-12-24 02:45:49 +00:00
|
|
|
|
CallGenericMethod(type, "GetComponent", new Type[] { typeof(Entity) }, new object[] { Entity });
|
|
|
|
|
CallGenericMethod(type, "HasComponent", new Type[] { typeof(Entity) }, new object[] { Entity });
|
2019-12-17 02:51:45 +00:00
|
|
|
|
CallGenericMethod(type, "SomeComponent", null);
|
|
|
|
|
CallGenericMethod(type, "DestroyWith", null);
|
|
|
|
|
CallGenericMethod(type, "DestroyAllWith", null);
|
2019-12-24 04:15:01 +00:00
|
|
|
|
CallGenericMethod(type, "RemoveComponent", new Type[] { typeof(Entity) }, new object[] { Entity });
|
2019-12-16 22:51:12 +00:00
|
|
|
|
}
|
2019-12-17 03:55:27 +00:00
|
|
|
|
|
|
|
|
|
foreach (var type in _messageTypes)
|
|
|
|
|
{
|
|
|
|
|
CallGenericMethod(type, "SendMessageIgnoringTimeDilation", new object[] { Activator.CreateInstance(type), 1 });
|
|
|
|
|
CallGenericMethod(type, "SendMessage", 1, new object[] { Activator.CreateInstance(type) });
|
|
|
|
|
CallGenericMethod(type, "SendMessage", 2, new object[] { Activator.CreateInstance(type), 1 });
|
|
|
|
|
CallGenericMethod(type, "ReadMessage", null);
|
|
|
|
|
CallGenericMethod(type, "ReadMessages", null);
|
|
|
|
|
CallGenericMethod(type, "SomeMessage", null);
|
2019-12-30 07:55:48 +00:00
|
|
|
|
if (typeof(IHasEntity).IsAssignableFrom(type))
|
|
|
|
|
{
|
|
|
|
|
CallGenericMethod(type, "ReadMessagesWithEntity", new Type[] { typeof(Entity) }, new object[] { Entity });
|
|
|
|
|
}
|
2019-12-17 03:55:27 +00:00
|
|
|
|
}
|
2019-12-16 22:51:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 02:51:45 +00:00
|
|
|
|
private void CallGenericMethod(Type type, string methodName, object[] parameters)
|
|
|
|
|
{
|
|
|
|
|
var readComponentMethod = typeof(Engine).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)
|
2019-12-16 22:51:12 +00:00
|
|
|
|
{
|
2019-12-17 02:51:45 +00:00
|
|
|
|
var readComponentMethod = typeof(Engine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);
|
2019-12-16 22:51:12 +00:00
|
|
|
|
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
|
|
|
|
|
genericReadComponentMethod.Invoke(this, parameters);
|
|
|
|
|
}
|
2019-12-17 03:55:27 +00:00
|
|
|
|
|
|
|
|
|
private void CallGenericMethod(Type type, string methodName, int argumentNum, object[] parameters)
|
|
|
|
|
{
|
|
|
|
|
var method = typeof(Engine).GetRuntimeMethods().Where(m => m.Name == methodName && m.GetParameters().Length == argumentNum).First();
|
|
|
|
|
var genericMethod = method.MakeGenericMethod(type);
|
|
|
|
|
genericMethod.Invoke(this, parameters);
|
|
|
|
|
}
|
2019-12-16 22:51:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|