Advertisement
Guest User

Untitled

a guest
Jul 26th, 2015
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. // main executable
  2.  
  3. ButtonCheckForUpdates:
  4. MsgBox, A
  5. Check_ForUpdate(1, 0, "", "")
  6. MsgBox, B
  7.  
  8. Check_ForUpdate(_ReplaceCurrentScript = 1, _SuppressMsgBox = 0, _CallbackFunction = "", ByRef _Information = "")
  9. {
  10. ;Version.ini file format - this is just an example of what the version.ini file would look like
  11. ;
  12. ;[Info]
  13. ;Version=2.1
  14. ;URL=192.168.1.106/files/No-Idle/No-Idle.exe
  15. ;MD5=00000000000000000000000000000000 or omit this key completly to skip the MD5 file validation
  16.  
  17. Static Script_Name := "No-Idle"
  18. , Version_Number := 2.1 ;The script's version number
  19. , Update_URL := "192.168.1.106/files/No-idle/update/Version.ini"
  20. , Retry_Count := 3 ;Retry count for if/when anything goes wrong
  21.  
  22. Random,Filler,10000000,99999999
  23. Version_File := A_Temp . "\" . Filler . ".ini"
  24. , Temp_FileName := A_Temp . "\" . Filler . ".tmp"
  25. , VBS_FileName := A_Temp . "\" . Filler . ".vbs"
  26.  
  27. Loop,% Retry_Count
  28. {
  29. _Information := ""
  30.  
  31. UrlDownloadToFile,%Update_URL%,%Version_File%
  32.  
  33. IniRead,Version,%Version_File%,Info,Version,N/A
  34.  
  35. If (Version = "N/A"){
  36. FileDelete,%Version_File%
  37.  
  38. If (A_Index = Retry_Count)
  39. _Information .= "The version info file doesn't have a ""Version"" key in the ""Info"" section or the file can't be downloaded."
  40. Else
  41. Sleep,500
  42.  
  43. Continue
  44. }
  45.  
  46. If (Version > Version_Number){
  47. If (_SuppressMsgBox != 1 and _SuppressMsgBox != 3){
  48. MsgBox,0x4,New version available,There is a new version of %Script_Name% available.`nCurrent version: %Version_Number%`nNew version: %Version%`n`nWould you like to download it now?
  49.  
  50. IfMsgBox,Yes
  51. MsgBox_Result := 1
  52. }
  53.  
  54. If (_SuppressMsgBox or MsgBox_Result){
  55. IniRead,URL,%Version_File%,Info,URL,N/A
  56.  
  57. If (URL = "N/A")
  58. _Information .= "The version info file doesn't have a valid URL key."
  59. Else {
  60. SplitPath,URL,,,Extension
  61.  
  62. If (Extension = "ahk" And A_AHKPath = "")
  63. _Information .= "The new version of the script is an .ahk filetype and you do not have AutoHotKey installed on this computer.`r`nReplacing the current script is not supported."
  64. Else If (Extension != "exe" And Extension != "ahk")
  65. _Information .= "The new file to download is not an .EXE or an .AHK file type. Replacing the current script is not supported."
  66. Else {
  67. IniRead,MD5,%Version_File%,Info,MD5,N/A
  68.  
  69. Loop,% Retry_Count
  70. {
  71. UrlDownloadToFile,%URL%,%Temp_FileName%
  72.  
  73. IfExist,%Temp_FileName%
  74. {
  75. If (MD5 = "N/A"){
  76. _Information .= "The version info file doesn't have a valid MD5 key."
  77. , Success := True
  78. Break
  79. } Else {
  80. Ptr := A_PtrSize ? "Ptr" : "UInt"
  81. , H := DllCall("CreateFile",Ptr,&Temp_FileName,"UInt",0x80000000,"UInt",3,"UInt",0,"UInt",3,"UInt",0,"UInt",0)
  82. , DllCall("GetFileSizeEx",Ptr,H,"Int64*",FileSize)
  83. , FileSize := FileSize = -1 ? 0 : FileSize
  84.  
  85. If (FileSize != 0){
  86. VarSetCapacity(Data,FileSize,0)
  87. , DllCall("ReadFile",Ptr,H,Ptr,&Data,"UInt",FileSize,"UInt",0,"UInt",0)
  88. , DllCall("CloseHandle",Ptr,H)
  89. , VarSetCapacity(MD5_CTX,104,0)
  90. , DllCall("advapi32\MD5Init",Ptr,&MD5_CTX)
  91. , DllCall("advapi32\MD5Update",Ptr,&MD5_CTX,Ptr,&Data,"UInt",FileSize)
  92. , DllCall("advapi32\MD5Final",Ptr,&MD5_CTX)
  93.  
  94. FileMD5 := ""
  95. Loop % StrLen(Hex:="123456789ABCDEF0")
  96. N := NumGet(MD5_CTX,87+A_Index,"Char"), FileMD5 .= SubStr(Hex,N>>4,1) . SubStr(Hex,N&15,1)
  97.  
  98. VarSetCapacity(Data,FileSize,0)
  99. , VarSetCapacity(Data,0)
  100.  
  101. If (FileMD5 != MD5){
  102. FileDelete,%Temp_FileName%
  103.  
  104. If (A_Index = Retry_Count)
  105. _Information .= "The MD5 hash of the downloaded file does not match the MD5 hash in the version info file."
  106. Else
  107. Sleep,500
  108.  
  109. Continue
  110. } Else
  111. Success := True
  112. } Else {
  113. DllCall("CloseHandle",Ptr,H)
  114. Success := True
  115. }
  116. }
  117. } Else {
  118. If (A_Index = Retry_Count)
  119. _Information .= "Unable to download the latest version of the file from " . URL . "."
  120. Else
  121. Sleep,500
  122. Continue
  123. }
  124. }
  125. }
  126. }
  127. }
  128. } Else
  129. _Information .= "No update was found."
  130.  
  131. FileDelete,%Version_File%
  132. Break
  133. }
  134.  
  135. If (_ReplaceCurrentScript And Success){
  136. SplitPath,URL,,,Extension
  137. Process,Exist
  138. MyPID := ErrorLevel
  139.  
  140. VBS_P1 =
  141. (LTrim Join`r`n
  142. On Error Resume Next
  143. Set objShell = CreateObject("WScript.Shell")
  144. objShell.Run "TaskKill /F /PID %MyPID%", 0, 1
  145. Set objFSO = CreateObject("Scripting.FileSystemObject")
  146. )
  147.  
  148. If (A_IsCompiled){
  149. SplitPath,A_ScriptFullPath,,Dir,,Name
  150. VBS_P2 =
  151. (LTrim Join`r`n
  152. Finished = False
  153. Count = 0
  154. Do Until (Finished = True Or Count = 5)
  155. Err.Clear
  156. objFSO.CopyFile "%Temp_FileName%", "%Dir%\%Name%.%Extension%", True
  157. If (Err.Number = 0) then
  158. Finished = True
  159. objShell.Run """%Dir%\%Name%.%Extension%"""
  160. Else
  161. WScript.Sleep(1000)
  162. Count = Count + 1
  163. End If
  164. Loop
  165. objFSO.DeleteFile "%Temp_FileName%", True
  166. )
  167.  
  168. Return_Val := Temp_FileName
  169. } Else {
  170. If (Extension = "ahk"){
  171. FileMove,%Temp_FileName%,%A_ScriptFullPath%,1
  172. If (Errorlevel)
  173. _Information .= "Error (" . Errorlevel . ") unable to replace current script with the latest version."
  174. Else {
  175. VBS_P2 =
  176. (LTrim Join`r`n
  177. objShell.Run """%A_ScriptFullPath%"""
  178. )
  179.  
  180. Return_Val := A_ScriptFullPath
  181. }
  182. } Else If (Extension = "exe"){
  183. SplitPath,A_ScriptFullPath,,FDirectory,,FName
  184. FileMove,%Temp_FileName%,%FDirectory%\%FName%.exe,1
  185. FileDelete,%A_ScriptFullPath%
  186.  
  187. VBS_P2 =
  188. (LTrim Join`r`n
  189. objShell.Run """%FDirectory%\%FName%.exe"""
  190. )
  191.  
  192. Return_Val := FDirectory . "\" . FName . ".exe"
  193. } Else {
  194. FileDelete,%Temp_FileName%
  195. _Information .= "The downloaded file is not an .EXE or an .AHK file type. Replacing the current script is not supported."
  196. }
  197. }
  198.  
  199. VBS_P3 =
  200. (LTrim Join`r`n
  201. objFSO.DeleteFile "%VBS_FileName%", True
  202. )
  203.  
  204. If (_SuppressMsgBox < 2)
  205. {
  206. If (InStr(VBS_P2, "Do Until (Finished = True Or Count = 5)"))
  207. {
  208. VBS_P3 .= "`r`nIf (Finished = False) Then"
  209. VBS_P3 .= "`r`nWScript.Echo ""Update failed."""
  210. VBS_P3 .= "`r`nElse"
  211. If (Extension != "exe")
  212. VBS_P3 .= "`r`nobjFSO.DeleteFile """ A_ScriptFullPath """"
  213. VBS_P3 .= "`r`nWScript.Echo ""Update completed successfully."""
  214. VBS_P3 .= "`r`nEnd If"
  215. } Else
  216. VBS_P3 .= "`r`nWScript.Echo ""Update complected successfully."""
  217. }
  218.  
  219. FileDelete,%VBS_FileName%
  220. FileAppend,%VBS_P1%`r`n%VBS_P2%`r`n%VBS_P3%,%VBS_FileName%
  221.  
  222. If (_CallbackFunction != ""){
  223. If (IsFunc(_CallbackFunction))
  224. %_CallbackFunction%()
  225. Else
  226. _Information .= "The callback function is not a valid function name."
  227. }
  228.  
  229. RunWait,%VBS_FileName%,%A_Temp%,VBS_PID
  230. Sleep,2000
  231.  
  232. Process,Close,%VBS_PID%
  233. _Information := "Error (?) unable to replace current script with the latest version.`r`nPlease make sure your computer supports running .vbs scripts and that the script isn't running in a pipe."
  234. }
  235.  
  236. _Information := _Information = "" ? "None" : _Information
  237.  
  238. Return Return_Val
  239. }
  240.  
  241. // version.ini
  242.  
  243. [Info]
  244. Version=2.2
  245. URL=192.168.1.106/files/No-Idle/No-Idle.exe
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement