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.

EnginesRoot.LocatorMap.cs 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System.Runtime.CompilerServices;
  2. using Svelto.Common;
  3. using Svelto.DataStructures;
  4. using Svelto.DataStructures.Native;
  5. using Svelto.ECS.DataStructures;
  6. using Svelto.ECS.Reference;
  7. namespace Svelto.ECS
  8. {
  9. // The EntityLocatorMap provides a bidirectional map to help locate entities without using an EGID which might
  10. // change in runtime. The Entity Locator map uses a reusable unique identifier struct called EntityLocator to
  11. // find the last known EGID from last entity submission.
  12. public partial class EnginesRoot
  13. {
  14. public struct LocatorMap
  15. {
  16. internal EntityReference ClaimReference()
  17. {
  18. int tempFreeIndex;
  19. int newFreeIndex;
  20. uint version;
  21. do
  22. {
  23. tempFreeIndex = _nextFreeIndex;
  24. // Check if we need to create a new EntityLocator or whether we can recycle an existing one.
  25. if ((uint)tempFreeIndex >= _entityReferenceMap.count)
  26. {
  27. newFreeIndex = tempFreeIndex + 1;
  28. version = 0;
  29. }
  30. else
  31. {
  32. ref EntityReferenceMapElement element = ref _entityReferenceMap[tempFreeIndex];
  33. // The recycle entities form a linked list, using the egid.entityID to store the next element.
  34. newFreeIndex = (int)element.egid.entityID;
  35. version = element.version;
  36. }
  37. } while (tempFreeIndex != _nextFreeIndex.CompareExchange(newFreeIndex, tempFreeIndex));
  38. #if DEBUG && !PROFILE_SVELTO
  39. // This code should be safe since we own the tempFreeIndex, this allows us to later check that nothing went wrong.
  40. if (tempFreeIndex < _entityReferenceMap.count)
  41. {
  42. _entityReferenceMap[tempFreeIndex] = new EntityReferenceMapElement(new EGID(0, 0), version);
  43. }
  44. #endif
  45. return new EntityReference((uint)tempFreeIndex + 1, version);
  46. }
  47. internal void SetReference(EntityReference reference, EGID egid)
  48. {
  49. // Since references can be claimed in parallel now, it might happen that they are set out of order,
  50. // so we need to resize instead of add.
  51. if (reference.index >= _entityReferenceMap.count)
  52. {
  53. #if DEBUG && !PROFILE_SVELTO //THIS IS TO VALIDATE DATE DBC LIKE
  54. for (var i = _entityReferenceMap.count; i <= reference.index; i++)
  55. {
  56. _entityReferenceMap.Add(new EntityReferenceMapElement(default, 0));
  57. }
  58. #else
  59. _entityReferenceMap.AddAt(reference.index);
  60. #endif
  61. }
  62. #if DEBUG && !PROFILE_SVELTO
  63. // These debug tests should be enough to detect if indices are being used correctly under native factories
  64. if (_entityReferenceMap[reference.index].version != reference.version ||
  65. _entityReferenceMap[reference.index].egid.groupID != ExclusiveGroupStruct.Invalid)
  66. {
  67. throw new ECSException("Entity reference already set. This should never happen, please report it.");
  68. }
  69. #endif
  70. _entityReferenceMap[reference.index] = new EntityReferenceMapElement(egid, reference.version);
  71. // Update reverse map from egid to locator.
  72. var groupMap =
  73. _egidToReferenceMap.GetOrCreate(egid.groupID
  74. , () => new SharedSveltoDictionaryNative<uint, EntityReference>(0));
  75. groupMap[egid.entityID] = reference;
  76. }
  77. internal void UpdateEntityReference(EGID from, EGID to)
  78. {
  79. var reference = FetchAndRemoveReference(@from);
  80. _entityReferenceMap[reference.index].egid = to;
  81. var groupMap =
  82. _egidToReferenceMap.GetOrCreate(
  83. to.groupID, () => new SharedSveltoDictionaryNative<uint, EntityReference>(0));
  84. groupMap[to.entityID] = reference;
  85. }
  86. internal void RemoveEntityReference(EGID egid)
  87. {
  88. var reference = FetchAndRemoveReference(@egid);
  89. // Invalidate the entity locator element by bumping its version and setting the egid to point to a not existing element.
  90. ref var entityReferenceMapElement = ref _entityReferenceMap[reference.index];
  91. entityReferenceMapElement.egid = new EGID((uint)(int)_nextFreeIndex, 0);
  92. entityReferenceMapElement.version++;
  93. // Mark the element as the last element used.
  94. _nextFreeIndex.Set((int)reference.index);
  95. }
  96. EntityReference FetchAndRemoveReference(EGID @from)
  97. {
  98. var egidToReference = _egidToReferenceMap[@from.groupID];
  99. var reference = egidToReference[@from.entityID];
  100. egidToReference.Remove(@from.entityID);
  101. return reference;
  102. }
  103. internal void RemoveAllGroupReferenceLocators(ExclusiveGroupStruct groupId)
  104. {
  105. if (_egidToReferenceMap.TryGetValue(groupId, out var groupMap) == false)
  106. return;
  107. // We need to traverse all entities in the group and remove the locator using the egid.
  108. // RemoveLocator would modify the enumerator so this is why we traverse the dictionary from last to first.
  109. foreach (var item in groupMap)
  110. RemoveEntityReference(new EGID(item.Key, groupId));
  111. _egidToReferenceMap.Remove(groupId);
  112. }
  113. internal void UpdateAllGroupReferenceLocators(ExclusiveGroupStruct fromGroupId, ExclusiveGroupStruct toGroupId)
  114. {
  115. if (_egidToReferenceMap.TryGetValue(fromGroupId, out var groupMap) == false)
  116. return;
  117. // We need to traverse all entities in the group and update the locator using the egid.
  118. // UpdateLocator would modify the enumerator so this is why we traverse the dictionary from last to first.
  119. foreach (var item in groupMap)
  120. UpdateEntityReference(new EGID(item.Key, fromGroupId), new EGID(item.Key, toGroupId));
  121. _egidToReferenceMap.Remove(fromGroupId);
  122. }
  123. public EntityReference GetEntityReference(EGID egid)
  124. {
  125. if (_egidToReferenceMap.TryGetValue(egid.groupID, out var groupMap))
  126. {
  127. if (groupMap.TryGetValue(egid.entityID, out var locator))
  128. return locator;
  129. #if DEBUG && !PROFILE_SVELTO
  130. else throw new ECSException($"Entity {egid} does not exist. Are you creating it? Try getting it from initializer.reference.");
  131. #endif
  132. }
  133. return EntityReference.Invalid;
  134. }
  135. public bool TryGetEGID(EntityReference reference, out EGID egid)
  136. {
  137. egid = default;
  138. if (reference == EntityReference.Invalid)
  139. return false;
  140. // Make sure we are querying for the current version of the locator.
  141. // Otherwise the locator is pointing to a removed entity.
  142. if (_entityReferenceMap[reference.index].version == reference.version)
  143. {
  144. egid = _entityReferenceMap[reference.index].egid;
  145. return true;
  146. }
  147. return false;
  148. }
  149. public EGID GetEGID(EntityReference reference)
  150. {
  151. if (reference == EntityReference.Invalid)
  152. throw new ECSException("Invalid Reference");
  153. // Make sure we are querying for the current version of the locator.
  154. // Otherwise the locator is pointing to a removed entity.
  155. if (_entityReferenceMap[reference.index].version != reference.version)
  156. throw new ECSException("outdated Reference");
  157. return _entityReferenceMap[reference.index].egid;
  158. }
  159. internal void PreallocateReferenceMaps(ExclusiveGroupStruct groupID, uint size)
  160. {
  161. _egidToReferenceMap
  162. .GetOrCreate(groupID, () => new SharedSveltoDictionaryNative<uint, EntityReference>(size))
  163. .ResizeTo(size);
  164. _entityReferenceMap.Resize(size);
  165. }
  166. internal void InitEntityReferenceMap()
  167. {
  168. _nextFreeIndex = SharedNativeInt.Create(0, Allocator.Persistent);
  169. _entityReferenceMap =
  170. new NativeDynamicArrayCast<EntityReferenceMapElement>(
  171. NativeDynamicArray.Alloc<EntityReferenceMapElement>());
  172. _egidToReferenceMap =
  173. new SharedSveltoDictionaryNative<ExclusiveGroupStruct,
  174. SharedSveltoDictionaryNative<uint, EntityReference>>(0);
  175. }
  176. internal void DisposeEntityReferenceMap()
  177. {
  178. _nextFreeIndex.Dispose();
  179. _entityReferenceMap.Dispose();
  180. foreach (var element in _egidToReferenceMap)
  181. element.Value.Dispose();
  182. _egidToReferenceMap.Dispose();
  183. }
  184. SharedNativeInt _nextFreeIndex;
  185. NativeDynamicArrayCast<EntityReferenceMapElement> _entityReferenceMap;
  186. SharedSveltoDictionaryNative<ExclusiveGroupStruct, SharedSveltoDictionaryNative<uint, EntityReference>>
  187. _egidToReferenceMap;
  188. }
  189. internal LocatorMap entityLocator => _entityLocator;
  190. LocatorMap _entityLocator;
  191. }
  192. }