Advertisement
cruftbrew

Untitled

Mar 28th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- File Updater Program
  2.  
  3. -- Varibles
  4.  
  5. local filesrv = 57  -- Your update server ID.
  6. local cmptype = "client"
  7.  
  8. -- setup rednet
  9. local sModemSide = nil
  10. for n,sSide in ipairs(rs.getSides()) do
  11.   if peripheral.getType(sSide) == "modem" then
  12.     sModemSide = sSide
  13.     break
  14.   end
  15. end
  16. if sModemSide == nil then
  17.   print("No wireless modem attached")
  18.   return
  19. end
  20. rednet.open(sModemSide)
  21.  
  22. -- end setup rednet
  23.  
  24.  
  25.  
  26. -- *************
  27. -- * Functions *
  28. -- *************
  29.  
  30. -- Receive File.
  31. local function receiveFile()
  32.     shell.run("id")
  33.     print("Waiting for a bt connection...")
  34.     rednet.send(filesrv,"update_client")
  35.    
  36.     while true do
  37.         local id,msg
  38.         local id,msg,p3 = rednet.receive()
  39.        
  40.         if msg == "bt.eol" then printError("Finished file update") shell.run("delete fileupdate") shell.run("rename fileupdater fileupdate") shell.run("startup") error() end
  41.             if msg == "bt.con" then
  42.                 print("Connected to " .. id .. "!")
  43.                 local remoteID = id
  44.                 rednet.send(id, "bt.acc")
  45.                
  46.                 local fileName
  47.                
  48.                 print("Getting the file name...")
  49.                
  50.                 local id,msg = "",""
  51.                 while true do
  52.                     id,msg = rednet.receive()
  53.                    
  54.                     if id == remoteID and string.sub(msg, 1, #"bt.fn:") == "bt.fn:" then
  55.                         fileName = string.sub(msg, #"bt.fn:" + 1)
  56.                         break
  57.                     end
  58.                 end
  59.    
  60.                 if fs.exists(fileName) then
  61.                     print("File already exists, Deleting old file... " .. fileName)
  62.                     fs.delete(fileName)
  63.                 end
  64.                
  65.                 print("Receiving file: " .. fileName)
  66.                
  67.                 local file = fs.open(fileName, "w")
  68.    
  69.                 local id,msg
  70.    
  71.                 while true do
  72.                     id,msg = rednet.receive()
  73.                     if id == remoteID then
  74.                         if string.sub(msg, 1, #"bt.file:") == "bt.file:" then
  75.                             local subbedData = string.sub(msg, #"bt.file:" + 1)
  76.                        
  77.                             if term.isColor() then
  78.                                 term.setTextColor(colors.lightBlue)
  79.                             end
  80.                            
  81.                             file.writeLine(subbedData)
  82.                         elseif msg == "bt.done" then
  83.                             file.flush()
  84.                             file.close()
  85.                        
  86.                             if term.isColor() then
  87.                                 term.setTextColor(colors.white)
  88.                             end
  89.                             print("Received file!")
  90.                        
  91.                             break
  92.                         end
  93.                     end
  94.                 end
  95.             end
  96.     end
  97. end
  98.  
  99.  
  100.  
  101. -- Main Program
  102. print("updating files")
  103.  
  104. receiveFile()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement