Advertisement
justync7

tape

May 18th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.79 KB | None | 0 0
  1. --[[ tape program, provides basic tape modification and access tools
  2. Authors: gamax92, Bizzycola, Vexatos
  3. fixed writing a bit and added http downloading
  4. Secondary author: Justyn (github.com/lustyn/tapestream)
  5. ]]
  6. local args = { ... }
  7. local tape = peripheral.find("tape_drive")
  8. if not tape then
  9.   print("This program requires a tape drive to run.")
  10.   return
  11. end
  12.  
  13. local function printUsage()
  14.   print("Usage:")
  15.   print(" - 'tape play' to start playing a tape")
  16.   print(" - 'tape pause' to pause playing the tape")
  17.   print(" - 'tape stop' to stop playing and rewind the tape")
  18.   print(" - 'tape rewind' to rewind the tape")
  19.   print(" - 'tape wipe' to wipe any data on the tape and erase it completely")
  20.   print(" - 'tape label [name]' to label the tape, leave 'name' empty to get current label")
  21.   print(" - 'tape speed <speed>' to set the playback speed. Needs to be between 0.25 and 2.0")
  22.   print(" - 'tape volume <volume>' to set the volume of the tape. Needs to be between 0.0 and 1.0")
  23.   print(" - 'tape write <path/of/audio/file>' to write to the tape from a file")
  24.   return
  25. end
  26.  
  27. if not tape.isReady() then
  28.   printError("The tape drive does not contain a tape.")
  29.   return
  30. end
  31.  
  32. local function label(name)
  33.   if not name then
  34.     if tape.getLabel() == "" then
  35.       print("Tape is currently not labeled.")
  36.       return
  37.     end
  38.     print("Tape is currently labeled: " .. tape.getLabel())
  39.     return
  40.   end
  41.   tape.setLabel(name)
  42.   print("Tape label set to " .. name)
  43. end
  44.  
  45. local function rewind()
  46.   print("Rewound tape")
  47.   tape.seek(-tape.getSize())
  48. end
  49.  
  50. local function play()
  51.   if tape.getState() == "PLAYING" then
  52.     print("Tape is already playing")
  53.   else
  54.     tape.play()
  55.     print("Tape started")
  56.   end
  57. end
  58.  
  59. local function stop()
  60.   if tape.getState() == "STOPPED" then
  61.     print("Tape is already stopped")
  62.   else
  63.     tape.stop()
  64.     tape.seek(-tape.getSize())
  65.     print("Tape stopped")
  66.   end
  67. end
  68.  
  69. local function pause()
  70.   if tape.getState() == "STOPPED" then
  71.     print("Tape is already paused")
  72.   else
  73.     tape.stop()
  74.     print("Tape paused")
  75.   end
  76. end
  77.  
  78. local function speed(sp)
  79.   local s = tonumber(sp)
  80.   if not s or s < 0.25 or s > 2 then
  81.     printError("Speed needs to be a number between 0.25 and 2.0")
  82.     return
  83.   end
  84.   tape.setSpeed(s)
  85.   print("Playback speed set to " .. sp)
  86. end
  87.  
  88. local function volume(vol)
  89.   local v = tonumber(vol)
  90.   if not v or v < 0 or v > 1 then
  91.     printError("Volume needs to be a number between 0.0 and 1.0")
  92.     return
  93.   end
  94.   tape.setVolume(v)
  95.   print("Volume set to " .. vol)
  96. end
  97.  
  98. local function confirm(msg)
  99.   print(msg)
  100.   print("Type `y` to confirm, `n` to cancel.")
  101.   repeat
  102.     local response = read()
  103.     if response and response:lower():sub(1, 1) == "n" then
  104.       print("Canceled.")
  105.       return false
  106.     end
  107.   until response and response:lower():sub(1, 1) == "y"
  108.   return true
  109. end
  110.  
  111. local function wipe()
  112.   if not confirm("Are you sure you want to wipe this tape?") then return end
  113.   local k = tape.getSize()
  114.   tape.stop()
  115.   tape.seek(-k)
  116.   tape.stop() --Just making sure
  117.   tape.seek(-90000)
  118.   local s = string.rep("\xAA", 8192)
  119.   for i = 1, k + 8191, 8192 do
  120.     tape.write(s)
  121.   end
  122.   tape.seek(-k)
  123.   tape.seek(-90000)
  124.   print("Done.")
  125. end
  126.  
  127. local function writeTape(relPath)
  128.   local file, msg, _, y, success
  129.   local block = 8192 --How much to read at a time
  130.  
  131.   if not confirm("Are you sure you want to write to this tape?") then return end
  132.   tape.stop()
  133.   tape.seek(-tape.getSize())
  134.   tape.stop() --Just making sure
  135.  
  136.   local path = shell.resolve(relPath)
  137.   local bytery = 0 --For the progress indicator
  138.   local filesize = fs.getSize(path)
  139.   print("Path: " .. path)
  140.   file, msg = fs.open(path, "rb")
  141.   if not fs.exists(path) then msg = "file not found" end
  142.   if not file then
  143.     printError("Failed to open file " .. relPath .. (msg and ": " .. tostring(msg)) or "")
  144.     return
  145.   end
  146.  
  147.   print("Writing...")
  148.  
  149.   _, y = term.getCursorPos()
  150.  
  151.   if filesize > tape.getSize() then
  152.     term.setCursorPos(1, y)
  153.     printError("Error: File is too large for tape, shortening file")
  154.     _, y = term.getCursorPos()
  155.     filesize = tape.getSize()
  156.   end
  157.  
  158.   repeat
  159.     local bytes = {}
  160.     for i = 1, block do
  161.       local byte = file.read()
  162.       if not byte then break end
  163.       bytes[#bytes + 1] = byte
  164.     end
  165.     if #bytes > 0 then
  166.       if not tape.isReady() then
  167.         io.stderr:write("\nError: Tape was removed during writing.\n")
  168.         file.close()
  169.         return
  170.       end
  171.       term.setCursorPos(1, y)
  172.       bytery = bytery + #bytes
  173.       term.write("Read " .. tostring(math.min(bytery, filesize)) .. " of " .. tostring(filesize) .. " bytes...")
  174.       for i = 1, #bytes do
  175.         tape.write(bytes[i])
  176.       end
  177.       sleep(0)
  178.     end
  179.   until not bytes or #bytes <= 0 or bytery > filesize
  180.   file.close()
  181.   tape.stop()
  182.   tape.seek(-tape.getSize())
  183.   tape.stop() --Just making sure
  184.   print("\nDone.")
  185. end
  186.  
  187. local function downloadTape(url)
  188.   local file, msg, _, y, success
  189.   local block = 8192 --How much to read at a time
  190.  
  191.   if not confirm("Are you sure you want to download to this tape?") then return end
  192.   tape.stop()
  193.   tape.seek(-tape.getSize())
  194.   tape.stop() --Just making sure
  195.  
  196.   local bytery = 0 --For the progress indicator
  197.   print("URL: " .. url)
  198.   file, msg = http.get(url, nil, true)
  199.   if not file then
  200.     printError("Failed to open url " .. url .. (msg and ": " .. tostring(msg)) or "")
  201.     return
  202.   end
  203.  
  204.   print("Writing...")
  205.  
  206.   _, y = term.getCursorPos()
  207.  
  208.   repeat
  209.     local bytes = {}
  210.     for i = 1, block do
  211.       local byte = file.read()
  212.       if not byte then break end
  213.       bytes[#bytes + 1] = byte
  214.     end
  215.     if #bytes > 0 then
  216.       if not tape.isReady() then
  217.         io.stderr:write("\nError: Tape was removed during writing.\n")
  218.         file.close()
  219.         return
  220.       end
  221.       term.setCursorPos(1, y)
  222.       bytery = bytery + #bytes
  223.       term.write("Read " .. tostring(bytery) .." bytes...")
  224.       for i = 1, #bytes do
  225.         tape.write(bytes[i])
  226.       end
  227.       sleep(0)
  228.     end
  229.   until not bytes or #bytes <= 0 or bytery > tape.getSize()
  230.   file.close()
  231.   tape.stop()
  232.   tape.seek(-tape.getSize())
  233.   tape.stop() --Just making sure
  234.   print("\nDone.")
  235. end
  236.  
  237. if args[1] == "play" then
  238.   play()
  239. elseif args[1] == "stop" then
  240.   stop()
  241. elseif args[1] == "pause" then
  242.   pause()
  243. elseif args[1] == "rewind" then
  244.   rewind()
  245. elseif args[1] == "label" then
  246.   label(args[2])
  247. elseif args[1] == "speed" then
  248.   speed(args[2])
  249. elseif args[1] == "volume" then
  250.   volume(args[2])
  251. elseif args[1] == "write" then
  252.   writeTape(args[2], args[3])
  253. elseif args[1] == "download" then
  254.   print(textutils.serialize(args))
  255.   print(args[3])
  256.   downloadTape(args[2], args[3])
  257. elseif args[1] == "wipe" then
  258.   wipe()
  259. else
  260.   printUsage()
  261. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement