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.

117 lines
4.5KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Svelto.Common;
  4. using Svelto.DataStructures;
  5. using Svelto.DataStructures.Native;
  6. using Svelto.ECS.DataStructures;
  7. namespace Svelto.ECS.Native
  8. {
  9. /// <summary>
  10. /// Note: this class should really be ref struct by design. It holds the reference of a dictionary that can become
  11. /// invalid. Unfortunately it can be a ref struct, because Jobs needs to hold if by paramater. So the deal is
  12. /// that a job can use it as long as nothing else is modifying the entities database and the NativeEGIDMapper
  13. /// is disposed right after the use.
  14. /// </summary>
  15. public readonly struct NativeEGIDMapper<T> : IEGIDMapper where T : unmanaged, IEntityComponent
  16. {
  17. public static readonly NativeEGIDMapper<T> empty = new NativeEGIDMapper<T>
  18. (default, new SharedNative<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>,
  19. NativeStrategy<T>, NativeStrategy<int>>>(
  20. new SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>,
  21. NativeStrategy<T>, NativeStrategy<int>>(0, Allocator.Persistent)));
  22. public NativeEGIDMapper(ExclusiveGroupStruct groupStructId,
  23. in SharedNative<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  24. NativeStrategy<int>>> toNative) : this()
  25. {
  26. groupID = groupStructId;
  27. _map = toNative;
  28. }
  29. public int count => _map.value.count;
  30. public Type entityType => TypeCache<T>.type;
  31. public ExclusiveGroupStruct groupID { get; }
  32. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  33. public ref T Entity(uint entityID)
  34. {
  35. var sveltoDictionary = _map.value;
  36. #if DEBUG && !PROFILE_SVELTO
  37. if (sveltoDictionary.TryFindIndex(entityID, out var findIndex) == false)
  38. throw new Exception($"Entity {entityID} not found in this group {groupID} - {typeof(T).Name}");
  39. #else
  40. sveltoDictionary.TryFindIndex(entityID, out var findIndex);
  41. #endif
  42. return ref sveltoDictionary.GetDirectValueByRef(findIndex);
  43. }
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public bool TryGetEntity(uint entityID, out T value)
  46. {
  47. var sveltoDictionary = _map.value;
  48. if (sveltoDictionary.count > 0 && sveltoDictionary.TryFindIndex(entityID, out var index))
  49. {
  50. var values = sveltoDictionary.unsafeValues.ToRealBuffer();
  51. value = values[index];
  52. return true;
  53. }
  54. value = default;
  55. return false;
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public NB<T> GetArrayAndEntityIndex(uint entityID, out uint index)
  59. {
  60. var sveltoDictionary = _map.value;
  61. if (sveltoDictionary.TryFindIndex(entityID, out index))
  62. return sveltoDictionary.unsafeValues.ToRealBuffer();
  63. #if DEBUG && !PROFILE_SVELTO
  64. throw new ECSException("Entity not found");
  65. #else
  66. return default;
  67. #endif
  68. }
  69. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  70. public bool TryGetArrayAndEntityIndex(uint entityID, out uint index, out NB<T> array)
  71. {
  72. index = 0;
  73. var sveltoDictionary = _map.value;
  74. if (sveltoDictionary.count > 0 && sveltoDictionary.TryFindIndex(entityID, out index))
  75. {
  76. array = sveltoDictionary.unsafeValues.ToRealBuffer();
  77. return true;
  78. }
  79. array = default;
  80. return false;
  81. }
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. public bool Exists(uint idEntityId)
  84. {
  85. var sveltoDictionary = _map.value;
  86. return sveltoDictionary.count > 0 && sveltoDictionary.TryFindIndex(idEntityId, out _);
  87. }
  88. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  89. public uint GetIndex(uint entityID)
  90. {
  91. return _map.value.GetIndex(entityID);
  92. }
  93. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  94. public bool FindIndex(uint valueKey, out uint index)
  95. {
  96. return _map.value.TryFindIndex(valueKey, out index);
  97. }
  98. readonly SharedNative<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  99. NativeStrategy<int>>> _map;
  100. }
  101. }