Advertisement
Tatantyler

ComputerCraft Package Manager

Oct 1st, 2012
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.93 KB | None | 0 0
  1. local fileHierarchy = {}
  2. local args = {...}
  3.  
  4. -- fileHierarchy Structure:
  5. -- {["dir1"] = true, ["file1"] = {false, content}, ["dir1/file2"] = {false, content}, ["dir1/dir2"] = true }
  6.  
  7. if fs.exists(args[1]) then
  8.     local fileHandle = fs.open(args[1], "r")
  9.     local fileHierarchyString = fileHandle.readAll()
  10.     fileHierarchy = textutils.unserialize(fileHierarchyString)
  11.     fileHandle.close()
  12. end
  13.  
  14. local function unpackZip(destination)
  15.     for i,v in pairs(fileHierarchy) do
  16.         if v == true then
  17.             fs.makeDir( fs.combine(destination, i) )
  18.         else
  19.             local namestart = string.find(i, fs.getName(i))
  20.             namestart = namestart-1
  21.             local basePath = string.sub(i, 1, namestart)
  22.             if not fs.exists(fs.combine(destination, basePath)) then
  23.                 fs.makeDir(fs.combine(destination, basePath))
  24.             end
  25.             local handle = fs.open( fs.combine(destination, i) , "w")
  26.             handle.write(v)
  27.             handle.close()
  28.         end
  29.     end
  30. end
  31.  
  32. local function flushZip()
  33.     local fileHandle = fs.open(args[1], "w")
  34.     fileHandle.write( textutils.serialize(fileHierarchy) )
  35.     fileHandle.close()
  36. end
  37.  
  38. local function addDirectory(directory)
  39.     if fileHierarchy[directory] == nil then
  40.         fileHierarchy[directory] = true
  41.         return true
  42.     else
  43.         return false
  44.     end
  45. end
  46.  
  47. local function addFile(localFilePath, zipFilePath)
  48.     if fs.exists(localFilePath) and fileHierarchy[zipFilePath] == nil then
  49.         local handle = fs.open(localFilePath, "r")
  50.         fileHierarchy[zipFilePath] = handle.readAll()
  51.         handle.close()
  52.         return true
  53.     else
  54.         return false
  55.     end
  56. end
  57.  
  58. local function moveFile(oldFilePath, newFilePath)
  59.     if fileHierarchy[oldFilePath] ~= nil then
  60.         fileHierarchy[newFilePath] = fileHierarchy[oldFilePath]
  61.         fileHierarchy[oldFilePath] = nil
  62.         return true
  63.     else
  64.         return false
  65.     end
  66. end
  67.  
  68. local function copyFile(oldFilePath, newFilePath)
  69.     if fileHierarchy[oldFilePath] ~= nil then
  70.         fileHierarchy[newFilePath] = fileHierarchy[oldFilePath]
  71.         return true
  72.     else
  73.         return false
  74.     end
  75. end
  76.  
  77. local function deleteFile(filePath)
  78.     if fileHierarchy[filePath] ~= nil then
  79.         fileHierarchy[filePath] = nil
  80.         return true
  81.     else
  82.         return false
  83.     end
  84. end
  85.  
  86. local function showMenu()
  87.     while true do
  88.         term.clear()
  89.         term.setCursorPos(1,1)
  90.         io.write("Package Manager V1")
  91.         term.setCursorPos(1,2)
  92.         io.write("1 - Add File")
  93.         term.setCursorPos(1,3)
  94.         io.write("2 - Delete File")
  95.         term.setCursorPos(1,4)
  96.         io.write("3 - Copy File")
  97.         term.setCursorPos(1,5)
  98.         io.write("4 - Move File")
  99.         term.setCursorPos(1,6)
  100.         io.write("5 - Add Directory")
  101.         term.setCursorPos(1,7)
  102.         io.write("6 - Add Existing Directory")
  103.         term.setCursorPos(1,8)
  104.         io.write("7 - List Files in Package")
  105.         term.setCursorPos(1,9)
  106.         io.write("8 - Extract Package")
  107.         term.setCursorPos(1,10)
  108.         io.write("9 - Exit")
  109.         local maxX, maxY = term.getSize()
  110.         term.setCursorPos(1, maxY-1)
  111.         io.write("Open:")
  112.         term.setCursorPos(1, maxY)
  113.         io.write(args[1])
  114.         term.setCursorPos(1, 11)
  115.         local event, key = os.pullEvent("key")
  116.         if key == 2 then
  117.             term.setCursorBlink(true)
  118.             print("Please type in the file's path on your computer. >")
  119.             local filePath = io.read()
  120.             print("Please type in where the file should be located in the package. >")
  121.             local zipPath = io.read()
  122.             if addFile(filePath, zipPath) then
  123.                 print("Operation successful.")
  124.             else
  125.                 print("Operation failed. Does the file exist?")
  126.             end
  127.             term.setCursorBlink(false)
  128.         elseif key == 3 then
  129.             term.setCursorBlink(true)
  130.             io.write("Please type in the path to the file to be deleted. >")
  131.             local filePath = io.read()
  132.             if deleteFile(filePath) then
  133.                 print("Operation successful.")
  134.             else
  135.                 print("Operation failed. Does the file exist?")
  136.             end
  137.             term.setCursorBlink(false)
  138.         elseif key == 4 then
  139.             term.setCursorBlink(true)
  140.             print("Please type in the file's current path. >")
  141.             local filePath = io.read()
  142.             print("Please type in where the file will be copied to. >")
  143.             local copyPath = io.read()
  144.             if copyFile(filePath, copyPath) then
  145.                 print("Operation successful.")
  146.             else
  147.                 print("Operation failed. Does the file exist?")
  148.             end
  149.             term.setCursorBlink(false)
  150.         elseif key == 5 then
  151.             term.setCursorBlink(true)
  152.             print("Please type in the file's current path. >")
  153.             local filePath = io.read()
  154.             print("Please type in where the file will be moved to. >")
  155.             local movePath = io.read()
  156.             if moveFile(filePath, movePath) then
  157.                 print("Operation successful.")
  158.             else
  159.                 print("Operation failed. Does the file exist?")
  160.             end
  161.             term.setCursorBlink(false)
  162.         elseif key == 6 then
  163.             term.setCursorBlink(true)
  164.             print("Please type in the new directory's full path >")
  165.             local dirPath = io.read()
  166.             if addDirectory(dirPath) then
  167.                 print("Operation successful.")
  168.             else
  169.                 print("Operation failed. Does the directory already exist?")
  170.             end
  171.             term.setCursorBlink(false)
  172.         elseif key == 7 then
  173.             term.setCursorBlink(true)
  174.             print("Please type in the directory's full path >")
  175.             local dirPath = io.read()
  176.             if fs.exists(dirPath) and fs.isDir(dirPath) then
  177.                 for i,v in ipairs(fs.list(dirPath)) do
  178.                     if not addFile(fs.combine(dirPath,v), v) then
  179.                         print("Operation failed. Does the file exist?")
  180.                         break
  181.                     end
  182.                 end
  183.                 print("Operation successful.")
  184.             end
  185.             term.setCursorBlink(false)
  186.         elseif key == 8 then
  187.             term.clear()
  188.             term.setCursorPos(1,1)
  189.             for i,v in pairs(fileHierarchy) do
  190.                 print(i)
  191.             end
  192.         elseif key == 9 then
  193.             term.setCursorBlink(true)
  194.             print("Please type in where to extract the package to. >")
  195.             local destination = io.read()
  196.             if not fs.exists(destination) then
  197.                 fs.makeDir(destination)
  198.             end
  199.             unpackZip(destination)
  200.             term.setCursorBlink(false)
  201.         elseif key == 10 then
  202.             flushZip()
  203.             term.clear()
  204.             term.setCursorPos(1,1)
  205.             return
  206.         end
  207.         flushZip()
  208.         if key ~= 8 then
  209.             term.setCursorPos(1, maxY-2)
  210.         else
  211.             term.setCursorPos(1, maxY)
  212.         end
  213.         io.write("Press any key to continue.")
  214.         os.pullEvent("key")
  215.     end
  216. end
  217.  
  218. showMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement