Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Self Extracting Package Creator
- --Writes a script that extracts files, then runs a file.
- local srcFolder,dstFolder,fileName,isRel = ...
- isRel = not (isRel=="false" or isRel=="no" or isRel=="notRelative")
- local function addTable(t1,t2)
- local l = table.getn(t1)
- for k,v in pairs(t2) do
- t1[k+l]=v
- end
- return t1
- end
- local function recursiveFileList(folder,ok,src)
- if not ok then src=folder end
- local absolute = shell.resolve(ok and fs.combine(src,folder) or folder)
- local content = fs.list(absolute)
- local toReturn = {}
- for k,v in pairs(content) do
- local c = fs.combine(absolute,v)
- if fs.isDir(c) then
- addTable(toReturn,recursiveFileList(ok and fs.combine(folder,v) or v,true,src))
- else
- toReturn[#toReturn+1]=ok and fs.combine(folder,v) or v
- end
- end
- return toReturn
- end
- --Lua doesn't like : on literals
- --(literal) works though
- --
- local function byt(c)
- return ("%02X"):format((c or ""):byte())
- end
- local function bytnum(n)
- return ("%02X"):format(n)
- end
- local function wordnum(n)
- return ("%04X"):format(n)
- end
- local function dwordnum(n)
- return ("%08X"):format(n)
- end
- local function encode(s)
- return s:gsub(".",byt)
- end
- if srcFolder and dstFolder and fileName then
- local files = recursiveFileList(srcFolder)
- local outputHandle = fs.open(shell.resolve(fileName),"w")
- if outputHandle then
- --Write file names and lengths.
- outputHandle.writeLine("--Hexadecimal Self Extracting Package.")
- outputHandle.writeLine("--For each file, there's two bytes for name length,")
- outputHandle.writeLine("--its name, and four bytes for the filesize.")
- outputHandle.writeLine("--After that is the file's data.")
- outputHandle.write("local fileData = \"")
- local fileData = ""
- for k,v in pairs(files) do
- fileData=fileData..wordnum(#v)
- fileData=fileData..encode(v)
- fileData=fileData..dwordnum(fs.getSize(shell.resolve(fs.combine(srcFolder,v))))
- local inputHandle = fs.open(shell.resolve(fs.combine(srcFolder,v)),"rb")
- fileData=fileData..encode(inputHandle.readAll())
- inputHandle.close()
- end
- outputHandle.write(fileData)
- outputHandle.write("\"\nlocal isRel = "..(isRel and "true" or "false").."\nlocal dstFolder = \""..dstFolder.."\"\n")
- local decodeRoutine = [=[print("Unpacking files...")
- --Hex -> Text
- fileData = fileData:gsub("..",function(c) return string.char(tonumber(c,16) or 0) end)
- --Write files using text.
- while true do
- local nhi,nlo = fileData:byte(1,2)
- local nameLength = nhi*256+nlo
- local name = fileData:sub(3,2+nameLength)
- fileData=fileData:sub(nameLength+3)
- local hist,hier,hi,lo = fileData:byte(1,4)
- local fileLength = (hist*16777216)+(hier*65536)+(hi*256)+lo
- print(name..": "..fileLength.."B")
- fileData=fileData:sub(5)
- local fileContent = fileData:sub(1,fileLength)
- local outName = isRel and shell.resolve(fs.combine(dstFolder,name)) or fs.combine(dstFolder,name)
- local outputHandle = fs.open(outName,"w")
- if not outputHandle then
- print("Could not open file for writing.")
- else
- outputHandle.write(fileContent)
- outputHandle.close()
- end
- fileData = fileData:sub(fileLength+1)
- if #fileData<=0 then
- break
- end
- end
- shell.run((isRel and "" or "/")..fs.combine(dstFolder,"main.lua"))
- --Done!]=]
- outputHandle.writeLine(decodeRoutine)
- outputHandle.close()
- else
- print("Failed to open "..fileName.." for writing.")
- end
- else
- print("Self Extracting Package Creator")
- print("Usage: sep srcFolder dstFolder fileName [isRel]")
- print("where isRel is yes or no, defaulting to yes.")
- print("Set isRel to no if dstFolder is an absolute path.")
- print("Manually put .lua on the filename, it won't be put there automatically.")
- end
Advertisement
Add Comment
Please, Sign In to add comment