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.

84 lines
4.3KB

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