Advertisement
billysback

packer

Nov 25th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1.  
  2. local function split( _sInput, _sDelimiter )
  3.     local tReturn = {}
  4.     local delimiter = string.gsub( _sDelimiter, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1" )
  5.     local searchPattern = "([^"..delimiter.."]+)"..delimiter.."?"
  6.  
  7.     for match in string.gmatch( _sInput, searchPattern ) do
  8.         table.insert( tReturn, match )
  9.     end
  10.     return tReturn
  11. end
  12.  
  13. local function getLines(dir)
  14.     local lines = {}
  15.     if fs.exists(dir) then
  16.         local file = fs.open(dir, "r")
  17.         local line = file.readLine()
  18.         while line ~= nil do
  19.             lines[#lines + 1] = line
  20.             line = file.readLine()
  21.         end
  22.         file.close()
  23.     end
  24.     return lines
  25. end
  26.  
  27. local function writeFile(output, dir)
  28.     local lines = getLines(dir)
  29.     if #lines > 0 then
  30.         local d = string.gsub(dir, "/", "___")
  31.         d = string.gsub(d, " ", "_sSs_")
  32.         d = string.gsub(d, "%.", "_dDd_")
  33.         output.writeLine("local "..d.." = ")
  34.         output.writeLine("{")
  35.         for i=1,#lines do
  36.             local ln = string.gsub(lines[i], '"', "#_#")
  37.             ln = '"'..ln..'"'
  38.             if i ~= #lines then ln = ln.."," end
  39.             output.writeLine(ln)
  40.         end
  41.         output.writeLine("}")
  42.     end
  43. end
  44.  
  45. local function writeCode(dirs, output)
  46.     output.writeLine("local file")
  47.     for i=2,#dirs do
  48.         local dir = string.gsub(dirs[i], "/", "___")
  49.         dir = string.gsub(dir, " ", "_sSs_")
  50.         dir = string.gsub(dir, "%.", "_dDd_")
  51.         output.writeLine("if fs.exists('"..dirs[i].."') then fs.delete('"..dirs[i].."') end")
  52.         output.writeLine("file = fs.open('"..dirs[i].."', 'a')")
  53.         output.writeLine("for i=1,#"..dir.." do")
  54.         local speech = '"'
  55.         output.writeLine("  file.writeLine("..dir.."[i]:gsub('#_#', '"..speech.."'))")
  56.         output.writeLine("end")
  57.         output.writeLine("file.close()")
  58.     end
  59. end
  60.  
  61. local stuff = {...}
  62. local str = ""
  63. for i=1,#stuff do
  64.     if i == 1 then
  65.         str = stuff[i]
  66.     else
  67.         str = str.." "..stuff[i]
  68.     end
  69. end
  70. local dirs = split(str, ":")
  71. if #dirs > 1 then
  72. local output = dirs[1]
  73. if fs.exists(output) then fs.delete(output) end
  74.     local out = fs.open(output, "a")
  75.     for i=2,#dirs do
  76.         writeFile(out, dirs[i])
  77.     end
  78.     writeCode(dirs, out)
  79.     out.close()
  80. else
  81.     print("Not enough directories, usage:")
  82.     print("    pack output:dir1:dir2:dir3 etc.")
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement