Merge pull request #1033 from LoveDoLove/lovedolove

修復 install.ps1 下載狀態條的問題
This commit is contained in:
Pin Studios 2025-06-18 10:18:31 +08:00 committed by GitHub
commit 1c36ff010e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -125,75 +125,61 @@ function Install-CursorFreeVIP {
Write-Styled "No existing installation file found, starting download..." -Color $Theme.Primary -Prefix "Download" Write-Styled "No existing installation file found, starting download..." -Color $Theme.Primary -Prefix "Download"
# Create WebClient and add progress event # Use HttpWebRequest for chunked download with real-time progress bar
$webClient = New-Object System.Net.WebClient $url = $asset.browser_download_url
$webClient.Headers.Add("User-Agent", "PowerShell Script") $outputFile = $downloadPath
Write-Styled "Downloading from: $url" -Color $Theme.Info -Prefix "URL"
Write-Styled "Saving to: $outputFile" -Color $Theme.Info -Prefix "Path"
# Define progress variables $request = [System.Net.HttpWebRequest]::Create($url)
$Global:downloadedBytes = 0 $request.UserAgent = "PowerShell Script"
$Global:totalBytes = 0 $response = $request.GetResponse()
$Global:lastProgress = 0 $totalLength = $response.ContentLength
$Global:lastBytes = 0 $responseStream = $response.GetResponseStream()
$Global:lastTime = Get-Date $fileStream = [System.IO.File]::OpenWrite($outputFile)
$buffer = New-Object byte[] 8192
# Download progress event $bytesRead = 0
$eventId = [guid]::NewGuid() $totalRead = 0
Register-ObjectEvent -InputObject $webClient -EventName DownloadProgressChanged -Action { $lastProgress = -1
$Global:downloadedBytes = $EventArgs.BytesReceived $startTime = Get-Date
$Global:totalBytes = $EventArgs.TotalBytesToReceive try {
$progress = [math]::Round(($Global:downloadedBytes / $Global:totalBytes) * 100, 1) do {
$bytesRead = $responseStream.Read($buffer, 0, $buffer.Length)
# Only update display when progress changes by more than 1% if ($bytesRead -gt 0) {
if ($progress -gt $Global:lastProgress + 1) { $fileStream.Write($buffer, 0, $bytesRead)
$Global:lastProgress = $progress $totalRead += $bytesRead
$downloadedMB = [math]::Round($Global:downloadedBytes / 1MB, 2) $progress = [math]::Round(($totalRead / $totalLength) * 100, 1)
$totalMB = [math]::Round($Global:totalBytes / 1MB, 2) if ($progress -ne $lastProgress) {
$elapsed = (Get-Date) - $startTime
# Calculate download speed $speed = if ($elapsed.TotalSeconds -gt 0) { $totalRead / $elapsed.TotalSeconds } else { 0 }
$currentTime = Get-Date $speedDisplay = if ($speed -gt 1MB) {
$timeSpan = ($currentTime - $Global:lastTime).TotalSeconds "{0:N2} MB/s" -f ($speed / 1MB)
if ($timeSpan -gt 0) { } elseif ($speed -gt 1KB) {
$bytesChange = $Global:downloadedBytes - $Global:lastBytes "{0:N2} KB/s" -f ($speed / 1KB)
$speed = $bytesChange / $timeSpan } else {
"{0:N2} B/s" -f $speed
# Choose appropriate unit based on speed }
$speedDisplay = if ($speed -gt 1MB) { $downloadedMB = [math]::Round($totalRead / 1MB, 2)
"$([math]::Round($speed / 1MB, 2)) MB/s" $totalMB = [math]::Round($totalLength / 1MB, 2)
} elseif ($speed -gt 1KB) { Write-Progress -Activity "Downloading CursorFreeVIP" -Status "$downloadedMB MB / $totalMB MB ($progress%) - $speedDisplay" -PercentComplete $progress
"$([math]::Round($speed / 1KB, 2)) KB/s" $lastProgress = $progress
} else {
"$([math]::Round($speed, 2)) B/s"
} }
Write-Host "`rDownloading: $downloadedMB MB / $totalMB MB ($progress%) - $speedDisplay" -NoNewline -ForegroundColor Cyan
# Update last data
$Global:lastBytes = $Global:downloadedBytes
$Global:lastTime = $currentTime
} }
} } while ($bytesRead -gt 0)
} | Out-Null } finally {
$fileStream.Close()
# Download completed event $responseStream.Close()
Register-ObjectEvent -InputObject $webClient -EventName DownloadFileCompleted -Action { $response.Close()
Write-Host "`r" -NoNewline
Write-Styled "Download completed!" -Color $Theme.Success -Prefix "Complete"
Unregister-Event -SourceIdentifier $eventId
} | Out-Null
# Start download
$webClient.DownloadFileAsync([Uri]$asset.browser_download_url, $downloadPath)
# Wait for download to complete
while ($webClient.IsBusy) {
Start-Sleep -Milliseconds 100
} }
Write-Progress -Activity "Downloading CursorFreeVIP" -Completed
Write-Styled "File location: $downloadPath" -Color $Theme.Info -Prefix "Location" # Check file exists and is not zero size
if (!(Test-Path $outputFile) -or ((Get-Item $outputFile).Length -eq 0)) {
throw "Download failed or file is empty."
}
Write-Styled "Download completed!" -Color $Theme.Success -Prefix "Complete"
Write-Styled "File location: $outputFile" -Color $Theme.Info -Prefix "Location"
Write-Styled "Starting program..." -Color $Theme.Primary -Prefix "Launch" Write-Styled "Starting program..." -Color $Theme.Primary -Prefix "Launch"
Start-Process $outputFile
# Run program
Start-Process $downloadPath
} }
catch { catch {
Write-Styled $_.Exception.Message -Color $Theme.Error -Prefix "Error" Write-Styled $_.Exception.Message -Color $Theme.Error -Prefix "Error"