#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.2 Author: TheSaint Script Function: Download a game file from GOG using curl.exe Template AutoIt script. (single or multiple threads) #ce ---------------------------------------------------------------------------- ; A BIG THANKS to TheDcoder for revealing a couple of command-line switches to me, that allowed me to use curk.exe to download my game files from GOG. #include #include #include #include #include #include Global $a, $ans, $array, $begin, $bytes, $checksum, $cliptxt, $curl, $delay, $down, $downfold, $download, $erred, $exists Global $f, $failed, $file, $filepth, $files, $filesize, $found, $gotten, $growth, $h, $handle, $hash, $link, $p, $params Global $part, $percent, $pid, $portion, $process, $progress, $range, $secs, $speed, $sum, $taken, $tempfold, $threads, $URL Global $validate $ans = MsgBox(262144 + 33, "Values Query", "Get values from the clipboard?") If $ans = 1 Then $cliptxt = ClipGet() If StringInStr($cliptxt, '"Url": "') > 0 Then $URL = StringSplit($cliptxt, '"Url": "', 1) $URL = $URL[2] $file = $URL $URL = StringSplit($URL, '",', 1) $URL = $URL[1] ;MsgBox(262192, "$URL", $URL, 0) $file = StringSplit($file, '"Name": "', 1) $file = $file[2] $filesize = $file $file = StringSplit($file, '",', 1) $file = $file[1] ;MsgBox(262192, "$file", $file, 0) $filesize = StringSplit($filesize, '"EstimatedSize": "', 1) $filesize = $filesize[2] $bytes = $filesize $filesize = StringSplit($filesize, '",', 1) $filesize = $filesize[1] ;MsgBox(262192, "$filesize", $filesize, 0) $bytes = StringSplit($bytes, '"VerifiedSize": ', 1) $bytes = $bytes[2] $checksum = $bytes $bytes = StringSplit($bytes, ',', 1) $bytes = $bytes[1] ;MsgBox(262192, "$bytes", $bytes, 0) $checksum = StringSplit($checksum, '"Checksum": "', 1) $checksum = $checksum[2] $checksum = StringSplit($checksum, '"', 1) $checksum = $checksum[1] Else MsgBox(262192, "Result", "Clipboard query has failed!", 0) Exit EndIf Else $bytes = "1136945520" $checksum = "14c2a0aadc35a302babfcff9949c564b" $file = "setup_brok_the_investigator_-_prologue_1.3.5_(64046).exe" $filesize = "1.1 GB" $URL = "/downloads/brok_the_investigator_prologue/en1installer0" EndIf $curl = @ScriptDir & "\curl.exe" $delay = 5000 $downfold = @ScriptDir & "\Files" $link = "https://www.gog.com" & $URL $tempfold = @ScriptDir & "\temp" $validate = 1 If $bytes > 1000000 Then $threads = 6 Else $threads = 1 EndIf If Not FileExists($downfold) Then DirCreate($downfold) If Not FileExists($tempfold) Then DirCreate($tempfold) FileDelete($tempfold & "\*.*") _Singleton("download-in-parts-thesaint", 0) If $threads > 1 Then $array = StringSplit("|", "|") $down = StringSplit("|", "|") $part = StringSplit("|", "|") $pid = StringSplit("|", "|") $range = StringSplit("|", "|") ; $portion = Floor($bytes / $threads) $array[0] = $threads $down[0] = $threads $part[0] = $threads $range[0] = $threads EndIf If FileExists($curl) Then If $bytes > 0 And $threads > 0 And $file <> "" And $URL <> "" And $link <> "" Then $download = $downfold & "\" & $file FileChangeDir(@ScriptDir) ; $erred = 0 $found = "" $gotten = 0 If $threads > 1 Then For $a = 1 To $array[0] If $a = 1 Then $range[1] = "0-" & $portion $part[1] = "part1.exe" $down[1] = $tempfold & "\" & $part[1] $params = $curl & ' -L -# -r ' & $range[1] & ' -o "' & $down[1] & '" --cookie gogcom_cookies.txt "' & $link & '"' $pid[1] = Run($params, "", @SW_MINIMIZE) ;@SW_SHOW If @error <> 0 Then $erred = "part1.exe" $sum = $portion ElseIf $a = 2 Then $range[2] = ($sum + 1) & "-" & ($portion * 2) $part[2] = "part2.exe" $down[2] = $tempfold & "\" & $part[2] $params = $curl & ' -L -# -r ' & $range[2] & ' -o "' & $down[2] & '" --cookie gogcom_cookies.txt "' & $link & '"' $pid[2] = Run($params, "", @SW_MINIMIZE) If @error <> 0 Then $erred = "part2.exe" ProcessClose($pid[1]) EndIf Else $sum = $sum + $portion _ArrayAdd($range, "") _ArrayAdd($part, "") $part[$a] = "part" & $a & ".exe" _ArrayAdd($down, "") $down[$a] = $tempfold & "\" & $part[$a] _ArrayAdd($pid, "") If $a = $array[0] Then $range[$a] = ($sum + 1) $params = $curl & ' -L -# -C ' & $range[$a] & ' -o "' & $down[$a] & '" --cookie gogcom_cookies.txt "' & $link & '"' Else $range[$a] = ($sum + 1) & "-" & ($portion * $a) $params = $curl & ' -L -# -r ' & $range[$a] & ' -o "' & $down[$a] & '" --cookie gogcom_cookies.txt "' & $link & '"' EndIf $pid[$a] = Run($params, "", @SW_MINIMIZE) If @error <> 0 Then $erred = $part[$a] For $e = 1 To $a ProcessClose($pid[$e]) Next EndIf EndIf If $erred <> 0 Then ExitLoop Next Else $params = $curl & ' -L -# -o "' & $download & '" --cookie gogcom_cookies.txt "' & $link & '"' $pid = Run($params, "", @SW_SHOW) If @error <> 0 Then $erred = $file EndIf ;MsgBox(262144, "Range Result", $range[1] & @LF & $part[1] & @LF & $down[1] & @LF & $params) ;MsgBox(262144, "PID Values Result", $pid[1] & @LF & $pid[2]) If $erred = 0 Then ; Continue with downloading of parts. ProgressOn("Download Progress Meter", $file, "0%", Default, Default, 16) Sleep($delay) While 1 If $threads > 1 Then $failed = 0 For $p = 1 To $threads $process = $pid[$p] If ProcessExists($process) = 0 Then $failed = $failed + 1 EndIf Next $p = 0 If $failed = $threads Then ExitLoop Sleep(500) $filepth = $down[1] If $found = "" Then $found = FileExists($filepth) Else If ProcessExists($pid) = 0 Then ExitLoop Sleep(500) If $found = "" Then $found = FileExists($download) EndIf If $found = 1 Then ; Get the file size(s) as they download. If $threads > 1 Then For $h = 1 To $threads $filepth = $down[$h] $handle = FileOpen($filepth, 0) DllCall("Kernel32.dll", "BOOLEAN", "FlushFileBuffers", "HANDLE", $handle) FileClose($handle) Next Else $handle = FileOpen($download, 0) DllCall("Kernel32.dll", "BOOLEAN", "FlushFileBuffers", "HANDLE", $handle) FileClose($handle) EndIf ; Get the folder size as the files download If $threads > 1 Then $growth = DirGetSize($tempfold) Else $growth = DirGetSize($downfold) EndIf If $growth <> 0 Then If $begin = "" Then $begin = TimerInit() $progress = $growth $progress = ($progress / $bytes) * 100 $percent = Floor($progress) & "%" $taken = TimerDiff($begin) $secs = $taken / 1000 ; Folder Size Growth $gotten = $growth / 1024 If $gotten < 1024 Then ; Kilkobytes downloaded $growth = Floor($gotten) & " Kb" Else ; Downloaded $growth = $gotten / 1024 If $growth < 1024 Then ; Megabytes downloaded $growth = Round($growth, 1) & " Mb" Else ; Gigabytes downloaded $growth = Round($growth / 1024, 2) & " Gb" EndIf EndIf ; MBs $gotten = $gotten / 1024 $speed = Round($gotten / $secs, 1) & " Mb/s" If $speed < 1 Then $speed = Floor($speed * 1024) & " Kb/s" ProgressSet($progress, $filesize & " -- " & $percent & " -- " & $growth & " -- " & $speed) EndIf Else MsgBox(262192, "Downloader Alert", "First download part appears to have failed!", 0) ; & @LF & $failed & @LF & $filepth ExitLoop EndIf WEnd ProgressSet(100, $filesize & " -- " & $percent & " -- " & $growth & " -- " & $speed, "Complete") Sleep(2000) ProgressSet(100, "Done", "Complete") Sleep(2000) $exists = 0 If ($gotten > $threads) Or $threads = 1 Then If $threads > 1 Then $files = "" For $f = 1 To $threads If FileExists($down[$f]) = 1 Then $exists = $exists + 1 If $files = "" Then $files = $down[$f] Else $files = $files & '" + "' & $down[$f] EndIf EndIf Next Else If FileExists($download) = 1 Then $exists = 1 EndIf Else MsgBox(262192, "Download Error", "File parts are too small.", 0) EndIf If $exists = $threads Then If $threads > 1 Then ProgressSet(100, "All Parts", "Joining") RunWait(@ComSpec & ' /c COPY /B "' & $files & '" "' & $download & '"', "", @SW_SHOW) Sleep(1000) EndIf If $validate = 1 Then ProgressSet(100, "Joined File", "Validating") If $checksum <> "" Then _Crypt_Startup() $hash = _Crypt_HashFile($download, $CALG_MD5) $hash = StringTrimLeft($hash, 2) If $hash = $checksum Then ProgressSet(100, "Download Result Checksum", "Passed") Else ProgressSet(100, "Download Result Checksum", "Failed") EndIf _Crypt_Shutdown() Else If $bytes = FileGetSize($download) Then ProgressSet(100, "Download Result Filesize", "Passed") Else ProgressSet(100, "Download Result Filesize", "Failed") EndIf EndIf Else ProgressSet(100, "Download Result", "Complete") EndIf Else ProgressSet(100, "Download Result", "Missing File(s)") EndIf Sleep(3000) If $validate = 1 Then MsgBox(262192, "Advice", "Downloading has finished.", 0) ProgressOff() Else ; Downloading of a part has failed, so abort. MsgBox(262192, "Download Error", "Downloading of a part has failed, so all are aborted." & @LF & @LF & $erred, 0) EndIf Else MsgBox(262192, "Downloader Alert", "A required value has not been specified!", 0) EndIf Else MsgBox(262192, "Downloader Alert", "The 'curl.exe' file could not be found!", 0) EndIf Exit