Skip to content

Commit 8f34065

Browse files
committed
Improves download speeds to support more than just mbps. Enables package installs and moves final installs to proper directory on macOS.
1 parent d5f9aed commit 8f34065

File tree

1 file changed

+55
-29
lines changed

1 file changed

+55
-29
lines changed

UnitySetup/UnitySetup.psm1

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class UnityVersion : System.IComparable {
219219
([OperatingSystem]::Mac) { echo "On Mac" }
220220
}
221221
.EXAMPLE
222-
if (Get-OperatingSystem == [OperatingSystem]::Linux) {
222+
if (Get-OperatingSystem -eq [OperatingSystem]::Linux) {
223223
echo "On Linux"
224224
}
225225
#>
@@ -495,7 +495,7 @@ function Select-UnitySetupInstaller {
495495
}
496496
}
497497

498-
filter Get-FileSize {
498+
filter Format-Bytes {
499499
return "{0:N2} {1}" -f $(
500500
if ($_ -lt 1kb) { $_, 'Bytes' }
501501
elseif ($_ -lt 1mb) { ($_/1kb), 'KB' }
@@ -506,6 +506,30 @@ filter Get-FileSize {
506506
)
507507
}
508508

509+
function Format-BitsPerSecond {
510+
[CmdletBinding()]
511+
param(
512+
[parameter(Mandatory = $true)]
513+
[int] $Bytes,
514+
515+
[parameter(Mandatory = $true)]
516+
[int] $Seconds
517+
)
518+
if ($Seconds -le 0.001) {
519+
return "0 Bps"
520+
}
521+
# Convert from bytes to bits
522+
$Bits = ($Bytes * 8) / $Seconds
523+
return "{0:N2} {1}" -f $(
524+
if ($Bits -lt 1kb) { $Bits, 'Bps' }
525+
elseif ($Bits -lt 1mb) { ($Bits/1kb), 'Kbps' }
526+
elseif ($Bits -lt 1gb) { ($Bits/1mb), 'Mbps' }
527+
elseif ($Bits -lt 1tb) { ($Bits/1gb), 'Gbps' }
528+
elseif ($Bits -lt 1pb) { ($Bits/1tb), 'Tbps' }
529+
else { ($Bits/1pb), 'Pbps' }
530+
)
531+
}
532+
509533
function Request-UnitySetupInstaller {
510534
[CmdletBinding()]
511535
param(
@@ -587,19 +611,19 @@ function Request-UnitySetupInstaller {
587611
$receivedBytes = $global:DownloadProgressEvent.SourceArgs.BytesReceived
588612
$progress = [int](($receivedBytes / [double]$totalBytes) * 100)
589613

590-
# Average speed in Mbps
591-
$averageSpeed = ($receivedBytes * 8 / 1mb) / $elapsedTime.TotalSeconds
592-
$secondsRemaining = ($totalBytes - $receivedBytes) * 8 / 1mb / $averageSpeed
614+
$averageSpeed = $receivedBytes / $elapsedTime.TotalSeconds
615+
$secondsRemaining = ($totalBytes - $receivedBytes) / $averageSpeed
593616

594617
if ([double]::IsInfinity($secondsRemaining)) {
595618
$averageSpeed = 0
596619
# -1 for Write-Progress prevents seconds remaining from showing.
597620
$secondsRemaining = -1
598621
}
599622

600-
# TODO: Display in Kbps on slow networks.
601-
Write-Progress -Activity "$("{0:N2}" -f $averageSpeed) Mbps | Downloading $installerFileName" `
602-
-Status "$($receivedBytes | Get-FileSize) of $($totalBytes | Get-FileSize)" `
623+
$downloadSpeed = Format-BitsPerSecond -Bytes $receivedBytes -Seconds $elapsedTime.TotalSeconds
624+
625+
Write-Progress -Activity "Downloading $installerFileName | $downloadSpeed" `
626+
-Status "$($receivedBytes | Format-Bytes) of $($totalBytes | Format-Bytes)" `
603627
-SecondsRemaining $secondsRemaining `
604628
-PercentComplete $progress
605629
}
@@ -718,7 +742,7 @@ function Install-UnitySetupInstance {
718742
)
719743
begin {
720744
$currentOS = Get-OperatingSystem
721-
if ($currentOS == [OperatingSystem]::Linux) {
745+
if ($currentOS -eq [OperatingSystem]::Linux) {
722746
throw "Install-UnitySetupInstance has not been implemented on the Linux platform. Contributions welcomed!";
723747
}
724748

@@ -744,25 +768,10 @@ function Install-UnitySetupInstance {
744768
}
745769
}
746770
end {
747-
# foreach unity version
748-
# If macOS, move previous install back to default directory
749-
# Install main Unity setup installer first
750-
# Install all components to default Unity version
751-
# If macOS, move install to versioned directory
752771
$versionInstallers.Keys | ForEach-Object {
753772
$installVersion = $_
754773
$installerInstances = $versionInstallers[$installVersion]
755774

756-
if ($currentOS == [OperatingSystem]::Mac) {
757-
# On macOS we must notify the user to take an action if the default location
758-
# is currently in use. Either there's a previous version of Unity installed
759-
# manually or another install through UnitySetup possibly failed.
760-
if (Test-UnitySetupInstance -Path /Applications/Unity/) {
761-
# TODO: Work in a `$host.ui.PromptForChoice` / -Force param for resolving this.
762-
throw "Install-UnitySetupInstance has not yet handled working around the base install directory already existing. Please move this manually and try again. Contributions welcomed!";
763-
}
764-
}
765-
766775
if ( $PSBoundParameters.ContainsKey('Destination') ) {
767776
# Slight API change here. If BasePath is also provided treat Destination as a relative path.
768777
if ( $PSBoundParameters.ContainsKey('BasePath') ) {
@@ -776,6 +785,18 @@ function Install-UnitySetupInstance {
776785
$installPath = "$defaultInstallPath-$installVersion"
777786
}
778787

788+
if ($currentOS -eq [OperatingSystem]::Mac) {
789+
# On macOS we must notify the user to take an action if the default location
790+
# is currently in use. Either there's a previous version of Unity installed
791+
# manually or another install through UnitySetup possibly failed.
792+
if (Test-UnitySetupInstance -Path /Applications/Unity/) {
793+
# TODO: Work in a `$host.ui.PromptForChoice` / -Force param for resolving this.
794+
throw "Install-UnitySetupInstance has not yet handled working around the base install directory already existing. Please move this manually and try again. Contributions welcomed!";
795+
}
796+
797+
# TODO: Test if $installPath contains a/this version of Unity to move back to /Applications/Unity/
798+
}
799+
779800
# TODO: Strip out components already installed in the destination.
780801

781802
$installerPaths = $installerInstances | Request-UnitySetupInstaller -Cache $Cache
@@ -789,17 +810,22 @@ function Install-UnitySetupInstance {
789810
$editorInstaller = $installerPaths | Where-Object { $_.ComponentType -band $editorComponent }
790811
if ($null -ne $editorInstaller) {
791812
Write-Verbose "Installing $($editorInstaller.ComponentType)"
792-
# Install-UnitySetupPackage -Package $editorInstaller -Destination $destination
813+
Install-UnitySetupPackage -Package $editorInstaller -Destination $installPath
793814
}
794815

795816
$installerPaths | ForEach-Object {
796-
Write-Verbose "Installing $($editorInstaller.ComponentType)"
797-
# Install-UnitySetupPackage -Package $_ -Destination $destination
817+
# Already installed this earlier. Skipping.
818+
if ($_.ComponentType -band $editorComponent) {
819+
return
820+
}
821+
822+
Write-Verbose "Installing $($_.ComponentType)"
823+
Install-UnitySetupPackage -Package $_ -Destination $installPath
798824
}
799825

800826
# Move the install from the staging area to the desired destination
801-
if ($currentOS == [OperatingSystem]::Mac) {
802-
#Move-Item -Path /Applications/Unity/ -Destination $installPath
827+
if ($currentOS -eq [OperatingSystem]::Mac) {
828+
Move-Item -Path /Applications/Unity/ -Destination $installPath
803829
}
804830
}
805831
}

0 commit comments

Comments
 (0)