Skip to content

Commit 09f593f

Browse files
Merge branch 'develop' into dev-lumin-component-installer
2 parents db1e904 + 8046ebf commit 09f593f

File tree

1 file changed

+112
-53
lines changed

1 file changed

+112
-53
lines changed

UnitySetup/UnitySetup.psm1

Lines changed: 112 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,11 @@ class UnitySetupInstance {
5050
[string]$Path
5151

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

6955
$this.Path = $path
70-
$this.Version = $xmlDoc.'ivy-module'.info.unityVersion
56+
$this.Version = Get-UnitySetupInstanceVersion -Path $path
57+
if ( -not $this.Version ) { throw "Unable to find version for $path" }
7158

7259
$playbackEnginePath = $null
7360
$componentTests = switch ($currentOS) {
@@ -164,7 +151,7 @@ class UnityVersion : System.IComparable {
164151
UnityVersion([string] $version) {
165152
$parts = $version.Split('-')
166153

167-
$parts[0] -match "(\d+)\.(\d+)\.(\d+)([fpb])(\d+)" | Out-Null
154+
$parts[0] -match "(\d+)\.(\d+)\.(\d+)([fpba])(\d+)" | Out-Null
168155
if ( $Matches.Count -ne 6 ) { throw "Invalid unity version: $version" }
169156
$this.Major = [int]($Matches[1]);
170157
$this.Minor = [int]($Matches[2]);
@@ -292,20 +279,20 @@ function Find-UnitySetupInstaller {
292279
$currentOS = Get-OperatingSystem
293280
switch ($currentOS) {
294281
([OperatingSystem]::Windows) {
295-
$unitySetupRegEx = "^(.+)\/([a-z0-9]+)\/Windows64EditorInstaller\/UnitySetup64-(\d+)\.(\d+)\.(\d+)([fpb])(\d+).exe$"
296282
$targetSupport = "TargetSupportInstaller"
297283
$installerExtension = "exe"
298284
}
299285
([OperatingSystem]::Linux) {
300286
throw "Find-UnitySetupInstaller has not been implemented on the Linux platform. Contributions welcomed!";
301287
}
302288
([OperatingSystem]::Mac) {
303-
$unitySetupRegEx = "^(.+)\/([a-z0-9]+)\/MacEditorInstaller\/Unity-(\d+)\.(\d+)\.(\d+)([fpb])(\d+).pkg$"
304289
$targetSupport = "MacEditorTargetInstaller"
305290
$installerExtension = "pkg"
306291
}
307292
}
308293

294+
$unitySetupRegEx = "^(.+)\/([a-z0-9]+)\/(.+)\/(.+)-(\d+)\.(\d+)\.(\d+)([fpba])(\d+).$installerExtension$"
295+
309296
$knownBaseUrls = @(
310297
"https://download.unity3d.com/download_unity",
311298
"https://netstorage.unity3d.com/unity",
@@ -314,7 +301,8 @@ function Find-UnitySetupInstaller {
314301

315302
$installerTemplates = @{
316303
[UnitySetupComponent]::UWP = "$targetSupport/UnitySetup-UWP-.NET-Support-for-Editor-$Version.$installerExtension",
317-
"$targetSupport/UnitySetup-Metro-Support-for-Editor-$Version.$installerExtension";
304+
"$targetSupport/UnitySetup-Metro-Support-for-Editor-$Version.$installerExtension",
305+
"$targetSupport/UnitySetup-Universal-Windows-Platform-Support-for-Editor-$Version.$installerExtension";
318306
[UnitySetupComponent]::UWP_IL2CPP = , "$targetSupport/UnitySetup-UWP-IL2CPP-Support-for-Editor-$Version.$installerExtension";
319307
[UnitySetupComponent]::Android = , "$targetSupport/UnitySetup-Android-Support-for-Editor-$Version.$installerExtension";
320308
[UnitySetupComponent]::iOS = , "$targetSupport/UnitySetup-iOS-Support-for-Editor-$Version.$installerExtension";
@@ -365,8 +353,23 @@ function Find-UnitySetupInstaller {
365353
# Every release type has a different pattern for finding installers
366354
$searchPages = @()
367355
switch ($Version.Release) {
368-
'f' { $searchPages += "https://unity3d.com/get-unity/download/archive" }
369-
'b' { $searchPages += "https://unity3d.com/unity/beta/unity$Version" }
356+
'a' { $searchPages += "https://unity3d.com/alpha/$($Version.Major).$($Version.Minor)" }
357+
'b' {
358+
$searchPages += "https://unity3d.com/unity/beta/unity$Version",
359+
"https://unity3d.com/unity/beta/$($Version.Major).$($Version.Minor)",
360+
"https://unity3d.com/unity/beta/$Version"
361+
}
362+
'f' {
363+
$searchPages += "https://unity3d.com/get-unity/download/archive",
364+
"https://unity3d.com/unity/whats-new/$($Version.Major).$($Version.Minor).$($Version.Revision)"
365+
366+
# Just in case it's a release candidate search the beta as well.
367+
if ($Version.Revision -eq '0') {
368+
$searchPages += "https://unity3d.com/unity/beta/unity$Version",
369+
"https://unity3d.com/unity/beta/$($Version.Major).$($Version.Minor)",
370+
"https://unity3d.com/unity/beta/$Version"
371+
}
372+
}
370373
'p' {
371374
$patchPage = "https://unity3d.com/unity/qa/patch-releases?version=$($Version.Major).$($Version.Minor)"
372375
$searchPages += $patchPage
@@ -379,35 +382,32 @@ function Find-UnitySetupInstaller {
379382
}
380383

381384
foreach ($page in $searchPages) {
382-
$webResult = Invoke-WebRequest $page -UseBasicParsing
383-
$prototypeLink = $webResult.Links | Select-Object -ExpandProperty href -ErrorAction SilentlyContinue | Where-Object {
384-
$_ -match "$($installerTemplates[$setupComponent])$"
385-
}
386-
387-
if ($null -ne $prototypeLink) { break }
388-
}
389-
390-
if ($null -eq $prototypeLink) {
391-
# Attempt to find Unity version and setup links based off builtin_shaders download.
392-
Write-Verbose "Attempting version search with builtin_shaders fallback"
393-
foreach ($page in $searchPages) {
385+
try {
394386
$webResult = Invoke-WebRequest $page -UseBasicParsing
395387
$prototypeLink = $webResult.Links | Select-Object -ExpandProperty href -ErrorAction SilentlyContinue | Where-Object {
396-
$_ -match "builtin_shaders-$($Version).zip$"
397-
}
388+
$link = $_
398389

399-
if ($null -ne $prototypeLink) { break }
400-
}
390+
foreach ( $installer in $installerTemplates.Keys ) {
391+
foreach ( $template in $installerTemplates[$installer] ) {
392+
if ( $link -like "*$template*" ) { return $true }
393+
}
394+
}
395+
396+
return $false
397+
398+
} | Select-Object -First 1
401399

402-
if ($null -eq $prototypeLink) {
403-
throw "Could not find archives for Unity version $Version"
400+
if ($null -ne $prototypeLink) { break }
404401
}
405-
else {
406-
# Regex needs to be reconfigured to parse builtin_shaders's url link
407-
$unitySetupRegEx = "^(.+)\/([a-z0-9]+)\/builtin_shaders-(\d+)\.(\d+)\.(\d+)([fpb])(\d+).zip$"
402+
catch {
403+
Write-Verbose "$page failed: $($_.Exception.Message)"
408404
}
409405
}
410406

407+
if ($null -eq $prototypeLink) {
408+
throw "Could not find archives for Unity version $Version"
409+
}
410+
411411
$linkComponents = $prototypeLink -split $unitySetupRegEx -ne ""
412412

413413
if ($knownBaseUrls -notcontains $linkComponents[0]) {
@@ -449,7 +449,7 @@ function Find-UnitySetupInstaller {
449449
break
450450
}
451451
catch {
452-
Write-Verbose "$endpoint failed: $_"
452+
Write-Verbose "$endpoint failed: $($_.Exception.Message)"
453453
}
454454
}
455455

@@ -887,8 +887,6 @@ function Install-UnitySetupInstance {
887887
$defaultInstallPath = $BasePath
888888
}
889889

890-
$unitySetupInstances = Get-UnitySetupInstance -BasePath $BasePath
891-
892890
$versionInstallers = @{}
893891
}
894892
process {
@@ -973,7 +971,7 @@ function Install-UnitySetupInstance {
973971

974972
$editorInstaller = $installerPaths | Where-Object { $_.ComponentType -band $editorComponent }
975973
if ($null -ne $editorInstaller) {
976-
Write-Verbose "Installing $($editorInstaller.ComponentType)"
974+
Write-Verbose "Installing $($editorInstaller.ComponentType) Editor"
977975
Install-UnitySetupPackage -Package $editorInstaller -Destination $packageDestination
978976
}
979977

@@ -1082,7 +1080,6 @@ function Get-UnitySetupInstance {
10821080
if (-not $BasePath) {
10831081
$BasePath = @('C:\Program Files*\Unity*', 'C:\Program Files\Unity\Hub\Editor\*')
10841082
}
1085-
$ivyPath = 'Editor\Data\UnityExtensions\Unity\Networking\ivy.xml'
10861083
}
10871084
([OperatingSystem]::Linux) {
10881085
throw "Get-UnitySetupInstance has not been implemented on the Linux platform. Contributions welcomed!";
@@ -1091,16 +1088,78 @@ function Get-UnitySetupInstance {
10911088
if (-not $BasePath) {
10921089
$BasePath = @('/Applications/Unity*', '/Applications/Unity/Hub/Editor/*')
10931090
}
1094-
$ivyPath = 'Unity.app/Contents/UnityExtensions/Unity/Networking/ivy.xml'
10951091
}
10961092
}
10971093

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

1101-
Get-ChildItem $path -Recurse -ErrorAction Ignore |
1102-
ForEach-Object {
1103-
[UnitySetupInstance]::new((Join-Path $_.Directory "..\..\..\..\..\" | Convert-Path))
1161+
Write-Verbose "`tFound version!"
1162+
return [UnityVersion]$version
11041163
}
11051164
}
11061165
}

0 commit comments

Comments
 (0)