A stable modding interface between Techblox and mods https://mod.exmods.org/
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.

114 lines
3.7KB

  1. using System;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS;
  4. namespace TechbloxModdingAPI.Utility
  5. {
  6. public ref struct OptionalRef<T> where T : struct, IBaseEntityComponent
  7. {
  8. private readonly EGID entityId;
  9. private readonly State state;
  10. private readonly uint index;
  11. private NB<T> array;
  12. private MB<T> managedArray;
  13. private readonly EntityInitializer initializer;
  14. //The possible fields are: (index && (array || managedArray)) || initializer
  15. public OptionalRef(NB<T> array, uint index, EGID entityId = default)
  16. {
  17. state = State.Native;
  18. this.array = array;
  19. this.index = index;
  20. this.entityId = entityId;
  21. initializer = default;
  22. }
  23. public OptionalRef(MB<T> array, uint index, EGID entityId = default)
  24. {
  25. state = State.Managed;
  26. managedArray = array;
  27. this.index = index;
  28. this.entityId = entityId;
  29. initializer = default;
  30. this.array = default;
  31. }
  32. /// <summary>
  33. /// Wraps the initializer data, if present.
  34. /// </summary>
  35. /// <param name="obj">The object with the initializer</param>
  36. /// <param name="unmanaged">Whether the struct is unmanaged</param>
  37. public OptionalRef(EcsObjectBase obj, bool unmanaged, EGID entityId = default)
  38. {
  39. this.entityId = entityId;
  40. if (obj.InitData.Valid)
  41. {
  42. initializer = obj.InitData.Initializer(obj.Id);
  43. state = (unmanaged ? State.Native : State.Managed) | State.Initializer;
  44. }
  45. else
  46. {
  47. initializer = default;
  48. state = State.Empty;
  49. }
  50. array = default;
  51. index = default;
  52. }
  53. /// <summary>
  54. /// Returns the value or a default value if empty. Supports objects that are being initialized.
  55. /// </summary>
  56. /// <returns>The value or the default value</returns>
  57. public ref T Get()
  58. {
  59. CompRefCache.Default = default; //The default value can be changed by mods
  60. if (state == State.Empty) return ref CompRefCache.Default;
  61. if ((state & State.Initializer) != State.Empty) return ref initializer.GetOrAdd<T>();
  62. if ((state & State.Native) != State.Empty) return ref array[index];
  63. return ref managedArray[index];
  64. }
  65. /// <summary>
  66. /// A non-by-ref view that allows getting and setting the value.
  67. /// </summary>
  68. public T Value
  69. {
  70. get => Get();
  71. set => Get() = value;
  72. }
  73. /// <summary>
  74. /// The ID of the entity this component belongs to or default if it doesn't exist.
  75. /// </summary>
  76. public EGID EGID => entityId;
  77. public bool Exists => state != State.Empty;
  78. public T? Nullable() => this ? Get() : default;
  79. public static implicit operator T(OptionalRef<T> opt) => opt.Get();
  80. public static implicit operator bool(OptionalRef<T> opt) => opt.state != State.Empty;
  81. public static implicit operator EGID(OptionalRef<T> opt) => opt.entityId;
  82. /// <summary>
  83. /// Creates an instance of a struct T that can be referenced.
  84. /// </summary>
  85. private struct CompRefCache
  86. {
  87. public static T Default;
  88. }
  89. /// <summary>
  90. /// A byte that holds state in its bits.
  91. /// </summary>
  92. [Flags]
  93. private enum State : byte
  94. {
  95. Empty,
  96. Native,
  97. Managed,
  98. Initializer = 4
  99. }
  100. }
  101. }