Skip to content

Commit 8046ebf

Browse files
authored
Merge pull request #171 from StephenHodgson/dev-UnitySetupInstance
Fixes 2019.1 Unity Instance Searching
2 parents 31008bc + 114e936 commit 8046ebf

File tree

1 file changed

+71
-25
lines changed

1 file changed

+71
-25
lines changed

UnitySetup/UnitySetup.psm1

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,11 @@ class UnitySetupInstance {
4949
[string]$Path
5050

5151
UnitySetupInstance([string]$path) {
52-
5352
$currentOS = Get-OperatingSystem
54-
$ivyPath = switch ($currentOS) {
55-
([OperatingSystem]::Windows) { 'Editor\Data\UnityExtensions\Unity\Networking\ivy.xml' }
56-
([OperatingSystem]::Linux) { throw "UnitySetupInstance has not been implemented on the Linux platform. Contributions welcomed!"; }
57-
([OperatingSystem]::Mac) { 'Unity.app/Contents/UnityExtensions/Unity/Networking/ivy.xml' }
58-
}
59-
60-
$ivyPath = [io.path]::Combine("$path", $ivyPath);
61-
if (!(Test-Path $ivyPath)) { throw "Path is not a Unity setup: $path"}
62-
[xml]$xmlDoc = Get-Content $ivyPath
63-
64-
if ( !($xmlDoc.'ivy-module'.info.unityVersion)) {
65-
throw "Unity setup ivy is missing version: $ivyPath"
66-
}
6753

6854
$this.Path = $path
69-
$this.Version = $xmlDoc.'ivy-module'.info.unityVersion
55+
$this.Version = Get-UnitySetupInstanceVersion -Path $path
56+
if ( -not $this.Version ) { throw "Unable to find version for $path" }
7057

7158
$playbackEnginePath = $null
7259
$componentTests = switch ($currentOS) {
@@ -897,8 +884,6 @@ function Install-UnitySetupInstance {
897884
$defaultInstallPath = $BasePath
898885
}
899886

900-
$unitySetupInstances = Get-UnitySetupInstance -BasePath $BasePath
901-
902887
$versionInstallers = @{}
903888
}
904889
process {
@@ -983,7 +968,7 @@ function Install-UnitySetupInstance {
983968

984969
$editorInstaller = $installerPaths | Where-Object { $_.ComponentType -band $editorComponent }
985970
if ($null -ne $editorInstaller) {
986-
Write-Verbose "Installing $($editorInstaller.ComponentType)"
971+
Write-Verbose "Installing $($editorInstaller.ComponentType) Editor"
987972
Install-UnitySetupPackage -Package $editorInstaller -Destination $packageDestination
988973
}
989974

@@ -1092,7 +1077,6 @@ function Get-UnitySetupInstance {
10921077
if (-not $BasePath) {
10931078
$BasePath = @('C:\Program Files*\Unity*', 'C:\Program Files\Unity\Hub\Editor\*')
10941079
}
1095-
$ivyPath = 'Editor\Data\UnityExtensions\Unity\Networking\ivy.xml'
10961080
}
10971081
([OperatingSystem]::Linux) {
10981082
throw "Get-UnitySetupInstance has not been implemented on the Linux platform. Contributions welcomed!";
@@ -1101,16 +1085,78 @@ function Get-UnitySetupInstance {
11011085
if (-not $BasePath) {
11021086
$BasePath = @('/Applications/Unity*', '/Applications/Unity/Hub/Editor/*')
11031087
}
1104-
$ivyPath = 'Unity.app/Contents/UnityExtensions/Unity/Networking/ivy.xml'
11051088
}
11061089
}
11071090

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

1111-
Get-ChildItem $path -Recurse -ErrorAction Ignore |
1112-
ForEach-Object {
1113-
[UnitySetupInstance]::new((Join-Path $_.Directory "..\..\..\..\..\" | Convert-Path))
1158+
Write-Verbose "`tFound version!"
1159+
return [UnityVersion]$version
11141160
}
11151161
}
11161162
}

0 commit comments

Comments
 (0)