Guest User

pkg normal

a guest
Nov 26th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.03 KB | None | 0 0
  1. --vars
  2. local file = [[--Packaged by pkg, a Badly-Programmed-Smash-Things-Togetherer program.
  3. --(c) Haddock, 2016
  4. --Run me in a directory to extract it.
  5.  
  6. ]]
  7. local tArgs = {...}
  8. local files = {};
  9. local usingPassword = false
  10. local password = ""
  11.  
  12. --functions
  13. local function addFolder(path, tree)
  14.   for i, v in ipairs(fs.list(path)) do
  15.     local subPath = path .. '/' .. v
  16.     if v == '.DS_Store' or v == '.gitignore' or v == '.git' or subPath == '/rom' or subPath == '/'..shell.getRunningProgram() or v == '/'..shell.getRunningProgram() or v == "pkg" or v == "pkg.lua" or subPath == "pkg" or subPath == "pkg.lua" then
  17.         print("Not including " .. v)
  18.     elseif fs.isDir(subPath) then
  19.       tree[v] = {}
  20.       addFolder(subPath, tree[v])
  21.     else
  22.       local h = fs.open(subPath, 'r')
  23.       if h then
  24.         tree[v] = h.readAll()
  25.         h.close()
  26.       end
  27.     end
  28.   end
  29. end
  30.  
  31. local function rotateString(str, offset)
  32.   return (str:gsub('%a', function(s)
  33.     local base = s:lower() == s and ('a'):byte() or ('A'):byte()
  34.     return string.char(((s:byte() - base + offset)%26)+base)
  35.   end))
  36. end
  37.  
  38. --program
  39. addFolder(shell.dir(), files)
  40.  
  41. write("Do you want a password [Y/N]? ")
  42. inp = read()
  43. print("")
  44. if inp:lower() == "y" then
  45.     write("Password: ")
  46.     inp = read(" ")
  47.     write("Confirm: ")
  48.     inpp = read(" ")
  49.     if inp == inpp then
  50.         usingPassword = true
  51.         password = inpp
  52.     end
  53. end
  54. print("Packing...")
  55.  
  56. file = file .. 'local usingPassword = ' .. tostring(usingPassword) .. '\n'
  57. if usingPassword then
  58.     local rotBy = 0
  59.     for i=1,#password do
  60.         rotBy = rotBy + password:byte(i)
  61.     end
  62.     file = file .. 'local files = ' .. rotateString(textutils.serialize(files),rotBy) .. '\n'
  63. else
  64.     file = file .. 'local files = ' .. textutils.serialize(files) .. '\n'
  65. end
  66. file = file .. [[
  67.  
  68. --rotate encryption, pretty weak but it works
  69. local function rotateString(str, offset)
  70.   return (str:gsub('%a', function(s)
  71.     local base = s:lower() == s and ('a'):byte() or ('A'):byte()
  72.     return string.char(((s:byte() - base + offset)%26)+base)
  73.   end))
  74. end
  75.  
  76. local function extract()
  77.     local function node(path, tree)
  78.         if type(tree) == 'table' then
  79.             fs.makeDir(path)
  80.             for k, v in pairs(tree) do
  81.                 node(path .. '/' .. k, v)
  82.             end
  83.         else
  84.             local f = fs.open(path, 'w')
  85.             if f then
  86.                 f.write(tree)
  87.                 f.close()
  88.             end
  89.         end
  90.     end
  91.     if usingPassword then
  92.         print("This package is secured with a password.")
  93.         print("Inputting the wrong password will extract the package,")
  94.         print("but you will hold corrupt files.")
  95.         write("Password: ")
  96.         password = read(" ")
  97.         local rotBy = 0
  98.         for i=1,#password do
  99.             rotBy = rotBy - password:byte(i)
  100.         end
  101.        
  102.         files = textutils.unserialize(rotateString(textutils.serialize(files),rotBy))
  103.     end
  104.     node(shell.dir(), files)
  105. end
  106.  
  107. extract()
  108.  
  109. ]]
  110.  
  111. local h = fs.open(shell.dir() .. '/out.pkg', 'w')
  112. h.write(file)
  113. h.close()
  114. print("Package wrote to " .. shell.dir() .. "/out.pkg")
Advertisement
Add Comment
Please, Sign In to add comment