KittyCat156

sep

Jul 11th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. --Self Extracting Package Creator
  2. --Writes a script that extracts files, then runs a file.
  3. local srcFolder,dstFolder,fileName,isRel = ...
  4. isRel = not (isRel=="false" or isRel=="no" or isRel=="notRelative")
  5. local function addTable(t1,t2)
  6. local l = table.getn(t1)
  7. for k,v in pairs(t2) do
  8. t1[k+l]=v
  9. end
  10. return t1
  11. end
  12.  
  13. local function recursiveFileList(folder,ok,src)
  14. if not ok then src=folder end
  15. local absolute = shell.resolve(ok and fs.combine(src,folder) or folder)
  16. local content = fs.list(absolute)
  17. local toReturn = {}
  18. for k,v in pairs(content) do
  19. local c = fs.combine(absolute,v)
  20. if fs.isDir(c) then
  21. addTable(toReturn,recursiveFileList(ok and fs.combine(folder,v) or v,true,src))
  22. else
  23. toReturn[#toReturn+1]=ok and fs.combine(folder,v) or v
  24. end
  25. end
  26. return toReturn
  27. end
  28.  
  29. --Lua doesn't like : on literals
  30. --(literal) works though
  31. --
  32.  
  33. local function byt(c)
  34. return ("%02X"):format((c or ""):byte())
  35. end
  36.  
  37. local function bytnum(n)
  38. return ("%02X"):format(n)
  39. end
  40.  
  41. local function wordnum(n)
  42. return ("%04X"):format(n)
  43. end
  44.  
  45. local function dwordnum(n)
  46. return ("%08X"):format(n)
  47. end
  48.  
  49. local function encode(s)
  50. return s:gsub(".",byt)
  51. end
  52.  
  53. if srcFolder and dstFolder and fileName then
  54. local files = recursiveFileList(srcFolder)
  55. local outputHandle = fs.open(shell.resolve(fileName),"w")
  56. if outputHandle then
  57. --Write file names and lengths.
  58. outputHandle.writeLine("--Hexadecimal Self Extracting Package.")
  59. outputHandle.writeLine("--For each file, there's two bytes for name length,")
  60. outputHandle.writeLine("--its name, and four bytes for the filesize.")
  61. outputHandle.writeLine("--After that is the file's data.")
  62. outputHandle.write("local fileData = \"")
  63. local fileData = ""
  64. for k,v in pairs(files) do
  65. fileData=fileData..wordnum(#v)
  66. fileData=fileData..encode(v)
  67. fileData=fileData..dwordnum(fs.getSize(shell.resolve(fs.combine(srcFolder,v))))
  68. local inputHandle = fs.open(shell.resolve(fs.combine(srcFolder,v)),"rb")
  69. fileData=fileData..encode(inputHandle.readAll())
  70. inputHandle.close()
  71. end
  72. outputHandle.write(fileData)
  73. outputHandle.write("\"\nlocal isRel = "..(isRel and "true" or "false").."\nlocal dstFolder = \""..dstFolder.."\"\n")
  74. local decodeRoutine = [=[print("Unpacking files...")
  75. --Hex -> Text
  76. fileData = fileData:gsub("..",function(c) return string.char(tonumber(c,16) or 0) end)
  77. --Write files using text.
  78. while true do
  79. local nhi,nlo = fileData:byte(1,2)
  80. local nameLength = nhi*256+nlo
  81. local name = fileData:sub(3,2+nameLength)
  82. fileData=fileData:sub(nameLength+3)
  83. local hist,hier,hi,lo = fileData:byte(1,4)
  84. local fileLength = (hist*16777216)+(hier*65536)+(hi*256)+lo
  85. print(name..": "..fileLength.."B")
  86. fileData=fileData:sub(5)
  87. local fileContent = fileData:sub(1,fileLength)
  88. local outName = isRel and shell.resolve(fs.combine(dstFolder,name)) or fs.combine(dstFolder,name)
  89. local outputHandle = fs.open(outName,"w")
  90. if not outputHandle then
  91. print("Could not open file for writing.")
  92. else
  93. outputHandle.write(fileContent)
  94. outputHandle.close()
  95. end
  96. fileData = fileData:sub(fileLength+1)
  97. if #fileData<=0 then
  98. break
  99. end
  100. end
  101. shell.run((isRel and "" or "/")..fs.combine(dstFolder,"main.lua"))
  102. --Done!]=]
  103. outputHandle.writeLine(decodeRoutine)
  104. outputHandle.close()
  105. else
  106. print("Failed to open "..fileName.." for writing.")
  107. end
  108. else
  109. print("Self Extracting Package Creator")
  110. print("Usage: sep srcFolder dstFolder fileName [isRel]")
  111. print("where isRel is yes or no, defaulting to yes.")
  112. print("Set isRel to no if dstFolder is an absolute path.")
  113. print("Manually put .lua on the filename, it won't be put there automatically.")
  114. end
Advertisement
Add Comment
Please, Sign In to add comment