Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EntityView.cs 2.1KB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Svelto.DataStructures;
  5. using Svelto.Utilities;
  6. namespace Svelto.ECS
  7. {
  8. public interface IEntityView
  9. {
  10. int ID { get; }
  11. }
  12. public interface IEntityStruct:IEntityView
  13. {
  14. new int ID { set; }
  15. }
  16. public abstract class EntityView : IEntityView
  17. {
  18. public int ID { get { return _ID; } }
  19. abstract internal KeyValuePair<Type, Action<EntityView, object>>[]
  20. EntityViewBlazingFastReflection(out int count);
  21. protected int _ID;
  22. }
  23. public class EntityView<T>: EntityView where T: EntityView
  24. {
  25. internal static TEntityViewType BuildEntityView<TEntityViewType>(int ID) where TEntityViewType: EntityView<T>, new()
  26. {
  27. if (FieldCache.list.Count == 0)
  28. {
  29. var type = typeof(TEntityViewType);
  30. var fields = type.GetFields(BindingFlags.Public |
  31. BindingFlags.Instance);
  32. for (int i = fields.Length - 1; i >= 0; --i)
  33. {
  34. var field = fields[i];
  35. Action<EntityView, object> setter = FastInvoke<EntityView>.MakeSetter(field);
  36. FieldCache.Add(new KeyValuePair<Type, Action<EntityView, object>>(field.FieldType, setter));
  37. }
  38. }
  39. return new TEntityViewType { _ID = ID };
  40. }
  41. override internal KeyValuePair<Type, Action<EntityView, object>>[]
  42. EntityViewBlazingFastReflection(out int count)
  43. {
  44. return FasterList<KeyValuePair<Type, Action<EntityView, object>>>.NoVirt.ToArrayFast(FieldCache.list, out count);
  45. }
  46. static class FieldCache
  47. {
  48. internal static void Add(KeyValuePair<Type, Action<EntityView, object>> setter)
  49. {
  50. list.Add(setter);
  51. }
  52. internal static readonly FasterList<KeyValuePair<Type, Action<EntityView, object>>> list = new FasterList<KeyValuePair<Type, Action<EntityView, object>>>();
  53. }
  54. }
  55. }