A stable modding interface between Techblox and mods https://mod.exmods.org/
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.

88 lines
2.7KB

  1. using System;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS;
  4. using Svelto.ECS.Hybrid;
  5. using Svelto.ECS.Internal;
  6. namespace TechbloxModdingAPI.Utility
  7. {
  8. public ref struct RefCollection<T> where T : struct, IBaseEntityComponent
  9. {
  10. private readonly bool managed;
  11. private int count;
  12. private NB<T> nativeArray;
  13. private MB<T> managedArray;
  14. private NativeEntityIDs nativeIDs;
  15. private ManagedEntityIDs managedIDs;
  16. private RefCollection(EntityCollection<T> coll, T inst = default)
  17. {
  18. if (inst is IEntityComponent)
  19. {
  20. DeconstructCollection<T, T>(coll);
  21. }
  22. if (typeof(T).IsAssignableFrom(typeof(IEntityViewComponent)))
  23. {
  24. (managedArray, managedIDs, count) = coll2;
  25. }
  26. }
  27. private void DeconstructCollection<TM, TN>(EntityCollection<T> coll) where TM : struct, IEntityViewComponent
  28. where TN : unmanaged, IEntityComponent
  29. {
  30. switch (coll)
  31. {
  32. case EntityCollection<TM> cm:
  33. {
  34. MB<TM> ma;
  35. (ma, managedIDs, count) = cm;
  36. if (ma is MB<T> mb)
  37. managedArray = mb;
  38. else
  39. throw new InvalidCastException("Expected managed buffer in managed entity collection! Wut");
  40. break;
  41. }
  42. case EntityCollection<TN> cn:
  43. {
  44. NB<TN> na;
  45. (na, nativeIDs, count) = cn;
  46. if (na is NB<T> nb)
  47. nativeArray = nb;
  48. else
  49. throw new InvalidCastException("Expected native buffer in native entity collection! Wut");
  50. break;
  51. }
  52. }
  53. }
  54. public static implicit operator RefCollection<T>(EntityCollection<T> coll)
  55. {
  56. return new RefCollection<T>(coll);
  57. }
  58. public Enumerator GetEnumerator() => new(this);
  59. public ref struct Enumerator
  60. {
  61. private RefCollection<T> collection;
  62. private uint index;
  63. public Enumerator(RefCollection<T> collection)
  64. {
  65. index = default;
  66. this.collection = collection;
  67. }
  68. public OptionalRef<T> Current => collection.coll.[index];
  69. public bool MoveNext()
  70. {
  71. return true;
  72. }
  73. }
  74. private static void Test<TN>(EntityCollection<TN> coll) where TN : unmanaged, IEntityComponent
  75. {
  76. }
  77. }
  78. }