@@ -29,6 +29,11 @@ enum OperatingSystem {
29
29
Mac
30
30
}
31
31
32
+ class UnitySetupResource {
33
+ [UnitySetupComponent ] $ComponentType
34
+ [string ] $Path
35
+ }
36
+
32
37
class UnitySetupInstaller {
33
38
[UnitySetupComponent ] $ComponentType
34
39
[UnityVersion ] $Version
@@ -523,9 +528,25 @@ function Request-UnitySetupInstaller {
523
528
($destinationItem.LastWriteTime -eq $_.LastModified ) ) {
524
529
Write-Verbose " Skipping download because it's already in the cache: $ ( $_.DownloadUrl ) "
525
530
526
- $downloads += , $destination
531
+ $resource = New-Object UnitySetupResource - Property @ {
532
+ ' ComponentType' = $_.ComponentType
533
+ ' Path' = $destination
534
+ }
535
+ $downloads += , $resource
527
536
return
528
537
}
538
+ elseif ($destinationItem.Length -eq $_.Length ) {
539
+ Write-Verbose " Skipping download because file size is the same: $ ( $_.DownloadUrl ) "
540
+
541
+ # Re-writes the last modified time for ensuring downloads cache.
542
+ $downloadedFile = Get-Item $destination
543
+ $downloadedFile.LastWriteTime = $_.LastModified
544
+
545
+ $downloadedFile = Get-Item $destination
546
+ if ( ($downloadedFile.LastWriteTime -ne $_.LastModified ) ) {
547
+ Write-Verbose " Modified time not set for $destination "
548
+ }
549
+ }
529
550
}
530
551
531
552
$destinationDirectory = [io.path ]::GetDirectoryName($destination )
@@ -536,13 +557,37 @@ function Request-UnitySetupInstaller {
536
557
try
537
558
{
538
559
Write-Verbose " Downloading $ ( $_.DownloadUrl ) to $destination "
539
- (New-Object System.Net.WebClient).DownloadFile($_.DownloadUrl , $destination )
560
+
561
+ $webClient = New-Object System.Net.WebClient
562
+ Register-ObjectEvent - InputObject $webClient - EventName DownloadFileCompleted `
563
+ - SourceIdentifier Web.DownloadFileCompleted - Action {
564
+ $global :isDownloaded = $true
565
+ }
566
+ Register-ObjectEvent - InputObject $webClient - EventName DownloadProgressChanged `
567
+ - SourceIdentifier Web.DownloadProgressChanged - Action {
568
+ $global :DownloadProgressEvent = $event
569
+ }
570
+ $webClient.DownloadFileAsync ($_.DownloadUrl , $destination )
571
+
572
+ $totalBytes = $_.Length
573
+ while (-not $isDownloaded ) {
574
+ $receivedBytes = $global :DownloadProgressEvent.SourceArgs.BytesReceived
575
+ $progress = [int ](($receivedBytes / [double ]$totalBytes ) * 100 )
576
+
577
+ Write-Progress - Activity " Downloading $ ( $_.DownloadUrl ) ..." - Status " $receivedBytes bytes \ $totalBytes bytes" - PercentComplete $progress
578
+ [System.Threading.Thread ]::Sleep(500 )
579
+ }
580
+ Write-Progress - Activity " Downloading $ ( $_.DownloadUrl ) ..." - Status " $receivedBytes bytes \ $totalBytes bytes" - Completed
540
581
541
582
# Re-writes the last modified time for ensuring downloads cache.
542
583
$downloadedFile = Get-Item $destination
543
584
$downloadedFile.LastWriteTime = $_.LastModified
544
585
545
- $downloads += , $destination
586
+ $resource = New-Object UnitySetupResource - Property @ {
587
+ ' ComponentType' = $_.ComponentType
588
+ ' Path' = $destination
589
+ }
590
+ $downloads += , $resource
546
591
}
547
592
catch [System.Net.WebException ]
548
593
{
@@ -555,6 +600,53 @@ function Request-UnitySetupInstaller {
555
600
}
556
601
}
557
602
603
+ function Install-UnitySetupPackage {
604
+ [CmdletBinding ()]
605
+ param (
606
+ [parameter (Mandatory = $true )]
607
+ [UnitySetupResource ] $Package ,
608
+
609
+ [parameter (Mandatory = $false )]
610
+ [string ]$Destination
611
+ )
612
+
613
+ $currentOS = Get-OperatingSystem
614
+ switch ($currentOS ) {
615
+ ([OperatingSystem ]::Windows) {
616
+ $startProcessArgs = @ {
617
+ ' FilePath' = $Package.Path ;
618
+ ' ArgumentList' = @ (" /S" , " /D=$Destination " );
619
+ ' PassThru' = $true ;
620
+ ' Wait' = $true ;
621
+ }
622
+ }
623
+ ([OperatingSystem ]::Linux) {
624
+ throw " Install-UnitySetupPackage has not been implemented on the Linux platform. Contributions welcomed!" ;
625
+ }
626
+ ([OperatingSystem ]::Mac) {
627
+ # Note, ignores $Destination on Mac.
628
+ # sudo installer -package $Package.Path -target /
629
+ $startProcessArgs = @ {
630
+ ' FilePath' = ' sudo' ;
631
+ ' ArgumentList' = @ (" installer" , " -package" , $Package.Path , " -target" , " /" );
632
+ ' PassThru' = $true ;
633
+ ' Wait' = $true ;
634
+ }
635
+ }
636
+ }
637
+
638
+ Write-Verbose " $ ( Get-Date ) : Installing $ ( $Package.ComponentType ) to $Destination ."
639
+ $process = Start-Process @startProcessArgs
640
+ if ( $process ) {
641
+ if ( $process.ExitCode -ne 0 ) {
642
+ Write-Error " $ ( Get-Date ) : Failed with exit code: $ ( $process.ExitCode ) "
643
+ }
644
+ else {
645
+ Write-Verbose " $ ( Get-Date ) : Succeeded."
646
+ }
647
+ }
648
+ }
649
+
558
650
<#
559
651
. Synopsis
560
652
Installs a UnitySetup instance.
@@ -637,8 +729,6 @@ function Install-UnitySetupInstance {
637
729
# TODO: Work in a `$host.ui.PromptForChoice` / -Force param for resolving this.
638
730
throw " Install-UnitySetupInstance has not yet handled working around the base install directory already existing. Please move this manually and try again. Contributions welcomed!" ;
639
731
}
640
-
641
-
642
732
}
643
733
644
734
if ( $PSBoundParameters.ContainsKey (' Destination' ) ) {
@@ -654,48 +744,30 @@ function Install-UnitySetupInstance {
654
744
$installPath = " $defaultInstallPath -$installVersion "
655
745
}
656
746
747
+ # TODO: Strip out components already installed in the destination.
748
+
657
749
$installerPaths = $installerInstances | Request-UnitySetupInstaller - Cache $Cache
658
750
659
- # TODO: Install Unity component first
660
-
661
- foreach ($componentInstallerPath in $installerPaths ) {
662
- switch ($currentOS ) {
663
- ([OperatingSystem ]::Windows) {
664
- $startProcessArgs = @ {
665
- ' FilePath' = $_ ;
666
- ' ArgumentList' = @ (" /S" , " /D=$installPath " );
667
- ' PassThru' = $true ;
668
- ' Wait' = $true ;
669
- }
670
- }
671
- ([OperatingSystem ]::Linux) {
672
- throw " Install-UnitySetupInstance has not been implemented on the Linux platform. Contributions welcomed!" ;
673
- }
674
- ([OperatingSystem ]::Mac) {
675
- $startProcessArgs = @ {
676
- ' FilePath' = $installer ;
677
- ' ArgumentList' = @ (" /S" , " /D=$destination " );
678
- ' PassThru' = $true ;
679
- ' Wait' = $true ;
680
- }
681
- }
682
- }
683
-
684
- Write-Verbose " $ ( Get-Date ) : Installing $installer to $destination ."
685
- $process = Start-Process @startProcessArgs
686
- if ( $process ) {
687
- if ( $process.ExitCode -ne 0 ) {
688
- Write-Error " $ ( Get-Date ) : Failed with exit code: $ ( $process.ExitCode ) "
689
- }
690
- else {
691
- Write-Verbose " $ ( Get-Date ) : Succeeded."
692
- }
693
- }
751
+ # First install the Unity editor before other components.
752
+ $editorComponent = switch ($currentOS ) {
753
+ ([OperatingSystem ]::Windows) { [UnitySetupComponent ]::Windows }
754
+ ([OperatingSystem ]::Linux) { [UnitySetupComponent ]::Linux }
755
+ ([OperatingSystem ]::Mac) { [UnitySetupComponent ]::Mac }
756
+ }
757
+ $editorInstaller = $installerPaths | Where-Object { $_.ComponentType -band $editorComponent }
758
+ if ($null -ne $editorInstaller ) {
759
+ Write-Verbose " Installing $ ( $editorInstaller.ComponentType ) "
760
+ # Install-UnitySetupPackage -Package $editorInstaller -Destination $destination
761
+ }
762
+
763
+ $installerPaths | ForEach-Object {
764
+ Write-Verbose " Installing $ ( $editorInstaller.ComponentType ) "
765
+ # Install-UnitySetupPackage -Package $_ -Destination $destination
694
766
}
695
767
696
768
# Move the install from the staging area to the desired destination
697
769
if ($currentOS == [OperatingSystem ]::Mac) {
698
- Move-Item - Path / Applications/ Unity/ - Destination $installPath
770
+ # Move-Item -Path /Applications/Unity/ -Destination $installPath
699
771
}
700
772
}
701
773
}
@@ -775,7 +847,7 @@ function Get-UnitySetupInstance {
775
847
}
776
848
([OperatingSystem ]::Mac) {
777
849
if (-not $BasePath ) {
778
- $BasePath = @ (' /Applications/Unity*' )
850
+ $BasePath = @ (' /Applications/Unity*' , ' /Applications/Unity/Hub/Editor/* ' )
779
851
}
780
852
$ivyPath = ' Unity.app/Contents/UnityExtensions/Unity/Networking/ivy.xml'
781
853
}
0 commit comments