Advertisement
Guest User

buildimgs.lua

a guest
Jan 12th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.36 KB | None | 0 0
  1. local loaded_ld, ld = pcall(require, "LibDeflate")
  2. --options
  3. local opts = {...}
  4. print("[build] #opts "..#opts)
  5. local output = table.remove(opts,1)
  6. print("[build] output_base "..output)
  7. local extract_path = table.remove(opts,1)
  8. print("[build] extraction path "..extract_path)
  9. local files = opts
  10.  
  11.  
  12. --to turn a folder into a table
  13. local function gen_disk(ph)
  14.     sleep()
  15.     local path = ph or ""
  16.     local pth = fs.getName(path)
  17.     local tree = {}
  18.     for _,v in pairs(fs.list(path)) do
  19.         if fs.isDir(path.."/"..v) then
  20.             if verbosity > 1 then print("[build] heading down to "..path.."/"..v) end
  21.             tree[v] = gen_disk(path.."/"..v)
  22.         else
  23.             if verbosity > 1 then print("[build] adding "..path.."/"..v) end
  24.             local chandle = fs.open(path.."/"..v,'rb')
  25.             tree[v] = chandle.readAll()
  26.             chandle.close()
  27.         end
  28.     end
  29.     return tree
  30. end
  31. --we require LibDeflate for this (since we make compressed images)
  32. if not loaded_ld then error("Unnable to load LibDeflate"..ld) end
  33. --make sure the minimised and stripped LibDeflate is also here
  34. if not fs.exists(fs.combine(fs.getDir(shell.getRunningProgram()),"LD.lua")) then error("Unnable to locate minified LibDeflate") end
  35. --make sure all paths are valid
  36. print("[build] validating files")
  37. for _,v in ipairs(files) do
  38.     local v = shell.resolve(v)
  39.     if not fs.exists(v) then error("File/Folder does not exist: "..v) end
  40. end
  41. --get the base name for the outputs
  42. local output_base_name = shell.resolve(output)
  43. local final = {}
  44. --add files to the table
  45. print("[build] starting tree gen")
  46. for _,v in ipairs(files) do
  47.     local sr = shell.resolve(v)
  48.     print("[build] adding: "..v)
  49.     if fs.isDir(sr) then
  50.         final[fs.getName(v)] = gen_disk(sr)
  51.     else
  52.         local hand = fs.open(sr,'rb')
  53.         local con = hand.readAll()
  54.         final[fs.getName(v)] = con
  55.         hand.close()
  56.     end
  57. end
  58. --uncompressed serialize (and write)
  59. print("[build] serialising")
  60. local ser = textutils.serialise(final)
  61. print("[build] writing VFS")
  62. local handle = fs.open(output_base_name..".vfs",'wb')
  63.     handle.write(ser)
  64.     handle.close()
  65. print("[build] compressing")
  66. --compress it with Gzip
  67. local compressed = ld:CompressGzip(ser)
  68. print("[build] writing compressed")
  69. local handle = fs.open(output_base_name..".vgz",'wb')
  70.     handle.write(compressed)
  71.     handle.close()
  72. --now make a Self-Extracting-Archive
  73. print("[build] creating Self Extracting Archive")
  74. local output_file = fs.open(output_base_name..".lua",'wb')
  75. output_file.write('local I,c,o,f,C,t,s,D = table.unpack({\nloadstring([=[')
  76. local ldh = fs.open(fs.combine(fs.getDir(shell.getRunningProgram()),"LD.lua"),'rb')
  77. local ldc = ldh.readAll()
  78. output_file.write(ldc)
  79. ldh.close()
  80. output_file.write(']=])()\n,(function()local u,g = fs.open(shell.getRunningProgram(),"rb")g=u.readAll()u.close()return g:match("%[===%[(.+)%]===%]") end)(),shell.resolve(""),fs.open,fs.combine,type,shell.setDir,shell.dir()})\nfunction u(p,z)fs.makeDir(C(o,p))s(C(o,p))for k, v in pairs(z) do if t(v) == "table" then u(p.."/"..k,v)elseif t(v) == "string" then local h = f(fs.combine(o,C(p,k)),"wb")h.write(v)h.close()end end end u("')
  81. output_file.write(extract_path..'",textutils.unserialise(I:d(c)))s(o)')
  82. output_file.write('\n--[===[')
  83. output_file.write(compressed)
  84. output_file.write(']===]')
  85. print("[build] done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement