perf(panel): optimize P1 hot paths and consolidate P2 shared primitives

This commit is contained in:
fish
2026-11-23 16:21:39 +09:00
parent 3a40bacc9d
commit 424ac73489
48 changed files with 4040 additions and 2745 deletions
@@ -34,7 +34,8 @@ import {
} from 'node:fs';
import { homedir } from 'node:os';
import { basename, dirname, join, win32 as pathWin32 } from 'node:path';
import { DATA_DIR, lsRuntimeEndpoint } from './config.mjs';
import { DATA_DIR, HERE, lsRuntimeEndpoint } from './config.mjs';
import { writeTextFileAtomicSync } from './fs-atomic.mjs';
export const REMOTE_DEBUG_LAUNCH_FLAG = '--remote-debugging-port=0';
@@ -589,20 +590,7 @@ async function spawnDetachedProcess(file, args, options = {}) {
}
function writeTextFileAtomically(filePath, content, fsOps) {
const { mkdirFn, writeFn, renameFn, unlinkFn } = fsOps;
mkdirFn(dirname(filePath), { recursive: true });
const tmpPath = `${filePath}.tmp-${process.pid}`;
try {
writeFn(tmpPath, content, 'utf8');
renameFn(tmpPath, filePath);
} catch {
try {
unlinkFn(tmpPath);
} catch {
// 忽略临时文件清理异常
}
writeFn(filePath, content, 'utf8');
}
writeTextFileAtomicSync(filePath, content, fsOps);
}
/**
@@ -769,293 +757,18 @@ export function probeWindowsShortcutsFast({
};
}
const WIN_SHORTCUT_PROVISION_PS = `
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$ErrorActionPreference = 'SilentlyContinue'
$enableFlag = $env:AGY_LAUNCH_ENABLED -eq '1'
$updated = New-Object System.Collections.Generic.List[string]
$configured = New-Object System.Collections.Generic.List[string]
$readOnly = New-Object System.Collections.Generic.List[string]
$discoveredDirs = New-Object System.Collections.Generic.List[string]
$ws = $null
$userDesktopDir = [Environment]::GetFolderPath('Desktop')
$commonDesktopDir = [Environment]::GetFolderPath('CommonDesktopDirectory')
$userProgramsDir = [Environment]::GetFolderPath('Programs')
$commonProgramsDir = [Environment]::GetFolderPath('CommonPrograms')
$userStartupDir = [Environment]::GetFolderPath('Startup')
$zhDesktop = ([string][char]0x684C + [string][char]0x9762)
let cachedWinShortcutProvisionPs = '';
$flatSpecs = New-Object System.Collections.Generic.List[object]
$seenScanDir = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
function Add-ScanDir([string]$dirPath, [bool]$isCommon, [string]$kind, [bool]$expandVendorSubdir, [bool]$recordDiscovered) {
if (-not $dirPath) { return }
if (-not $seenScanDir.Add($dirPath)) { return }
$exists = $false
try { $exists = [bool](Test-Path -LiteralPath $dirPath -ErrorAction SilentlyContinue) } catch { $exists = $false }
if (-not $exists) { return }
$flatSpecs.Add(@{ Dir = $dirPath; IsCommon = $isCommon; Kind = $kind })
if ($recordDiscovered) { $discoveredDirs.Add($dirPath) }
if ($expandVendorSubdir) {
try {
foreach ($sub in @(Get-ChildItem -LiteralPath $dirPath -Directory -ErrorAction SilentlyContinue)) {
if ($sub -and $sub.Name -and ($sub.Name -match '(?i)antigravity|^google(?:\\b|[\\s_-]|$)') -and $sub.FullName -and $seenScanDir.Add([string]$sub.FullName)) {
$flatSpecs.Add(@{ Dir = [string]$sub.FullName; IsCommon = $isCommon; Kind = $kind })
if ($recordDiscovered) { $discoveredDirs.Add([string]$sub.FullName) }
}
}
} catch {}
function getWinShortcutProvisionPs() {
if (!cachedWinShortcutProvisionPs) {
cachedWinShortcutProvisionPs = readFileSync(
join(HERE, 'backend', 'windows-shortcut-provision.ps1'),
'utf8'
).trim();
}
return cachedWinShortcutProvisionPs;
}
Add-ScanDir $userDesktopDir $false 'desktop' $false $true
foreach ($d in @(
$(if ($env:USERPROFILE) { Join-Path $env:USERPROFILE 'Desktop' } else { '' }),
$(if ($env:USERPROFILE) { Join-Path $env:USERPROFILE $zhDesktop } else { '' })
)) {
Add-ScanDir $d $false 'desktop' $false $false
}
foreach ($od in @($env:OneDrive, $env:OneDriveConsumer, $env:OneDriveCommercial)) {
if ($od) {
Add-ScanDir (Join-Path $od 'Desktop') $false 'desktop' $false $false
Add-ScanDir (Join-Path $od $zhDesktop) $false 'desktop' $false $false
}
}
Add-ScanDir $userProgramsDir $false 'programs' $true $true
Add-ScanDir ([Environment]::GetFolderPath('StartMenu')) $false 'startmenu' $false $false
Add-ScanDir $userStartupDir $false 'startup' $false $true
if ($env:APPDATA) {
Add-ScanDir (Join-Path $env:APPDATA 'Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\TaskBar') $false 'taskbar' $false $false
}
Add-ScanDir $commonDesktopDir $true 'desktop' $false $true
Add-ScanDir $commonProgramsDir $true 'programs' $true $true
$seen = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
foreach ($spec in $flatSpecs) {
$dir = [string]$spec.Dir
$items = @()
try {
$items = @(Get-ChildItem -LiteralPath $dir -Filter '*.lnk' -File -ErrorAction SilentlyContinue)
} catch { continue }
foreach ($item in $items) {
if ($item.Length -lt 32 -or $item.Length -gt 65536) { continue }
$lnkPath = [string]$item.FullName
if (-not $seen.Add($lnkPath)) { continue }
try {
$rawBytes = [System.IO.File]::ReadAllBytes($lnkPath)
$u16 = [System.Text.Encoding]::Unicode.GetString($rawBytes)
$lat = [System.Text.Encoding]::GetEncoding(28591).GetString($rawBytes)
if ($u16.IndexOf('antigravity.exe', [System.StringComparison]::OrdinalIgnoreCase) -lt 0 -and
$lat.IndexOf('antigravity.exe', [System.StringComparison]::OrdinalIgnoreCase) -lt 0) {
continue
}
if (-not $ws) { $ws = New-Object -ComObject WScript.Shell }
$sc = $ws.CreateShortcut($lnkPath)
$target = [string]$sc.TargetPath
if ([System.IO.Path]::GetFileName($target) -ine 'Antigravity.exe') { continue }
$curArgs = [string]$sc.Arguments
if ($enableFlag) {
if ($curArgs -match '(?:^|\\s)--remote-debugging-port(?:=|\\s|$)') {
$configured.Add($lnkPath)
continue
}
$nextArgs = ($curArgs.Trim() + ' --remote-debugging-port=0').Trim()
try {
$sc.Arguments = $nextArgs
$sc.Save()
$updated.Add($lnkPath)
$configured.Add($lnkPath)
} catch {
$readOnly.Add($lnkPath)
if ($spec.IsCommon -and $spec.Kind -eq 'programs' -and $userProgramsDir -and (Test-Path -LiteralPath $userProgramsDir -ErrorAction SilentlyContinue)) {
$fallbackLnk = Join-Path $userProgramsDir ([System.IO.Path]::GetFileName($lnkPath))
if (-not (Test-Path -LiteralPath $fallbackLnk -ErrorAction SilentlyContinue)) {
try {
$uSc = $ws.CreateShortcut($fallbackLnk)
$uSc.TargetPath = $target
$uSc.Arguments = $nextArgs
if ($sc.WorkingDirectory) { $uSc.WorkingDirectory = $sc.WorkingDirectory }
if ($sc.IconLocation) { $uSc.IconLocation = $sc.IconLocation }
if ($sc.Description) { $uSc.Description = $sc.Description }
$uSc.Save()
$updated.Add($fallbackLnk)
$configured.Add($fallbackLnk)
} catch {}
}
}
}
} else {
$stripped = ([regex]::Replace($curArgs, '(?:^|\\s+)--remote-debugging-port=(?:"0"|''0''|0)(?=\\s|$)', ' ')).Trim()
if ($stripped -ne $curArgs.Trim()) {
try {
$sc.Arguments = $stripped
$sc.Save()
$updated.Add($lnkPath)
} catch {
$readOnly.Add($lnkPath)
}
}
if ($stripped -match '(?:^|\\s)--remote-debugging-port(?:=|\\s|$)') {
$configured.Add($lnkPath)
}
}
} catch {}
}
}
$runTargets = @(
@{ Root = [Microsoft.Win32.Registry]::CurrentUser; Label = 'HKCU'; SubPath = 'Software\\Microsoft\\Windows\\CurrentVersion\\Run' },
@{ Root = [Microsoft.Win32.Registry]::LocalMachine; Label = 'HKLM'; SubPath = 'Software\\Microsoft\\Windows\\CurrentVersion\\Run' },
@{ Root = [Microsoft.Win32.Registry]::LocalMachine; Label = 'HKLM'; SubPath = 'Software\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run' }
)
foreach ($rt in $runTargets) {
try {
$runReadKey = $rt.Root.OpenSubKey($rt.SubPath, $false)
if (-not $runReadKey) { continue }
$runNames = @($runReadKey.GetValueNames())
$runReadKey.Close()
foreach ($valName in $runNames) {
$rk = $rt.Root.OpenSubKey($rt.SubPath, $false)
if (-not $rk) { continue }
$rawCmd = [string]$rk.GetValue($valName, '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
$rk.Close()
if (-not $rawCmd) { continue }
if ($rawCmd -notmatch '^\\s*(?:"([^"]+)"|([^\\s"]+))(.*)$') { continue }
$exeCandidate = if ($Matches[1]) { $Matches[1] } else { $Matches[2] }
$restPart = [string]$Matches[3]
if ([System.IO.Path]::GetFileName($exeCandidate) -ine 'Antigravity.exe') { continue }
$quotedExe = if ($Matches[1] -or $exeCandidate -match '\\s') { '"' + $exeCandidate + '"' } else { $exeCandidate }
$entryId = if ($rt.Label -eq 'HKLM') { "HKLM:Run:$($rt.SubPath)|$valName" } else { "HKCU:Run:$valName" }
if ($enableFlag) {
if ($restPart -match '(?:^|\\s)--remote-debugging-port(?:=|\\s|$)') {
$configured.Add($entryId)
continue
}
$nextRest = ($restPart.Trim() + ' --remote-debugging-port=0').Trim()
$nextVal = "$quotedExe $nextRest"
$saved = $false
try {
$wk = $rt.Root.OpenSubKey($rt.SubPath, $true)
if ($wk) {
$wk.SetValue($valName, $nextVal, [Microsoft.Win32.RegistryValueKind]::String)
$wk.Close()
$saved = $true
}
} catch {}
if ($saved) {
$updated.Add($entryId)
$configured.Add($entryId)
} elseif ($rt.Label -eq 'HKLM') {
$readOnly.Add($entryId)
}
} else {
$strippedRest = ([regex]::Replace($restPart, '(?:^|\\s+)--remote-debugging-port=(?:"0"|''0''|0)(?=\\s|$)', ' ')).Trim()
$nextVal = if ($strippedRest) { "$quotedExe $strippedRest" } else { $quotedExe }
if ($nextVal -ne $rawCmd.Trim()) {
$saved = $false
try {
$wk = $rt.Root.OpenSubKey($rt.SubPath, $true)
if ($wk) {
$wk.SetValue($valName, $nextVal, [Microsoft.Win32.RegistryValueKind]::String)
$wk.Close()
$saved = $true
}
} catch {}
if ($saved) {
$updated.Add($entryId)
} elseif ($rt.Label -eq 'HKLM') {
$readOnly.Add($entryId)
}
}
if ($strippedRest -match '(?:^|\\s)--remote-debugging-port(?:=|\\s|$)') {
$configured.Add($entryId)
}
}
}
} catch {}
}
$exactClassSubKeys = @(
'Software\\Classes\\antigravity\\shell\\open\\command',
'Software\\Classes\\Applications\\Antigravity.exe\\shell\\open\\command',
'Software\\Classes\\AntigravitySourceFile\\shell\\open\\command'
)
$classRoots = @(
@{ Root = [Microsoft.Win32.Registry]::CurrentUser; Label = 'HKCU'; FullPrefix = 'HKEY_CURRENT_USER' },
@{ Root = [Microsoft.Win32.Registry]::LocalMachine; Label = 'HKLM'; FullPrefix = 'HKEY_LOCAL_MACHINE' }
)
foreach ($cr in $classRoots) {
foreach ($relSubKey in $exactClassSubKeys) {
try {
$rOpen = $cr.Root.OpenSubKey($relSubKey, $false)
if (-not $rOpen) { continue }
$rawOpen = [string]$rOpen.GetValue('', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
$rOpen.Close()
if (-not $rawOpen) { continue }
if ($rawOpen -notmatch '^\\s*(?:"([^"]+)"|([^\\s"]+))(.*)$') { continue }
$exeCand = if ($Matches[1]) { $Matches[1] } else { $Matches[2] }
$restOpen = ([string]$Matches[3]).Trim()
if ([System.IO.Path]::GetFileName($exeCand) -ine 'Antigravity.exe') { continue }
$quotedOpenExe = if ($Matches[1] -or $exeCand -match '\\s') { '"' + $exeCand + '"' } else { $exeCand }
$fullKeyName = if ($cr.Label -eq 'HKLM') { "HKLM:Class:$relSubKey" } else { "$($cr.FullPrefix)\\$relSubKey" }
if ($enableFlag) {
if ($restOpen -match '(?:^|\\s)--remote-debugging-port(?:=|\\s|$)') {
$configured.Add($fullKeyName)
continue
}
$nextOpen = if ($restOpen) { "$quotedOpenExe --remote-debugging-port=0 $restOpen" } else { "$quotedOpenExe --remote-debugging-port=0" }
$saved = $false
try {
$wOpen = $cr.Root.OpenSubKey($relSubKey, $true)
if ($wOpen) {
$wOpen.SetValue('', $nextOpen, [Microsoft.Win32.RegistryValueKind]::String)
$wOpen.Close()
$saved = $true
}
} catch {}
if ($saved) {
$updated.Add($fullKeyName)
$configured.Add($fullKeyName)
} elseif ($cr.Label -eq 'HKLM') {
$readOnly.Add($fullKeyName)
}
} else {
$strippedOpen = ([regex]::Replace($restOpen, '(?:^|\\s+)--remote-debugging-port=(?:"0"|''0''|0)(?=\\s|$)', ' ')).Trim()
$nextOpen = if ($strippedOpen) { "$quotedOpenExe $strippedOpen" } else { $quotedOpenExe }
if ($nextOpen -ne $rawOpen.Trim()) {
$saved = $false
try {
$wOpen = $cr.Root.OpenSubKey($relSubKey, $true)
if ($wOpen) {
$wOpen.SetValue('', $nextOpen, [Microsoft.Win32.RegistryValueKind]::String)
$wOpen.Close()
$saved = $true
}
} catch {}
if ($saved) {
$updated.Add($fullKeyName)
} elseif ($cr.Label -eq 'HKLM') {
$readOnly.Add($fullKeyName)
}
}
if ($strippedOpen -match '(?:^|\\s)--remote-debugging-port(?:=|\\s|$)') {
$configured.Add($fullKeyName)
}
}
} catch {}
}
}
[pscustomobject]@{
ok = $true
updatedPaths = @($updated)
configuredPaths = @($configured)
readOnlyPaths = @($readOnly)
discoveredDirs = @($discoveredDirs)
} | ConvertTo-Json -Compress
`.trim();
const cachedDiscoveredWinDirsByHome = new Map();
function getCachedWinDiscoveredDirs(homeDir) {
@@ -1226,7 +939,7 @@ export async function ensureDesktopLaunchEntriesConfigured({
const psExe = execFileFn ? 'powershell.exe' : resolveWindowsPowerShellExecutable(env, ops.existsFn);
const out = await runner(
psExe,
['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command', WIN_SHORTCUT_PROVISION_PS],
['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command', getWinShortcutProvisionPs()],
{
timeoutMs: 10000,
env: {
@@ -1590,6 +1303,83 @@ function psSingleQuote(val) {
* @param {{ readOnlyShortcutPaths?: string[], psExePath?: string }} [extra]
* @returns {string}
*/
function buildReadonlyShortcutPatchPsBlock() {
const coreLines = [
'$ws = $null',
'foreach ($item in $targets) {',
' if (-not $item) { continue }',
" if ($item.StartsWith('HKLM:Run:')) {",
' try {',
' $restKey = $item.Substring(9)',
" $sepIdx = $restKey.IndexOf('|')",
' if ($sepIdx -gt 0) {',
' $subPath = $restKey.Substring(0, $sepIdx)',
' $valName = $restKey.Substring($sepIdx + 1)',
' $rk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($subPath, $false)',
' if ($rk) {',
" $rawCmd = [string]$rk.GetValue($valName, '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)",
' $rk.Close()',
' if ($rawCmd -match \'^\\s*(?:"([^"]+)"|([^\\s"]+))(.*)$\') {',
' $exeCand = if ($Matches[1]) { $Matches[1] } else { $Matches[2] }',
' $restPart = [string]$Matches[3]',
" if ([System.IO.Path]::GetFileName($exeCand) -ieq 'Antigravity.exe' -and $restPart -notmatch '(?:^|\\s)--remote-debugging-port(?:=|\\s|$)') {",
' $qExe = if ($Matches[1] -or $exeCand -match \'\\s\') { \'"\' + $exeCand + \'"\' } else { $exeCand }',
" $nextVal = ($qExe + ' ' + ($restPart.Trim() + ' --remote-debugging-port=0').Trim()).Trim()",
' $wk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($subPath, $true)',
' if ($wk) { $wk.SetValue($valName, $nextVal, [Microsoft.Win32.RegistryValueKind]::String); $wk.Close() }',
' }',
' }',
' }',
' }',
' } catch {}',
' continue',
' }',
" if ($item.StartsWith('HKLM:Class:')) {",
' try {',
' $relKey = $item.Substring(11)',
' $rk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($relKey, $false)',
' if ($rk) {',
" $rawOpen = [string]$rk.GetValue('', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)",
' $rk.Close()',
' if ($rawOpen -match \'^\\s*(?:"([^"]+)"|([^\\s"]+))(.*)$\') {',
' $exeCand = if ($Matches[1]) { $Matches[1] } else { $Matches[2] }',
' $restOpen = ([string]$Matches[3]).Trim()',
" if ([System.IO.Path]::GetFileName($exeCand) -ieq 'Antigravity.exe' -and $restOpen -notmatch '(?:^|\\s)--remote-debugging-port(?:=|\\s|$)') {",
' $qExe = if ($Matches[1] -or $exeCand -match \'\\s\') { \'"\' + $exeCand + \'"\' } else { $exeCand }',
" $nextOpen = if ($restOpen) { $qExe + ' --remote-debugging-port=0 ' + $restOpen } else { $qExe + ' --remote-debugging-port=0' }",
' $wk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($relKey, $true)',
" if ($wk) { $wk.SetValue('', $nextOpen, [Microsoft.Win32.RegistryValueKind]::String); $wk.Close() }",
' }',
' }',
' }',
' } catch {}',
' continue',
' }',
' if (Test-Path -LiteralPath $item) {',
' try {',
' if (-not $ws) { $ws = New-Object -ComObject WScript.Shell }',
' $sc = $ws.CreateShortcut($item)',
' $cur = [string]$sc.Arguments',
" if ($cur -notmatch '(?:^|\\s)--remote-debugging-port(?:=|\\s|$)') {",
" $sc.Arguments = ($cur.Trim() + ' --remote-debugging-port=0').Trim()",
' $sc.Save()',
' }',
' } catch {}',
' }',
'}',
];
const fnLines = [
'function Patch-ShortcutList($targets) {',
' if (-not $targets -or $targets.Count -eq 0) { return }',
...coreLines.map((line) => ` ${line}`),
'}',
].join('\n');
const quotedLines = coreLines
.map((line) => ` '${line.replace(/'/g, "''")}',`)
.join('\n');
return { fnLines, quotedLines };
}
export function buildWindowsRelaunchInnerScript(
executablePath,
stalePortFiles = [],
@@ -1598,6 +1388,7 @@ export function buildWindowsRelaunchInnerScript(
) {
const procName = kind === 'ide' ? 'Antigravity IDE' : 'Antigravity';
const psExe = String(extra?.psExePath || 'powershell.exe').trim() || 'powershell.exe';
const { fnLines, quotedLines } = buildReadonlyShortcutPatchPsBlock();
const portLiterals = (Array.isArray(stalePortFiles) ? stalePortFiles : [])
.map((p) => String(p || '').trim())
.filter(Boolean)
@@ -1626,70 +1417,7 @@ if (-not $exe -or -not (Test-Path -LiteralPath $exe)) {
if ($livePath) { $exe = [string]$livePath }
}
function Patch-ShortcutList($paths) {
if (-not $paths -or $paths.Count -eq 0) { return }
$ws = $null
foreach ($item in $paths) {
if (-not $item) { continue }
if ($item.StartsWith('HKLM:Run:')) {
try {
$restKey = $item.Substring(9)
$sepIdx = $restKey.IndexOf('|')
if ($sepIdx -gt 0) {
$subPath = $restKey.Substring(0, $sepIdx)
$valName = $restKey.Substring($sepIdx + 1)
$rk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($subPath, $false)
if ($rk) {
$rawCmd = [string]$rk.GetValue($valName, '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
$rk.Close()
if ($rawCmd -match '^\\s*(?:"([^"]+)"|([^\\s"]+))(.*)$') {
$exeCand = if ($Matches[1]) { $Matches[1] } else { $Matches[2] }
$restPart = [string]$Matches[3]
if ([System.IO.Path]::GetFileName($exeCand) -ieq 'Antigravity.exe' -and $restPart -notmatch '(?:^|\\s)--remote-debugging-port(?:=|\\s|$)') {
$qExe = if ($Matches[1] -or $exeCand -match '\\s') { '"' + $exeCand + '"' } else { $exeCand }
$nextVal = ($qExe + ' ' + ($restPart.Trim() + ' --remote-debugging-port=0').Trim()).Trim()
$wk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($subPath, $true)
if ($wk) { $wk.SetValue($valName, $nextVal, [Microsoft.Win32.RegistryValueKind]::String); $wk.Close() }
}
}
}
}
} catch {}
continue
}
if ($item.StartsWith('HKLM:Class:')) {
try {
$relKey = $item.Substring(11)
$rk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($relKey, $false)
if ($rk) {
$rawOpen = [string]$rk.GetValue('', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
$rk.Close()
if ($rawOpen -match '^\\s*(?:"([^"]+)"|([^\\s"]+))(.*)$') {
$exeCand = if ($Matches[1]) { $Matches[1] } else { $Matches[2] }
$restOpen = ([string]$Matches[3]).Trim()
if ([System.IO.Path]::GetFileName($exeCand) -ieq 'Antigravity.exe' -and $restOpen -notmatch '(?:^|\\s)--remote-debugging-port(?:=|\\s|$)') {
$qExe = if ($Matches[1] -or $exeCand -match '\\s') { '"' + $exeCand + '"' } else { $exeCand }
$nextOpen = if ($restOpen) { "$qExe --remote-debugging-port=0 $restOpen" } else { "$qExe --remote-debugging-port=0" }
$wk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($relKey, $true)
if ($wk) { $wk.SetValue('', $nextOpen, [Microsoft.Win32.RegistryValueKind]::String); $wk.Close() }
}
}
}
} catch {}
continue
}
if (-not (Test-Path -LiteralPath $item)) { continue }
try {
if (-not $ws) { $ws = New-Object -ComObject WScript.Shell }
$sc = $ws.CreateShortcut($item)
$cur = [string]$sc.Arguments
if ($cur -notmatch '(?:^|\\s)--remote-debugging-port(?:=|\\s|$)') {
$sc.Arguments = ($cur.Trim() + ' --remote-debugging-port=0').Trim()
$sc.Save()
}
} catch {}
}
}
${fnLines}
if ($readOnlyShortcuts.Count -gt 0) {
if ($isSelfElevated) {
@@ -1699,68 +1427,7 @@ if ($readOnlyShortcuts.Count -gt 0) {
$roJoined = ($readOnlyShortcuts | ForEach-Object { "'" + ($_.Replace("'", "''")) + "'" }) -join ', '
$elevBody = @(
('$targets = @(' + $roJoined + ')'),
'$ws = $null',
'foreach ($item in $targets) {',
' if (-not $item) { continue }',
' if ($item.StartsWith(''HKLM:Run:'')) {',
' try {',
' $restKey = $item.Substring(9)',
' $sepIdx = $restKey.IndexOf(''|'')',
' if ($sepIdx -gt 0) {',
' $subPath = $restKey.Substring(0, $sepIdx)',
' $valName = $restKey.Substring($sepIdx + 1)',
' $rk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($subPath, $false)',
' if ($rk) {',
' $rawCmd = [string]$rk.GetValue($valName, '''', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)',
' $rk.Close()',
' if ($rawCmd -match ''^\\s*(?:"([^"]+)"|([^\\s"]+))(.*)$'') {',
' $exeCand = if ($Matches[1]) { $Matches[1] } else { $Matches[2] }',
' $restPart = [string]$Matches[3]',
' if ([System.IO.Path]::GetFileName($exeCand) -ieq ''Antigravity.exe'' -and $restPart -notmatch ''(?:^|\\s)--remote-debugging-port(?:=|\\s|$)'') {',
' $qExe = if ($Matches[1] -or $exeCand -match ''\\s'') { ''"'' + $exeCand + ''"'' } else { $exeCand }',
' $nextVal = ($qExe + '' '' + ($restPart.Trim() + '' --remote-debugging-port=0'').Trim()).Trim()',
' $wk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($subPath, $true)',
' if ($wk) { $wk.SetValue($valName, $nextVal, [Microsoft.Win32.RegistryValueKind]::String); $wk.Close() }',
' }',
' }',
' }',
' }',
' } catch {}',
' continue',
' }',
' if ($item.StartsWith(''HKLM:Class:'')) {',
' try {',
' $relKey = $item.Substring(11)',
' $rk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($relKey, $false)',
' if ($rk) {',
' $rawOpen = [string]$rk.GetValue('''', '''', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)',
' $rk.Close()',
' if ($rawOpen -match ''^\\s*(?:"([^"]+)"|([^\\s"]+))(.*)$'') {',
' $exeCand = if ($Matches[1]) { $Matches[1] } else { $Matches[2] }',
' $restOpen = ([string]$Matches[3]).Trim()',
' if ([System.IO.Path]::GetFileName($exeCand) -ieq ''Antigravity.exe'' -and $restOpen -notmatch ''(?:^|\\s)--remote-debugging-port(?:=|\\s|$)'') {',
' $qExe = if ($Matches[1] -or $exeCand -match ''\\s'') { ''"'' + $exeCand + ''"'' } else { $exeCand }',
' $nextOpen = if ($restOpen) { $qExe + '' --remote-debugging-port=0 '' + $restOpen } else { $qExe + '' --remote-debugging-port=0'' }',
' $wk = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($relKey, $true)',
' if ($wk) { $wk.SetValue('''', $nextOpen, [Microsoft.Win32.RegistryValueKind]::String); $wk.Close() }',
' }',
' }',
' }',
' } catch {}',
' continue',
' }',
' if (Test-Path -LiteralPath $item) {',
' try {',
' if (-not $ws) { $ws = New-Object -ComObject WScript.Shell }',
' $sc = $ws.CreateShortcut($item)',
' $cur = [string]$sc.Arguments',
' if ($cur -notmatch ''(?:^|\\s)--remote-debugging-port(?:=|\\s|$)'') {',
' $sc.Arguments = ($cur.Trim() + '' --remote-debugging-port=0'').Trim()',
' $sc.Save()',
' }',
' } catch {}',
' }',
'}',
${quotedLines}
'Remove-Item -LiteralPath $PSCommandPath -Force -ErrorAction SilentlyContinue'
) -join "\`r\`n"
try {