@@ -486,13 +486,26 @@ function Select-UnitySetupInstaller {
486
486
# Keep only the matching component(s).
487
487
$Installers = $Installers | Where-Object { $Components -band $_.ComponentType } | ForEach-Object { $_ }
488
488
489
- $selectedInstallers += $Installers
489
+ if ($Installers.Length -ne 0 ) {
490
+ $selectedInstallers += $Installers
491
+ }
490
492
}
491
493
end {
492
494
return $selectedInstallers
493
495
}
494
496
}
495
497
498
+ filter Get-FileSize {
499
+ return " {0:N2} {1}" -f $ (
500
+ if ($_ -lt 1 kb ) { $_ , ' Bytes' }
501
+ elseif ($_ -lt 1 mb ) { ($_ / 1 kb ), ' KB' }
502
+ elseif ($_ -lt 1 gb ) { ($_ / 1 mb ), ' MB' }
503
+ elseif ($_ -lt 1 tb ) { ($_ / 1 gb ), ' GB' }
504
+ elseif ($_ -lt 1 pb ) { ($_ / 1 tb ), ' TB' }
505
+ else { ($_ / 1 pb ), ' PB' }
506
+ )
507
+ }
508
+
496
509
function Request-UnitySetupInstaller {
497
510
[CmdletBinding ()]
498
511
param (
@@ -535,64 +548,83 @@ function Request-UnitySetupInstaller {
535
548
$downloads += , $resource
536
549
return
537
550
}
538
- elseif ($destinationItem.Length -eq $_.Length ) {
539
- Write-Verbose " Skipping download because file size is the same: $ ( $_.DownloadUrl ) "
540
-
541
- # Re-writes the last modified time for ensuring downloads cache.
542
- $downloadedFile = Get-Item $destination
543
- $downloadedFile.LastWriteTime = $_.LastModified
544
-
545
- $downloadedFile = Get-Item $destination
546
- if ( ($downloadedFile.LastWriteTime -ne $_.LastModified ) ) {
547
- Write-Verbose " Modified time not set for $destination "
548
- }
549
- }
550
551
}
551
552
552
553
$destinationDirectory = [io.path ]::GetDirectoryName($destination )
553
554
if (! (Test-Path $destinationDirectory - PathType Container)) {
554
555
New-Item " $destinationDirectory " - ItemType Directory | Out-Null
555
556
}
556
557
558
+ $webClient = New-Object System.Net.WebClient
559
+
560
+ # Register to events for showing progress of file download.
561
+ Register-ObjectEvent - InputObject $webClient - EventName DownloadProgressChanged - SourceIdentifier DownloadProgressChanged - Action {
562
+ $global :DownloadProgressEvent = $event
563
+ } | Out-Null
564
+ Register-ObjectEvent - InputObject $webClient - EventName DownloadFileCompleted - SourceIdentifier DownloadFileCompleted - Action {
565
+ $global :isDownloaded = $true
566
+ } | Out-Null
567
+
557
568
try
558
569
{
559
- Write-Verbose " Downloading $ ( $_ .DownloadUrl ) to $destination "
570
+ $startTime = Get-Date
560
571
561
- $webClient = New-Object System.Net.WebClient
562
- Register-ObjectEvent - InputObject $webClient - EventName DownloadFileCompleted `
563
- - SourceIdentifier Web.DownloadFileCompleted - Action {
564
- $global :isDownloaded = $true
565
- }
566
- Register-ObjectEvent - InputObject $webClient - EventName DownloadProgressChanged `
567
- - SourceIdentifier Web.DownloadProgressChanged - Action {
568
- $global :DownloadProgressEvent = $event
569
- }
572
+ $global :DownloadProgressEvent = $null
573
+ $global :isDownloaded = $false
574
+
575
+ Write-Verbose " Downloading $ ( $_.DownloadUrl ) to $destination "
570
576
$webClient.DownloadFileAsync ($_.DownloadUrl , $destination )
571
577
578
+ # Showing progress of file download
572
579
$totalBytes = $_.Length
573
- while (-not $isDownloaded ) {
580
+ while (-not $global :isDownloaded ) {
581
+ if ($null -eq $global :DownloadProgressEvent ) {
582
+ continue
583
+ }
584
+
585
+ $elapsedTime = (Get-Date ) - $startTime
586
+
574
587
$receivedBytes = $global :DownloadProgressEvent.SourceArgs.BytesReceived
575
588
$progress = [int ](($receivedBytes / [double ]$totalBytes ) * 100 )
576
589
577
- Write-Progress - Activity " Downloading $ ( $_.DownloadUrl ) ..." - Status " $receivedBytes bytes \ $totalBytes bytes" - PercentComplete $progress
578
- [System.Threading.Thread ]::Sleep(500 )
579
- }
580
- Write-Progress - Activity " Downloading $ ( $_.DownloadUrl ) ..." - Status " $receivedBytes bytes \ $totalBytes bytes" - Completed
590
+ # Average speed in Mbps
591
+ $averageSpeed = ($receivedBytes * 8 / 1 mb ) / $elapsedTime.TotalSeconds
592
+ $secondsRemaining = ($totalBytes - $receivedBytes ) * 8 / 1 mb / $averageSpeed
581
593
582
- # Re-writes the last modified time for ensuring downloads cache.
583
- $downloadedFile = Get-Item $destination
584
- $downloadedFile.LastWriteTime = $_.LastModified
594
+ if ([double ]::IsInfinity($secondsRemaining )) {
595
+ $averageSpeed = 0
596
+ # -1 for Write-Progress prevents seconds remaining from showing.
597
+ $secondsRemaining = -1
598
+ }
585
599
586
- $resource = New-Object UnitySetupResource - Property @ {
587
- ' ComponentType' = $_.ComponentType
588
- ' Path' = $destination
600
+ # TODO: Display in Kbps on slow networks.
601
+ Write-Progress - Activity " $ ( " {0:N2}" -f $averageSpeed ) Mbps`n Downloading $ ( $_.DownloadUrl ) " `
602
+ - Status " $ ( $receivedBytes | Get-FileSize ) of $ ( $totalBytes | Get-FileSize ) " `
603
+ - SecondsRemaining $secondsRemaining `
604
+ - PercentComplete $progress
589
605
}
590
- $downloads += , $resource
591
606
}
592
- catch [System.Net.WebException ]
593
- {
594
- Write-Error " Failed downloading $ ( $installerFileName ) : $ ( $_.Exception.Message ) "
607
+ catch [System.Net.WebException ] {
608
+ Write-Error " Failed downloading $installerFileName - $ ( $_.Exception.Message ) "
595
609
}
610
+ finally {
611
+ Unregister-Event - SourceIdentifier DownloadFileCompleted - Force
612
+ Unregister-Event - SourceIdentifier DownloadProgressChanged - Force
613
+
614
+ $webClient.Dispose ()
615
+
616
+ Write-Progress - Activity " Downloading $ ( $_.DownloadUrl ) " - Status " Done" - Completed
617
+ }
618
+
619
+ # Re-writes the last modified time for ensuring downloads are cached properly.
620
+ $downloadedFile = Get-Item $destination
621
+ $downloadedFile.LastWriteTime = $_.LastModified
622
+
623
+ $resource = New-Object UnitySetupResource - Property @ {
624
+ ' ComponentType' = $_.ComponentType
625
+ ' Path' = $destination
626
+ }
627
+ $downloads += , $resource
596
628
}
597
629
}
598
630
end {
@@ -606,7 +638,7 @@ function Install-UnitySetupPackage {
606
638
[parameter (Mandatory = $true )]
607
639
[UnitySetupResource ] $Package ,
608
640
609
- [parameter (Mandatory = $false )]
641
+ [parameter (Mandatory = $true )]
610
642
[string ]$Destination
611
643
)
612
644
0 commit comments