Skip to content

Commit 0881fff

Browse files
authored
Merge pull request #166 from jwittner/fix/shutdownHang
Fix hang with Start-UnityEditor -Quit -Wait
2 parents d4db5cd + eb01668 commit 0881fff

File tree

1 file changed

+91
-97
lines changed

1 file changed

+91
-97
lines changed

UnitySetup/UnitySetup.psm1

Lines changed: 91 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ class UnitySetupInstance {
7373
$this.Components = [UnitySetupComponent]::Windows
7474
$playbackEnginePath = [io.path]::Combine("$Path", "Editor\Data\PlaybackEngines");
7575
@{
76-
[UnitySetupComponent]::Documentation = , [io.path]::Combine("$Path", "Editor\Data\Documentation");
76+
[UnitySetupComponent]::Documentation = , [io.path]::Combine("$Path", "Editor\Data\Documentation");
7777
[UnitySetupComponent]::StandardAssets = , [io.path]::Combine("$Path", "Editor\Standard Assets");
7878
[UnitySetupComponent]::Windows_IL2CPP = , [io.path]::Combine("$playbackEnginePath", "windowsstandalonesupport\Variations\win32_development_il2cpp");
79-
[UnitySetupComponent]::UWP = [io.path]::Combine("$playbackEnginePath", "MetroSupport\Templates\UWP_.NET_D3D"),
79+
[UnitySetupComponent]::UWP = [io.path]::Combine("$playbackEnginePath", "MetroSupport\Templates\UWP_.NET_D3D"),
8080
[io.path]::Combine("$playbackEnginePath", "MetroSupport\Templates\UWP_D3D");
81-
[UnitySetupComponent]::UWP_IL2CPP = , [io.path]::Combine("$playbackEnginePath", "MetroSupport\Templates\UWP_IL2CPP_D3D");
82-
[UnitySetupComponent]::Linux = , [io.path]::Combine("$playbackEnginePath", "LinuxStandaloneSupport");
83-
[UnitySetupComponent]::Mac = , [io.path]::Combine("$playbackEnginePath", "MacStandaloneSupport");
81+
[UnitySetupComponent]::UWP_IL2CPP = , [io.path]::Combine("$playbackEnginePath", "MetroSupport\Templates\UWP_IL2CPP_D3D");
82+
[UnitySetupComponent]::Linux = , [io.path]::Combine("$playbackEnginePath", "LinuxStandaloneSupport");
83+
[UnitySetupComponent]::Mac = , [io.path]::Combine("$playbackEnginePath", "MacStandaloneSupport");
8484
}
8585
}
8686
([OperatingSystem]::Linux) {
@@ -92,10 +92,10 @@ class UnitySetupInstance {
9292
$this.Components = [UnitySetupComponent]::Mac
9393
$playbackEnginePath = [io.path]::Combine("$Path", "PlaybackEngines");
9494
@{
95-
[UnitySetupComponent]::Documentation = , [io.path]::Combine("$Path", "Documentation");
95+
[UnitySetupComponent]::Documentation = , [io.path]::Combine("$Path", "Documentation");
9696
[UnitySetupComponent]::StandardAssets = , [io.path]::Combine("$Path", "Standard Assets");
97-
[UnitySetupComponent]::Windows = , [io.path]::Combine("$playbackEnginePath", "WindowsStandaloneSupport");
98-
[UnitySetupComponent]::Linux = , [io.path]::Combine("$playbackEnginePath", "LinuxStandaloneSupport");
97+
[UnitySetupComponent]::Windows = , [io.path]::Combine("$playbackEnginePath", "WindowsStandaloneSupport");
98+
[UnitySetupComponent]::Linux = , [io.path]::Combine("$playbackEnginePath", "LinuxStandaloneSupport");
9999
}
100100
}
101101
}
@@ -309,18 +309,18 @@ function Find-UnitySetupInstaller {
309309
)
310310

311311
$installerTemplates = @{
312-
[UnitySetupComponent]::UWP = "$targetSupport/UnitySetup-UWP-.NET-Support-for-Editor-$Version.$installerExtension",
313-
"$targetSupport/UnitySetup-Metro-Support-for-Editor-$Version.$installerExtension";
314-
[UnitySetupComponent]::UWP_IL2CPP = , "$targetSupport/UnitySetup-UWP-IL2CPP-Support-for-Editor-$Version.$installerExtension";
315-
[UnitySetupComponent]::Android = , "$targetSupport/UnitySetup-Android-Support-for-Editor-$Version.$installerExtension";
316-
[UnitySetupComponent]::iOS = , "$targetSupport/UnitySetup-iOS-Support-for-Editor-$Version.$installerExtension";
317-
[UnitySetupComponent]::AppleTV = , "$targetSupport/UnitySetup-AppleTV-Support-for-Editor-$Version.$installerExtension";
318-
[UnitySetupComponent]::Facebook = , "$targetSupport/UnitySetup-Facebook-Games-Support-for-Editor-$Version.$installerExtension";
319-
[UnitySetupComponent]::Linux = , "$targetSupport/UnitySetup-Linux-Support-for-Editor-$Version.$installerExtension";
320-
[UnitySetupComponent]::Mac = "$targetSupport/UnitySetup-Mac-Support-for-Editor-$Version.$installerExtension",
312+
[UnitySetupComponent]::UWP = "$targetSupport/UnitySetup-UWP-.NET-Support-for-Editor-$Version.$installerExtension",
313+
"$targetSupport/UnitySetup-Metro-Support-for-Editor-$Version.$installerExtension";
314+
[UnitySetupComponent]::UWP_IL2CPP = , "$targetSupport/UnitySetup-UWP-IL2CPP-Support-for-Editor-$Version.$installerExtension";
315+
[UnitySetupComponent]::Android = , "$targetSupport/UnitySetup-Android-Support-for-Editor-$Version.$installerExtension";
316+
[UnitySetupComponent]::iOS = , "$targetSupport/UnitySetup-iOS-Support-for-Editor-$Version.$installerExtension";
317+
[UnitySetupComponent]::AppleTV = , "$targetSupport/UnitySetup-AppleTV-Support-for-Editor-$Version.$installerExtension";
318+
[UnitySetupComponent]::Facebook = , "$targetSupport/UnitySetup-Facebook-Games-Support-for-Editor-$Version.$installerExtension";
319+
[UnitySetupComponent]::Linux = , "$targetSupport/UnitySetup-Linux-Support-for-Editor-$Version.$installerExtension";
320+
[UnitySetupComponent]::Mac = "$targetSupport/UnitySetup-Mac-Support-for-Editor-$Version.$installerExtension",
321321
"$targetSupport/UnitySetup-Mac-Mono-Support-for-Editor-$Version.$installerExtension";
322-
[UnitySetupComponent]::Vuforia = , "$targetSupport/UnitySetup-Vuforia-AR-Support-for-Editor-$Version.$installerExtension";
323-
[UnitySetupComponent]::WebGL = , "$targetSupport/UnitySetup-WebGL-Support-for-Editor-$Version.$installerExtension";
322+
[UnitySetupComponent]::Vuforia = , "$targetSupport/UnitySetup-Vuforia-AR-Support-for-Editor-$Version.$installerExtension";
323+
[UnitySetupComponent]::WebGL = , "$targetSupport/UnitySetup-WebGL-Support-for-Editor-$Version.$installerExtension";
324324
[UnitySetupComponent]::Windows_IL2CPP = , "$targetSupport/UnitySetup-Windows-IL2CPP-Support-for-Editor-$Version.$installerExtension";
325325
}
326326

@@ -434,10 +434,10 @@ function Find-UnitySetupInstaller {
434434
}
435435
$result = New-Object UnitySetupInstaller -Property @{
436436
'ComponentType' = $_;
437-
'Version' = $Version;
438-
'DownloadUrl' = $endpoint;
439-
'Length' = $installerLength;
440-
'LastModified' = $lastModified;
437+
'Version' = $Version;
438+
'DownloadUrl' = $endpoint;
439+
'Length' = $installerLength;
440+
'LastModified' = $lastModified;
441441
}
442442

443443
break
@@ -540,13 +540,13 @@ function Select-UnitySetupInstaller {
540540
}
541541

