Advertisement
PSquishyP

Basic Installer w/ GUI

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