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/>.
#
2024-02-08 18:28:45 +08:00
# Copyright (C) 2024 LSPosed Contributors
2023-03-20 01:41:43 +08:00
#
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
)
2023-05-16 08:44:53 +08:00
PROCESS {
If ( $null -Ne $ProcessorArchitecture ) {
return Get-AppxPackage -Name $Name | ForEach-Object { if ( $_ . Architecture -Eq $ProcessorArchitecture ) { $_ } } | Sort-Object -Property Version | Select-Object -ExpandProperty Version -Last 1 ;
}
2023-01-11 13:54:39 +01:00
}
}
2023-05-16 08:44:53 +08:00
Function Test-CommandExist {
Param ( $Command )
$OldPreference = $ErrorActionPreference
2023-03-20 01:41:43 +08:00
$ErrorActionPreference = 'stop'
2023-05-16 08:44:53 +08:00
try { if ( Get-Command $Command ) { RETURN $true } }
2023-03-26 03:02:05 +08:00
Catch { RETURN $false }
2023-05-16 08:44:53 +08:00
Finally { $ErrorActionPreference = $OldPreference }
} #end function Test-CommandExist
2023-03-20 01:41:43 +08:00
2023-06-03 01:19:51 +08:00
Function Finish {
2023-01-11 13:54:39 +01:00
Clear-Host
Start-Process " wsa://com.topjohnwu.magisk "
Start-Process " wsa://com.android.vending "
}
2023-05-17 21:56:39 +08:00
If ( Test-CommandExist pwsh . exe ) {
$pwsh = " pwsh.exe "
}
Else {
$pwsh = " powershell.exe "
}
2023-01-11 13:54:39 +01:00
If ( -Not ( Test-Administrator ) ) {
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force
2023-05-17 21:56:39 +08:00
$Proc = Start-Process -PassThru -Verb RunAs $pwsh -Args " -ExecutionPolicy Bypass -Command Set-Location ' $PSScriptRoot '; &' $PSCommandPath ' EVAL "
If ( $null -Ne $Proc ) {
$Proc . WaitForExit ( )
}
If ( $null -Eq $Proc -Or $Proc . ExitCode -Ne 0 ) {
2023-01-11 13:54:39 +01:00
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 " ) ) {
2023-05-17 21:56:39 +08:00
Start-Process $pwsh -NoNewWindow -Args " -ExecutionPolicy Bypass -Command Set-Location ' $PSScriptRoot '; &' $PSCommandPath ' "
2023-01-11 13:54:39 +01:00
exit
}
$FileList = Get-Content -Path . \ filelist . txt
If ( ( ( Test-Path -Path $FileList ) -Eq $false ) . Count ) {
2023-05-17 21:56:39 +08:00
Write-Error " Some files are missing in the folder. Please try to build again. Press any key to exit "
2023-01-11 13:54:39 +01:00
$null = $Host . UI . RawUI . ReadKey ( 'NoEcho,IncludeKeyDown' )
exit 1
}
2023-05-17 21:56:39 +08:00
If ( ( ( Test-Path -Path " MakePri.ps1 " ) -And ( Test-Path -Path " makepri.exe " ) ) -Eq $true ) {
$ProcMakePri = Start-Process $pwsh -PassThru -NoNewWindow -Args " -ExecutionPolicy Bypass -File MakePri.ps1 " -WorkingDirectory $PSScriptRoot
$null = $ProcMakePri . Handle
2023-03-20 01:23:33 +08:00
$ProcMakePri . WaitForExit ( )
If ( $ProcMakePri . ExitCode -Ne 0 ) {
2023-05-17 21:56:39 +08:00
Write-Warning " Failed to merge resources, WSA Seetings will always be in English `r `n Press any key to continue "
$null = $Host . UI . RawUI . ReadKey ( 'NoEcho,IncludeKeyDown' )
2023-03-20 01:23:33 +08:00
}
2023-05-17 21:56:39 +08:00
$Host . UI . RawUI . WindowTitle = " Installing MagiskOnWSA.... "
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 "
2023-05-17 21:56:39 +08:00
# When using PowerShell which is installed with MSIX
# Get-WindowsOptionalFeature and Enable-WindowsOptionalFeature will fail
# See https://github.com/PowerShell/PowerShell/issues/13866
if ( $PSHOME . contains ( " 8wekyb3d8bbwe " ) ) {
Import-Module DISM -UseWindowsPowerShell
}
2023-01-11 13:54:39 +01:00
If ( $ ( Get-WindowsOptionalFeature -Online -FeatureName 'VirtualMachinePlatform' ) . State -Ne " Enabled " ) {
Enable-WindowsOptionalFeature -Online -NoRestart -FeatureName 'VirtualMachinePlatform'
Write-Warning " Need restart to enable virtual machine platform `r `n Press y to restart or press any key to exit "
2023-05-16 08:44:53 +08:00
$Key = $Host . UI . RawUI . ReadKey ( 'NoEcho,IncludeKeyDown' )
If ( " y " -Eq $Key . Character ) {
2023-01-11 13:54:39 +01:00
Restart-Computer -Confirm
}
Else {
exit 1
}
}
[ xml ] $Xml = Get-Content " .\AppxManifest.xml " ;
$Name = $Xml . Package . Identity . Name ;
2023-05-16 08:44:53 +08:00
Write-Output " 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 ) {
2023-05-17 21:56:39 +08:00
If ( $env:WT_SESSION ) {
$env:WT_SESSION = $null
Write-Output " Dependency should be installed but Windows Terminal is in use. Restarting to conhost.exe "
Start-Process conhost . exe -Args " powershell.exe -ExecutionPolicy Bypass -Command Set-Location ' $PSScriptRoot '; &' $PSCommandPath ' "
exit 1
}
2023-05-16 08:44:53 +08:00
Write-Output " Dependency package $( $_ . Name ) $ProcessorArchitecture required minimum version: $( $_ . MinVersion ) . Installing.... "
2023-04-11 00:43:27 +08:00
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 {
2023-05-16 08:44:53 +08:00
Write-Output " 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 ) ) ) {
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
}
}
2023-05-17 21:56:39 +08:00
If ( Test-CommandExist WsaClient ) {
Write-Output " Shutting down WSA.... "
Start-Process WsaClient -Wait -Args " /shutdown "
}
2023-01-11 13:54:39 +01:00
Stop-Process -Name " WsaClient " -ErrorAction SilentlyContinue
2023-05-17 21:56:39 +08:00
Write-Output " Installing MagiskOnWSA.... "
2023-01-11 13:54:39 +01:00
Add-AppxPackage -ForceApplicationShutdown -ForceUpdateFromAnyVersion -Register . \ AppxManifest . xml
If ( $ ? ) {
Finish
}
ElseIf ( $null -Ne $Installed ) {
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
}
}
2023-05-16 08:44:53 +08:00
Write-Output " All Done! `r `n Press any key to exit "
2023-01-11 13:54:39 +01:00
$null = $Host . UI . RawUI . ReadKey ( 'NoEcho,IncludeKeyDown' )