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.

96 lines
2.8KB

  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Svelto.ECS.Experimental
  4. {
  5. [Serialization.DoNotSerialize]
  6. [StructLayout(LayoutKind.Explicit)]
  7. ///
  8. /// Note: I should extend this to reuse unused id
  9. ///
  10. //todo ResourcesECSDB<T> must be used only inside entity components. Same for ECSString.
  11. //what I could do is that if the component is removed from the database, a reference counter to the object
  12. //will be modified. If 0 is reached, the ID should be recycled.
  13. public struct ECSString:IEquatable<ECSString>
  14. {
  15. [FieldOffset(0)] uint _id;
  16. [FieldOffset(4)] uint _versioning;
  17. [FieldOffset(0)] long _realID;
  18. public ECSString(string newText):this()
  19. {
  20. _id = ResourcesECSDB<string>.ToECS(newText);
  21. }
  22. ECSString(uint id):this()
  23. {
  24. _id = id;
  25. }
  26. public static implicit operator string(ECSString ecsString)
  27. {
  28. return ResourcesECSDB<string>.FromECS(ecsString._id);
  29. }
  30. /// <summary>
  31. /// Note: Setting null String could be a good way to signal a disposing of the ID so that
  32. /// it can be recycled.
  33. /// Zero id must be a null string
  34. /// </summary>
  35. /// <param name="newText"></param>
  36. public void Set(string newText)
  37. {
  38. if (_id != 0)
  39. {
  40. if (ResourcesECSDB<string>.resources(_id).Equals(newText) == false)
  41. {
  42. ResourcesECSDB<string>.resources(_id) = newText;
  43. _versioning++;
  44. }
  45. }
  46. else
  47. _id = ResourcesECSDB<string>.ToECS(newText);
  48. }
  49. public ECSString Copy()
  50. {
  51. DBC.ECS.Check.Require(_id != 0, "copying not initialized string");
  52. var id = ResourcesECSDB<string>.ToECS(ResourcesECSDB<string>.resources(_id));
  53. return new ECSString(id);
  54. }
  55. public override string ToString()
  56. {
  57. return ResourcesECSDB<string>.FromECS(_id);
  58. }
  59. public bool Equals(ECSString other)
  60. {
  61. return _realID == other._realID;
  62. }
  63. public static bool operator==(ECSString options1, ECSString options2)
  64. {
  65. return options1._realID == options2._realID;
  66. }
  67. public static bool operator!=(ECSString options1, ECSString options2)
  68. {
  69. return options1._realID != options2._realID;
  70. }
  71. public override bool Equals(object obj)
  72. {
  73. throw new NotSupportedException(); //this is on purpose
  74. }
  75. public override int GetHashCode()
  76. {
  77. return _realID.GetHashCode();
  78. }
  79. }
  80. }