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.

TypeSafeDictionaryMethods.cs 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using DBC.ECS;
  4. using Svelto.Common;
  5. using Svelto.DataStructures;
  6. namespace Svelto.ECS.Internal
  7. {
  8. public static class TypeSafeDictionaryMethods
  9. {
  10. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  11. public static void AddEntitiesToDictionary<Strategy1, Strategy2, Strategy3, TValue>(
  12. in SveltoDictionary<uint, TValue, Strategy1, Strategy2, Strategy3> fromDictionary
  13. , ITypeSafeDictionary<TValue> toDic
  14. #if SLOW_SVELTO_SUBMISSION
  15. , in EnginesRoot.EntityReferenceMap entityLocator
  16. #endif
  17. , ExclusiveGroupStruct toGroupID)
  18. where Strategy1 : struct, IBufferStrategy<SveltoDictionaryNode<uint>>
  19. where Strategy2 : struct, IBufferStrategy<TValue>
  20. where Strategy3 : struct, IBufferStrategy<int>
  21. where TValue : struct, _IInternalEntityComponent
  22. {
  23. foreach (var tuple in fromDictionary)
  24. {
  25. #if SLOW_SVELTO_SUBMISSION
  26. var egid = new EGID(tuple.key, toGroupID);
  27. if (SlowSubmissionInfo<TValue>.hasEgid)
  28. SetEGIDWithoutBoxing<TValue>.SetIDWithoutBoxing(ref tuple.value, egid);
  29. if (SlowSubmissionInfo<TValue>.hasReference)
  30. SetEGIDWithoutBoxing<TValue>.SetRefWithoutBoxing(
  31. ref tuple.value,
  32. entityLocator.GetEntityReference(egid));
  33. #endif
  34. #if DEBUG && !PROFILE_SVELTO
  35. try
  36. {
  37. #endif
  38. toDic.Add(tuple.key, tuple.value);
  39. #if DEBUG && !PROFILE_SVELTO
  40. }
  41. catch (Exception e)
  42. {
  43. Console.LogException(
  44. e,
  45. "trying to add an EntityComponent with the same ID more than once Entity: ".FastConcat(typeof(TValue).ToString())
  46. .FastConcat(", group ").FastConcat(toGroupID.ToName()).FastConcat(", id ").FastConcat(tuple.key));
  47. throw;
  48. }
  49. #endif
  50. #if PARANOID_CHECK && SLOW_SVELTO_SUBMISSION
  51. DBC.ECS.Check.Ensure(_hasEgid == false || ((INeedEGID)fromDictionary[egid.entityID]).ID == egid, "impossible situation happened during swap");
  52. #endif
  53. }
  54. }
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. public static void ExecuteEnginesAddCallbacks<Strategy1, Strategy2, Strategy3, TValue>(
  57. ref SveltoDictionary<uint, TValue, Strategy1, Strategy2, Strategy3> fromDictionary
  58. , ITypeSafeDictionary<TValue> todic, ExclusiveGroupStruct togroup
  59. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnAdd>>> entitycomponentenginesdb
  60. , in PlatformProfiler sampler)
  61. where Strategy1 : struct, IBufferStrategy<SveltoDictionaryNode<uint>>
  62. where Strategy2 : struct, IBufferStrategy<TValue>
  63. where Strategy3 : struct, IBufferStrategy<int>
  64. where TValue : struct, _IInternalEntityComponent
  65. {
  66. if (entitycomponentenginesdb.TryGetValue(
  67. ComponentTypeID<TValue>.id
  68. , out var entityComponentsEngines))
  69. {
  70. if (entityComponentsEngines.count == 0)
  71. return;
  72. var dictionaryKeyEnumerator = fromDictionary.unsafeKeys;
  73. var count = fromDictionary.count;
  74. for (var i = 0; i < count; ++i)
  75. try
  76. {
  77. var key = dictionaryKeyEnumerator[i].key;
  78. ref var entity = ref todic.GetValueByRef(key);
  79. var egid = new EGID(key, togroup);
  80. //get all the engines linked to TValue
  81. for (var j = 0; j < entityComponentsEngines.count; j++)
  82. using (sampler.Sample(entityComponentsEngines[j].name))
  83. {
  84. #pragma warning disable CS0612
  85. ((IReactOnAdd<TValue>)entityComponentsEngines[j].engine).Add(ref entity, egid);
  86. #pragma warning restore CS0612
  87. }
  88. }
  89. catch (Exception e)
  90. {
  91. Console.LogException(e, "Code crashed inside Add callback with Type ".FastConcat(TypeCache<TValue>.name));
  92. throw;
  93. }
  94. }
  95. }
  96. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  97. public static void ExecuteEnginesDisposeCallbacks_Group<Strategy1, Strategy2, Strategy3, TValue>(
  98. ref SveltoDictionary<uint, TValue, Strategy1, Strategy2, Strategy3> fromDictionary
  99. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnDispose>>> reactiveEnginesDispose
  100. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnDisposeEx>>> reactiveEnginesDisposeEx
  101. , IEntityIDs entityids, ExclusiveGroupStruct group, in PlatformProfiler sampler)
  102. where Strategy1 : struct, IBufferStrategy<SveltoDictionaryNode<uint>>
  103. where Strategy2 : struct, IBufferStrategy<TValue>
  104. where Strategy3 : struct, IBufferStrategy<int>
  105. where TValue : struct, _IInternalEntityComponent
  106. {
  107. if (reactiveEnginesDispose.TryGetValue(ComponentTypeID<TValue>.id, out var entityComponentsEngines) == true)
  108. {
  109. var resultCount = entityComponentsEngines.count;
  110. for (var i = 0; i < resultCount; i++)
  111. try
  112. {
  113. using (sampler.Sample(entityComponentsEngines[i].name))
  114. {
  115. foreach (var value in fromDictionary)
  116. {
  117. ref var entity = ref value.value;
  118. var egid = new EGID(value.key, group);
  119. #pragma warning disable CS0618
  120. var reactOnRemove = (IReactOnDispose<TValue>)entityComponentsEngines[i].engine;
  121. #pragma warning restore CS0618
  122. reactOnRemove.Remove(ref entity, egid);
  123. }
  124. }
  125. }
  126. catch
  127. {
  128. Console.LogError("Code crashed inside Remove callback ".FastConcat(entityComponentsEngines[i].name));
  129. throw;
  130. }
  131. }
  132. if (reactiveEnginesDisposeEx.TryGetValue(ComponentTypeID<TValue>.id, out var reactiveEnginesDisposeExPerType))
  133. {
  134. var count = fromDictionary.count;
  135. var enginesCount = reactiveEnginesDisposeExPerType.count;
  136. if (count > 0)
  137. {
  138. for (var i = 0; i < enginesCount; i++)
  139. {
  140. try
  141. {
  142. using (sampler.Sample(reactiveEnginesDisposeExPerType[i].name))
  143. {
  144. ((IReactOnDisposeEx<TValue>)reactiveEnginesDisposeExPerType[i].engine).Remove(
  145. (0, (uint)count)
  146. , new EntityCollection<TValue>(fromDictionary.UnsafeGetValues(out _), entityids, (uint)count), group);
  147. }
  148. }
  149. catch
  150. {
  151. Console.LogError("Code crashed inside Remove callback ".FastConcat(reactiveEnginesDisposeExPerType[i].name));
  152. throw;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  159. public static void ExecuteEnginesRemoveCallbacks<Strategy1, Strategy2, Strategy3, TValue>(FasterList<(uint, string)> infostoprocess
  160. , ref SveltoDictionary<uint, TValue, Strategy1, Strategy2, Strategy3> fromDictionary
  161. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnRemove>>> reactiveenginesremove
  162. , ExclusiveGroupStruct fromgroup, in PlatformProfiler profiler)
  163. where Strategy1 : struct, IBufferStrategy<SveltoDictionaryNode<uint>>
  164. where Strategy2 : struct, IBufferStrategy<TValue>
  165. where Strategy3 : struct, IBufferStrategy<int>
  166. where TValue : struct, _IInternalEntityComponent
  167. {
  168. if (reactiveenginesremove.TryGetValue(
  169. ComponentTypeID<TValue>.id
  170. , out var entityComponentsEngines))
  171. {
  172. if (entityComponentsEngines.count == 0)
  173. return;
  174. var iterations = infostoprocess.count;
  175. for (var i = 0; i < iterations; i++)
  176. {
  177. var (entityID, trace) = infostoprocess[i];
  178. try
  179. {
  180. ref var entity = ref fromDictionary.GetValueByRef(entityID);
  181. var egid = new EGID(entityID, fromgroup);
  182. for (var j = 0; j < entityComponentsEngines.count; j++)
  183. using (profiler.Sample(entityComponentsEngines[j].name))
  184. {
  185. #pragma warning disable CS0612
  186. ((IReactOnRemove<TValue>)entityComponentsEngines[j].engine).Remove(ref entity, egid);
  187. #pragma warning restore CS0612
  188. }
  189. }
  190. catch
  191. {
  192. var str = "Crash while executing Remove Entity callback on ".FastConcat(TypeCache<TValue>.name)
  193. .FastConcat(" from : ", trace);
  194. Console.LogError(str);
  195. throw;
  196. }
  197. }
  198. }
  199. }
  200. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  201. public static void ExecuteEnginesRemoveCallbacks_Group<Strategy1, Strategy2, Strategy3, TValue>(
  202. ref SveltoDictionary<uint, TValue, Strategy1, Strategy2, Strategy3> fromDictionary
  203. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnRemove>>> reactiveenginesremove
  204. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnRemoveEx>>> reactiveenginesremoveex
  205. , IEntityIDs entityids, ExclusiveGroupStruct group, in PlatformProfiler sampler)
  206. where Strategy1 : struct, IBufferStrategy<SveltoDictionaryNode<uint>>
  207. where Strategy2 : struct, IBufferStrategy<TValue>
  208. where Strategy3 : struct, IBufferStrategy<int>
  209. where TValue : struct, _IInternalEntityComponent
  210. {
  211. if (reactiveenginesremove.TryGetValue(
  212. ComponentTypeID<TValue>.id
  213. , out var reactiveEnginesRemovePerType))
  214. {
  215. var enginesCount = reactiveEnginesRemovePerType.count;
  216. for (var i = 0; i < enginesCount; i++)
  217. try
  218. {
  219. foreach (var value in fromDictionary)
  220. {
  221. ref var entity = ref value.value;
  222. var egid = new EGID(value.key, group);
  223. using (sampler.Sample(reactiveEnginesRemovePerType[i].name))
  224. {
  225. #pragma warning disable CS0612
  226. ((IReactOnRemove<TValue>)reactiveEnginesRemovePerType[i].engine).Remove(
  227. #pragma warning restore CS0612
  228. ref entity, egid);
  229. }
  230. }
  231. }
  232. catch
  233. {
  234. Console.LogError("Code crashed inside Remove callback ".FastConcat(reactiveEnginesRemovePerType[i].name));
  235. throw;
  236. }
  237. }
  238. if (reactiveenginesremoveex.TryGetValue(
  239. ComponentTypeID<TValue>.id
  240. , out var reactiveEnginesRemoveExPerType))
  241. {
  242. var count = fromDictionary.count;
  243. var enginesCount = reactiveEnginesRemoveExPerType.count;
  244. for (var i = 0; i < enginesCount; i++)
  245. try
  246. {
  247. using (sampler.Sample(reactiveEnginesRemoveExPerType[i].name))
  248. {
  249. ((IReactOnRemoveEx<TValue>)reactiveEnginesRemoveExPerType[i].engine).Remove(
  250. (0, (uint)count)
  251. , new EntityCollection<TValue>(
  252. fromDictionary.UnsafeGetValues(out _), entityids
  253. , (uint)count), group);
  254. }
  255. }
  256. catch
  257. {
  258. Console.LogError("Code crashed inside Remove callback ".FastConcat(reactiveEnginesRemoveExPerType[i].name));
  259. throw;
  260. }
  261. }
  262. }
  263. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  264. public static void ExecuteEnginesSwapCallbacks<Strategy1, Strategy2, Strategy3, TValue>(FasterDictionary<uint, SwapInfo> infostoprocess
  265. , ref SveltoDictionary<uint, TValue, Strategy1, Strategy2, Strategy3> fromDictionary
  266. , FasterList<ReactEngineContainer<IReactOnSwap>> reactiveenginesswap, ExclusiveGroupStruct togroup
  267. , ExclusiveGroupStruct fromgroup, in PlatformProfiler sampler)
  268. where Strategy1 : struct, IBufferStrategy<SveltoDictionaryNode<uint>>
  269. where Strategy2 : struct, IBufferStrategy<TValue>
  270. where Strategy3 : struct, IBufferStrategy<int>
  271. where TValue : struct, _IInternalEntityComponent
  272. {
  273. if (reactiveenginesswap.count == 0)
  274. return;
  275. var iterations = infostoprocess.count;
  276. var infostoprocessUnsafeValues = infostoprocess.unsafeValues;
  277. for (var i = 0; i < iterations; i++)
  278. {
  279. var (fromEntityID, toEntityID, trace) = infostoprocessUnsafeValues[i];
  280. try
  281. {
  282. ref var entityComponent = ref fromDictionary.GetValueByRef(toEntityID);
  283. var newEgid = new EGID(toEntityID, togroup);
  284. for (var j = 0; j < reactiveenginesswap.count; j++)
  285. using (sampler.Sample(reactiveenginesswap[j].name))
  286. {
  287. #pragma warning disable CS0612
  288. #pragma warning disable CS0618
  289. ((IReactOnSwap<TValue>)reactiveenginesswap[j].engine).MovedTo(
  290. #pragma warning restore CS0618
  291. #pragma warning restore CS0612
  292. ref entityComponent, fromgroup, newEgid);
  293. }
  294. }
  295. catch
  296. {
  297. var str = "Crash while executing Swap Entity callback on ".FastConcat(TypeCache<TValue>.name)
  298. .FastConcat(" from : ", trace);
  299. Console.LogError(str);
  300. throw;
  301. }
  302. }
  303. }
  304. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  305. public static void ExecuteEnginesSwapCallbacks_Group<Strategy1, Strategy2, Strategy3, TValue>(
  306. ref SveltoDictionary<uint, TValue, Strategy1, Strategy2, Strategy3> fromDictionary
  307. , ITypeSafeDictionary<TValue> toDic, ExclusiveGroupStruct togroup, ExclusiveGroupStruct fromgroup
  308. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnSwap>>> reactiveenginesswap
  309. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnSwapEx>>> reactiveenginesswapex
  310. , IEntityIDs entityids, in PlatformProfiler sampler)
  311. where Strategy1 : struct, IBufferStrategy<SveltoDictionaryNode<uint>>
  312. where Strategy2 : struct, IBufferStrategy<TValue>
  313. where Strategy3 : struct, IBufferStrategy<int>
  314. where TValue : struct, _IInternalEntityComponent
  315. {
  316. //get all the engines linked to TValue
  317. if (!reactiveenginesswap.TryGetValue(
  318. ComponentTypeID<TValue>.id
  319. , out var reactiveEnginesSwapPerType))
  320. return;
  321. var componentsEnginesCount = reactiveEnginesSwapPerType.count;
  322. for (var i = 0; i < componentsEnginesCount; i++)
  323. try
  324. {
  325. foreach (var value in fromDictionary)
  326. {
  327. ref var entityComponent = ref toDic.GetValueByRef(value.key);
  328. var newEgid = new EGID(value.key, togroup);
  329. using (sampler.Sample(reactiveEnginesSwapPerType[i].name))
  330. {
  331. #pragma warning disable CS0612
  332. #pragma warning disable CS0618
  333. ((IReactOnSwap<TValue>)reactiveEnginesSwapPerType[i].engine).MovedTo(
  334. #pragma warning restore CS0618
  335. #pragma warning restore CS0612
  336. ref entityComponent, fromgroup, newEgid);
  337. }
  338. }
  339. }
  340. catch (Exception)
  341. {
  342. Console.LogError("Code crashed inside MoveTo callback ".FastConcat(reactiveEnginesSwapPerType[i].name));
  343. throw;
  344. }
  345. if (reactiveenginesswapex.TryGetValue(
  346. ComponentTypeID<TValue>.id
  347. , out var reactiveEnginesRemoveExPerType))
  348. {
  349. var enginesCount = reactiveEnginesRemoveExPerType.count;
  350. var count = fromDictionary.count;
  351. for (var i = 0; i < enginesCount; i++)
  352. try
  353. {
  354. using (sampler.Sample(reactiveEnginesRemoveExPerType[i].name))
  355. {
  356. ((IReactOnSwapEx<TValue>)reactiveEnginesRemoveExPerType[i].engine).MovedTo(
  357. (0, (uint)count)
  358. , new EntityCollection<TValue>(
  359. fromDictionary.UnsafeGetValues(out _), entityids
  360. , (uint)count), fromgroup, togroup);
  361. }
  362. }
  363. catch
  364. {
  365. Console.LogError("Code crashed inside Remove callback ".FastConcat(reactiveEnginesRemoveExPerType[i].name));
  366. throw;
  367. }
  368. }
  369. }
  370. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  371. public static void RemoveEntitiesFromDictionary<Strategy1, Strategy2, Strategy3, TValue>(FasterList<(uint, string)> infostoprocess
  372. , ref SveltoDictionary<uint, TValue, Strategy1, Strategy2, Strategy3> fromDictionary
  373. , FasterDictionary<uint, uint> entityIDsAffectedByRemoveAtSwapBack)
  374. where Strategy1 : struct, IBufferStrategy<SveltoDictionaryNode<uint>>
  375. where Strategy2 : struct, IBufferStrategy<TValue>
  376. where Strategy3 : struct, IBufferStrategy<int>
  377. where TValue : struct, _IInternalEntityComponent
  378. {
  379. var iterations = infostoprocess.count;
  380. for (var i = 0; i < iterations; i++)
  381. {
  382. var (id, trace) = infostoprocess[i];
  383. #if DEBUG && !PROFILE_SVELTO
  384. try
  385. {
  386. #endif
  387. if (fromDictionary.Remove(id, out var index, out var value))
  388. {
  389. //Note I am doing this to be able to use a range of values even with the
  390. //remove Ex callbacks. Basically I am copying back the deleted value
  391. //at the end of the array, so I can use as range count, count + number of deleted entities
  392. //I need to swap the keys too to have matching EntityIDs
  393. var fromDictionaryCount = fromDictionary.count;
  394. var fromDictionaryUnsafeKeys = fromDictionary.unsafeKeys;
  395. if (index != fromDictionaryCount)
  396. {
  397. fromDictionary.unsafeValues[(uint)fromDictionaryCount] = value;
  398. fromDictionaryUnsafeKeys[(uint)fromDictionaryCount] = new SveltoDictionaryNode<uint>(id, 0);
  399. }
  400. //when a component is removed from a component array, a remove swap back happens. This means
  401. //that not only we have to remove the index of the component of the entity deleted from the array
  402. //but we need also to update the index of the component that has been swapped in the cell
  403. //of the deleted component
  404. //entityIDsAffectedByRemoval tracks all the entitiesID of the components that need to be updated
  405. //in the filters because their indices in the array changed.
  406. entityIDsAffectedByRemoveAtSwapBack[fromDictionaryUnsafeKeys[index].key] = index;
  407. }
  408. #if DEBUG && !PROFILE_SVELTO
  409. }
  410. catch
  411. {
  412. var str = "Crash while executing Remove Entity operation on ".FastConcat(TypeCache<TValue>.name)
  413. .FastConcat(" from : ", trace);
  414. Console.LogError(str);
  415. throw;
  416. }
  417. #endif
  418. }
  419. }
  420. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  421. public static void SwapEntitiesBetweenDictionaries<Strategy1, Strategy2, Strategy3, TValue>(in FasterDictionary<uint, SwapInfo> entitiesIDsToSwap
  422. , ref SveltoDictionary<uint, TValue, Strategy1, Strategy2, Strategy3> fromDictionary
  423. , ITypeSafeDictionary<TValue> toDictionary, ExclusiveGroupStruct fromgroup, ExclusiveGroupStruct togroup
  424. , FasterDictionary<uint, uint> entityIDsAffectedByRemoveAtSwapBack)
  425. where Strategy1 : struct, IBufferStrategy<SveltoDictionaryNode<uint>>
  426. where Strategy2 : struct, IBufferStrategy<TValue>
  427. where Strategy3 : struct, IBufferStrategy<int>
  428. where TValue : struct, _IInternalEntityComponent
  429. {
  430. var iterations = entitiesIDsToSwap.count;
  431. var entitiesToSwapInfo = entitiesIDsToSwap.unsafeValues;
  432. var fromDictionaryUnsafeKeys = fromDictionary.unsafeKeys;
  433. for (var i = 0; i < iterations; i++)
  434. {
  435. ref SwapInfo swapInfo = ref entitiesToSwapInfo[i];
  436. #if DEBUG && !PROFILE_SVELTO
  437. try
  438. {
  439. #endif
  440. var fromEntityGid = new EGID(swapInfo.fromID, fromgroup);
  441. var toEntityEgid = new EGID(swapInfo.toID, togroup);
  442. Check.Require(togroup.isInvalid == false, "Invalid To Group");
  443. if (fromDictionary.Remove(fromEntityGid.entityID, out var index, out var value))
  444. entityIDsAffectedByRemoveAtSwapBack[fromDictionaryUnsafeKeys[index].key] = index; //after the removal, the entity ad index is the entity that was at the end of the buffer (swapped back).
  445. else
  446. Check.Assert(false, "Swapping an entity that doesn't exist");
  447. #if SLOW_SVELTO_SUBMISSION
  448. if (SlowSubmissionInfo<TValue>.hasEgid)
  449. SetEGIDWithoutBoxing<TValue>.SetIDWithoutBoxing(ref value, toEntityEgid);
  450. #endif
  451. swapInfo.toIndex = toDictionary.Add(toEntityEgid.entityID, value);
  452. #if PARANOID_CHECK
  453. DBC.ECS.Check.Ensure(_hasEgid == false || ((INeedEGID)toGroupCasted[toEntityEGID.entityID]).ID == toEntityEGID, "impossible situation happened during swap");
  454. #endif
  455. #if DEBUG && !PROFILE_SVELTO
  456. }
  457. catch
  458. {
  459. var str = "Crash while executing Swap Entity operation on ".FastConcat(TypeCache<TValue>.name)
  460. .FastConcat(" from : ", swapInfo.trace);
  461. Console.LogError(str);
  462. throw;
  463. }
  464. #endif
  465. }
  466. }
  467. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  468. public static void ExecuteEnginesAddEntityCallbacksFast<TValue>(
  469. FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnAddEx>>> fasterDictionary
  470. , ExclusiveGroupStruct groupId, (uint, uint) rangeTuple, IEntityIDs entityids
  471. , ITypeSafeDictionary<TValue> typeSafeDictionary, PlatformProfiler profiler)
  472. where TValue : struct, _IInternalEntityComponent
  473. {
  474. //get all the engines linked to TValue
  475. if (!fasterDictionary.TryGetValue(ComponentTypeID<TValue>.id, out var entityComponentsEngines))
  476. return;
  477. for (var i = 0; i < entityComponentsEngines.count; i++)
  478. try
  479. {
  480. using (profiler.Sample(entityComponentsEngines[i].name))
  481. {
  482. ((IReactOnAddEx<TValue>)entityComponentsEngines[i].engine).Add(
  483. rangeTuple
  484. , new EntityCollection<TValue>(typeSafeDictionary.GetValues(out var count), entityids, count)
  485. , groupId);
  486. }
  487. }
  488. catch (Exception e)
  489. {
  490. Console.LogException(e, "Code crashed inside Add callback ".FastConcat(entityComponentsEngines[i].name));
  491. throw;
  492. }
  493. }
  494. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  495. public static void ExecuteEnginesSwapCallbacksFast<TValue>(
  496. FasterList<ReactEngineContainer<IReactOnSwapEx>> callbackEngines,
  497. ExclusiveGroupStruct fromGroup, ExclusiveGroupStruct toGroup, IEntityIDs entityids, ITypeSafeDictionary<TValue> typeSafeDictionary
  498. , (uint, uint) rangeOfSubmittedEntitiesIndicies, PlatformProfiler sampler)
  499. where TValue : struct, _IInternalEntityComponent
  500. {
  501. for (var i = 0; i < callbackEngines.count; i++)
  502. try
  503. {
  504. using (sampler.Sample(callbackEngines[i].name))
  505. {
  506. var values = typeSafeDictionary.GetValues(out var count);
  507. ((IReactOnSwapEx<TValue>)callbackEngines[i].engine).MovedTo(
  508. rangeOfSubmittedEntitiesIndicies, new EntityCollection<TValue>(values, entityids, count), fromGroup, toGroup);
  509. }
  510. }
  511. catch (Exception e)
  512. {
  513. Console.LogException(e, "Code crashed inside Add callback ".FastConcat(callbackEngines[i].name));
  514. throw;
  515. }
  516. }
  517. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  518. public static void ExecuteEnginesRemoveCallbacksFast<TValue>(FasterList<ReactEngineContainer<IReactOnRemoveEx>> fasterList,
  519. ExclusiveGroupStruct exclusiveGroupStruct
  520. , (uint, uint) valueTuple, IEntityIDs entityids, ITypeSafeDictionary<TValue> typeSafeDictionary
  521. , PlatformProfiler sampler)
  522. where TValue : struct, _IInternalEntityComponent
  523. {
  524. for (var i = 0; i < fasterList.count; i++)
  525. try
  526. {
  527. using (sampler.Sample(fasterList[i].name))
  528. {
  529. ((IReactOnRemoveEx<TValue>)fasterList[i].engine).Remove(
  530. valueTuple
  531. , new EntityCollection<TValue>(typeSafeDictionary.GetValues(out var count), entityids, count)
  532. , exclusiveGroupStruct);
  533. }
  534. }
  535. catch (Exception e)
  536. {
  537. Console.LogException(e, "Code crashed inside Add callback ".FastConcat(fasterList[i].name));
  538. throw;
  539. }
  540. }
  541. }
  542. }