Advertisement
HPWebcamAble

Advanced Installer Creator

Feb 20th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.32 KB | None | 0 0
  1. --Coded by HPWebcamAble--
  2. os.loadAPI("button")
  3. local but = button
  4.  
  5. --[[
  6. This is a pretty advanced program. I don't recommend
  7. using it if you aren't sure how it works!
  8. ]]
  9.  
  10.  
  11. --Test for problems--
  12. but.clear(true)
  13. if not term.isColor() then
  14.   print("This program requires an advanced computer")
  15.   return
  16. end
  17.  
  18. if not http then
  19.   print("The HTTP API is not enabled")
  20.   return
  21. end
  22.  
  23. --variables--
  24. local vers = 1.1
  25. local off = colors.red
  26. local on = colors.lime
  27. local inac = colors.gray
  28. local w,h = term.getSize()
  29. local drives = false
  30. local saveMode = "None"
  31. local doWarn1 = false
  32. local addDir = false
  33. local dirName = nil
  34. local programs = {}
  35.  
  36. --functions--
  37. function printCentered(text,ypos)
  38.   term.setCursorPos(w/2-#text/2,ypos)
  39.   term.write(text)
  40. end
  41.  
  42. function drawMain()
  43.   term.setBackgroundColor(colors.white)
  44.   but.clear(true)
  45.   term.setBackgroundColor(colors.orange)
  46.   for i = 1, w do
  47.     term.write(" ")
  48.   end
  49.   printCentered("Advanced Installer Creator",1)
  50. end
  51.  
  52. function drawSaveOption()
  53.   drawMain()
  54.   term.setBackgroundColor(colors.white)
  55.   printCentered("Choose a save location:",6)
  56.   if doWarn1 then
  57.     printCentered("No drive attached!",15)
  58.   end
  59.   drives = false
  60.   for a,b in pairs(rs.getSides()) do
  61.     if peripheral.getType(b) == "drive" then
  62.       drives = true
  63.       break
  64.     end
  65.   end
  66.   if drives then
  67.      but.draw("Disk",16,10,on,off,inac,2,1,nil)
  68.   else
  69.     but.draw("Disk",16,10,on,off,inac,2,1,nil)
  70.     but.setState("Disk",3)
  71.   end
  72.   but.draw("Rom",31,10,on,off,inac,2,1,nil)
  73. end
  74.  
  75. function drawDirOption()
  76.   drawMain()
  77.   term.setBackgroundColor(colors.white)
  78.   printCentered("Add a directory?",6)
  79.   but.draw("Yes",16,10,colors.lime,colors.lime,inac,2,1,nil)
  80.   but.draw("No",31,10,colors.red,colors.red,inac,2,1,nil)
  81. end
  82.  
  83. function drawYNOption(text)
  84.   drawMain()
  85.   term.setBackgroundColor(colors.white)
  86.   printCentered(text,6)
  87.   but.draw("Yes",16,10,colors.lime,colors.lime,inac,2,1,nil)
  88.   but.draw("No",31,10,colors.red,colors.red,inac,2,1,nil)
  89. end
  90.  
  91. function addProgram()
  92.   drawMain()
  93.   term.setBackgroundColor(colors.white)
  94.   printCentered("Add a program",2)
  95.   printCentered("Program name:",6)
  96.   printCentered("Pastebin Code:",8)
  97.   term.setCursorPos(18,7)
  98.   tempvar = read()
  99.   term.setCursorPos(18,9)
  100.   programs[tempvar] = read()
  101. end
  102.  
  103. function writting()
  104.   but.clear(true)
  105.   drawMain()
  106.   term.setBackgroundColor(colors.white)
  107.   printCentered("Writing program...",7)
  108. end
  109.  
  110. function findDrives()
  111.   for a,b in pairs(rs.getSides()) do
  112.     if peripheral.getType(b) == "drive" then
  113.       return b
  114.     end
  115.   end
  116.   return false
  117. end
  118.  
  119. --program--
  120. term.setBackgroundColor(colors.white)
  121. term.clear()
  122. term.setTextColor(colors.black)
  123. printCentered("Advanced Installer Creator",6)
  124. printCentered("V. "..vers,h)
  125. but.draw("Start",22,8,on,off,inac,1,0,nil)
  126.  
  127. while true do
  128.   event,par1,par2,par3 = os.pullEvent()
  129.   if event == "mouse_click" then
  130.     if but.checkCords(par2,par3) == "Start" then
  131.       break
  132.     end
  133.   end
  134. end
  135.  
  136. local button = nil
  137.  
  138. while true do
  139.   drawSaveOption()
  140.   event,par1,par2,par3 = os.pullEvent()
  141.   if event == "peripheral" then
  142.     doWarn1 = false
  143.   elseif event == "mouse_click" then
  144.     button = but.checkCords(par2,par3)
  145.     if button == "Disk" and drives == false then
  146.       doWarn1 = true
  147.     elseif button == "Disk" and drives then
  148.       saveMode = "Disk"
  149.       break
  150.     elseif button == "Rom" then
  151.       saveMode = "Rom"
  152.       break
  153.     end
  154.   end
  155. end
  156.  
  157. while true do
  158.   drawYNOption("Add a directory?")
  159.   event,par1,par2,par3 = os.pullEvent()
  160.   if event == "mouse_click" then
  161.     button = but.checkCords(par2,par3)
  162.     if button == "Yes" then
  163.       addDir = true
  164.       break
  165.     elseif button == "No" then
  166.       break
  167.     end
  168.   end
  169. end
  170.  
  171. drawMain()
  172. term.setBackgroundColor(colors.white)
  173. if addDir then
  174.   printCentered("Directory Name:",6)
  175.   term.setCursorPos(18,7)
  176.   dirName = read()
  177. end
  178.  
  179. addProgram()
  180. while true do
  181.   drawYNOption("Add another program?")
  182.   event,par1,par2,par3 = os.pullEvent()
  183.   if event == "mouse_click" then
  184.     button = but.checkCords(par2,par3)
  185.     if button == "Yes" then
  186.       addProgram()
  187.     elseif button == "No" then
  188.       break
  189.     end
  190.   end
  191. end
  192.  
  193. writting()
  194. if saveMode == "Disk" then
  195.   drives = findDrives()
  196.   if not drives then
  197.     while true do
  198.       drawMain()
  199.       term.setBackgroundColor(colors.white)
  200.       printCentered("No drives attached!",7)
  201.       printCentered("Please add one",8)
  202.       event,par1 = os.pullEvent("peripheral")
  203.       if peripheral.getType(par1) == "drive" then
  204.         break
  205.       end
  206.     end
  207.   end
  208.   drives = findDrives()
  209.   if not disk.isPresent(drives) then
  210.     while true do
  211.       drawMain()
  212.       term.setBackgroundColor(colors.white)
  213.       printCentered("No disk in drive on "..drives.." side",7)
  214.       printCentered("Please put one in and press any key",8)
  215.       os.pullEvent("char")
  216.       if disk.isPresent(drives) then
  217.         break
  218.       end
  219.     end
  220.   end
  221.   but.clear()
  222.   drawMain()
  223.   term.setBackgroundColor(colors.white)
  224.   printCentered("Enter a disk name:",7)
  225.   term.setCursorPos(18,8)
  226.   disk.setLabel(drives,read())
  227.   writting()
  228.   f = fs.open("/disk/installer","w")
  229.   f.write([[
  230.   --Made with Advanced Installer Creater--
  231.  
  232.   print("Ready to install")
  233.   print("Press any key to continue...")
  234.   os.pullEvent("key")
  235.  
  236.   --This is a function is part of Pastebin! I didn't write this--
  237.   --Well, I rewrote it to be used inside of a program rather than as a program itself--
  238.   function get(sCode,sFile)
  239.     local sPath = shell.resolve( sFile )
  240.     if fs.exists( sPath ) then
  241.       return false
  242.     end
  243.     local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode ))
  244.     if response then       
  245.       local sResponse = response.readAll()
  246.       response.close()     
  247.       local file = fs.open( sPath, "w" )
  248.       file.write( sResponse )
  249.       file.close()
  250.       return true      
  251.     else
  252.       return false
  253.     end
  254.   end
  255.    
  256.   if not http then
  257.     error("Http API not enabled")
  258.   end
  259.  
  260.   print("Downloading...")]])
  261.   if dirName then
  262.     path = "/"..dirName.."/"
  263.     for a,b in pairs(programs) do
  264.       f.write([[
  265.      
  266.       fs.makeDir("]]..dirName..[[")
  267.       if fs.exists("]]..path..a..[[") then
  268.         print("File conflict! Deleting...")
  269.         shell.run("delete ]]..path..a..[[")
  270.         print("Fixed. Retrying...")
  271.       end
  272.       if get("]]..b..[[","]]..path..a..[[") then
  273.         print("Successfully downloaded ]]..a..[[")
  274.       else
  275.         print("Something went wrong while downloading ]]..a..[[")
  276.         return
  277.       end
  278.       ]])
  279.     end
  280.   else
  281.     for a,b in pairs(programs) do
  282.     f.write([[
  283.      
  284.       if fs.exits("]]..a..[[") then
  285.         print("File conflict! Deleting...")
  286.         shell.run("delete ]]..a..[[")
  287.         print("Fixed. Retrying...")
  288.       end
  289.       if get("]]..b..[[","]]..a..[[") then
  290.         print("Succesfully downloaded ]]..a..[[")
  291.       else
  292.         print("Something went wrong while downloading ]]..a..[[")
  293.         return
  294.       end
  295.       ]])
  296.     end
  297.   end
  298.   f.write([[
  299.  
  300.   print("Done!")
  301.   ]])
  302.   f.close()
  303. elseif saveMode == "Rom" then
  304.   f = fs.open("installer","w")
  305.   f.write([[
  306.   --Made with Advanced Installer Creater--
  307.  
  308.   print("Ready to install")
  309.   print("Press any key to continue...")
  310.   os.pullEvent("key")
  311.  
  312.   --This is a function is part of Pastebin! I didn't write this--
  313.   --Well, I rewrote it to be used inside of a program rather than as a program itself--
  314.   function get(sCode,sFile)
  315.     local sPath = shell.resolve( sFile )
  316.     if fs.exists( sPath ) then
  317.       return false
  318.     end
  319.     local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode ))
  320.     if response then       
  321.       local sResponse = response.readAll()
  322.       response.close()     
  323.       local file = fs.open( sPath, "w" )
  324.       file.write( sResponse )
  325.       file.close()
  326.       return true      
  327.     else
  328.       return false
  329.     end
  330.   end
  331.    
  332.   if not http then
  333.     error("Http API not enabled")
  334.   end
  335.  
  336.   print("Downloading...")]])
  337.   if dirName then
  338.     path = "/"..dirName.."/"
  339.     for a,b in pairs(programs) do
  340.       f.write([[
  341.      
  342.       fs.makeDir("]]..dirName..[[")
  343.       if fs.exists("]]..path..a..[[") then
  344.         print("File conflict! Deleting...")
  345.         shell.run("delete ]]..path..a..[[")
  346.         print("Fixed. Retrying...")
  347.       end
  348.       if get("]]..b..[[","]]..path..a..[[") then
  349.         print("Successfully downloaded ]]..a..[[")
  350.       else
  351.         print("Something went wrong while downloading ]]..a..[[")
  352.         return
  353.       end
  354.       ]])
  355.     end
  356.   else
  357.     for a,b in pairs(programs) do
  358.     f.write([[
  359.      
  360.       if fs.exits("]]..a..[[") then
  361.         print("File conflict! Deleting...")
  362.         shell.run("delete ]]..a..[[")
  363.         print("Fixed. Retrying...")
  364.       end
  365.       if get("]]..b..[[","]]..a..[[") then
  366.         print("Succesfully downloaded ]]..a..[[")
  367.       else
  368.         print("Something went wrong while downloading ]]..a..[[")
  369.         return
  370.       end
  371.       ]])
  372.     end
  373.   end
  374.   f.write([[
  375.  
  376.   print("Done!")
  377.   ]])
  378.   f.close()
  379. end
  380.  
  381. printCentered("Program Created!",8)
  382. printCentered("Thank you for using",9)
  383. printCentered("Advanced Installer Creator",10)
  384. printCentered("Press any key...",h)
  385. os.pullEvent("char")
  386. term.setBackgroundColor(colors.black)
  387. shell.run("clear")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement