Advertisement
toast_account_i_made

Untitled

Oct 27th, 2023 (edited)
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.49 KB | None | 0 0
  1. local dbModem = peripheral.wrap("back") or error("No data modem attached", 0)
  2.  
  3. -- PostMonitor.lua - Basic debugging / logging put on a monitor
  4. local displayModem = dbModem
  5.  
  6. function checkMonitor(monitorName)
  7.     if not displayModem.isPresentRemote(monitorName) then
  8.         print("Missing post monitor")
  9.    
  10.         print("Change 'displayMonitorName' to any of the following values if applicable")
  11.         for _, name in displayModem.getNamesRemote() do
  12.             print("   >'"..name.."'")
  13.         end
  14.         return
  15.     end
  16. end
  17.  
  18. function wrapPostMonitor(monitorName)
  19.  
  20.     local display = peripheral.wrap(monitorName)
  21.  
  22.     display.setBackgroundColour(0x8000)
  23.     display.clear()
  24.     display.setCursorPos(1,1)
  25.     display.setCursorBlink(true)
  26.     display.setTextScale(0.5)
  27.  
  28.     nextLine = function (monitor, screenHeight)
  29.         local newYPos = ({monitor.getCursorPos()})[1] +1;
  30.         monitor.setCursorPos(1, newYPos)
  31.         if (newYPos > screenHeight) then
  32.             monitor.scroll(1)
  33.         end
  34.     end
  35.  
  36.     writeWrapped = function(postMonitor, log)
  37.         local screenHeight, maxLineLength = postMonitor.getSize()
  38.         local lineLength = 0
  39.  
  40.         nextLine(postMonitor.display, screenHeight)
  41.         local words = {}
  42.         for word in log:gmatch("%w+") do
  43.             table.insert(words, word)
  44.         end
  45.  
  46.         for index, word in words do
  47.             lineLength = lineLength + word:len()
  48.             postMonitor.display.write(word.." ")
  49.             if (index ~= #words and lineLength >= maxLineLength) then
  50.                 nextLine(postMonitor.display, screenHeight)
  51.                 lineLength = 0
  52.             end
  53.         end
  54.     end
  55.  
  56.     return {
  57.         display = display,
  58.         postLog = function(postMonitor, log)
  59.             postMonitor:resetColor()
  60.             writeWrapped(postMonitor, log)
  61.         end,
  62.         postError = function(postMonitor, log)
  63.             postMonitor:textColor(0x4000)
  64.             writeWrapped(postMonitor, log)
  65.         end,
  66.         resetColor = function(postMonitor)
  67.             postMonitor.display.setBackgroundColour(0x8000)
  68.             postMonitor.display.setTextColour(0x1)
  69.         end,
  70.         textColor = function(postMonitor, color)
  71.             postMonitor.resetColor()
  72.             postMonitor.display.setTextColour(color)
  73.         end,
  74.         paintColor = function(postMonitor, color)
  75.             postMonitor.display.setBackgroundColour(color)
  76.             postMonitor.display.setTextColour(color)
  77.         end,
  78.     }
  79. end
  80.  
  81. function safeWrapPostMonitor(monitorName)
  82.     checkMonitor(monitorName)
  83.     return wrapPostMonitor(monitorName)
  84. end
  85.  
  86. --End of PostMonitor.lua
  87.  
  88. local driveMonitor = safeWrapPostMonitor("monitor_6")
  89. local networkMonitor = safeWrapPostMonitor("monitor_8")
  90.  
  91. --Load a list of drives from manifest.txt in a drive below
  92. local driveManifest = peripheral.wrap("bottom")
  93.  
  94. if (not driveManifest.hasData()) then
  95.     driveMonitor:postError("ERR: Missing drive in manifest!")
  96.     return
  97. end
  98.  
  99. driveMonitor:postLog("Found manifest drive with path " .. driveManifest.getMountPath())
  100. driveMonitor:postLog("Loading manifest...")
  101.  
  102. local manifestFile = fs.open(driveManifest.getMountPath() .. "/manifest.txt", "r")
  103. local manifest = {}
  104. local i = 0
  105. for driveName in string.gmatch(manifestFile.readAll(), "(.-),") do
  106.     manifest[driveName] = { listIndex = i, fileExists = false, filePath = "" }
  107.     i=i+1
  108. end
  109. manifestFile.close()
  110.  
  111. driveMonitor:postLog("Read manifest, found " .. tostring(#manifest) .. " entries")
  112.  
  113. --Post initaliser
  114.  
  115. driveMonitor.display.setBackgroundColour(0x8000)
  116. driveMonitor.display.clear()
  117. driveMonitor.display.setCursorBlink(false)
  118. driveMonitor.display.setTextScale(0.5)
  119.  
  120. local maxNameLength = 20
  121.  
  122. driveMonitor.display.setCursorPos(1, 1)
  123. driveMonitor.display.write("DRIVE          |CONNECTED |CONTENTS ")
  124.  
  125. --Look for drives that have disks inside
  126. local discoveredDrives = {}
  127. for _, peripheralName in pairs(dbModem.getNamesRemote()) do
  128.     local peripheral = peripheral.wrap(peripheralName)
  129.     if (dbModem.hasTypeRemote(peripheralName, "drive") and
  130.         peripheral.isDiskPresent()) then
  131.         discoveredDrives[peripheral.getDiskLabel()] = peripheral
  132.     end
  133. end
  134.  
  135. function checkDrive(driveName)
  136.     if (manifest[driveName] == nil) then
  137.         return false
  138.     end
  139.     local isAvaliable = false
  140.     local yPos = manifest[driveName].listIndex +2
  141.  
  142.     driveMonitor.display.setCursorPos(1, yPos)
  143.  
  144.     driveMonitor:resetColor()
  145.     driveMonitor.display.write(driveName)
  146.  
  147.     driveMonitor.display.setCursorPos(16, yPos)
  148.     driveMonitor.display.write("|")
  149.     if (discoveredDrives[driveName] ~= nil) then
  150.         driveMonitor:paintColor(0x2000)
  151.     else
  152.         driveMonitor:paintColor(0x4000)
  153.     end
  154.     driveMonitor.display.write((" "):rep(10))
  155.    
  156.     driveMonitor:resetColor()
  157.     driveMonitor.display.setCursorPos(27, yPos)
  158.     driveMonitor.display.write("|")
  159.    
  160.     if (discoveredDrives[driveName] ~= nil
  161.         and fs.exists(discoveredDrives[driveName].getMountPath().."/"..driveName..".lua")) then
  162.         driveMonitor:paintColor(0x2000)
  163.         isAvaliable = true
  164.     else
  165.         driveMonitor:paintColor(0x4000)
  166.     end
  167.     driveMonitor.display.write((" "):rep(10))
  168.  
  169.     return isAvaliable
  170. end
  171.  
  172. --Display the discoveredDrives, by checking through the items from the manifest
  173. for driveName, data in pairs(manifest) do
  174.     --Fetching the drive will also display its state in the event it fails
  175.     checkDrive(driveName)
  176. end
  177.  
  178. --Shipnet.lua - Networking module for ship components,
  179. local shipnetModem = peripheral.wrap("top") or error("No shipnet modem attached", 0)
  180. local openRequests = {}
  181.  
  182. local shipnetChannel = 1
  183.  
  184. shipnetModem.open(shipnetChannel)
  185.  
  186. -- print("Awaiting channel "..shipnetChannel.." to be opened...")
  187. -- local isOpen
  188. -- repeat
  189. --     isOpen = shipnetModem.isOpen(shipnetChannel)
  190. --     sleep()
  191. -- until isOpen
  192. -- print("Channel "..shipnetChannel.." is open!")
  193.  
  194. function sendPacketRequest(channel, requestInfo, stringData)
  195.  
  196.     math.randomseed(os.time())
  197.     local requestId = math.floor(math.random()*1000)
  198.     openRequests[requestId] = requestInfo
  199.     shipnetModem.transmit(channel, channel, "request:"..requestId..":"..stringData)
  200.     return requestId
  201.  
  202. end
  203. function sendPacketResponse(channel, requestId, stringData)
  204.  
  205.     math.randomseed(os.time())
  206.     shipnetModem.transmit(channel, channel, "response:"..requestId..":"..stringData)
  207.     return requestId
  208.  
  209. end
  210.  
  211. function parseSection(sections, remainingMessage)
  212.     local colonPos = remainingMessage:find(":")
  213.     if (colonPos == nil) then
  214.         return false
  215.     end
  216.     table.insert(sections, remainingMessage:sub(1, colonPos -1))
  217.     return true, remainingMessage:sub(colonPos +1)
  218. end
  219.  
  220. function parsePacket(message)
  221.     local remainingMessage = message
  222.     local parsed = true
  223.     local sections = {}
  224.     for _ = 0, 1 do
  225.         parsed, remainingMessage = parseSection(sections, remainingMessage)
  226.         if not parsed then return false, {} end
  227.     end
  228.     if sections[1] == "response" then --Read the response type error or success
  229.         parsed, remainingMessage = parseSection(sections, remainingMessage)
  230.         if not parsed then return false, {} end
  231.     end
  232.  
  233.     return true, {
  234.         packetType = sections[1],
  235.         id = sections[2],
  236.         responseCode = sections[3],
  237.         data = remainingMessage
  238.     }
  239. end
  240.  
  241. function pullPacket(isAwaitingResponsePackets)
  242.     local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  243.  
  244.     if side ~= peripheral.getName(shipnetModem) and channel ~= shipnetChannel then
  245.         return false, 0, {}
  246.     end
  247.    
  248.     local parsed, parsedMessage = parsePacket(message)
  249.  
  250.     print(parsed)
  251.     print(parsedMessage.packetType)
  252.     print((isAwaitingResponsePackets and "response" or "request"))
  253.     if parsed and parsedMessage.packetType == (isAwaitingResponsePackets and "response" or "request")
  254.         and (not isAwaitingResponsePackets or openRequests[parsedMessage.id] ~= nil) then
  255.         return true, parsedMessage.id, parsedMessage
  256.     end
  257. end
  258.  
  259. --Packet handlers
  260.  
  261. --Request info is used to track what the packet will be, i.e. the file requested
  262. local onResponse = function (packetId, requestInfo, parsedMessage) end
  263.  
  264. local onRequest = function (packetId, parsedMessage)
  265.     local driveName = parsedMessage.data
  266.  
  267.     if not checkDrive(parsedMessage.data) then
  268.         sendPacketResponse(shipnetChannel, packetId, "error:notfound")
  269.         networkMonitor:postError("File not found for request "..parsedMessage.id.." for "..driveName..".lua")
  270.         return
  271.     end
  272.  
  273.     local requestedFile = fs.open(discoveredDrives[driveName].getMountPath().."/"..driveName..".lua", "r")
  274.     fileData = requestedFile.readAll()
  275.     requestedFile.close()
  276.  
  277.     networkMonitor:postLog("Answered request "..parsedMessage.id.." for "..driveName..".lua")
  278.     sendPacketResponse(shipnetChannel, packetId, "success:"..fileData)
  279. end
  280.  
  281. function awaitPackets(isAwaitingResponsePackets)
  282.     while true do
  283.         local foundPacket, packetId, parsedMessage = pullPacket(isAwaitingResponsePackets)
  284.         print("recived")
  285.         if (foundPacket) then
  286.             print("a packet")
  287.             if (isAwaitingResponsePackets) then
  288.                 onResponse(packetId, openRequests[parsedMessage.id], parsedMessage)
  289.             else
  290.                 print("a request")
  291.                 onRequest(packetId, parsedMessage)
  292.             end
  293.         end
  294.     end
  295. end
  296.  
  297. awaitPackets(false)
  298.  
  299. --End of Shipnet.lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement