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.

105 lines
2.9KB

  1. using System;
  2. using Gamecraft.Wires;
  3. using Svelto.ECS;
  4. using Unity.Mathematics;
  5. using GamecraftModdingAPI;
  6. using GamecraftModdingAPI.Utility;
  7. namespace GamecraftModdingAPI.Blocks
  8. {
  9. /// <summary>
  10. /// Common implementation for blocks that support wiring.
  11. /// </summary>
  12. public class SignalingBlock : Block
  13. {
  14. public SignalingBlock(EGID id) : base(id)
  15. {
  16. if (!BlockEngine.GetBlockInfoExists<BlockPortsStruct>(this.Id))
  17. {
  18. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  19. }
  20. }
  21. public SignalingBlock(uint id) : base(id)
  22. {
  23. if (!BlockEngine.GetBlockInfoExists<BlockPortsStruct>(this.Id))
  24. {
  25. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  26. }
  27. }
  28. protected ref BlockPortsStruct GetBlockPortsStruct()
  29. {
  30. return ref BlockEngine.GetBlockInfo<BlockPortsStruct>(Id);
  31. }
  32. /// <summary>
  33. /// Generates the input port identifiers.
  34. /// </summary>
  35. /// <returns>The input identifiers.</returns>
  36. protected EGID[] GetInputIds()
  37. {
  38. return SignalEngine.GetSignalInputs(Id);
  39. }
  40. /// <summary>
  41. /// Generates the output port identifiers.
  42. /// </summary>
  43. /// <returns>The output identifiers.</returns>
  44. protected EGID[] GetOutputIds()
  45. {
  46. return SignalEngine.GetSignalOutputs(Id);
  47. }
  48. /// <summary>
  49. /// Gets the port struct.
  50. /// </summary>
  51. /// <returns>The port struct.</returns>
  52. /// <param name="portId">Port identifier.</param>
  53. protected ref PortEntityStruct GetPortStruct(EGID portId)
  54. {
  55. return ref BlockEngine.GetBlockInfo<PortEntityStruct>(portId);
  56. }
  57. /// <summary>
  58. /// Gets the connected wire.
  59. /// </summary>
  60. /// <returns>The connected wire.</returns>
  61. /// <param name="portId">Port identifier.</param>
  62. /// <param name="connected">Whether the port has a wire connected to it.</param>
  63. protected ref WireEntityStruct GetConnectedWire(EGID portId, out bool connected)
  64. {
  65. return ref SignalEngine.MatchPortToWire(portId, Id, out connected);
  66. }
  67. /// <summary>
  68. /// [EXPERIMENTAL] Gets the channel data.
  69. /// </summary>
  70. /// <returns>The channel data.</returns>
  71. /// <param name="portId">Port identifier.</param>
  72. /// <param name="exists">Whether the channel actually exists.</param>
  73. protected ref ChannelDataStruct GetChannelData(EGID portId, out bool exists)
  74. {
  75. return ref SignalEngine.GetChannelDataStruct(portId, out exists);
  76. }
  77. /// <summary>
  78. /// The input port count.
  79. /// </summary>
  80. public uint InputCount
  81. {
  82. get => GetBlockPortsStruct().inputCount;
  83. }
  84. /// <summary>
  85. /// The output port count.
  86. /// </summary>
  87. public uint OutputCount
  88. {
  89. get => GetBlockPortsStruct().outputCount;
  90. }
  91. }
  92. }