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.

94 lines
3.0KB

  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, IEntityComponent
  7. {
  8. private readonly State state;
  9. private readonly uint index;
  10. private NB<T> array;
  11. private MB<T> managedArray;
  12. private readonly EntityInitializer initializer;
  13. //The possible fields are: (index && (array || managedArray)) || initializer
  14. public OptionalRef(NB<T> array, uint index)
  15. {
  16. state = State.Native;
  17. this.array = array;
  18. this.index = index;
  19. initializer = default;
  20. }
  21. public OptionalRef(MB<T> array, uint index)
  22. {
  23. state = State.Managed;
  24. managedArray = array;
  25. this.index = index;
  26. initializer = default;
  27. this.array = default;
  28. }
  29. /// <summary>
  30. /// Wraps the initializer data, if present.
  31. /// </summary>
  32. /// <param name="obj">The object with the initializer</param>
  33. /// <param name="unmanaged">Whether the struct is unmanaged</param>
  34. public OptionalRef(EcsObjectBase obj, bool unmanaged)
  35. {
  36. if (obj.InitData.Valid)
  37. {
  38. initializer = obj.InitData.Initializer(obj.Id);
  39. state = (unmanaged ? State.Native : State.Managed) | State.Initializer;
  40. }
  41. else
  42. {
  43. initializer = default;
  44. state = State.Empty;
  45. }
  46. array = default;
  47. index = default;
  48. }
  49. /// <summary>
  50. /// Returns the value or a default value if empty. Supports objects that are being initialized.
  51. /// </summary>
  52. /// <returns>The value or the default value</returns>
  53. public ref T Get()
  54. {
  55. CompRefCache.Default = default; //The default value can be changed by mods
  56. if (state == State.Empty) return ref CompRefCache.Default;
  57. if ((state & State.Initializer) != State.Empty) return ref initializer.GetOrAdd<T>();
  58. if ((state & State.Native) != State.Empty) return ref array[index];
  59. return ref managedArray[index];
  60. }
  61. public bool Exists => state != State.Empty;
  62. public T? Nullable() => this ? Get() : default;
  63. public static implicit operator T(OptionalRef<T> opt) => opt.Get();
  64. public static implicit operator bool(OptionalRef<T> opt) => opt.state != State.Empty;
  65. /// <summary>
  66. /// Creates an instance of a struct T that can be referenced.
  67. /// </summary>
  68. private struct CompRefCache
  69. {
  70. public static T Default;
  71. }
  72. /// <summary>
  73. /// A byte that holds state in its bits.
  74. /// </summary>
  75. [Flags]
  76. private enum State : byte
  77. {
  78. Empty,
  79. Native,
  80. Managed,
  81. Initializer = 4
  82. }
  83. }
  84. }