Advertisement
1lann

Mag-Card Utility

Aug 19th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 KB | None | 0 0
  1. local reader = peripheral.wrap("left")
  2.  
  3. if not reader then
  4.     error("No card reader found!")
  5. end
  6.  
  7. reader.cancelWrite()
  8.  
  9. local cardBackup = "/card-data"
  10.  
  11. local function readCard()
  12.     reader.setInsertCardLight(true)
  13.     while true do
  14.         local e, data, side = os.pullEvent()
  15.         if e == "mag_swipe" then
  16.             reader.setInsertCardLight(false)
  17.             return data
  18.         end
  19.     end
  20. end
  21.  
  22. local function writeCard(data, label)
  23.     local success, err = reader.beginWrite(data, label)
  24.     if success then
  25.         while true do
  26.             local e = os.pullEvent()
  27.             if e == "mag_write_done" then
  28.                 reader.cancelWrite()
  29.                 return
  30.             end
  31.         end
  32.     else
  33.         error("Error while writing card: "..err)
  34.     end
  35. end
  36.  
  37. local args = {...}
  38.  
  39. local arg = args[1]
  40.  
  41. if not fs.exists(cardBackup) then
  42.     print("A backup of your card is required")
  43.     term.setTextColor(colors.yellow)
  44.     print("Please insert your mag-card")
  45.  
  46.     cardData = readCard()
  47.  
  48.     local f = io.open(cardBackup, "w")
  49.     f:write(cardData)
  50.     f:close()
  51.     print("Card data saved!")
  52. end
  53.  
  54. if arg == "backup" then
  55.     if fs.exists(cardBackup) then
  56.         if fs.exists(cardBackup.."-old") then
  57.             fs.delete(cardBackup.."-old")
  58.         end
  59.         fs.move(cardBackup, cardBackup.."-old")
  60.     end
  61.  
  62.     term.setTextColor(colors.yellow)
  63.     print("Insert the card you wish to backup")
  64.  
  65.     cardData = readCard()
  66.  
  67.     local f = io.open(cardBackup, "w")
  68.     f:write(cardData)
  69.     f:close()
  70.  
  71.     print("Card data saved!")
  72. elseif arg == "pattern" then
  73.     term.setTextColor(colors.yellow)
  74.     print("Insert card to write")
  75.     writeCard("[", "Pattern Exploit Card")
  76.     term.setTextColor(colors.white)
  77.     print("Card written!")
  78. elseif arg == "exists" then
  79.     term.setTextColor(colors.yellow)
  80.     print("Insert card to write")
  81.     writeCard("../rom/programs/help", "Exists Exploit Card")
  82.     term.setTextColor(colors.white)
  83.     print("Card written!")
  84. elseif arg == "restore" then
  85.     local readLocation = cardBackup
  86.     if args[2] then
  87.         readLocation = args[2]
  88.     end
  89.  
  90.     if fs.exists(readLocation) and not fs.isDir(readLocation) then
  91.         local f = io.open(readLocation)
  92.         local cardData = f:read("*a")
  93.         f:close()
  94.  
  95.         term.setTextColor(colors.yellow)
  96.         print("Insert card to write")
  97.         writeCard(cardData, "Restored Card")
  98.         term.setTextColor(colors.white)
  99.         print("Card written!")
  100.     else
  101.         print("No card data found!")
  102.     end
  103. elseif arg == "overwrite" then
  104.     term.setTextColor(colors.yellow)
  105.     print("Enter data to write")
  106.     write("> ")
  107.     local data = read()
  108.     print("Insert card to write")
  109.     writeCard(data, "Overwritten Card")
  110.     term.setTextColor(colors.white)
  111.     print("Card written!")
  112. elseif arg == "read" then
  113.     term.setTextColor(colors.yellow)
  114.     print("Insert the card to read")
  115.     local data = readCard()
  116.     term.setTextColor(colors.white)
  117.     print("The card reads:")
  118.     print(data)
  119. elseif arg == "label" then
  120.     term.setTextColor(colors.yellow)
  121.     write("Enter a label: ")
  122.     local label = read()
  123.     print("Insert the card you want to label twice")
  124.  
  125.     cardData = readCard()
  126.  
  127.     print("And again")
  128.  
  129.     writeCard(cardData, label)
  130.  
  131.     term.setTextColor(colors.white)
  132.     print("Done! The new label has been written")
  133. else
  134.     term.setTextColor(colors.white)
  135.     print("Run the program with an argument:")
  136.     print("backup, pattern, exists, restore, overwrite, read")
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement