Advertisement
faubiguy

Mag Card Writer

Mar 13th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.24 KB | None | 0 0
  1. arguments = { ... }
  2. if #arguments == 0 then
  3.  print("Usage: writeCode <path to code> [card label]")
  4.  error()
  5. end
  6. if not fs.exists(arguments[1]) then
  7.  print("Error: file not found")
  8.  error()
  9. end
  10. if fs.isDir(arguments[1]) then
  11.  print("Error: directory given")
  12.  error()
  13. end
  14. code = {}
  15. file = fs.open(arguments[1], "rb")
  16. while true do
  17.  local byte = file.read()
  18.  if not byte then break end
  19.  table.insert(code, string.char(byte))
  20. end
  21. code = table.concat(code)
  22. if code:len() > 100 then
  23.  print("Error: code length exceeds 100 bytes")
  24.  error()
  25. end
  26.  
  27. function constructLabel()
  28.  label = ""
  29.  for n = 2, #arguments do
  30.   label = label .. arguments[n]
  31.   if n < #arguments then label = label .. " " end
  32.  end
  33.  return label
  34. end
  35.  
  36. cardLabel = constructLabel()
  37. if cardLabel == "" then cardLabel = "No Label Given" end
  38.  
  39. local side = nil;
  40. for _,side1 in ipairs(rs.getSides()) do
  41.     if peripheral.getType(side1)=='mag card reader' then
  42.         side = side1
  43.     end
  44. end
  45.  
  46. if not side then
  47.     print("Error: No mag-card writer found")
  48.     error()
  49. end
  50.  
  51. peripheral.call(side, "beginWrite", code, cardLabel)
  52. while true do
  53.  event = os.pullEvent("mag_write_done")
  54.  if event == "mag_write_done" then
  55.   print("Successfully wrote code to magCard")
  56.   break
  57.  end
  58. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement