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.

71 lines
2.2KB

  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. public ref struct Enumerator
  37. {
  38. private RefCollection<T> coll;
  39. private int index;
  40. public Enumerator(RefCollection<T> collection)
  41. {
  42. index = -1;
  43. coll = collection;
  44. }
  45. public OptionalRef<T> Current
  46. {
  47. get
  48. {
  49. if (coll.count <= index && index >= 0) return default;
  50. if (coll.managed)
  51. return new OptionalRef<T>(coll.managedArray, (uint)index,
  52. new EGID(coll.managedIDs[index], coll.group));
  53. return new OptionalRef<T>(coll.nativeArray, (uint)index,
  54. new EGID(coll.nativeIDs[index], coll.group));
  55. }
  56. }
  57. public bool MoveNext()
  58. {
  59. return ++index < coll.count;
  60. }
  61. }
  62. }
  63. }