Advertisement
ecoMeco

Bundel

Mar 31st, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. --CC
  2. --Bundel by Admicos
  3.  
  4. -- BASE64 Encoding
  5.  
  6. -- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss <alexthkloss@web.de>
  7. -- licensed under the terms of the LGPL2
  8.  
  9. -- character table string
  10. local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  11.  
  12. -- encoding
  13. function enc(data)
  14.     return ((data:gsub('.', function(x)
  15.         local r,b='',x:byte()
  16.         for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
  17.         return r;
  18.     end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
  19.         if (#x < 6) then return '' end
  20.         local c=0
  21.         for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
  22.         return b:sub(c+1,c+1)
  23.     end)..({ '', '==', '=' })[#data%3+1])
  24. end
  25.  
  26. --ACTUAL PROGRAM STARTS NOW
  27.  
  28. local args = { ... }
  29.  
  30. local folder = args[1]
  31. local bundlefile = args[2]
  32.  
  33. local exportedTable = {}
  34.  
  35. local function bundle(bundlefolder)
  36.     for id, file in pairs(fs.list(bundlefolder)) do
  37.  
  38.         if fs.isDir(bundlefolder .. "/" .. file) then
  39.             print("[" .. id .. "] Bundling Directory " .. bundlefolder .. "/" .. file)
  40.             bundle(bundlefolder .. "/" .. file)
  41.         else
  42.             print("[" .. id .. "] Bundling " .. bundlefolder .. "/" .. file)
  43.  
  44.             local handle = fs.open(bundlefolder .. "/" .. file, "r")
  45.             if not handle then
  46.                 print("[" .. id .. "] Error occured bundling " .. bundlefolder .. "/" .. file)
  47.             end
  48.                 exportedTable[bundlefolder .. "/" .. file] = enc(handle.readAll())
  49.             handle.close()
  50.         end
  51.     end
  52. end
  53.  
  54. local function main()
  55.     local extractionScript = [[
  56.     --Bundel Extraction Script version 1.0
  57.  
  58.     -- BASE64 Encoding
  59.  
  60.     -- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss <alexthkloss@web.de>
  61.     -- licensed under the terms of the LGPL2
  62.  
  63.     -- character table string
  64.     local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  65.  
  66.     -- decoding
  67.     function dec(data)
  68.         data = string.gsub(data, '[^'..b..'=]', '')
  69.         return (data:gsub('.', function(x)
  70.             if (x == '=') then return '' end
  71.             local r,f='',(b:find(x)-1)
  72.             for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
  73.             return r;
  74.         end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
  75.             if (#x ~= 8) then return '' end
  76.             local c=0
  77.             for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
  78.             return string.char(c)
  79.         end))
  80.     end
  81.  
  82.     -- ACTUAL PROGRAM STARTS NOW
  83.  
  84.     print("Bundel by Admicos.")
  85.     print("\nExtracting bundle here.")
  86.  
  87.     for name, file in pairs(textutils.unserialize(bundle)) do
  88.         print("[#] Extracting " .. name)
  89.  
  90.         local handle = fs.open(shell.dir() .. "/" .. name, "w")
  91.             handle.write(dec(file))
  92.         handle.close()
  93.     end
  94.     ]]
  95.  
  96.     print("Bundel by Admicos.")
  97.     print("\nFolder to get bundled: " .. folder)
  98.     print("Resulting file: " .. bundlefile .. "\n")
  99.  
  100.     bundle(folder)
  101.  
  102.     print("\n[#] Saving bundle to file...")
  103.     local handle = fs.open(bundlefile, "w")
  104.         handle.write("local bundle = [[")
  105.         handle.writeLine(textutils.serialize(exportedTable))
  106.         handle.writeLine("]] --End of bundle \n\n")
  107.       print("[#] Appending extraction scripts...")
  108.         handle.write(extractionScript)
  109.     handle.close()
  110.  
  111.     print("\nBundling done. You can extract the bundle at any point by executing \"" .. bundlefile .. "\".")
  112. end
  113.  
  114. if #args < 2 then
  115.     print("bundel <folder to bundle> <bundled file name>")
  116. else
  117.  
  118.     if not fs.isDir(args[1]) then
  119.         print("Argument 1 must be a folder")
  120.     else
  121.         if fs.exists(args[2]) then
  122.             print("Argument 2 already exists.")
  123.         else
  124.             main()
  125.         end
  126.     end
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement