Advertisement
PSquishyP

Basic Installer w/ GUI 2.0

Jan 13th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Basic Installer w/ GUI by Noahc3
  2. ;HTTP File Downloader by Bruttosozialprodukt
  3. ;Built with Autohotkey 1.1.22.09
  4. ;Installer Version 2.0
  5.  
  6. ;Source Code available at http://pastebin.com/xBbCPhtP
  7.  
  8. ;Instruction Set Codes:
  9. ;mkdir: Makes new directory at ~workingdir~/[Given Directory] unless it already exists
  10. ;rmdir: Removes directory at ~workingdir~/[Given Directory] unless it contains files (aka the folder must be empty otherwise it wont be deleted)
  11. ;rmdirF: Removes directory at ~workingdir~/[Given Directory] even if it has files in it
  12. ;del: Deletes file at ~workingdir~/[Given Directory]
  13. ;ver: This will display the version number before installing. Make instructionDir the version number in the instruction set. Only use once, and put it at the top of the instruction set! (1.2+)
  14. ;
  15. ;Otherwise, you can leave a literal URL for it to be downloaded.
  16.  
  17.  
  18. ;PreInit
  19. #NoEnv
  20. SendMode Input
  21. SetWorkingDir %A_ScriptDir%
  22.  
  23.  
  24. ; Bundle title image when compiled, and extract image on launch if ran as an EXE
  25. FileInstall, brunchExpansion.png, %A_ScriptDir%\brunchExpansion.png, 0
  26.  
  27. ;Download instruction set (1.1+)
  28. DownloadFile("https://s3-us-west-2.amazonaws.com/worldofgrinding/Breakfast+Special%3A+Brunch+Files/brunchDownloads.txt", "brunchDownloads.txt")
  29.  
  30.  
  31. ;Set default radio selection
  32. directorySelected = 1
  33.  
  34. ;Default text for custom directory edit
  35. selectedCustomDirectory = Breakfast Special Install Directory
  36.  
  37. ;Build GUI
  38. Gui, Show , w480 h300, Breakfast Special: Brunch Expansion Installer
  39. Gui, Add, Text, x20 y270 w250,Installer Version 2.0
  40. Gui, Add, Edit, w300 h19 x50 y140 vCustomDirectory, %selectedCustomDirectory%
  41. Gui, Add, Edit, w375 h19 x50 y90 vCurrentDirectory ReadOnly, %A_ScriptDir%
  42. Gui, Add, Button, x50 y200 w375 h50 gINSTALL vINSTALL,Install
  43. Gui, Add, Button, x350 y139 w75 h21 gBROWSE vBROWSEBUTTON,Browse
  44. Gui, Add, Radio, x50 y70  gDirectorySelect1 vDirectorySelect1 checked, Use This Directory
  45. Gui, Add, Radio, x50 y120  gDirectorySelect2 vDirectorySelect2, Use Custom Directory
  46. Gui, Add, Picture, x70 y10 w325 h-1, brunchExpansion.png
  47.  
  48. ;Create but instantly hide the install complete message
  49. Gui, Add, Text, x10 y90 w460 Center vInstallComplete,Installation Complete. The mods and configs were added to your game.
  50. Gui, Add, Button, x365 y270 w100 h21 gFINISH vFINISH,Finish
  51. GuiControl, hide, InstallComplete
  52. GuiControl, hide, FINISH
  53.  
  54. ;Disable the Custom Directory edit and the browse button by default
  55. GuiControl, disable, CustomDirectory
  56. GuiControl, disable, BROWSEBUTTON
  57.  
  58. ;Instantly delete extracted logo once loaded into memory
  59. FileDelete, brunchExpansion.png
  60.  
  61. ;Loop for developer commands, blank in public releases
  62. Loop
  63. {
  64. }
  65.  
  66. return
  67.  
  68.  
  69.  
  70. ;When Radio 1 is selected, enable Current Directory text box and set selected directory to 1, disable Custom Directory options
  71. DirectorySelect1:
  72. {
  73.     Gui, Submit, NoHide
  74.     GuiControl, disable, CustomDirectory
  75.     GuiControl, disable, BROWSEBUTTON
  76.     GuiControl, enable, CurrentDirectory
  77.     directorySelected = 1
  78. }
  79. return
  80.  
  81. ;When Radio 2 is selected, enable the Custom Directory edit and browse button and set selected directory to 2, disable Current Directory options
  82. DirectorySelect2:
  83. {
  84.     Gui, Submit, NoHide
  85.     GuiControl, enable, CustomDirectory
  86.     GuiControl, enable, BROWSEBUTTON
  87.     GuiControl, disable, CurrentDirectory
  88.     directorySelected = 2
  89. }
  90. return
  91.  
  92. ;When browse is pressed, handle folder selection
  93. BROWSE:
  94. {
  95.     Gui, Submit, NoHide
  96.     FileSelectFolder, vFolderSelected, C:, 3, Select Breakfast Special Installation Folder
  97.     GuiControl, Text, CustomDirectory, %vFolderSelected%
  98.     Gui, Submit, NoHide
  99. }
  100. return
  101.  
  102.  
  103. ;When install is pressed, begin installation
  104. INSTALL:
  105.    {
  106.     IfNotExist, brunchDownloads.txt
  107.     {
  108.         MsgBox, 52, Error, The instruction set was deleted or malformed. Would you like to redownload the instruction set?
  109.         IfMsgBox Yes
  110.             DownloadFile("https://s3-us-west-2.amazonaws.com/worldofgrinding/Breakfast+Special%3A+Brunch+Files/brunchDownloads.txt", "brunchDownloads.txt")
  111.             IfNotExist, brunchDownloads.txt
  112.             {
  113.                 MsgBox, 0, Error, The instruction set is still missing or malformed. Please check your internet connection and rerun the installer.
  114.                 IfMsgBox OK
  115.                 {
  116.                     FileDelete, brunchExpansion.png
  117.                     FileDelete, brunchDownloads.txt
  118.                     ExitApp
  119.                 }
  120.             }
  121.         else
  122.         {
  123.             FileDelete, brunchDownloads.txt
  124.             FileDelete, brunchExpansion.png
  125.             ExitApp
  126.             return
  127.         }
  128.     }
  129.     ;Set error count to 0
  130.     errors = 0
  131.    
  132.     ;Check which radio was selected and set install directory based on that.
  133.     if directorySelected = 1
  134.     {
  135.         downloadTo = %A_ScriptDir%
  136.     }
  137.     else if directorySelected = 2
  138.     {
  139.         downloadTo = %vFolderSelected%
  140.     }
  141.  
  142.     ;Make sure that the directory the user selected exists, and that it contains a mods and config folder. If any errors occur, send user back to installer.
  143.     IfNotExist, %downloadTo%
  144.     {
  145.         MsgBox, 0, Error, The install folder you selected does not exist.
  146.         errors = errors + 1
  147.     }
  148.  
  149.     if errors > 0
  150.     {
  151.         return
  152.     }
  153.  
  154.     IfNotExist, %downloadTo%\mods
  155.     {
  156.         MsgBox, 0, Error, The mods folder is missing. Please make sure you have installed the pack before running this and have selected the correct install directory.
  157.         errors = errors + 1
  158.     }
  159.  
  160.     if errors > 0
  161.     {
  162.         return
  163.     }
  164.  
  165.     IfNotExist, %downloadTo%\config
  166.     {
  167.         MsgBox, 0, Error, The config folder is missing. Please make sure you have installed the pack before running this and have selected the correct install directory.
  168.         errors = errors + 1
  169.     }
  170.  
  171.     if errors > 0
  172.     {
  173.         return
  174.     }
  175.    
  176.     ;If no errors are detected, hide installer GUI components;
  177.     GuiControl, Hide, CustomDirectory
  178.     GuiControl, Hide, CurrentDirectory
  179.     GuiControl, Hide, BROWSEBUTTON
  180.     GuiControl, Hide, INSTALL
  181.     GuiControl, Hide, DirectorySelect1
  182.     GuiControl, Hide, DirectorySelect2
  183.    
  184.     instruction =
  185.     instructionDir =
  186.     readLine = 0
  187.     downloadsFinished = 0
  188.    
  189.    
  190.     while downloadsFinished = 0
  191.     {
  192.         IfNotExist, brunchDownloads.txt
  193.         {
  194.             MsgBox, 52, Error, The instruction set was deleted or malformed. Would you like to redownload the instruction set?
  195.             IfMsgBox Yes
  196.                 DownloadFile("https://s3-us-west-2.amazonaws.com/worldofgrinding/Breakfast+Special%3A+Brunch+Files/brunchDownloads.txt", "brunchDownloads.txt")
  197.                 IfNotExist, brunchDownloads.txt
  198.                 {
  199.                     MsgBox, 0, Error, The instruction set is still missing or malformed. Please check your internet connection and rerun the installer.
  200.                     IfMsgBox OK
  201.                     {
  202.                         FileDelete, brunchExpansion.png
  203.                         FileDelete, brunchDownloads.txt
  204.                         ExitApp
  205.                     }
  206.                 }
  207.             else
  208.                 FileDelete, brunchDownloads.txt
  209.                 FileDelete, brunchExpansion.png
  210.                 ExitApp
  211.                 return
  212.         }
  213.        
  214.         ;Check instructions from downloaded instruction set
  215.         readLine += 1
  216.         FileReadLine, instruction, brunchDownloads.txt, %readLine%
  217.         readLine += 1
  218.         FileReadLine, instructionDir, brunchDownloads.txt, %readLine%
  219.        
  220.         ;Handle current instructions using instruction codes
  221.        
  222.         if instruction = end
  223.         {
  224.             downloadsFinished = 1
  225.             EndInstallation()
  226.             break
  227.         }
  228.        
  229.         else if instruction = del
  230.         {
  231.             FileDelete, %downloadTo%%instructionDir%
  232.         }
  233.        
  234.         else if instruction = mkdir
  235.         {
  236.             IfNotExist, %downloadTo%%instructionDir%
  237.             {
  238.                 FileCreateDir, %downloadTo%%instructionDir%
  239.             }
  240.         }
  241.        
  242.         else if instruction = rmdir
  243.         {
  244.             FileRemoveDir, %downloadTo%%instructionDir%, 0
  245.         }
  246.        
  247.         else if instruction = rmdirF
  248.         {
  249.             FileRemoveDir, %downloadTo%%instructionDir%, 1
  250.         }
  251.        
  252.         else if instruction = ver
  253.         {
  254.             MsgBox, 68, Confirm Install, This will install version %instructionDir% of the Brunch Expansion. Are you sure you want to continue?
  255.             IfMsgBox No
  256.             {
  257.                 FileDelete, brunchDownloads.txt
  258.                 FileDelete, brunchExpansion.png
  259.                 ExitApp
  260.                 return
  261.             }
  262.         }
  263.        
  264.         else
  265.         {
  266.             DownloadFile(instruction, downloadTo . instructionDir)
  267.         }
  268.     }
  269. }
  270. return
  271.    
  272.    
  273. EndInstallation()
  274. {
  275.     ;When installation finishes, show install complete message and finish button.
  276.     GuiControl, show, InstallComplete
  277.     GuiControl, show, FINISH
  278.     return
  279. }
  280.  
  281.  
  282. ;File Downloader by Bruttosozialprodukt
  283. DownloadFile(UrlToFile, SaveFileAs, Overwrite := True, UseProgressBar := True) {
  284.     ;Check if the file already exists and if we must not overwrite it
  285.       If (!Overwrite && FileExist(SaveFileAs))
  286.           Return
  287.     ;Check if the user wants a progressbar
  288.       If (UseProgressBar) {
  289.           ;Initialize the WinHttpRequest Object
  290.             WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
  291.           ;Download the headers
  292.             WebRequest.Open("HEAD", UrlToFile)
  293.             WebRequest.Send()
  294.           ;Store the header which holds the file size in a variable:
  295.             FinalSize := WebRequest.GetResponseHeader("Content-Length")
  296.           ;Create the progressbar and the timer
  297.             Progress, H80, , Downloading..., %UrlToFile%
  298.             SetTimer, __UpdateProgressBar, 100
  299.       }
  300.     ;Download the file
  301.       UrlDownloadToFile, %UrlToFile%, %SaveFileAs%
  302.     ;Remove the timer and the progressbar because the download has finished
  303.       If (UseProgressBar) {
  304.           Progress, Off
  305.           SetTimer, __UpdateProgressBar, Off
  306.       }
  307.     Return
  308.    
  309.     ;The label that updates the progressbar
  310.       __UpdateProgressBar:
  311.          ;Get the current filesize and tick
  312.             CurrentSize := FileOpen(SaveFileAs, "r").Length ;FileGetSize wouldn't return reliable results
  313.             CurrentSizeTick := A_TickCount
  314.           ;Calculate the downloadspeed
  315.             Speed := Round((CurrentSize/1024-LastSize/1024)/((CurrentSizeTick-LastSizeTick)/1000)) . " Kb/s"
  316.           ;Save the current filesize and tick for the next time
  317.             LastSizeTick := CurrentSizeTick
  318.             LastSize := FileOpen(SaveFileAs, "r").Length
  319.           ;Calculate percent done
  320.             PercentDone := Round(CurrentSize/FinalSize*100)
  321.           ;Update the ProgressBar
  322.             Progress, %PercentDone%, %PercentDone%`% Done, Downloading...  (%Speed%), Downloading %SaveFileAs% (%PercentDone%`%)
  323.       Return
  324. }
  325.  
  326. <^<!l::
  327.  
  328. ;Debug enabler
  329. InputBox, vDebug, Debug Password, Enter Debug Password, HIDE, 300, 150,
  330. if vDebug = rainbows
  331. {
  332.     debugEnabled = 1
  333.     MsgBox, 0, Debug Enabled, Debug Hotkeys Enabled,
  334. }
  335. else
  336. {
  337.     MsgBox, 0, Incorrect, Specified Password was Incorrect,
  338. }
  339. return
  340.  
  341.  
  342. ;Handle close of installer if the user closes the GUI or presses finish.
  343. FINISH:
  344. FileDelete, brunchExpansion.png
  345. FileDelete, brunchDownloads.txt
  346. ExitApp
  347.  
  348.  
  349. GuiClose:
  350. FileDelete, brunchExpansion.png
  351. FileDelete, brunchDownloads.txt
  352. ExitApp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement