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.

92 lines
2.6KB

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