Advertisement
Oleshe

tape lib

Feb 9th, 2023
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.38 KB | None | 0 0
  1. local component = require("component")
  2. local fs = require("filesystem")
  3. local tapee = {}
  4. local options = {}
  5.  
  6. local function getTapeDrive()
  7.   --Credits to gamax92 for this
  8.   local tape
  9.   if options.address then
  10.     if type(options.address) ~= "string" then
  11.       say("'address' may only be a string.")
  12.       return
  13.     end
  14.     local fulladdr = component.get(options.address)
  15.     if fulladdr == nil then
  16.       say("No component at this address.")
  17.       return
  18.     end
  19.     if component.type(fulladdr) ~= "tape_drive" then
  20.       say("No tape drive at this address.")
  21.       return
  22.     end
  23.     tape = component.proxy(fulladdr)
  24.   else
  25.     tape = component.tape_drive
  26.   end
  27.   return tape
  28.   --End of gamax92's part
  29. end
  30.  
  31. local tape = getTapeDrive()
  32.  
  33. if not tape.isReady() then
  34.   say("The tape drive does not contain a tape.")
  35.   return
  36. end
  37.  
  38. function tapee.label(name)
  39.   if not name then
  40.     if tape.getLabel() == "" then
  41.       say("Tape is currently not labeled.")
  42.       return
  43.     end
  44.     say("Tape is currently labeled: " .. tape.getLabel())
  45.     return
  46.   end
  47.   tape.setLabel(name)
  48.   say("Tape label set to " .. name)
  49. end
  50. function tapee.rewind()
  51.   say("Rewound tape")
  52.   tape.seek(-tape.getSize())
  53. end
  54.  
  55. function tapee.play()
  56.   if tape.getState() == "PLAYING" then
  57.     say("Tape is already playing")
  58.   else
  59.     tape.play()
  60.     say("Tape started")
  61.   end
  62. end
  63. function tapee.stop()
  64.   if tape.getState() == "STOPPED" then
  65.     say("Tape is already stopped")
  66.   else
  67.     tape.stop()
  68.     tape.seek(-tape.getSize())
  69.     say("Tape stopped")
  70.   end
  71. end
  72. function tapee.pause()
  73.   if tape.getState() == "STOPPED" then
  74.     say("Tape is already paused")
  75.   else
  76.     tape.stop()
  77.     say("Tape paused")
  78.   end
  79. end
  80.  
  81. function tapee.speed(sp)
  82.   local s = tonumber(sp)
  83.   if not s or s < 0.25 or s > 2 then
  84.     say("Speed needs to be a number between 0.25 and 2.0")
  85.     return
  86.   end
  87.   tape.setSpeed(s)
  88.   say("Playback speed set to " .. sp)
  89. end
  90.  
  91. function tapee.volume(vol)
  92.   local v = tonumber(vol)
  93.   if not v or v < 0 or v > 1 then
  94.     say("Volume needs to be a number between 0.0 and 1.0")
  95.     return
  96.   end
  97.   tape.setVolume(v)
  98.   say("Volume set to " .. vol)
  99. end
  100.  
  101. function confirm(msg)
  102.   if not options.y then
  103.     say(msg)
  104.     say("Type `y` to confirm, `n` to cancel.")
  105.     repeat
  106.       local response = answer()
  107.       if response and response:lower():sub(1, 1) == "n" then
  108.         say("Canceled.")
  109.         return false
  110.       end
  111.     until response and response:lower():sub(1, 1) == "y"
  112.   end
  113.   return true
  114. end
  115.  
  116. function tapee.wipe()
  117.   if not confirm("Are you sure you want to wipe this tape?") then return end
  118.   local k = tape.getSize()
  119.   tape.stop()
  120.   tape.seek(-k)
  121.   tape.stop() --Just making sure
  122.   tape.seek(-90000)
  123.   local s = string.rep("\xAA", 8192)
  124.   for i = 1, k + 8191, 8192 do
  125.     tape.write(s)
  126.   end
  127.   tape.seek(-k)
  128.   tape.seek(-90000)
  129.   say("Done.")
  130. end
  131.  
  132. function tapee.writeTape(path)
  133.   local file, msg, _, y
  134.   local block = 2048 --How much to read at a time
  135.   if not confirm("Are you sure you want to write to this tape?") then return end
  136.   tape.stop()
  137.   tape.seek(-tape.getSize())
  138.   tape.stop() --Just making sure
  139.  
  140.   local bytery = 0 --For the progress indicator
  141.   local filesize = tape.getSize()
  142.     filesize = fs.size(path)
  143.     say("Path: " .. path)
  144.     file, msg = fs.open(path, "rb")
  145.     if not file then
  146.       say("Error: " .. msg)
  147.       return
  148.     end
  149.  
  150.     say("Writing...")
  151.   end
  152.  
  153.   if filesize > tape.getSize() then
  154.     say("Warning: File is too large for tape, shortening file")
  155.     filesize = tape.getSize()
  156.   end
  157.  
  158.   --Displays long numbers with commas
  159.   local function fancyNumber(n)
  160.     return tostring(math.floor(n)):reverse():gsub("(%d%d%d)", "%1,"):gsub("%D$", ""):reverse()
  161.   end
  162.  
  163.   repeat
  164.     local bytes = file:read(block)
  165.     if bytes and #bytes > 0 then
  166.       if not tape.isReady() then
  167.         say("Error: Tape was removed during writing.")
  168.         file:close()
  169.         return
  170.       end
  171.       bytery = bytery + #bytes
  172.       local displaySize = math.min(bytery, filesize)
  173.       say(string.format("Read %s of %s bytes... (%.2f %%)", fancyNumber(displaySize), fancyNumber(filesize), 100 * displaySize / filesize))
  174.       tape.write(bytes)
  175.     end
  176.   until not bytes or bytery > filesize
  177.   file:close()
  178.   tape.stop()
  179.   tape.seek(-tape.getSize())
  180.   tape.stop() --Just making sure
  181.   say("Done.")
  182. end
  183.  
  184. return tapee
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement