2023-03-20 01:41:43 +08:00
# This file is part of MagiskOnWSALocal.
#
# MagiskOnWSALocal is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# MagiskOnWSALocal is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with MagiskOnWSALocal. If not, see <https://www.gnu.org/licenses/>.
#
# Copyright (C) 2023 LSPosed Contributors
#
2023-03-20 01:23:33 +08:00
$Host . UI . RawUI . WindowTitle = " Installing MagiskOnWSA.... "
2023-01-11 13:54:39 +01:00
function Test-Administrator {
[ OutputType ( [ bool ] ) ]
param ( )
process {
[ Security.Principal.WindowsPrincipal ] $user = [ Security.Principal.WindowsIdentity ] :: GetCurrent ( ) ;
return $user . IsInRole ( [ Security.Principal.WindowsBuiltinRole ] :: Administrator ) ;
}
}
function Get-InstalledDependencyVersion {
param (
[ string ] $Name ,
[ string ] $ProcessorArchitecture
)
process {
return Get-AppxPackage -Name $Name | ForEach-Object { if ( $_ . Architecture -eq $ProcessorArchitecture ) { $_ } } | Sort-Object -Property Version | Select-Object -ExpandProperty Version -Last 1 ;
}
}
2023-03-20 01:41:43 +08:00
Function Test-CommandExists {
Param ( $command )
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try { if ( Get-Command $command ) { RETURN $true } }
2023-03-26 03:02:05 +08:00
Catch { RETURN $false }
2023-03-20 01:41:43 +08:00
Finally { $ErrorActionPreference = $oldPreference }
} #end function test-CommandExists
2023-01-11 13:54:39 +01:00
function Finish {
2023-03-20 01:41:43 +08:00
Write-Host " Optimizing VHDX size.... "
If ( Test-CommandExists Optimize-VHD ) { Optimize-VHD " .\*.vhdx " -Mode Full }
2023-01-11 13:54:39 +01:00
Clear-Host
Start-Process " wsa://com.topjohnwu.magisk "
Start-Process " wsa://com.android.vending "
}
If ( -Not ( Test-Administrator ) ) {
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force
$proc = Start-Process -PassThru -WindowStyle Hidden -Verb RunAs ConHost . exe -Args " powershell -ExecutionPolicy Bypass -Command Set-Location ' $PSScriptRoot '; &' $PSCommandPath ' EVAL "
$proc . WaitForExit ( )
If ( $proc . ExitCode -Ne 0 ) {
Clear-Host
Write-Warning " Failed to launch start as Administrator `r `n Press any key to exit "
$null = $Host . UI . RawUI . ReadKey ( 'NoEcho,IncludeKeyDown' ) ;
}
exit
}
ElseIf ( ( $args . Count -Eq 1 ) -And ( $args [ 0 ] -Eq " EVAL " ) ) {
Start-Process ConHost . exe -Args " powershell -ExecutionPolicy Bypass -Command Set-Location ' $PSScriptRoot '; &' $PSCommandPath ' "
exit
}
$FileList = Get-Content -Path . \ filelist . txt
If ( ( ( Test-Path -Path $FileList ) -Eq $false ) . Count ) {
Write-Error " Some files are missing in the folder. Please try to build again. Press any key to exist "
$null = $Host . UI . RawUI . ReadKey ( 'NoEcho,IncludeKeyDown' )
exit 1
}
2023-03-18 17:49:49 +08:00
If ( ( Test-Path -Path " MakePri.ps1 " ) -Eq $true ) {
2023-03-20 01:23:33 +08:00
$ProcMakePri = Start-Process powershell . exe -PassThru -Args " -ExecutionPolicy Bypass -File MakePri.ps1 " -WorkingDirectory $PSScriptRoot
$ProcMakePri . WaitForExit ( )
If ( $ProcMakePri . ExitCode -Ne 0 ) {
Write-Warning " Failed to merge resources, WSA Seetings will always be in English `r `n "
}
2023-03-18 17:49:49 +08:00
}
2023-01-11 13:54:39 +01:00
reg add " HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock " / t REG_DWORD / f / v " AllowDevelopmentWithoutDevLicense " / d " 1 "
If ( $ ( Get-WindowsOptionalFeature -Online -FeatureName 'VirtualMachinePlatform' ) . State -Ne " Enabled " ) {
Enable-WindowsOptionalFeature -Online -NoRestart -FeatureName 'VirtualMachinePlatform'
Clear-Host
Write-Warning " Need restart to enable virtual machine platform `r `n Press y to restart or press any key to exit "
$key = $Host . UI . RawUI . ReadKey ( 'NoEcho,IncludeKeyDown' )
If ( " y " -Eq $key . Character ) {
Restart-Computer -Confirm
}
Else {
exit 1
}
}
[ xml ] $Xml = Get-Content " .\AppxManifest.xml " ;
$Name = $Xml . Package . Identity . Name ;
2023-04-11 00:43:27 +08:00
Write-Host " Installing $Name version: $( $Xml . Package . Identity . Version ) "
2023-01-11 13:54:39 +01:00
$ProcessorArchitecture = $Xml . Package . Identity . ProcessorArchitecture ;
$Dependencies = $Xml . Package . Dependencies . PackageDependency ;
$Dependencies | ForEach-Object {
2023-04-11 00:43:27 +08:00
$InstalledVersion = Get-InstalledDependencyVersion -Name $_ . Name -ProcessorArchitecture $ProcessorArchitecture ;
If ( $InstalledVersion -Lt $_ . MinVersion ) {
Write-Host " Dependency package $( $_ . Name ) $ProcessorArchitecture required minimum version: $( $_ . MinVersion ) . Installing.... "
Add-AppxPackage -ForceApplicationShutdown -ForceUpdateFromAnyVersion -Path " $( $_ . Name ) _ $ProcessorArchitecture .appx "
2023-01-11 13:54:39 +01:00
}
2023-04-11 00:43:27 +08:00
Else {
Write-Host " Dependency package $( $_ . Name ) $ProcessorArchitecture current version: $InstalledVersion . Nothing to do. "
2023-01-11 13:54:39 +01:00
}
}
$Installed = $null
$Installed = Get-AppxPackage -Name $Name
If ( ( $null -Ne $Installed ) -And ( -Not ( $Installed . IsDevelopmentMode ) ) ) {
Clear-Host
Write-Warning " There is already one installed WSA. Please uninstall it first. `r `n Press y to uninstall existing WSA or press any key to exit "
$key = $Host . UI . RawUI . ReadKey ( 'NoEcho,IncludeKeyDown' )
If ( " y " -Eq $key . Character ) {
2023-03-27 17:53:24 +08:00
Clear-Host
2023-01-11 13:54:39 +01:00
Remove-AppxPackage -Package $Installed . PackageFullName
}
Else {
exit 1
}
}
Clear-Host
2023-03-20 01:23:33 +08:00
Write-Host " Installing MagiskOnWSA.... "
2023-03-20 02:38:16 +08:00
If ( Test-CommandExists WsaClient ) { Start-Process WsaClient -Wait -Args " /shutdown " }
2023-01-11 13:54:39 +01:00
Stop-Process -Name " WsaClient " -ErrorAction SilentlyContinue
Add-AppxPackage -ForceApplicationShutdown -ForceUpdateFromAnyVersion -Register . \ AppxManifest . xml
If ( $ ? ) {
Finish
}
ElseIf ( $null -Ne $Installed ) {
Clear-Host
Write-Error " Failed to update. `r `n Press any key to uninstall existing installation while preserving user data. `r `n Take in mind that this will remove the Android apps' icon from the start menu. `r `n If you want to cancel, close this window now. "
$null = $Host . UI . RawUI . ReadKey ( 'NoEcho,IncludeKeyDown' )
2023-03-27 17:53:24 +08:00
Clear-Host
2023-01-11 13:54:39 +01:00
Remove-AppxPackage -PreserveApplicationData -Package $Installed . PackageFullName
Add-AppxPackage -ForceApplicationShutdown -ForceUpdateFromAnyVersion -Register . \ AppxManifest . xml
If ( $ ? ) {
Finish
}
}
Write-Host " All Done! `r `n Press any key to exit "
$null = $Host . UI . RawUI . ReadKey ( 'NoEcho,IncludeKeyDown' )