Skip to content

Commit 700c481

Browse files
committed
Fix instance discovery, better error handling
1 parent 51d8d94 commit 700c481

File tree

1 file changed

+72
-43
lines changed

1 file changed

+72
-43
lines changed

UnitySetup/UnitySetup.psm1

Lines changed: 72 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -50,42 +50,10 @@ class UnitySetupInstance {
5050

5151
UnitySetupInstance([string]$path) {
5252
$currentOS = Get-OperatingSystem
53-
$foundVersion = $false
54-
55-
Write-Verbose "Attempting to get Unity Setup Instance at $path"
56-
57-
if ( Test-Path "$path\modules.json" ) {
58-
59-
$modules = (Get-Content "$path\modules.json" -Raw) | ConvertFrom-Json
60-
61-
foreach ( $module in $modules ) {
62-
Write-Verbose "Found module $($module.id)"
63-
Write-Verbose "Download Url $($module.DownloadUrl)"
64-
65-
if ( $module.DownloadUrl -match "(\d+)\.(\d+)\.(\d+)([fpab])(\d+)" ) {
66-
$this.Version = [UnityVersion]$Matches[0]
67-
$foundVersion = $true
68-
Write-Verbose "Found $($this.Version)"
69-
break
70-
}
71-
}
72-
}
73-
else {
74-
# We'll attempt to search for the version using the ivy.xml definitions for legacy editor compatibility.
75-
$ivy = Get-ChildItem -Path $path -Filter ivy.xml -Recurse -ErrorAction SilentlyContinue -Force | Select-Object -First 1
76-
77-
if ( $null -ne $ivy ) {
78-
[xml]$xmlDoc = Get-Content $ivy.FullName
79-
$this.Version = [UnityVersion]$xmlDoc.'ivy-module'.info.unityVersion
80-
$foundVersion = $true
81-
}
82-
}
83-
84-
if ( $foundVersion -eq $false ) {
85-
throw "Failed to find a valid version identifier for installation at $path!";
86-
}
8753

8854
$this.Path = $path
55+
$this.Version = Get-UnitySetupInstanceVersion -Path $path
56+
if ( -not $this.Version ) { throw "Unable to find version for $path" }
8957

9058
$playbackEnginePath = $null
9159
$componentTests = switch ($currentOS) {
@@ -903,8 +871,6 @@ function Install-UnitySetupInstance {
903871
$defaultInstallPath = $BasePath
904872
}
905873

906-
$unitySetupInstances = Get-UnitySetupInstance -BasePath $BasePath
907-
908874
$versionInstallers = @{}
909875
}
910876
process {
@@ -1109,16 +1075,79 @@ function Get-UnitySetupInstance {
11091075
}
11101076
}
11111077

1112-
$searchPaths = Get-ChildItem $BasePath -Directory
1113-
$setupInstances = [UnitySetupInstance[]]@()
1114-
1115-
foreach ( $path in $searchPaths ) {
1116-
if( $path -match "(\d+)\.(\d+)\.(\d+)([fpab])(\d+)") {
1117-
$setupInstances += , [UnitySetupInstance]::new($path)
1078+
$results = Get-ChildItem $BasePath -Directory | Where-Object {
1079+
Test-Path (Join-Path $_.FullName 'Editor\Unity.exe') -PathType Leaf
1080+
} | ForEach-Object {
1081+
$path = $_.FullName
1082+
try {
1083+
Write-Verbose "Creating UnitySetupInstance for $path"
1084+
[UnitySetupInstance]::new($path)
1085+
}
1086+
catch {
1087+
Write-Warning "$_"
11181088
}
11191089
}
11201090

1121-
return $setupInstances
1091+
$results
1092+
}
1093+
1094+
<#
1095+
.Synopsis
1096+
Gets the UnityVersion for a UnitySetupInstance at Path
1097+
.DESCRIPTION
1098+
Given a set of unity setup instances, this will select the best one matching your requirements
1099+
.PARAMETER Path
1100+
Path to a UnitySetupInstance
1101+
.OUTPUTS
1102+
UnityVersion
1103+
Returns the UnityVersion for the UnitySetupInstance at Path, or nothing if there isn't one
1104+
.EXAMPLE
1105+
Get-UnitySetupInstanceVersion -Path 'C:\Program Files\Unity'
1106+
#>
1107+
function Get-UnitySetupInstanceVersion {
1108+
[CmdletBinding()]
1109+
param(
1110+
[ValidateNotNullOrEmpty()]
1111+
[ValidateScript( {Test-Path $_ -PathType Container})]
1112+
[Parameter(Mandatory = $true, Position = 0)]
1113+
[string]$Path
1114+
)
1115+
1116+
Write-Verbose "Attempting to find UnityVersion in $path"
1117+
1118+
if ( Test-Path "$path\modules.json" -PathType Leaf ) {
1119+
1120+
Write-Verbose "Searching $path\modules.json for module versions"
1121+
$modules = (Get-Content "$path\modules.json" -Raw) | ConvertFrom-Json
1122+
1123+
foreach ( $module in $modules ) {
1124+
Write-Verbose "`tTesting DownloadUrl $($module.DownloadUrl)"
1125+
if ( $module.DownloadUrl -notmatch "(\d+)\.(\d+)\.(\d+)([fpab])(\d+)" ) { continue; }
1126+
1127+
Write-Verbose "`tFound version!"
1128+
return [UnityVersion]$Matches[0]
1129+
}
1130+
}
1131+
1132+
if ( Test-Path "$path\Editor" -PathType Container ) {
1133+
# We'll attempt to search for the version using the ivy.xml definitions for legacy editor compatibility.
1134+
1135+
Write-Verbose "Looking for ivy.xml files under $path\Editor\"
1136+
$ivyFiles = Get-ChildItem -Path "$path\Editor\" -Filter 'ivy.xml' -Recurse -ErrorAction SilentlyContinue -Force -File
1137+
foreach ( $ivy in $ivyFiles) {
1138+
if ( $null -eq $ivy ) { continue; }
1139+
1140+
Write-Verbose "`tLooking for version in $($ivy.FullName)"
1141+
1142+
[xml]$xmlDoc = Get-Content $ivy.FullName
1143+
1144+
[string]$version = $xmlDoc.'ivy-module'.info.unityVersion
1145+
if ( -not $version ) { continue; }
1146+
1147+
Write-Verbose "`tFound version!"
1148+
return [UnityVersion]$version
1149+
}
1150+
}
11221151
}
11231152

11241153
<#

0 commit comments

Comments
 (0)