JPiolho

[Minecraft][CC] Setup Wizard

Mar 2nd, 2012
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | None | 0 0
  1. -- Setup Wizard
  2. -- By JPiolho
  3. -- Version 1.0
  4. -- Feel free to copy and edit as long you credit the original author
  5. -- More Info @ http://bit.ly/zoxn6V
  6.  
  7.  
  8. function explode(div,str) -- credit: http://richard.warburton.it
  9.   if (div=='') then return false end
  10.   local pos,arr = 0,{}
  11.   -- for each divider found
  12.   for st,sp in function() return string.find(str,div,pos,true) end do
  13.     table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
  14.     pos = sp + 1 -- Jump past current divider
  15.   end
  16.   table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
  17.   return arr
  18. end
  19.  
  20. local tArgs = { ... }
  21. if #tArgs == 0 then
  22.     print( "Usage: setupwizard <folder path> [options]" )
  23.     return
  24. end
  25.  
  26.  
  27. local sPath = shell.resolve( tArgs[1] )
  28. local bReadOnly = fs.isReadOnly( sPath )
  29. if fs.exists( sPath ) == false then
  30.     print("The specified path doesn't exist")
  31.     return
  32. end
  33.  
  34. if fs.isDir( sPath ) == false then
  35.     print("You must target a folder")
  36.     return
  37. end
  38.  
  39. local options = ""
  40. local i
  41.  
  42. for i = 2,#tArgs do
  43.  options = options .. tArgs[i] .. " "
  44. end
  45.  
  46.  
  47.  
  48. print("Listing files...")
  49.  
  50. local function ListFiles(path)
  51.     if path == "rom" or path == "/rom" then return {} end
  52.  
  53.  
  54.     local list = {}
  55.  
  56.     local f
  57.     for _,f in pairs(fs.list(path)) do
  58.  
  59.         if fs.isDir(path .. "/" .. f) then
  60.             local retList = ListFiles(path .. "/" .. f)
  61.  
  62.             local v
  63.             for _,v in pairs(retList) do
  64.                 table.insert(list,v)
  65.             end
  66.         else
  67.             if string.find(path,"setupwizard") == nil then
  68.                 table.insert(list,path .. "/" .. f)
  69.             end
  70.         end
  71.     end
  72.  
  73.     return list
  74. end
  75.  
  76.  
  77. local finalLua = ""
  78.  
  79.  
  80. local fileList = ListFiles(sPath)
  81.  
  82. io.write("Packaging " .. #fileList .. " file")
  83. if #fileList > 1 then io.write("s") end
  84. print("...")
  85.  
  86.  
  87.  
  88. finalLua = "local data={"
  89.  
  90. local firstFile = true
  91. local file
  92. for _,file in pairs(fileList) do
  93.     if firstFile == false then finalLua = finalLua .. "," else firstFile = false end
  94.  
  95.  
  96.  
  97.  
  98.     finalLua = finalLua .. "{\"" .. shell.resolve( file ) .. "\"," -- Start File entry
  99.  
  100.     local f = io.open(file,"r")
  101.  
  102.     local firstLine = true
  103.  
  104.  
  105.     finalLua = finalLua .. "{" -- Start lines
  106.     while true do
  107.         local line = f:read()
  108.  
  109.         if line == nil then break end
  110.  
  111.         if firstLine == false then
  112.             finalLua = finalLua .. ","
  113.         else
  114.             firstLine = false
  115.         end
  116.  
  117.         line = string.gsub(line,"\\","\\\\")
  118.         line = string.gsub(line,"\"","\\\"")
  119.         line = string.gsub(line,"\'","\\\'")
  120.  
  121.  
  122.         finalLua = finalLua .. "\"" .. line .. "\""
  123.     end
  124.     finalLua = finalLua .. "}" -- End Lines
  125.  
  126.     f:close()
  127.  
  128.     finalLua = finalLua .. "}" -- End File Entry
  129. end
  130.  
  131. finalLua = finalLua .. "}\n" -- End Data Entry
  132.  
  133.  
  134. finalLua = finalLua .. "local tArgs = { ... };" ..
  135. "if #tArgs == 0 then " ..
  136. "print( \"Usage: \" .. shell.getRunningProgram() .. \" <install path>\" );" ..
  137. "return " ..
  138. "end " ..
  139. "local sPath = shell.resolve( tArgs[1] );" ..
  140. "local bReadOnly = fs.isReadOnly( sPath );" ..
  141. "if fs.exists( sPath ) == false then " ..
  142. "print(\"The specified path doesn't exist. Do you want to create? (y/n)\");" ..
  143. "local r = read();" ..
  144. "if r == \"y\" then " ..
  145. "fs.makeDir(sPath) " ..
  146. "else " ..
  147. "return " ..
  148. "end " ..
  149. "end " ..
  150. "if fs.isDir( sPath ) == false then " ..
  151. "print(\"You must specify a folder\") " ..
  152. "return " ..
  153. "end " ..
  154. "if sPath ~= \"\" and bReadOnly then " ..
  155. "print(\"The specified path is read-only\");" ..
  156. "return " ..
  157. "end " ..
  158. "print(\"Installing...\");" ..
  159. "local i;" ..
  160. "for i = 1,#data do " ..
  161. "local file = data[i];" ..
  162. "local filename = file[1];" ..
  163. "local folder = sPath;" ..
  164. "local _1,_2;" ..
  165. "for _1,_2 in string.gmatch(file[1],\"(.*)/(.*)\") do " ..
  166. "filename = _2;" ..
  167. "folder = sPath .. \"/\" .. _1 " ..
  168. "end " ..
  169. "if folder ~= \"\" and fs.exists(folder) == false then " ..
  170. "fs.makeDir(folder) " ..
  171. "end " ..
  172. "local f = io.open(folder .. \"/\" .. filename,\"w\");" ..
  173. "local lines = file[2];" ..
  174. "local v;" ..
  175. "for _,v in pairs(lines) do " ..
  176. "f:write(v .. \"\\n\") " ..
  177. "end " ..
  178. "f:close() " ..
  179. "end " ..
  180. "print(\"Installed!\")"
  181.  
  182. if fs.exists("setup") then fs.delete("setup") end
  183. local f = io.open("setup","w")
  184. f:write(finalLua)
  185. f:close()
Advertisement
Add Comment
Please, Sign In to add comment