A stable modding interface between Techblox and mods https://mod.exmods.org/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

80 行
2.5KB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using TechbloxModdingAPI.Blocks;
  7. using Svelto.DataStructures;
  8. using Svelto.ECS;
  9. namespace TechbloxModdingAPI
  10. {
  11. public ref struct OptionalRef<T> where T : unmanaged, IEntityComponent
  12. {
  13. private bool exists;
  14. private NB<T> array;
  15. private uint index;
  16. private EntityInitializer initializer;
  17. public OptionalRef(NB<T> array, uint index)
  18. {
  19. exists = true;
  20. this.array = array;
  21. this.index = index;
  22. initializer = default;
  23. }
  24. /// <summary>
  25. /// Wraps the initializer data, if present.
  26. /// </summary>
  27. /// <param name="obj">The object with the initializer</param>
  28. public OptionalRef(EcsObjectBase obj)
  29. {
  30. if (obj.InitData.Valid)
  31. {
  32. initializer = obj.InitData.Initializer(obj.Id);
  33. exists = true;
  34. }
  35. else
  36. {
  37. initializer = default;
  38. exists = false;
  39. }
  40. array = default;
  41. index = default;
  42. }
  43. /// <summary>
  44. /// Returns the value or a default value if empty. Supports objects that are being initialized.
  45. /// </summary>
  46. /// <returns>The value or the default value</returns>
  47. public ref T Get()
  48. {
  49. if (!exists) return ref CompRefCache<T>.Default;
  50. if (initializer.EGID == EGID.Empty)
  51. return ref array[index];
  52. return ref initializer.GetOrCreate<T>();
  53. }
  54. public bool Exists => exists;
  55. public static implicit operator T(OptionalRef<T> opt) => opt.Get();
  56. public static implicit operator bool(OptionalRef<T> opt) => opt.exists;
  57. /*public delegate ref TR Mapper<TR>(ref T component) where TR : unmanaged;
  58. public unsafe delegate TR* PMapper<TR>(T* component) where TR : unmanaged;
  59. public unsafe OptionalRef<TR> Map<TR>(PMapper<TR> mapper) where TR : unmanaged =>
  60. exists ? new OptionalRef<TR>(ref *mapper(pointer)) : new OptionalRef<TR>();*/
  61. /// <summary>
  62. /// Creates an instance of a struct T that can be referenced.
  63. /// </summary>
  64. /// <typeparam name="TR">The struct type to cache</typeparam>
  65. private struct CompRefCache<TR> where TR : unmanaged
  66. {
  67. public static TR Default;
  68. }
  69. }
  70. }