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.3KB

  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("CHECK_ALL")]
  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 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(" operation was: ")
  28. .FastConcat(_multipleOperationOnSameEGIDChecker[egid] == 1 ? "add" : "remove"));
  29. if (_idChecker.TryGetValue(egid.groupID, out var hash))
  30. if (hash.Contains(egid.entityID) == false)
  31. throw new ECSException("Trying to remove an Entity never submitted in the database "
  32. .FastConcat(" caller: ", caller, " ").FastConcat(egid.entityID)
  33. .FastConcat(" groupid: ").FastConcat(egid.groupID.ToName())
  34. .FastConcat(" type: ")
  35. .FastConcat(entityDescriptorType != null
  36. ? entityDescriptorType.Name
  37. : "not available"));
  38. else
  39. hash.Remove(egid.entityID);
  40. _multipleOperationOnSameEGIDChecker.Add(egid, 0);
  41. }
  42. #if DONT_USE
  43. [Conditional("CHECK_ALL")]
  44. #endif
  45. void CheckAddEntityID(EGID egid, Type entityDescriptorType, string caller = "")
  46. {
  47. if (_multipleOperationOnSameEGIDChecker.ContainsKey(egid) == true)
  48. throw new ECSException(
  49. "Executing multiple structural changes in one submission on the same entity is not supported "
  50. .FastConcat(" caller: ", caller, " ").FastConcat(egid.entityID).FastConcat(" groupid: ")
  51. .FastConcat(egid.groupID.ToName()).FastConcat(" type: ")
  52. .FastConcat(entityDescriptorType != null ? entityDescriptorType.Name : "not available")
  53. .FastConcat(" operation was: ")
  54. .FastConcat(_multipleOperationOnSameEGIDChecker[egid] == 1 ? "add" : "remove"));
  55. var hash = _idChecker.GetOrCreate(egid.groupID, () => new HashSet<uint>());
  56. if (hash.Contains(egid.entityID) == true)
  57. throw new ECSException("Trying to add an Entity already submitted to the database "
  58. .FastConcat(" caller: ", caller, " ").FastConcat(egid.entityID)
  59. .FastConcat(" groupid: ").FastConcat(egid.groupID.ToName()).FastConcat(" type: ")
  60. .FastConcat(entityDescriptorType != null
  61. ? entityDescriptorType.Name
  62. : "not available"));
  63. hash.Add(egid.entityID);
  64. _multipleOperationOnSameEGIDChecker.Add(egid, 1);
  65. }
  66. #if DONT_USE
  67. [Conditional("CHECK_ALL")]
  68. #endif
  69. void RemoveGroupID(ExclusiveBuildGroup groupID) { _idChecker.Remove(groupID); }
  70. #if DONT_USE
  71. [Conditional("CHECK_ALL")]
  72. #endif
  73. void ClearChecks() { _multipleOperationOnSameEGIDChecker.FastClear(); }
  74. readonly FasterDictionary<EGID, uint> _multipleOperationOnSameEGIDChecker = new FasterDictionary<EGID, uint>();
  75. readonly FasterDictionary<uint, HashSet<uint>> _idChecker = new FasterDictionary<uint, HashSet<uint>>();
  76. }
  77. }