forked from Zuosizhu/Alas-with-Dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(adb-vision): restore live piloting and audit backend helpers #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Capture a screenshot of the MEmu emulator window using .NET drawing. | ||
| .PARAMETER OutputPath | ||
| Path to save the PNG screenshot. | ||
| .PARAMETER AutoDelete | ||
| If set, deletes the screenshot after this many seconds. Default: 300 (5 min). | ||
| #> | ||
| param( | ||
| [Parameter(Mandatory=$true)] | ||
| [string]$OutputPath, | ||
|
|
||
| [int]$AutoDelete = 300 | ||
| ) | ||
|
|
||
| Add-Type -AssemblyName System.Windows.Forms | ||
| Add-Type -AssemblyName System.Drawing | ||
| Add-Type -AssemblyName Microsoft.VisualBasic | ||
|
|
||
| # Find the actual MEmu VM window first. | ||
| # Exact title lookup for "MEmu" can resolve to a tiny hidden helper window, | ||
| # which produces useless 50x15 captures. Prefer the real process window. | ||
| Add-Type @" | ||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| public class WinAPI { | ||
| [DllImport("user32.dll")] | ||
| public static extern IntPtr GetForegroundWindow(); | ||
|
|
||
| [DllImport("user32.dll")] | ||
| [return: MarshalAs(UnmanagedType.Bool)] | ||
| public static extern bool SetForegroundWindow(IntPtr hWnd); | ||
|
|
||
| [DllImport("user32.dll")] | ||
| [return: MarshalAs(UnmanagedType.Bool)] | ||
| public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); | ||
|
|
||
| [DllImport("user32.dll")] | ||
| [return: MarshalAs(UnmanagedType.Bool)] | ||
| public static extern bool IsIconic(IntPtr hWnd); | ||
|
|
||
| [DllImport("user32.dll")] | ||
| [return: MarshalAs(UnmanagedType.Bool)] | ||
| public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); | ||
|
|
||
| [DllImport("user32.dll")] | ||
| [return: MarshalAs(UnmanagedType.Bool)] | ||
| public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags); | ||
| } | ||
|
|
||
| public struct RECT { | ||
| public int Left; | ||
| public int Top; | ||
| public int Right; | ||
| public int Bottom; | ||
| } | ||
| "@ | ||
|
|
||
| function Get-WindowRectObject { | ||
| param([IntPtr]$Handle) | ||
| $rect = New-Object RECT | ||
| if (-not [WinAPI]::GetWindowRect($Handle, [ref]$rect)) { | ||
| return $null | ||
| } | ||
| [pscustomobject]@{ | ||
| Rect = $rect | ||
| Width = $rect.Right - $rect.Left | ||
| Height = $rect.Bottom - $rect.Top | ||
| Area = ($rect.Right - $rect.Left) * ($rect.Bottom - $rect.Top) | ||
| } | ||
| } | ||
|
|
||
| function Get-CandidateWindow { | ||
| param([System.Diagnostics.Process[]]$Processes) | ||
|
|
||
| $best = $null | ||
| foreach ($proc in $Processes) { | ||
| if ($proc.MainWindowHandle -eq 0) { continue } | ||
| $title = ($proc.MainWindowTitle | Out-String).Trim() | ||
| if (-not $title) { continue } | ||
|
|
||
| $rectInfo = Get-WindowRectObject -Handle ([IntPtr]$proc.MainWindowHandle) | ||
| if ($null -eq $rectInfo) { continue } | ||
| if ($rectInfo.Width -le 200 -or $rectInfo.Height -le 200) { continue } | ||
|
|
||
| $candidate = [pscustomobject]@{ | ||
| ProcessId = $proc.Id | ||
| Handle = [IntPtr]$proc.MainWindowHandle | ||
| Title = $title | ||
| Rect = $rectInfo.Rect | ||
| Width = $rectInfo.Width | ||
| Height = $rectInfo.Height | ||
| Area = $rectInfo.Area | ||
| } | ||
|
|
||
| if ($null -eq $best -or $candidate.Area -gt $best.Area) { | ||
| $best = $candidate | ||
| } | ||
| } | ||
| return $best | ||
| } | ||
|
|
||
| $memuWindow = Get-CandidateWindow -Processes ( | ||
| Get-Process -Name "MEmu" -ErrorAction SilentlyContinue | ||
| ) | ||
|
|
||
| if ($null -eq $memuWindow) { | ||
| $fallbackProcesses = Get-Process | Where-Object { | ||
| $_.MainWindowHandle -ne 0 -and ( | ||
| $_.ProcessName -like "*MEmu*" -or | ||
| $_.MainWindowTitle -like "*(MEmu*" -or | ||
| $_.MainWindowTitle -like "*MEmu*" | ||
| ) | ||
| } | ||
| $memuWindow = Get-CandidateWindow -Processes $fallbackProcesses | ||
| } | ||
|
|
||
| if ($null -eq $memuWindow) { | ||
| Write-Error "MEmu window not found" | ||
| exit 1 | ||
| } | ||
|
|
||
| $memuHwnd = $memuWindow.Handle | ||
|
|
||
| # Save the currently active window so we can restore it | ||
| $previousWindow = [WinAPI]::GetForegroundWindow() | ||
|
|
||
| # If MEmu is minimized, restore it | ||
| if ([WinAPI]::IsIconic($memuHwnd)) { | ||
| [WinAPI]::ShowWindow($memuHwnd, 9) # SW_RESTORE | ||
| Start-Sleep -Milliseconds 500 | ||
| } | ||
|
|
||
| # Bring MEmu to front | ||
| [WinAPI]::ShowWindow($memuHwnd, 5) | Out-Null # SW_SHOW | ||
| try { | ||
| [Microsoft.VisualBasic.Interaction]::AppActivate($memuWindow.ProcessId) | Out-Null | ||
| } catch { | ||
| [WinAPI]::SetForegroundWindow($memuHwnd) | Out-Null | ||
| } | ||
| Start-Sleep -Milliseconds 300 | ||
|
|
||
| # Get window rect | ||
| $rect = $memuWindow.Rect | ||
| $width = $memuWindow.Width | ||
| $height = $memuWindow.Height | ||
|
|
||
| if ($width -le 0 -or $height -le 0) { | ||
| Write-Error "Invalid window dimensions: ${width}x${height}" | ||
| # Restore previous window | ||
| [WinAPI]::SetForegroundWindow($previousWindow) | Out-Null | ||
| exit 1 | ||
| } | ||
|
|
||
| # Capture the screen region | ||
| $bitmap = New-Object System.Drawing.Bitmap($width, $height) | ||
| $graphics = [System.Drawing.Graphics]::FromImage($bitmap) | ||
| $hdc = $graphics.GetHdc() | ||
| $printed = $false | ||
| try { | ||
| $printed = [WinAPI]::PrintWindow($memuHwnd, $hdc, 2) | ||
| } finally { | ||
| $graphics.ReleaseHdc($hdc) | ||
| } | ||
| if (-not $printed) { | ||
| $graphics.CopyFromScreen($rect.Left, $rect.Top, 0, 0, | ||
| (New-Object System.Drawing.Size($width, $height))) | ||
| } | ||
| $graphics.Dispose() | ||
|
|
||
| # Save | ||
| $dir = Split-Path -Parent $OutputPath | ||
| if ($dir -and !(Test-Path $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null } | ||
| $bitmap.Save($OutputPath, [System.Drawing.Imaging.ImageFormat]::Png) | ||
| $bitmap.Dispose() | ||
|
|
||
| Write-Output "Screenshot saved: $OutputPath" | ||
|
|
||
| # Restore previous window | ||
| [WinAPI]::SetForegroundWindow($previousWindow) | Out-Null | ||
|
|
||
| # Schedule auto-delete | ||
| if ($AutoDelete -gt 0) { | ||
| Start-Job -ScriptBlock { | ||
| param($path, $delay) | ||
| Start-Sleep -Seconds $delay | ||
| if (Test-Path $path) { Remove-Item $path -Force } | ||
| } -ArgumentList $OutputPath, $AutoDelete | Out-Null | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥 The Roast:
Start-Jobfires a background PowerShell job inside a script that's invoked as a one-shot subprocess from Python. When this script exits, the background job dies with it — it's not a persistent daemon, it's a fire-and-forget that gets immediately forgotten by the OS. Your 5-minute auto-delete should be called the "optimistic cleanup feature" because it will never run unless the caller holds the PS session open.🩹 The Fix: Either use a scheduled task (
Register-ScheduledJob/Register-ScheduledTask) for deferred cleanup, or just let the Python caller handle deletion after it's done consuming the file. PSStart-Jobonly works reliably in interactive sessions, not in-Commandone-shots.📏 Severity: warning