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.

166 lines
5.5KB

  1. using System;
  2. using Gamecraft.Wires;
  3. using Svelto.ECS;
  4. using Unity.Mathematics;
  5. using TechbloxModdingAPI;
  6. using TechbloxModdingAPI.Utility;
  7. namespace TechbloxModdingAPI.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. }
  17. public SignalingBlock(uint id) : base(id)
  18. {
  19. }
  20. /// <summary>
  21. /// Generates the input port identifiers.
  22. /// </summary>
  23. /// <returns>The input identifiers.</returns>
  24. protected EGID[] GetInputIds()
  25. {
  26. return SignalEngine.GetSignalInputs(Id);
  27. }
  28. /// <summary>
  29. /// Generates the output port identifiers.
  30. /// </summary>
  31. /// <returns>The output identifiers.</returns>
  32. protected EGID[] GetOutputIds()
  33. {
  34. return SignalEngine.GetSignalOutputs(Id);
  35. }
  36. /// <summary>
  37. /// Gets the connected wire.
  38. /// </summary>
  39. /// <returns>The connected wire.</returns>
  40. /// <param name="portId">Port identifier.</param>
  41. /// <param name="connected">Whether the port has a wire connected to it.</param>
  42. protected ref WireEntityStruct GetConnectedWire(PortEntityStruct port, out bool connected)
  43. {
  44. return ref SignalEngine.MatchPortToWire(port, Id, out connected);
  45. }
  46. /// <summary>
  47. /// [EXPERIMENTAL] Gets the channel data.
  48. /// </summary>
  49. /// <returns>The channel data.</returns>
  50. /// <param name="portId">Port identifier.</param>
  51. protected OptionalRef<ChannelDataStruct> GetChannelData(EGID portId)
  52. {
  53. return SignalEngine.GetChannelDataStruct(portId);
  54. }
  55. /// <summary>
  56. /// The input port count.
  57. /// </summary>
  58. public uint InputCount
  59. {
  60. get => BlockEngine.GetBlockInfo<BlockPortsStruct>(this).inputCount;
  61. }
  62. /// <summary>
  63. /// The output port count.
  64. /// </summary>
  65. public uint OutputCount
  66. {
  67. get => BlockEngine.GetBlockInfo<BlockPortsStruct>(this).outputCount;
  68. }
  69. /// <summary>
  70. /// Connect an output on this block to an input on another block.
  71. /// </summary>
  72. /// <param name="sourcePort">Output port number.</param>
  73. /// <param name="destination">Input block.</param>
  74. /// <param name="destinationPort">Input port number.</param>
  75. /// <returns>The wire connection</returns>
  76. /// <exception cref="WiringException">The wire could not be created.</exception>
  77. public Wire Connect(byte sourcePort, SignalingBlock destination, byte destinationPort)
  78. {
  79. if (sourcePort >= OutputCount)
  80. {
  81. throw new WiringException("Source port does not exist");
  82. }
  83. if (destinationPort >= destination.InputCount)
  84. {
  85. throw new WiringException("Destination port does not exist");
  86. }
  87. return Wire.Connect(this, sourcePort, destination, destinationPort);
  88. }
  89. /// <summary>
  90. /// The port's name.
  91. /// This is localized to the user's language, so this is not reliable for port identification.
  92. /// </summary>
  93. /// <param name="port">Port number.</param>
  94. /// <param name="input">Whether the port is an input (true) or an output (false).</param>
  95. /// <returns>The localized port name.</returns>
  96. public string PortName(byte port, bool input)
  97. {
  98. PortEntityStruct pes = SignalEngine.GetPortByOffset(this, port, input);
  99. return pes.portNameLocalised;
  100. }
  101. /// <summary>
  102. /// The input port's name.
  103. /// </summary>
  104. /// <param name="port">Input port number.</param>
  105. /// <returns>The port name, localized to the user's language.</returns>
  106. public string InputPortName(byte port) => PortName(port, true);
  107. /// <summary>
  108. /// The output port's name.
  109. /// </summary>
  110. /// <param name="port">Output port number.</param>
  111. /// <returns>The port name, localized to the user's language.</returns>
  112. public string OutputPortName(byte port) => PortName(port, false);
  113. /// <summary>
  114. /// All wires connected to the input port.
  115. /// These wires will always be wired output -> input.
  116. /// </summary>
  117. /// <param name="port">Port number.</param>
  118. /// <returns>Wires connected to the input port.</returns>
  119. public Wire[] ConnectedToInput(byte port)
  120. {
  121. if (port >= InputCount) throw new WiringException($"Port input {port} does not exist");
  122. EGID[] wireEgids = SignalEngine.WiredToInput(Id, port);
  123. Wire[] wires = new Wire[wireEgids.Length];
  124. for (uint i = 0; i < wireEgids.Length; i++)
  125. {
  126. wires[i] = new Wire(wireEgids[i]);
  127. }
  128. return wires;
  129. }
  130. /// <summary>
  131. /// All wires connected to the output port.
  132. /// These wires will always be wired output -> input.
  133. /// </summary>
  134. /// <param name="port">Port number.</param>
  135. /// <returns>Wires connected to the output port.</returns>
  136. public Wire[] ConnectedToOutput(byte port)
  137. {
  138. if (port >= OutputCount) throw new WiringException($"Port output {port} does not exist");
  139. EGID[] wireEgids = SignalEngine.WiredToOutput(Id, port);
  140. Wire[] wires = new Wire[wireEgids.Length];
  141. for (uint i = 0; i < wireEgids.Length; i++)
  142. {
  143. wires[i] = new Wire(wireEgids[i]);
  144. }
  145. return wires;
  146. }
  147. }
  148. }