& { Set-StrictMode -Version 2.0 $ErrorActionPreference = 'Stop' $script:ApexWsl = $null $script:ApexRerun = 'irm https://apexclient.org/wininstall.ps1 | iex' function Write-ApexStep([string]$Text) { Write-Host '' Write-Host $Text -ForegroundColor Cyan } function Write-ApexNote([string]$Text) { Write-Host (' ' + $Text) } function Read-ApexConsent([string]$Question) { while ($true) { $answer = ([string](Read-Host ($Question + ' [Y/n]'))).Trim() if ($answer -eq '' -or $answer -match '^(?i:y|yes)$') { return $true } if ($answer -match '^(?i:n|no)$') { return $false } Write-Host 'Type Y and press Enter to continue, or N to stop.' } } function Stop-ApexForRestart { Write-Host '' Write-Host 'Windows needs to restart to finish setting up WSL.' -ForegroundColor Yellow Write-Host 'Restart your PC, open PowerShell again and run the same command:' Write-Host (' ' + $script:ApexRerun) -ForegroundColor Yellow Write-Host 'The installer continues where it stopped.' } function ConvertTo-ApexText($Items) { $lines = foreach ($item in @($Items)) { if ($null -eq $item) { continue } if ($item -is [Management.Automation.ErrorRecord]) { [string]$item.Exception.Message } else { [string]$item } } return ((@($lines) -join "`n").Replace([string][char]0, '').Replace("`r", '')).Trim() } function Invoke-ApexWslQuery([string[]]$Arguments) { $previous = $ErrorActionPreference $ErrorActionPreference = 'Continue' $output = @() $errors = @() $code = -1 try { $all = @(& $script:ApexWsl @Arguments 2>&1) $code = $LASTEXITCODE $output = @($all | Where-Object { $_ -isnot [Management.Automation.ErrorRecord] }) $errors = @($all | Where-Object { $_ -is [Management.Automation.ErrorRecord] }) } catch { $errors = @($_) } finally { $ErrorActionPreference = $previous } $text = ConvertTo-ApexText $output $value = '' foreach ($line in ($text -split "`n")) { if ($line.Trim()) { $value = $line.Trim(); break } } return [pscustomobject]@{ Code=$code; Text=$text; Value=$value; Messages=(ConvertTo-ApexText $errors) } } function Invoke-ApexWslInteractive([string[]]$Arguments) { $process = [Diagnostics.Process]::new() try { $process.StartInfo.FileName = $script:ApexWsl $process.StartInfo.Arguments = ($Arguments | ForEach-Object { if ($_ -match '^(--?[A-Za-z][A-Za-z-]*|~)$') { $_ } else { Quote-ApexWindowsArgument $_ } }) -join ' ' $process.StartInfo.UseShellExecute = $false [void]$process.Start() $process.WaitForExit() return $process.ExitCode } catch { Write-ApexNote ('Could not run WSL: ' + $_.Exception.Message) return -1 } finally { $process.Dispose() } } function Get-ApexWslProblem([string]$Text) { if ($Text -match '(?i)0x80370102|virtuali[sz]ation|BIOS') { return 'firmware' } if ($Text -match '(?i)0x8004032d|0x80370114|HCS_E_HYPERV_NOT_INSTALLED|Virtual Machine Platform') { return 'component' } if ($Text -match '(?i)reboot|restart') { return 'restart' } return '' } function Test-ApexRestartPending { return (Test-Path -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending') } function Test-ApexWslInstalled { if (-not (Test-Path -LiteralPath $script:ApexWsl)) { return $false } return (Invoke-ApexWslQuery @('--version')).Code -eq 0 } function Invoke-ApexWslSetup { Write-ApexNote 'Windows will ask for administrator permission. A restart may be needed afterwards.' if (-not (Read-ApexConsent 'Set up WSL now?')) { Write-Host 'Installation stopped. Nothing was changed.' return 'stopped' } try { $setup = Start-Process -FilePath $script:ApexWsl -ArgumentList @('--install', '--no-distribution') -Verb RunAs -Wait -PassThru } catch { Write-Host 'WSL setup did not start because administrator permission was not given. Nothing was changed.' return 'stopped' } if ($setup.ExitCode -in @(3010, 1641) -or (Test-ApexRestartPending)) { Stop-ApexForRestart return 'restart' } if (-not (Test-ApexWslInstalled)) { if ($setup.ExitCode -eq 0) { Stop-ApexForRestart return 'restart' } throw 'Windows could not finish setting up WSL. Install the latest updates in Settings > Windows Update, restart, then run this installer again.' } return 'ready' } function Quote-ApexWindowsArgument([string]$Value) { if ($Value.IndexOf([char]0) -ge 0 -or $Value.Contains("`n") -or $Value.Contains("`r")) { throw 'A shortcut argument contains an invalid character.' } return '"' + [regex]::Replace([regex]::Replace($Value, '(\\*)"', '$1$1\"'), '(\\+)$', '$1$1') + '"' } function Get-ApexUbuntuRegistrations { $key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss' if (-not (Test-Path -LiteralPath $key)) { return } foreach ($item in @(Get-ChildItem -LiteralPath $key -ErrorAction SilentlyContinue)) { try { $entry = Get-ItemProperty -LiteralPath $item.PSPath -Name DistributionName, Version if ($entry.DistributionName -match '^Ubuntu(?:-\d+(?:\.\d+)?)?$') { [pscustomobject]@{ Name=$entry.DistributionName; Version=[int]$entry.Version } } } catch { } } } function Find-ApexUbuntu { $listed = Invoke-ApexWslQuery @('--list', '--quiet') $names = @() if ($listed.Code -eq 0) { $names += @($listed.Text -split "`n") } $names += @(Get-ApexUbuntuRegistrations | ForEach-Object { $_.Name }) $ubuntu = @($names | ForEach-Object { ([string]$_).Trim() } | Where-Object { $_ -match '^Ubuntu(?:-\d+(?:\.\d+)?)?$' } | Sort-Object -Unique) if ($ubuntu -contains 'Ubuntu') { return 'Ubuntu' } if ($ubuntu.Count -gt 0) { return @($ubuntu | Sort-Object -Descending)[0] } return $null } function Get-ApexFolder([string]$Kind) { return [Environment]::GetFolderPath($Kind) } function Get-ApexGuiLauncher([string]$SystemDirectory) { $candidates = @() foreach ($root in @($env:ProgramW6432, $env:ProgramFiles)) { if ($root) { $path = Join-Path (Join-Path $root 'WSL') 'wslg.exe' $candidates += [pscustomobject]@{ Check=$path; Target=$path } } } $candidates += [pscustomobject]@{ Check=(Join-Path $SystemDirectory 'wslg.exe'); Target=(Join-Path (Join-Path $env:WINDIR 'System32') 'wslg.exe') } foreach ($candidate in $candidates) { if (Test-Path -LiteralPath $candidate.Check -PathType Leaf) { return $candidate.Target } } return $null } function Get-ApexDataFolder { $local = Get-ApexFolder 'LocalApplicationData' if (-not $local) { return $null } $directory = Join-Path $local 'ApexClient' if ((Test-Path -LiteralPath $directory) -and ((Get-Item -LiteralPath $directory).Attributes -band [IO.FileAttributes]::ReparsePoint)) { return $null } New-Item -ItemType Directory -Path $directory -Force | Out-Null return $directory } function Get-ApexWindowsIcon([string]$Base) { $temporaryIcon = $null try { $directory = Get-ApexDataFolder if (-not $directory) { return $null } $destination = Join-Path $directory 'apex.ico' if ((Test-Path -LiteralPath $destination) -and ((Get-Item -LiteralPath $destination).Attributes -band [IO.FileAttributes]::ReparsePoint)) { return $null } $temporaryIcon = Join-Path $directory ([IO.Path]::GetRandomFileName()) Invoke-WebRequest -UseBasicParsing -Uri ($Base + '/_shared/apex.ico') -MaximumRedirection 0 -TimeoutSec 30 -OutFile $temporaryIcon $size = (Get-Item -LiteralPath $temporaryIcon).Length if ($size -lt 4 -or $size -gt 1048576) { return $null } $header = [IO.File]::ReadAllBytes($temporaryIcon) if ($header[0] -ne 0 -or $header[1] -ne 0 -or $header[2] -ne 1 -or $header[3] -ne 0) { return $null } Move-Item -LiteralPath $temporaryIcon -Destination $destination -Force return $destination } catch { Write-ApexNote 'The Apex icon could not be downloaded. The shortcut uses the normal icon.' return $null } finally { if ($temporaryIcon -and (Test-Path -LiteralPath $temporaryIcon)) { Remove-Item -LiteralPath $temporaryIcon -Force -ErrorAction SilentlyContinue } } } function Get-ApexShortcutArguments([string]$Distribution, [string]$Program, [string]$Theme) { $gtk = 'Adwaita' if ($Theme -eq 'dark') { $gtk = 'Adwaita:dark' } return '--distribution ' + (Quote-ApexWindowsArgument $Distribution) + ' --cd ~ --exec /usr/bin/env SDL_VIDEO_WAYLAND_PREFER_LIBDECOR=1 GTK_THEME=' + $gtk + ' ' + (Quote-ApexWindowsArgument $Program) } function Write-ApexShortcut([string]$Destination, [string]$Launcher, [string]$Arguments, [string]$IconPath = '', [int]$WindowStyle = 1) { $directory = Split-Path -Parent $Destination if (-not (Test-Path -LiteralPath $directory -PathType Container)) { throw 'The shortcut folder is unavailable.' } $shell = New-Object -ComObject WScript.Shell $shortcut = $shell.CreateShortcut($Destination) $shortcut.TargetPath = $Launcher $shortcut.Arguments = $Arguments if ($IconPath) { $shortcut.IconLocation = $IconPath } $shortcut.Description = 'Apex' $shortcut.WindowStyle = $WindowStyle $shortcut.Save() } function Find-ApexInstalledProgram([string]$Distribution) { $check = 'set -f; IFS=; n=; for p in $HOME/.local/bin/apex $HOME/.apex-bin/apex; do if [ -f $p ] && [ ! -L $p ] && [ -x $p ] && [ -O $p ]; then if [ -z $n ] || [ $p -nt $n ]; then n=$p; fi; fi; done; [ -z $n ] && exit 1; printf %s\\n $n' $found = Invoke-ApexWslQuery @('--distribution', $Distribution, '--cd', '~', '--exec', 'sh', '-c', $check) if ($found.Code -ne 0 -or -not $found.Value.StartsWith('/')) { return $null } return $found.Value } function ConvertTo-ApexLiteral([string]$Value) { if ($Value.IndexOf([char]0) -ge 0 -or $Value.Contains("`n") -or $Value.Contains("`r")) { throw 'A value contains an invalid character.' } return "'" + $Value.Replace("'", "''") + "'" } function Register-ApexProgram([string]$Distribution, [string]$Program, [string]$Launcher, [string]$IconPath, [string]$PowerShell) { $directory = Get-ApexDataFolder if (-not $directory) { throw 'The Apex folder in your user profile is unavailable.' } $uninstaller = Join-Path $directory 'uninstall.ps1' if ((Test-Path -LiteralPath $uninstaller) -and ((Get-Item -LiteralPath $uninstaller).Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw 'The Apex uninstaller file is not a normal file.' } $key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\ApexClient' $template = @' $ErrorActionPreference = 'Continue' $distribution = __DISTRIBUTION__ $program = __PROGRAM__ $folder = __FOLDER__ $key = __KEY__ Write-Host ('This removes Apex from Windows and from ' + $distribution + '. Your Apex accounts, settings and scripts in ' + $distribution + ' are kept.') $answer = ([string](Read-Host 'Remove Apex? [y/N]')).Trim() if ($answer -notmatch '^(?i:y|yes)$') { Write-Host 'Nothing was removed.'; Start-Sleep -Seconds 2; exit 0 } $system = Join-Path $env:WINDIR 'System32' if ([Environment]::Is64BitOperatingSystem -and -not [Environment]::Is64BitProcess) { $system = Join-Path $env:WINDIR 'Sysnative' } $wsl = Join-Path $system 'wsl.exe' $remove = 'set -f; IFS=; rm -f -- $1; d=${XDG_DATA_HOME:-$HOME/.local/share}; case $d in /*) ;; *) d=$HOME/.local/share;; esac; rm -f -- $d/applications/apex.desktop $d/applications/org.apexclient.GameWindow.desktop; command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database $d/applications >/dev/null 2>&1; exit 0' $removed = $false if (Test-Path -LiteralPath $wsl) { & $wsl --distribution $distribution --exec sh -c $remove apex-uninstall $program $removed = $LASTEXITCODE -eq 0 } if (-not $removed) { Write-Host ('Apex could not be removed from ' + $distribution + '. Open ' + $distribution + ' and delete ' + $program + ' yourself.') } $shell = New-Object -ComObject WScript.Shell foreach ($place in @('Programs', 'Desktop')) { $root = [Environment]::GetFolderPath($place) if (-not $root) { continue } $link = Join-Path $root 'Apex.lnk' if (-not (Test-Path -LiteralPath $link -PathType Leaf)) { continue } $shortcut = $shell.CreateShortcut($link) if ($shortcut.TargetPath -match '(?i)\\wslg?\.exe$' -and $shortcut.Arguments.Contains($program)) { Remove-Item -LiteralPath $link -Force } } Remove-Item -LiteralPath $key -Recurse -Force -ErrorAction SilentlyContinue Remove-Item -LiteralPath (Join-Path $folder 'apex.ico') -Force -ErrorAction SilentlyContinue Remove-Item -LiteralPath (Join-Path $folder 'uninstall.ps1') -Force -ErrorAction SilentlyContinue Remove-Item -LiteralPath $folder -Force -ErrorAction SilentlyContinue Write-Host 'Apex was removed.' Start-Sleep -Seconds 3 '@ $text = $template.Replace('__DISTRIBUTION__', (ConvertTo-ApexLiteral $Distribution)).Replace('__PROGRAM__', (ConvertTo-ApexLiteral $Program)).Replace('__FOLDER__', (ConvertTo-ApexLiteral $directory)).Replace('__KEY__', (ConvertTo-ApexLiteral $key)) $temporary = Join-Path $directory ([IO.Path]::GetRandomFileName()) try { [IO.File]::WriteAllText($temporary, ($text.Replace("`r`n", "`n").Replace("`n", "`r`n")), [Text.UTF8Encoding]::new($true)) Move-Item -LiteralPath $temporary -Destination $uninstaller -Force } finally { if (Test-Path -LiteralPath $temporary) { Remove-Item -LiteralPath $temporary -Force -ErrorAction SilentlyContinue } } New-Item -Path $key -Force | Out-Null $icon = $Launcher if ($IconPath) { $icon = $IconPath } $values = [ordered]@{ DisplayName = 'Apex'; Publisher = 'Apex'; Comments = ('Runs in WSL (' + $Distribution + ')') DisplayIcon = $icon; InstallLocation = $directory; URLInfoAbout = 'https://apexclient.org' InstallDate = (Get-Date -Format 'yyyyMMdd') UninstallString = ((Quote-ApexWindowsArgument $PowerShell) + ' -NoProfile -ExecutionPolicy Bypass -File ' + (Quote-ApexWindowsArgument $uninstaller)) } foreach ($name in $values.Keys) { New-ItemProperty -LiteralPath $key -Name $name -Value $values[$name] -PropertyType String -Force | Out-Null } foreach ($name in @('NoModify', 'NoRepair')) { New-ItemProperty -LiteralPath $key -Name $name -Value 1 -PropertyType DWord -Force | Out-Null } } function Get-ApexGuestCommand([string]$Base) { $url = $Base + '/install.sh' return (@( 'set -f; IFS=; umask 077; t=$(mktemp) || exit 1; trap ''rm -f -- $t'' EXIT;' ('if command -v curl >/dev/null 2>&1; then curl -fsSL --proto =https --proto-redir =https --tlsv1.2 --connect-timeout 20 --max-time 60 --retry 2 -o $t ' + $url + ';') ('elif command -v wget >/dev/null 2>&1; then wget -q --https-only --max-redirect=0 --timeout=20 --tries=3 -O $t ' + $url + ';') 'else echo Ubuntu needs curl or wget. Open Ubuntu and run: sudo apt install curl >&2; exit 1; fi' '|| { echo The Apex Linux installer could not be downloaded. Check your internet connection and try again. >&2; exit 1; };' 'bash $t --wsl-theme $1' ) -join ' ') } function Get-ApexWslGraphics([string]$Distribution) { $probe = @( 'set -f; IFS=; [ -d /mnt/wslg ] || exit 1;' 'case ${WAYLAND_DISPLAY:-} in /*) w=$WAYLAND_DISPLAY;; ?*) w=${XDG_RUNTIME_DIR:-/mnt/wslg/runtime-dir}/$WAYLAND_DISPLAY;; *) w=;; esac;' 'if [ -n $w ] && [ -S $w ]; then echo wayland; fi;' 'case ${DISPLAY:-} in :*|unix:*) x=${DISPLAY#*:}; x=${x%%.*};' 'case $x in ''''|*[!0-9]*) ;; *) [ ! -S /tmp/.X11-unix/X$x ] || echo x11;; esac;; esac;' 'if [ -c /dev/dxg ] && [ -r /dev/dxg ] && [ -w /dev/dxg ]; then echo dxg; fi' ) -join ' ' $result = Invoke-ApexWslQuery @('--distribution', $Distribution, '--exec', 'sh', '-c', $probe) $markers = $result.Text -split "`n" return [pscustomobject]@{ Ready=($result.Code -eq 0 -and ($markers -contains 'wayland' -or $markers -contains 'x11')) X11=($markers -contains 'x11') GpuDevice=($markers -contains 'dxg') } } function Write-ApexWslRenderer([string]$Distribution, $Graphics) { if ($Graphics.X11) { $result = Invoke-ApexWslQuery @('--distribution', $Distribution, '--exec', 'timeout', '-k', '2s', '8s', 'glxinfo', '-B') if ($result.Code -eq 0 -and $result.Text -match '(?m)^OpenGL renderer string:[ \t]*(\S[^\r\n]*)') { $renderer = $Matches[1].Trim() if ($renderer -match '(?i)llvmpipe|softpipe|software rasterizer|Microsoft Basic Render Driver|\bWARP\b' -or $result.Text -match '(?im)^[ \t]*Accelerated:[ \t]*no[ \t]*$') { Write-ApexNote ('WSLg X11 uses software OpenGL (' + $renderer + '). Update your Windows GPU driver and WSL for hardware acceleration.') } elseif ($renderer -match '^D3D12\b' -or $result.Text -match '(?im)^[ \t]*Accelerated:[ \t]*yes[ \t]*$') { Write-ApexNote ('WSLg X11 hardware OpenGL: ' + $renderer) } else { Write-ApexNote ('WSLg X11 OpenGL: ' + $renderer + ' (hardware acceleration unverified)') } return } } if ($Graphics.GpuDevice) { Write-ApexNote 'WSL can access a GPU, but this installer could not verify OpenGL acceleration.' } } function Write-ApexWindowsAdapters { try { $adapters = @(Get-CimInstance -ClassName Win32_VideoController -OperationTimeoutSec 3 -ErrorAction Stop) foreach ($adapter in ($adapters | Select-Object -First 8)) { if ($adapter.Name -and $adapter.ConfigManagerErrorCode -eq 0) { Write-ApexNote ('Windows display adapter: ' + [string]$adapter.Name) } } } catch { Write-ApexNote 'Could not read the Windows adapter list. The WSL renderer check can still identify the GPU in use.' } } function Install-ApexOnWindows { if ($env:OS -ne 'Windows_NT') { throw 'Run this installer in PowerShell on Windows.' } if (-not [Environment]::Is64BitOperatingSystem) { throw 'Apex needs 64-bit Windows.' } if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64' -or $env:PROCESSOR_ARCHITEW6432 -eq 'ARM64') { throw 'This Apex release needs an Intel or AMD (x64) PC. ARM PCs are not supported yet.' } [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 $base = 'https://apexclient.org' $systemDirectory = Join-Path $env:WINDIR 'System32' if ([Environment]::Is64BitOperatingSystem -and -not [Environment]::Is64BitProcess) { $systemDirectory = Join-Path $env:WINDIR 'Sysnative' } $script:ApexWsl = Join-Path $systemDirectory 'wsl.exe' $env:WSL_UTF8 = '1' Write-Host 'Apex installer for Windows' -ForegroundColor Green Write-ApexNote 'Apex runs inside WSL, the Linux environment built into Windows.' Write-ApexNote 'This installer sets up WSL and Ubuntu if needed, installs Apex, and adds it to your Start menu.' Write-ApexStep 'Step 1 of 5: Checking WSL' if (-not (Test-Path -LiteralPath $script:ApexWsl)) { throw 'This version of Windows does not include WSL. Install the latest updates in Settings > Windows Update, restart, then run this installer again.' } if (-not (Test-ApexWslInstalled)) { Write-ApexNote 'WSL is not installed yet, or it needs an update.' if ((Invoke-ApexWslSetup) -ne 'ready') { return } } Write-ApexNote 'WSL is installed.' Write-ApexStep 'Step 2 of 5: Checking Ubuntu' $distribution = Find-ApexUbuntu if (-not $distribution) { Write-ApexNote 'Apex needs Ubuntu, a free Linux system from the WSL catalog. Other Linux installs are kept.' if (-not (Read-ApexConsent 'Install Ubuntu now? (about 1 GB download)')) { Write-Host 'Installation stopped. Nothing was changed.' return } Write-Host '' Write-Host 'When Ubuntu opens it asks you to create a username and password.' -ForegroundColor Yellow Write-ApexNote 'This is a separate account just for Ubuntu. Use a short lowercase name, like your first name.' Write-ApexNote 'The password does not appear while you type. That is normal. Remember it: Apex setup asks for it next.' Write-ApexNote 'When you see a line ending in $, type exit and press Enter to continue here.' $code = Invoke-ApexWslInteractive @('--install', '--distribution', 'Ubuntu') $distribution = Find-ApexUbuntu if (-not $distribution -and $code -ne 0 -and -not (Test-ApexRestartPending)) { Write-ApexNote 'Trying the direct download instead of the Microsoft Store...' $code = Invoke-ApexWslInteractive @('--install', '--distribution', 'Ubuntu', '--web-download') $distribution = Find-ApexUbuntu } if (-not $distribution) { if (Test-ApexRestartPending) { Stop-ApexForRestart; return } throw 'Ubuntu was not installed. Check the message above, then run this installer again.' } } Write-ApexNote ('Using ' + $distribution + '.') $registration = @(Get-ApexUbuntuRegistrations | Where-Object { $_.Name -eq $distribution }) $version = 0 if ($registration.Count -eq 1 -and $registration[0].Version -in @(1, 2)) { $version = $registration[0].Version } else { $verbose = Invoke-ApexWslQuery @('--list', '--verbose') $row = @($verbose.Text -split "`n" | Where-Object { $_ -match ('^\s*\*?\s*' + [regex]::Escape($distribution) + '\s+\S+\s+[12]\s*$') }) if ($row.Count -eq 1 -and $row[0] -match '([12])\s*$') { $version = [int]$Matches[1] } } if ($version -eq 1) { Write-ApexNote 'This Ubuntu uses WSL 1. Apex needs WSL 2, which supports app windows. Your Ubuntu files are kept.' if (-not (Read-ApexConsent 'Convert Ubuntu to WSL 2 now? This can take several minutes.')) { Write-Host 'Installation stopped. Nothing was changed.' return } if ((Invoke-ApexWslInteractive @('--set-version', $distribution, '2')) -ne 0) { throw 'Ubuntu could not be converted to WSL 2. Its files were not removed. Restart Windows and run this installer again.' } } $identity = Invoke-ApexWslQuery @('--distribution', $distribution, '--exec', 'id', '-u') if ($identity.Code -ne 0) { $details = ($identity.Text + "`n" + $identity.Messages).Trim() switch (Get-ApexWslProblem $details) { 'firmware' { throw 'WSL could not start its virtual machine. Turn on virtualization in your PC''s BIOS/UEFI settings (it may be called Intel Virtualization Technology, VT-x, AMD-V or SVM Mode), restart Windows, then run this installer again.' } 'component' { Write-ApexNote 'WSL needs a Windows component (Virtual Machine Platform) that is not turned on yet.' [void](Invoke-ApexWslSetup) return } 'restart' { Stop-ApexForRestart; return } } throw ('Ubuntu did not start. ' + $details) } if ($identity.Value -notmatch '^[1-9][0-9]*$') { Write-Host '' Write-Host 'Ubuntu still needs its first-time setup.' -ForegroundColor Yellow Write-ApexNote 'Create your Ubuntu username and password if asked. The password does not appear while you type.' Write-ApexNote 'When you see a line ending in $, type exit and press Enter.' [void](Invoke-ApexWslInteractive @('--distribution', $distribution)) $identity = Invoke-ApexWslQuery @('--distribution', $distribution, '--exec', 'id', '-u') if ($identity.Code -ne 0 -or $identity.Value -notmatch '^[1-9][0-9]*$') { throw ('Ubuntu is still using its administrator (root) account. Open ' + $distribution + ' from the Start menu, finish creating your user, then run this installer again.') } } $architecture = Invoke-ApexWslQuery @('--distribution', $distribution, '--exec', 'uname', '-m') if ($architecture.Value -ne 'x86_64') { throw 'This Apex release needs x64 Ubuntu. ARM versions are not supported yet.' } Write-ApexStep 'Step 3 of 5: Checking app window support' $guiLauncher = Get-ApexGuiLauncher $systemDirectory $graphics = Get-ApexWslGraphics $distribution if (-not $graphics.Ready) { Write-ApexNote 'Linux app windows (WSLg) are not available yet. Updating WSL usually fixes this.' Write-ApexNote 'Open Linux windows and terminals will close while WSL restarts.' if (-not (Read-ApexConsent 'Update and restart WSL now?')) { Write-Host 'Installation stopped. Apex needs WSL app windows to run.' return } if ((Invoke-ApexWslInteractive @('--update')) -ne 0) { throw 'The WSL update failed. Check the Windows message above and try again.' } if ((Invoke-ApexWslInteractive @('--shutdown')) -ne 0) { throw 'WSL could not restart. Restart Windows and run this installer again.' } $guiLauncher = Get-ApexGuiLauncher $systemDirectory $graphics = Get-ApexWslGraphics $distribution if (-not $graphics.Ready) { throw 'Linux app windows are still unavailable. If the .wslconfig file in your user folder contains guiApplications=false, remove that line. Then restart Windows and run this installer again.' } } Write-ApexNote 'App windows are supported.' Write-ApexWindowsAdapters if (-not $graphics.GpuDevice) { Write-ApexNote 'Ubuntu cannot access the Windows GPU device. App windows can still open, but rendering may be slower. Update your Windows GPU driver and WSL, then restart Windows.' } $theme = 'dark' try { $personalize = Get-ItemProperty -LiteralPath 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize' -Name AppsUseLightTheme if ($personalize.AppsUseLightTheme -eq 1) { $theme = 'light' } } catch { } Write-ApexStep 'Step 4 of 5: Installing Apex in Ubuntu' Write-ApexNote 'Ubuntu may list a few packages Apex needs and ask before installing them.' Write-ApexNote 'If it asks for a password, type your Ubuntu password (not your Windows PIN). It does not appear while you type.' $code = Invoke-ApexWslInteractive @('--distribution', $distribution, '--cd', '~', '--exec', 'bash', '-c', (Get-ApexGuestCommand $base), 'apex-installer', $theme) if ($code -notin @(0, 3)) { throw ('Apex installation did not finish (WSL exit code ' + $code + '). Check the Ubuntu output above and run this installer again. Your saved accounts and settings were kept.') } $program = Find-ApexInstalledProgram $distribution if (-not $program) { throw ('Apex was installed, but its program file was not found in ' + $distribution + '. Run this installer again.') } Write-ApexWslRenderer $distribution $graphics Write-ApexStep 'Step 5 of 5: Adding Apex to the Start menu' $windowStyle = 1 $launcher = $guiLauncher if (-not $launcher) { $launcher = Join-Path (Join-Path $env:WINDIR 'System32') 'wsl.exe' $windowStyle = 7 } $arguments = Get-ApexShortcutArguments $distribution $program $theme $iconPath = Get-ApexWindowsIcon $base $startReady = $false try { $programs = Get-ApexFolder 'Programs' if (-not $programs) { throw 'Windows did not provide a Start menu folder.' } Write-ApexShortcut (Join-Path $programs 'Apex.lnk') $launcher $arguments $iconPath $windowStyle $startReady = $true } catch { Write-ApexNote ('The Start menu shortcut could not be added: ' + $_.Exception.Message) } try { $powerShell = Join-Path (Join-Path (Join-Path (Join-Path $env:WINDIR 'System32') 'WindowsPowerShell') 'v1.0') 'powershell.exe' Register-ApexProgram $distribution $program $launcher $iconPath $powerShell Write-ApexNote 'Apex is listed in Settings > Apps > Installed apps, where it can be uninstalled.' } catch { Write-ApexNote ('Apex could not be listed in Settings > Apps: ' + $_.Exception.Message) } if (Read-ApexConsent 'Also add an Apex shortcut to your Desktop?') { try { $desktop = Get-ApexFolder 'Desktop' if (-not $desktop) { throw 'Windows did not provide a Desktop folder.' } Write-ApexShortcut (Join-Path $desktop 'Apex.lnk') $launcher $arguments $iconPath $windowStyle Write-ApexNote 'The Desktop shortcut is ready.' } catch { Write-ApexNote ('The Desktop shortcut could not be added: ' + $_.Exception.Message) } } Write-Host '' Write-Host 'Apex is installed.' -ForegroundColor Green if ($code -eq 3) { Write-ApexNote 'Complete the Ubuntu system setup shown above before starting a game.' } if ($startReady) { Write-ApexNote 'Open the Start menu and search for Apex.' } else { Write-ApexNote ('Open ' + $distribution + ' and run apex to start it.') } Write-ApexNote 'Apex updates itself. The shortcut always opens the current version.' } $previousUtf8 = $env:WSL_UTF8 try { Install-ApexOnWindows } catch { Write-Host '' Write-Host ('Installation stopped: ' + $_.Exception.Message) -ForegroundColor Red $global:LASTEXITCODE = 1 } finally { $env:WSL_UTF8 = $previousUtf8 } }