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.

92 lines
2.9KB

  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 readonly ref struct RefCollection<T> where T : struct, IBaseEntityComponent
  9. {
  10. private readonly bool managed;
  11. private readonly int count;
  12. private readonly NB<T> nativeArray;
  13. private readonly MB<T> managedArray;
  14. private readonly NativeEntityIDs nativeIDs;
  15. private readonly ManagedEntityIDs managedIDs;
  16. private readonly ExclusiveGroupStruct group;
  17. public RefCollection(int count, MB<T> managedArray, ManagedEntityIDs managedIDs, ExclusiveGroupStruct group)
  18. {
  19. this.count = count;
  20. this.managedArray = managedArray;
  21. this.managedIDs = managedIDs;
  22. this.group = group;
  23. managed = true;
  24. nativeArray = default;
  25. nativeIDs = default;
  26. }
  27. public RefCollection(int count, NB<T> nativeArray, NativeEntityIDs nativeIDs, ExclusiveGroupStruct group)
  28. {
  29. this.count = count;
  30. this.nativeArray = nativeArray;
  31. this.nativeIDs = nativeIDs;
  32. this.group = group;
  33. managed = false;
  34. }
  35. public Enumerator GetEnumerator() => new(this);
  36. /// <summary>
  37. /// The amount of items in the collection.
  38. /// </summary>
  39. public int Count => count;
  40. public T[] ToArray() => ToArray(a => a.Component);
  41. public TA[] ToArray<TA>(Func<(T Component, EGID ID), TA> transformFunction, Predicate<(T Component, EGID ID)> predicateFunction = null)
  42. {
  43. var result = new TA[Count];
  44. int i = 0;
  45. foreach (var opt in this)
  46. {
  47. if (predicateFunction != null && !predicateFunction((opt.Get(), opt.EGID))) continue;
  48. result[i] = transformFunction((opt.Get(), opt.EGID));
  49. i++;
  50. }
  51. return result;
  52. }
  53. public ref struct Enumerator
  54. {
  55. private RefCollection<T> coll;
  56. private int index;
  57. public Enumerator(RefCollection<T> collection)
  58. {
  59. index = -1;
  60. coll = collection;
  61. }
  62. public OptionalRef<T> Current
  63. {
  64. get
  65. {
  66. if (coll.count <= index && index >= 0) return default;
  67. if (coll.managed)
  68. return new OptionalRef<T>(coll.managedArray, (uint)index,
  69. new EGID(coll.managedIDs[index], coll.group));
  70. return new OptionalRef<T>(coll.nativeArray, (uint)index,
  71. new EGID(coll.nativeIDs[index], coll.group));
  72. }
  73. }
  74. public bool MoveNext()
  75. {
  76. return ++index < coll.count;
  77. }
  78. }
  79. }
  80. }