zefie

Minecraft High Speed Tape Dubbing Thing (OpenComputers + Computronics Tapes)

Feb 17th, 2021 (edited)
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.30 KB | None | 0 0
  1. local component = require("component")
  2. local shell = require("shell")
  3.  
  4. local buffer_size = 131072
  5. local drive_l_addr = "caea8bb0-4507-438f-bc74-45d24c1d5ec7"
  6. local drive_r_addr = "08fe3266-25c4-4391-afb4-06eed05e3b86"
  7. local drive_l
  8. local drive_r
  9.  
  10. local function formatKB(num)
  11.   return string.format("%02.f",math.floor(num / 1024)).."KB"
  12. end
  13.  
  14. local function getTapeDrive(address)
  15.   --Credits to gamax92 for this
  16.   --Taken crom Computronics tape.lua
  17.   local tape
  18.   if address then
  19.     if type(address) ~= "string" then
  20.       io.stderr:write("'address' may only be a string")
  21.       return
  22.     end
  23.     local fulladdr = component.get(address)
  24.     if fulladdr == nil then
  25.       io.stderr:write("No component at this address.")
  26.       return
  27.     end
  28.     if component.type(fulladdr) ~= "tape_drive" then
  29.       io.stderr:write("No tape drive at this address.")
  30.       return
  31.     end
  32.     tape = component.proxy(fulladdr)
  33.   else
  34.     tape = component.tape_drive
  35.   end
  36.   return tape
  37. end
  38.  
  39. drive_l = getTapeDrive(drive_l_addr)
  40. drive_r = getTapeDrive(drive_r_addr)
  41.  
  42. if not drive_l.isReady() then
  43.   io.stderr:write("Could not find tape in left drive.")
  44.   return
  45. end
  46.  
  47. if not drive_r.isReady() then
  48.   io.stderr:write("Could not find tape in right drive.")
  49.   return
  50. end
  51.  
  52. local srcsize = drive_l.getSize()
  53. local destsize = drive_r.getSize();
  54.  
  55. if srcsize > destsize then
  56.   io.stderr:write("Destination tape is smaller than source tape! Data will be truncated.")
  57. end
  58.  
  59. drive_r.setLabel(drive_l.getLabel())
  60.  
  61. drive_l.stop()
  62. drive_l.seek(-srcsize)
  63. drive_l.stop()
  64.  
  65. drive_r.stop()
  66. drive_r.seek(-drive_r.getSize())
  67. drive_r.stop()
  68.  
  69. local tread = 0
  70. local written = 0
  71. local bytes_read = 0
  72. local bytes_written = 0
  73. local buffer = {};
  74. local eot = false;
  75.  
  76. io.write("High-speed dubbing tape "..drive_l.getLabel().." ("..formatKB(srcsize)..") to tape in deck 2 ("..formatKB(drive_r.getSize())..")...\n")
  77.  
  78. repeat
  79.     if not drive_l.isReady() then
  80.       io.stderr:write("Could not find tape in left drive.")
  81.     end
  82.     buffer = drive_l.read(buffer_size,false)
  83.     tread = tread + #buffer
  84.     if not drive_r.isReady() then
  85.       io.stderr:write("Could not find tape in right drive.")
  86.     end
  87.     drive_r.write(buffer)
  88.     written = written + #buffer
  89.     io.write("Copying... Tape1 Pos: "..formatKB(tread).." - Tape2 Pos: "..formatKB(written).."\r")
  90.     if written >= destsize then
  91.       eot = true
  92.     end
  93.     --if we find three nil bytes in a row, assume silence
  94.     if drive_l.read(false) == 0 then
  95.       drive_l.seek(1)
  96.       if drive_l.read(true) == 0 then
  97.         if drive_l.read(true) == 0 then
  98.           io.write("\nDetected silence at end of recorded tape...\n")
  99.           eot = true
  100.         else
  101.           --unseek our check bytes
  102.           drive_l.seek(-2)
  103.         end
  104.       else
  105.         --unseek our check byte
  106.         drive_l.seek(-1)
  107.       end
  108.     end
  109. until eot
  110.  
  111. io.write("Wiping the remainder of the destination tape... ")
  112. while written < destsize do
  113.   buffer = string.rep("\xAA",buffer_size*2)
  114.   drive_r.write(buffer);
  115.   written = written + #buffer;
  116.   io.write("\rWiping the remainder of the destination tape... "..string.format("%02.f",math.floor(((written / destsize) * 100))).."%\r")
  117. end  
  118.  
  119.  
  120. drive_l.seek(-drive_l.getSize())
  121. drive_r.seek(-drive_r.getSize())
  122. io.write("\nCopy complete.")
Add Comment
Please, Sign In to add comment