Advertisement
D6Bell

Untitled

Apr 7th, 2021
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. --- The Peripheral API is for interacting with peripherals connected to the
  2. -- computer, such as the Disk Drive, the Advanced Monitor and Monitor.
  3. --
  4. -- Each peripheral block has a name, either referring to the side the peripheral
  5. -- can be found on, or a name on an adjacent wired network.
  6. --
  7. -- If the peripheral is next to the computer, its side is either `front`,
  8. -- `back`, `left`, `right`, `top` or `bottom`. If the peripheral is attached by
  9. -- a cable, its side will follow the format `type_id`, for example `printer_0`.
  10. --
  11. -- Peripheral functions are called *methods*, a term borrowed from Java.
  12. --
  13. -- @module peripheral
  14.  
  15. local expect = dofile("rom/modules/main/cc/expect.lua").expect
  16.  
  17. local native = peripheral
  18. local sides = rs.getSides()
  19.  
  20. --- Provides a list of all peripherals available.
  21. --
  22. -- If a device is located directly next to the system, then its name will be
  23. -- listed as the side it is attached to. If a device is attached via a Wired
  24. -- Modem, then it'll be reported according to its name on the wired network.
  25. --
  26. -- @treturn { string... } A list of the names of all attached peripherals.
  27. function getNames()
  28. local results = {}
  29. for n = 1, #sides do
  30. local side = sides[n]
  31. if native.isPresent(side) then
  32. table.insert(results, side)
  33. if native.getType(side) == "modem" and not native.call(side, "isWireless") then
  34. local remote = native.call(side, "getNamesRemote")
  35. for _, name in ipairs(remote) do
  36. table.insert(results, name)
  37. end
  38. end
  39. end
  40. end
  41. return results
  42. end
  43.  
  44. --- Determines if a peripheral is present with the given name.
  45. --
  46. -- @tparam string name The side or network name that you want to check.
  47. -- @treturn boolean If a peripheral is present with the given name.
  48. -- @usage peripheral.isPresent("top")
  49. -- @usage peripheral.isPresent("monitor_0")
  50. function isPresent(name)
  51. expect(1, name, "string")
  52. if native.isPresent(name) then
  53. return true
  54. end
  55.  
  56. for n = 1, #sides do
  57. local side = sides[n]
  58. if native.getType(side) == "modem" and not native.call(side, "isWireless") and
  59. native.call(side, "isPresentRemote", name)
  60. then
  61. return true
  62. end
  63. end
  64. return false
  65. end
  66.  
  67. --- Get the type of a wrapped peripheral, or a peripheral with the given name.
  68. --
  69. -- @tparam string|table peripheral The name of the peripheral to find, or a
  70. -- wrapped peripheral instance.
  71. -- @treturn string|nil The peripheral's type, or `nil` if it is not present.
  72. function getType(peripheral)
  73. expect(1, peripheral, "string", "table")
  74. if type(peripheral) == "string" then -- Peripheral name passed
  75. if native.isPresent(peripheral) then
  76. return native.getType(peripheral)
  77. end
  78. for n = 1, #sides do
  79. local side = sides[n]
  80. if native.getType(side) == "modem" and not native.call(side, "isWireless") and
  81. native.call(side, "isPresentRemote", peripheral)
  82. then
  83. return native.call(side, "getTypeRemote", peripheral)
  84. end
  85. end
  86. return nil
  87. else
  88. local mt = getmetatable(peripheral)
  89. if not mt or mt.__name ~= "peripheral" or type(mt.type) ~= "string" then
  90. error("bad argument #1 (table is not a peripheral)", 2)
  91. end
  92. return mt.type
  93. end
  94. end
  95.  
  96. --- Get all available methods for the peripheral with the given name.
  97. --
  98. -- @tparam string name The name of the peripheral to find.
  99. -- @treturn { string... }|nil A list of methods provided by this peripheral, or `nil` if
  100. -- it is not present.
  101. function getMethods(name)
  102. expect(1, name, "string")
  103. if native.isPresent(name) then
  104. return native.getMethods(name)
  105. end
  106. for n = 1, #sides do
  107. local side = sides[n]
  108. if native.getType(side) == "modem" and not native.call(side, "isWireless") and
  109. native.call(side, "isPresentRemote", name)
  110. then
  111. return native.call(side, "getMethodsRemote", name)
  112. end
  113. end
  114. return nil
  115. end
  116.  
  117. --- Get the name of a peripheral wrapped with @{peripheral.wrap}.
  118. --
  119. -- @tparam table peripheral The peripheral to get the name of.
  120. -- @treturn string The name of the given peripheral.
  121. function getName(peripheral)
  122. expect(1, peripheral, "table")
  123. local mt = getmetatable(peripheral)
  124. if not mt or mt.__name ~= "peripheral" or type(mt.name) ~= "string" then
  125. error("bad argument #1 (table is not a peripheral)", 2)
  126. end
  127. return mt.name
  128. end
  129.  
  130. --- Call a method on the peripheral with the given name.
  131. --
  132. -- @tparam string name The name of the peripheral to invoke the method on.
  133. -- @tparam string method The name of the method
  134. -- @param ... Additional arguments to pass to the method
  135. -- @return The return values of the peripheral method.
  136. --
  137. -- @usage Open the modem on the top of this computer.
  138. --
  139. -- peripheral.call("top", "open", 1)
  140. function call(name, method, ...)
  141. expect(1, name, "string")
  142. expect(2, method, "string")
  143. if native.isPresent(name) then
  144. return native.call(name, method, ...)
  145. end
  146.  
  147. for n = 1, #sides do
  148. local side = sides[n]
  149. if native.getType(side) == "modem" and not native.call(side, "isWireless") and
  150. native.call(side, "isPresentRemote", name)
  151. then
  152. return native.call(side, "callRemote", name, method, ...)
  153. end
  154. end
  155. return nil
  156. end
  157.  
  158. --- Get a table containing functions pointing to the peripheral's methods, which
  159. -- can then be called as if using @{peripheral.call}.
  160. --
  161. -- @tparam string name The name of the peripheral to wrap.
  162. -- @treturn table|nil The table containing the peripheral's methods, or `nil` if
  163. -- there is no peripheral present with the given name.
  164. -- @usage peripheral.wrap("top").open(1)
  165. function wrap(name)
  166. expect(1, name, "string")
  167.  
  168. local methods = peripheral.getMethods(name)
  169. if not methods then
  170. return nil
  171. end
  172.  
  173. local result = setmetatable({}, {
  174. __name = "peripheral",
  175. name = name,
  176. type = peripheral.getType(name),
  177. })
  178. for _, method in ipairs(methods) do
  179. result[method] = function(...)
  180. return peripheral.call(name, method, ...)
  181. end
  182. end
  183. return result
  184. end
  185.  
  186. --- Find all peripherals of a specific type, and return the
  187. -- @{peripheral.wrap|wrapped} peripherals.
  188. --
  189. -- @tparam string ty The type of peripheral to look for.
  190. -- @tparam[opt] function(name:string, wrapped:table):boolean filter A
  191. -- filter function, which takes the peripheral's name and wrapped table
  192. -- and returns if it should be included in the result.
  193. -- @treturn table... 0 or more wrapped peripherals matching the given filters.
  194. -- @usage { peripheral.find("monitor") }
  195. -- @usage peripheral.find("modem", rednet.open)
  196. function find(ty, filter)
  197. expect(1, ty, "string")
  198. expect(2, filter, "function", "nil")
  199.  
  200. local results = {}
  201. for _, name in ipairs(peripheral.getNames()) do
  202. if peripheral.getType(name) == ty then
  203. local wrapped = peripheral.wrap(name)
  204. if filter == nil or filter(name, wrapped) then
  205. table.insert(results, wrapped)
  206. end
  207. end
  208. end
  209. return table.unpack(results)
  210. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement