A stable modding interface between Techblox and mods https://mod.exmods.org/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

167 рядки
5.6KB

  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(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<BlockPortsStruct>(this).inputCount;
  62. }
  63. /// <summary>
  64. /// The output port count.
  65. /// </summary>
  66. public uint OutputCount
  67. {
  68. get => BlockEngine.GetBlockInfo<BlockPortsStruct>(this).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. PortEntityStruct pes = SignalEngine.GetPortByOffset(this, port, input);
  100. return pes.portNameLocalised;
  101. }
  102. /// <summary>
  103. /// The input port's name.
  104. /// </summary>
  105. /// <param name="port">Input port number.</param>
  106. /// <returns>The port name, localized to the user's language.</returns>
  107. public string InputPortName(byte port) => PortName(port, true);
  108. /// <summary>
  109. /// The output port's name.
  110. /// </summary>
  111. /// <param name="port">Output port number.</param>
  112. /// <returns>The port name, localized to the user's language.</returns>
  113. public string OutputPortName(byte port) => PortName(port, false);
  114. /// <summary>
  115. /// All wires connected to the input port.
  116. /// These wires will always be wired output -> input.
  117. /// </summary>
  118. /// <param name="port">Port number.</param>
  119. /// <returns>Wires connected to the input port.</returns>
  120. public Wire[] ConnectedToInput(byte port)
  121. {
  122. if (port >= InputCount) throw new WiringException($"Port input {port} does not exist");
  123. EGID[] wireEgids = SignalEngine.WiredToInput(Id, port);
  124. Wire[] wires = new Wire[wireEgids.Length];
  125. for (uint i = 0; i < wireEgids.Length; i++)
  126. {
  127. wires[i] = new Wire(wireEgids[i]);
  128. }
  129. return wires;
  130. }
  131. /// <summary>
  132. /// All wires connected to the output port.
  133. /// These wires will always be wired output -> input.
  134. /// </summary>
  135. /// <param name="port">Port number.</param>
  136. /// <returns>Wires connected to the output port.</returns>
  137. public Wire[] ConnectedToOutput(byte port)
  138. {
  139. if (port >= OutputCount) throw new WiringException($"Port output {port} does not exist");
  140. EGID[] wireEgids = SignalEngine.WiredToOutput(Id, port);
  141. Wire[] wires = new Wire[wireEgids.Length];
  142. for (uint i = 0; i < wireEgids.Length; i++)
  143. {
  144. wires[i] = new Wire(wireEgids[i]);
  145. }
  146. return wires;
  147. }
  148. }
  149. }