542542
filter Format-Bytes {
543-
return "{0:N2} {1}" -f $(
544-
if ($_ -lt 1kb) { $_, 'Bytes' }
545-
elseif ($_ -lt 1mb) { ($_/1kb), 'KB' }
546-
elseif ($_ -lt 1gb) { ($_/1mb), 'MB' }
547-
elseif ($_ -lt 1tb) { ($_/1gb), 'GB' }
548-
elseif ($_ -lt 1pb) { ($_/1tb), 'TB' }
549-
else { ($_/1pb), 'PB' }
543+
return "{0:N2} {1}" -f $(
544+
if ($_ -lt 1kb) { $_, 'Bytes' }
545+
elseif ($_ -lt 1mb) { ($_ / 1kb), 'KB' }
546+
elseif ($_ -lt 1gb) { ($_ / 1mb), 'MB' }
547+
elseif ($_ -lt 1tb) { ($_ / 1gb), 'GB' }
548+
elseif ($_ -lt 1pb) { ($_ / 1tb), 'TB' }
549+
else { ($_ / 1pb), 'PB' }
550550
)
551551
}
552552

@@ -564,13 +564,13 @@ function Format-BitsPerSecond {
564564
}
565565
# Convert from bytes to bits
566566
$Bits = ($Bytes * 8) / $Seconds
567-
return "{0:N2} {1}" -f $(
568-
if ($Bits -lt 1kb) { $Bits, 'Bps' }
569-
elseif ($Bits -lt 1mb) { ($Bits/1kb), 'Kbps' }
570-
elseif ($Bits -lt 1gb) { ($Bits/1mb), 'Mbps' }
571-
elseif ($Bits -lt 1tb) { ($Bits/1gb), 'Gbps' }
572-
elseif ($Bits -lt 1pb) { ($Bits/1tb), 'Tbps' }
573-
else { ($Bits/1pb), 'Pbps' }
567+
return "{0:N2} {1}" -f $(
568+
if ($Bits -lt 1kb) { $Bits, 'Bps' }
569+
elseif ($Bits -lt 1mb) { ($Bits / 1kb), 'Kbps' }
570+
elseif ($Bits -lt 1gb) { ($Bits / 1mb), 'Mbps' }
571+
elseif ($Bits -lt 1tb) { ($Bits / 1gb), 'Gbps' }
572+
elseif ($Bits -lt 1pb) { ($Bits / 1tb), 'Tbps' }
573+
else { ($Bits / 1pb), 'Pbps' }
574574
)
575575
}
576576

@@ -638,7 +638,7 @@ function Request-UnitySetupInstaller {
638638

639639
$resource = New-Object UnitySetupResource -Property @{
640640
'ComponentType' = $_.ComponentType
641-
'Path' = $destination
641+
'Path' = $destination
642642
}
643643
$downloads += , $resource
644644
return
@@ -655,15 +655,15 @@ function Request-UnitySetupInstaller {
655655
++$downloadIndex
656656
$global:downloadData[$installerFileName] = New-Object PSObject -Property @{
657657
installerFileName = $installerFileName
658-
startTime = Get-Date
659-
totalBytes = $_.Length
660-
receivedBytes = 0
661-
isDownloaded = $false
662-
destination = $destination
663-
lastModified = $_.LastModified
664-
componentType = $_.ComponentType
665-
webClient = $webClient
666-
downloadIndex = $downloadIndex
658+
startTime = Get-Date
659+
totalBytes = $_.Length
660+
receivedBytes = 0
661+
isDownloaded = $false
662+
destination = $destination
663+
lastModified = $_.LastModified
664+
componentType = $_.ComponentType
665+
webClient = $webClient
666+
downloadIndex = $downloadIndex
667667
}
668668

669669
# Register to events for showing progress of file download.
@@ -674,8 +674,7 @@ function Request-UnitySetupInstaller {
674674
$global:downloadData[$event.MessageData].isDownloaded = $true
675675
} | Out-Null
676676

677-
try
678-
{
677+
try {
679678
Write-Verbose "Downloading $($_.DownloadUrl) to $destination"
680679
$webClient.DownloadFileAsync($_.DownloadUrl, $destination)
681680
}
@@ -720,7 +719,7 @@ function Request-UnitySetupInstaller {
720719

721720
$resource = New-Object UnitySetupResource -Property @{
722721
'ComponentType' = $data.componentType
723-
'Path' = $data.destination
722+
'Path' = $data.destination
724723
}
725724
$downloads += , $resource
726725
return
@@ -789,10 +788,10 @@ function Install-UnitySetupPackage {
789788
switch ($currentOS) {
790789
([OperatingSystem]::Windows) {
791790
$startProcessArgs = @{
792-
'FilePath' = $Package.Path;
791+
'FilePath' = $Package.Path;
793792
'ArgumentList' = @("/S", "/D=$Destination");
794-
'PassThru' = $true;
795-
'Wait' = $true;
793+
'PassThru' = $true;
794+
'Wait' = $true;
796795
}
797796
}
798797
([OperatingSystem]::Linux) {
@@ -802,10 +801,10 @@ function Install-UnitySetupPackage {
802801
# Note that $Destination has to be a disk path.
803802
# sudo installer -package $Package.Path -target /
804803
$startProcessArgs = @{
805-
'FilePath' = 'sudo';
804+
'FilePath' = 'sudo';
806805
'ArgumentList' = @("installer", "-package", $Package.Path, "-target", $Destination);
807-
'PassThru' = $true;
808-
'Wait' = $true;
806+
'PassThru' = $true;
807+
'Wait' = $true;
809808
}
810809
}
811810
}
@@ -985,8 +984,7 @@ function Install-UnitySetupInstance {
985984
# Move the install from the sparse bundle disk to the install directory.
986985
if ($currentOS -eq [OperatingSystem]::Mac) {
987986
# rsync does not recursively create the directory path.
988-
if (-not (Test-Path $installPath -PathType Container))
989-
{
987+
if (-not (Test-Path $installPath -PathType Container)) {
990988
Write-Verbose "Creating directory $installPath."
991989
New-Item $installPath -ItemType Directory -ErrorAction Stop | Out-Null
992990
}
@@ -1039,10 +1037,10 @@ function Uninstall-UnitySetupInstance {
10391037
}
10401038

10411039
$startProcessArgs = @{
1042-
'FilePath' = $uninstaller;
1043-
'PassThru' = $true;
1044-
'Wait' = $true;
1045-
'ErrorAction' = 'Stop';
1040+
'FilePath' = $uninstaller;
1041+
'PassThru' = $true;
1042+
'Wait' = $true;
1043+
'ErrorAction' = 'Stop';
10461044
'ArgumentList' = @("/S");
10471045
}
10481046

@@ -1186,10 +1184,10 @@ function Get-UnityProjectInstance {
11861184
)
11871185

11881186
$args = @{
1189-
'Path' = $BasePath;
1190-
'Filter' = 'ProjectSettings';
1187+
'Path' = $BasePath;
1188+
'Filter' = 'ProjectSettings';
11911189
'ErrorAction' = 'Ignore';
1192-
'Directory' = $true;
1190+
'Directory' = $true;
11931191
}
11941192

11951193
if ( $Recurse ) {
@@ -1500,37 +1498,34 @@ function Start-UnityEditor {
15001498
$unityArgs = $sharedArgs | ForEach-Object { $_ }
15011499
if ( $instanceArgs[$i] ) { $unityArgs += $instanceArgs[$i] }
15021500

1503-
$setProcessArgs = @{
1504-
'FilePath' = $editor;
1505-
'PassThru' = $true;
1506-
'ErrorAction' = 'Stop';
1507-
'RedirectStandardOutput' = New-TemporaryFile;
1508-
'RedirectStandardError' = New-TemporaryFile;
1509-
}
1510-
1511-
if ($Wait) { $setProcessArgs['Wait'] = $true }
1512-
1513-
Write-Verbose "Redirecting standard output to $($setProcessArgs['RedirectStandardOutput'])"
1514-
Write-Verbose "Redirecting standard error to $($setProcessArgs['RedirectStandardError'])"
1515-
15161501
$actionString = "$editor $unityArgs"
1517-
if( $Credential ) { $actionString += " -password (hidden)"}
1518-
if( $Serial ) { $actionString += " -serial (hidden)"}
1502+
if ( $Credential ) { $actionString += " -password (hidden)"}
1503+
if ( $Serial ) { $actionString += " -serial (hidden)"}
15191504

1520-
if (-not $PSCmdlet.ShouldProcess($actionString, "Start-Process")) {
1505+
if (-not $PSCmdlet.ShouldProcess($actionString, "System.Diagnostics.Process.Start()")) {
15211506
continue
15221507
}
15231508

15241509
# Defered till after potential display by ShouldProcess
15251510
if ( $Credential ) { $unityArgs += '-password', $Credential.GetNetworkCredential().Password }
15261511
if ( $Serial ) { $unityArgs += '-serial', [System.Net.NetworkCredential]::new($null, $Serial).Password }
15271512

1528-
if ($unityArgs -and $unityArgs.Length -gt 0) {
1529-
$setProcessArgs['ArgumentList'] = $unityArgs
1530-
}
1531-
1532-
$process = Start-Process @setProcessArgs
1513+
# We've experienced issues with Start-Process -Wait and redirecting
1514+
# output so we're using the Process class directly now.
1515+
$process = New-Object System.Diagnostics.Process
1516+
$process.StartInfo.Filename = $editor
1517+
$process.StartInfo.Arguments = $unityArgs
1518+
$process.StartInfo.RedirectStandardOutput = $true
1519+
$process.StartInfo.RedirectStandardError = $true
1520+
$process.StartInfo.UseShellExecute = $false
1521+
$process.StartInfo.CreateNoWindow = $true
1522+
$process.StartInfo.WorkingDirectory = $PWD
1523+
$process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
1524+
$process.Start() | Out-Null
1525+
15331526
if ( $Wait ) {
1527+
$process.WaitForExit()
1528+
15341529
if ( $LogFile -and (Test-Path $LogFile -Type Leaf) ) {
15351530
# Note that Unity sometimes returns a success ExitCode despite the presence of errors, but we want
15361531
# to make sure that we flag such errors.
@@ -1593,7 +1588,7 @@ function Get-IsUnityError {
15931588
function ConvertTo-DateTime {
15941589
param([string] $Text)
15951590

1596-
if( -not $text -or $text.Length -eq 0 ) { [DateTime]::MaxValue }
1591+
if ( -not $text -or $text.Length -eq 0 ) { [DateTime]::MaxValue }
15971592
else { [DateTime]$Text }
15981593
}
15991594

@@ -1605,10 +1600,9 @@ function ConvertTo-DateTime {
16051600
.EXAMPLE
16061601
Get-UnityLicense
16071602
#>
1608-
function Get-UnityLicense
1609-
{
1603+
function Get-UnityLicense {
16101604
[CmdletBinding()]
1611-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Justification="Used to convert discovered plaintext serials into secure strings.")]
1605+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Justification = "Used to convert discovered plaintext serials into secure strings.")]
16121606
param([SecureString]$Serial)
16131607

16141608
$licenseFiles = Get-ChildItem "C:\ProgramData\Unity\Unity_*.ulf" -ErrorAction 'SilentlyContinue'
@@ -1619,18 +1613,18 @@ function Get-UnityLicense
16191613

16201614
# The first four bytes look like a count so skip that to pull out the serial string
16211615
$licenseSerial = [String]::new($devBytes[4..($devBytes.Length - 1)])
1622-
if( $Serial -and [System.Net.NetworkCredential]::new($null, $Serial).Password -ne $licenseSerial ) { continue; }
1616+
if ( $Serial -and [System.Net.NetworkCredential]::new($null, $Serial).Password -ne $licenseSerial ) { continue; }
16231617

16241618
$license = $doc.root.License
16251619
[PSCustomObject]@{
16261620
'LicenseVersion' = $license.LicenseVersion.Value
1627-
'Serial' = ConvertTo-SecureString $licenseSerial -AsPlainText -Force
1628-
'UnityVersion' = [UnityVersion]$license.ClientProvidedVersion.Value
1629-
'DisplaySerial' = $license.SerialMasked.Value
1621+
'Serial' = ConvertTo-SecureString $licenseSerial -AsPlainText -Force
1622+
'UnityVersion' = [UnityVersion]$license.ClientProvidedVersion.Value
1623+
'DisplaySerial' = $license.SerialMasked.Value
16301624
'ActivationDate' = ConvertTo-DateTime $license.InitialActivationDate.Value
1631-
'StartDate' = ConvertTo-DateTime $license.StartDate.Value
1632-
'StopDate' = ConvertTo-DateTime $license.StopDate.Value
1633-
'UpdateDate' = ConvertTo-DateTime $license.UpdateDate.Value
1625+
'StartDate' = ConvertTo-DateTime $license.StartDate.Value
1626+
'StopDate' = ConvertTo-DateTime $license.StopDate.Value
1627+
'UpdateDate' = ConvertTo-DateTime $license.UpdateDate.Value
16341628
}
16351629
}
16361630
}

0 commit comments

Comments
 (0)