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.

85 lines
4.4KB

  1. #if !DEBUG || PROFILE_SVELTO
  2. #define DONT_USE
  3. using System.Diagnostics;
  4. #endif
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Runtime.CompilerServices;
  8. using Svelto.DataStructures;
  9. namespace Svelto.ECS
  10. {
  11. /// <summary>
  12. /// Note: this check doesn't catch the case when an add and remove is done on the same entity before the nextI am
  13. /// submission. Two operations on the same entity are not allowed between submissions.
  14. /// </summary>
  15. public partial class EnginesRoot
  16. {
  17. #if DONT_USE
  18. [Conditional("CHECK_ALL")]
  19. #endif
  20. void CheckRemoveEntityID(EGID egid, Type entityDescriptorType, [CallerMemberName] string caller = null)
  21. {
  22. if (_multipleOperationOnSameEGIDChecker.ContainsKey(egid) == true)
  23. throw new ECSException(
  24. "Executing multiple structural changes (remove) in one submission on the same entity is not supported "
  25. .FastConcat(" caller: ", caller, " ").FastConcat(egid.entityID).FastConcat(" groupid: ")
  26. .FastConcat(egid.groupID.ToName()).FastConcat(" type: ")
  27. .FastConcat(entityDescriptorType != null ? entityDescriptorType.Name : "not available")
  28. .FastConcat(" previous operation was: ")
  29. .FastConcat(_multipleOperationOnSameEGIDChecker[egid] == 1 ? "add" : "remove"));
  30. if (_idChecker.TryGetValue(egid.groupID, out var hash))
  31. if (hash.Contains(egid.entityID) == false)
  32. throw new ECSException("Trying to remove an Entity never submitted in the database "
  33. .FastConcat(" caller: ", caller, " ").FastConcat(egid.entityID)
  34. .FastConcat(" groupid: ").FastConcat(egid.groupID.ToName())
  35. .FastConcat(" type: ")
  36. .FastConcat(entityDescriptorType != null
  37. ? entityDescriptorType.Name
  38. : "not available"));
  39. else
  40. hash.Remove(egid.entityID);
  41. _multipleOperationOnSameEGIDChecker.Add(egid, 0);
  42. }
  43. #if DONT_USE
  44. [Conditional("CHECK_ALL")]
  45. #endif
  46. void CheckAddEntityID(EGID egid, Type entityDescriptorType, [CallerMemberName] string caller = null)
  47. {
  48. if (_multipleOperationOnSameEGIDChecker.ContainsKey(egid) == true)
  49. throw new ECSException(
  50. "Executing multiple structural changes (build) on the same entity is not supported "
  51. .FastConcat(" caller: ", caller, " ").FastConcat(egid.entityID).FastConcat(" groupid: ")
  52. .FastConcat(egid.groupID.ToName()).FastConcat(" type: ")
  53. .FastConcat(entityDescriptorType != null ? entityDescriptorType.Name : "not available")
  54. .FastConcat(" previous operation was: ")
  55. .FastConcat(_multipleOperationOnSameEGIDChecker[egid] == 1 ? "add" : "remove"));
  56. var hash = _idChecker.GetOrCreate(egid.groupID, () => new HashSet<uint>());
  57. if (hash.Contains(egid.entityID) == true)
  58. throw new ECSException("Trying to add an Entity already submitted to the database "
  59. .FastConcat(" caller: ", caller, " ").FastConcat(egid.entityID)
  60. .FastConcat(" groupid: ").FastConcat(egid.groupID.ToName()).FastConcat(" type: ")
  61. .FastConcat(entityDescriptorType != null
  62. ? entityDescriptorType.Name
  63. : "not available"));
  64. hash.Add(egid.entityID);
  65. _multipleOperationOnSameEGIDChecker.Add(egid, 1);
  66. }
  67. #if DONT_USE
  68. [Conditional("CHECK_ALL")]
  69. #endif
  70. void RemoveGroupID(ExclusiveBuildGroup groupID) { _idChecker.Remove(groupID); }
  71. #if DONT_USE
  72. [Conditional("CHECK_ALL")]
  73. #endif
  74. void ClearChecks() { _multipleOperationOnSameEGIDChecker.FastClear(); }
  75. readonly FasterDictionary<EGID, uint> _multipleOperationOnSameEGIDChecker = new FasterDictionary<EGID, uint>();
  76. readonly FasterDictionary<ExclusiveGroupStruct, HashSet<uint>> _idChecker = new FasterDictionary<ExclusiveGroupStruct, HashSet<uint>>();
  77. }
  78. }