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.

SignalingBlock.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. }
  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(EGID portId, out bool connected)
  43. {
  44. return ref SignalEngine.MatchPortToWire(portId, 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. /// <param name="exists">Whether the channel actually exists.</param>
  52. protected ref ChannelDataStruct GetChannelData(EGID portId, out bool exists)
  53. {
  54. return ref SignalEngine.GetChannelDataStruct(portId, out exists);
  55. }
  56. /// <summary>
  57. /// The input port count.
  58. /// </summary>
  59. public uint InputCount
  60. {
  61. get => BlockEngine.GetBlockInfo(this, (BlockPortsStruct st) => st.inputCount);
  62. }
  63. /// <summary>
  64. /// The output port count.
  65. /// </summary>
  66. public uint OutputCount
  67. {
  68. get => BlockEngine.GetBlockInfo(this, (BlockPortsStruct st) => st.outputCount);
  69. }
  70. /// <summary>
  71. /// Connect an output on this block to an input on another block.
  72. /// </summary>
  73. /// <param name="sourcePort">Output port number.</param>
  74. /// <param name="destination">Input block.</param>
  75. /// <param name="destinationPort">Input port number.</param>
  76. /// <returns>The wire connection</returns>
  77. /// <exception cref="WiringException">The wire could not be created.</exception>
  78. public Wire Connect(byte sourcePort, SignalingBlock destination, byte destinationPort)
  79. {
  80. if (sourcePort >= OutputCount)
  81. {
  82. throw new WiringException("Source port does not exist");
  83. }
  84. if (destinationPort >= destination.InputCount)
  85. {
  86. throw new WiringException("Destination port does not exist");
  87. }
  88. return Wire.Connect(this, sourcePort, destination, destinationPort);
  89. }
  90. /// <summary>
  91. /// The port's name.
  92. /// This is localized to the user's language, so this is not reliable for port identification.
  93. /// </summary>
  94. /// <param name="port">Port number.</param>
  95. /// <param name="input">Whether the port is an input (true) or an output (false).</param>
  96. /// <returns>The localized port name.</returns>
  97. public string PortName(byte port, bool input)
  98. {
  99. BlockPortsStruct bps = BlockEngine.GetBlockInfo(this, (BlockPortsStruct a) => a);
  100. PortEntityStruct pes = SignalEngine.GetPortByOffset(this, port, input);
  101. return pes.portNameLocalised;
  102. }
  103. /// <summary>
  104. /// The input port's name.
  105. /// </summary>
  106. /// <param name="port">Input port number.</param>
  107. /// <returns>The port name, localized to the user's language.</returns>
  108. public string InputPortName(byte port) => PortName(port, true);
  109. /// <summary>
  110. /// The output port's name.
  111. /// </summary>
  112. /// <param name="port">Output port number.</param>
  113. /// <returns>The port name, localized to the user's language.</returns>
  114. public string OutputPortName(byte port) => PortName(port, false);
  115. /// <summary>
  116. /// All wires connected to the input port.
  117. /// These wires will always be wired output -> input.
  118. /// </summary>
  119. /// <param name="port">Port number.</param>
  120. /// <returns>Wires connected to the input port.</returns>
  121. public Wire[] ConnectedToInput(byte port)
  122. {
  123. if (port >= InputCount) throw new WiringException($"Port input {port} does not exist");
  124. EGID[] wireEgids = SignalEngine.WiredToInput(Id, port);
  125. Wire[] wires = new Wire[wireEgids.Length];
  126. for (uint i = 0; i < wireEgids.Length; i++)
  127. {
  128. wires[i] = new Wire(wireEgids[i]);
  129. }
  130. return wires;
  131. }
  132. /// <summary>
  133. /// All wires connected to the output port.
  134. /// These wires will always be wired output -> input.
  135. /// </summary>
  136. /// <param name="port">Port number.</param>
  137. /// <returns>Wires connected to the output port.</returns>
  138. public Wire[] ConnectedToOutput(byte port)
  139. {
  140. if (port >= OutputCount) throw new WiringException($"Port output {port} does not exist");
  141. EGID[] wireEgids = SignalEngine.WiredToOutput(Id, port);
  142. Wire[] wires = new Wire[wireEgids.Length];
  143. for (uint i = 0; i < wireEgids.Length; i++)
  144. {
  145. wires[i] = new Wire(wireEgids[i]);
  146. }
  147. return wires;
  148. }
  149. }
  150. }