Advertisement
MageCoven

fishnet.lua

Jul 17th, 2025
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.60 KB | None | 0 0
  1. settings.define("fishnet.download_folder", {
  2.     type = "string",
  3.     default = "downloads",
  4.     description = "Folder where files received via Fishnet will be saved."
  5. })
  6.  
  7. local modem = peripheral.find("modem")
  8. if not modem then
  9.     error("No modem found.")
  10. end
  11.  
  12. rednet.open(modem)
  13.  
  14. local fishnet = {
  15.     PROTOCOL = "fishnet",
  16.     PROTOCOL_STATUS = "fishnet::status",
  17.     PROTOCOL_FILE = "fishnet::file"
  18. }
  19.  
  20. --- Send a message to a specific receiver.
  21. ---@param receiver_id integer
  22. ---@param message any
  23. ---@param protocol string|nil
  24. function fishnet.send(receiver_id, message, protocol)
  25.     rednet.send(receiver_id, message, protocol or "fishnet")
  26. end
  27.  
  28. --- Receive a message from any sender.
  29. ---@param protocol string|nil
  30. ---@param timeout number|nil
  31. function fishnet.receive(protocol, timeout)
  32.     return rednet.receive(protocol, timeout)
  33. end
  34.  
  35. --- Send a status message to a specific receiver.
  36. ---@param receiver_id integer
  37. ---@param status any
  38. function fishnet.send_status(receiver_id, status)
  39.     fishnet.send(receiver_id, status, fishnet.PROTOCOL_STATUS)
  40. end
  41.  
  42. --- Handle a received status message.
  43. ---@param id integer
  44. ---@param message any
  45. ---@param protocol string
  46. local function handle_receive_status(id, message, protocol)
  47.     return id, message, protocol
  48. end
  49.  
  50. --- Receive a status message from any sender.
  51. ---@param timeout number|nil
  52. function fishnet.receive_status(timeout)
  53.     local id, message, protocol = fishnet.receive(fishnet.PROTOCOL_STATUS, timeout)
  54.     return handle_receive_status(id, message, protocol)
  55. end
  56.  
  57. --- Send a file to a specific receiver.
  58. ---@param receiver_id integer
  59. ---@param file_path string
  60. function fishnet.send_file(receiver_id, file_path)
  61.     local file = fs.open(file_path, "r")
  62.     if not file then
  63.         error("Failed to open file: " .. file_path, 2)
  64.     end
  65.  
  66.     local data = file.readAll()
  67.     file.close()
  68.  
  69.     fishnet.send(receiver_id, {
  70.         file_name = fs.getName(file_path),
  71.         data = data
  72.     }, fishnet.PROTOCOL_FILE)
  73. end
  74.  
  75. --- Handle a received file message.
  76. --- @param id integer
  77. --- @param message any
  78. --- @param protocol string
  79. --- @return string|nil, string
  80. local function handle_receive_file(id, message, protocol)
  81.     if type(message) ~= "table" or not message.file_name or not message.data then
  82.         return nil, "Invalid file message format"
  83.     end
  84.  
  85.     local folder = settings.get("fishnet.download_folder")
  86.     local file_path = fs.combine(folder, message.file_name)
  87.     local file = fs.open(file_path, "w")
  88.     if not file then
  89.         return nil, "Failed to open file for writing: " .. file_path
  90.     end
  91.  
  92.     file.write(message.data)
  93.     file.close()
  94.  
  95.     return file_path, ""
  96. end
  97.  
  98. --- Receive a file from any sender.
  99. ---@param timeout number|nil
  100. ---@return string|nil, string
  101. function fishnet:receive_file(timeout)
  102.     local id, message = fishnet.receive(fishnet.PROTOCOL_FILE, timeout)
  103.     return handle_receive_file(id, message, fishnet.PROTOCOL_FILE)
  104. end
  105.  
  106. --- Host a service with a specific hostname.
  107. ---@param hostname string
  108. function fishnet:host(hostname)
  109.     rednet.host(fishnet.PROTOCOL, hostname)
  110. end
  111.  
  112. --- Unhost a service with a specific hostname.
  113. ---@param hostname string
  114. function fishnet:unhost(hostname)
  115.     rednet.unhost(fishnet.PROTOCOL, hostname)
  116. end
  117.  
  118. --- Find a service with a specific hostname.
  119. ---@param hostname string
  120. ---@return integer|nil
  121. function fishnet:lookup(hostname)
  122.     local ids = {rednet.lookup(fishnet.PROTOCOL, hostname)}
  123.     return ids[1]
  124. end
  125.  
  126. --- Close the modem connection.
  127. function fishnet:close()
  128.     rednet.close()
  129. end
  130.  
  131. --- Receive any message with a specific protocol.
  132. ---@param timeout number|nil
  133. ---@return table, string|nil
  134. function fishnet:receive_any(timeout)
  135.     local id, message, protocol = rednet.receive(nil, timeout)
  136.     if not message then
  137.         return {}, "No message received"
  138.     end
  139.  
  140.     if protocol == fishnet.PROTOCOL_STATUS then
  141.         return {
  142.             type = "status",
  143.             id = id,
  144.             data = {
  145.                 handle_receive_status(id, message, protocol)
  146.             }
  147.         }, nil
  148.     elseif protocol == fishnet.PROTOCOL_FILE then
  149.         return {
  150.             type = "file",
  151.             id = id,
  152.             data = {
  153.                 handle_receive_file(id, message, protocol)
  154.             }
  155.         }, nil
  156.     elseif protocol == fishnet.PROTOCOL then
  157.         return {
  158.             type = "message",
  159.             id = id,
  160.             data = {
  161.                 message = message,
  162.             }
  163.         }, nil
  164.     else
  165.         return {}, "Unknown protocol: " .. protocol
  166.     end
  167. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement