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.

94 lines
4.6KB

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