sots-re/verify/harness/l2-ui/ui.ps1

62 lines
4.1 KiB
PowerShell

param([int]$TargetPid = 0, [string]$Cmd = 'C:\SOTS\l2\cmd.txt')
if ($TargetPid -eq 0 -and (Test-Path C:\SOTS\l2\pid.txt)) { $TargetPid = [int]((Get-Content C:\SOTS\l2\pid.txt -Raw).Trim()) }
Start-Transcript -Path C:\SOTS\l2\uilog.txt -Force | Out-Null
Add-Type -AssemblyName System.Windows.Forms
Add-Type @"
using System; using System.Runtime.InteropServices;
public struct RECT { public int L,T,R,B; }
public class L2 {
[DllImport("user32.dll")] public static extern bool SetCursorPos(int x,int y);
[DllImport("user32.dll")] public static extern void mouse_event(uint f,uint x,uint y,uint d,UIntPtr e);
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")] public static extern int GetWindowTextW(IntPtr h, System.Text.StringBuilder s, int n);
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r);
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr h);
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr h, int n);
[DllImport("user32.dll")] public static extern bool BringWindowToTop(IntPtr h);
}
"@
$out = @()
$hw = [IntPtr]::Zero
if ($TargetPid -lt 0) { $hw = [IntPtr]::Zero } elseif ($TargetPid -gt 0) {
$p = Get-Process -Id $TargetPid -ErrorAction SilentlyContinue
if ($p) { $hw = $p.MainWindowHandle }
}
if ($hw -eq [IntPtr]::Zero) { $out += "NO WINDOW for pid $TargetPid" }
else {
$r = New-Object RECT; [L2]::GetWindowRect($hw,[ref]$r) | Out-Null
$out += "pid=$TargetPid hwnd=$hw rect=$($r.L),$($r.T)-$($r.R),$($r.B)"
[L2]::ShowWindow($hw,9)|Out-Null; [L2]::BringWindowToTop($hw)|Out-Null; [L2]::SetForegroundWindow($hw)|Out-Null
Start-Sleep -m 900
}
foreach ($line in Get-Content $Cmd) {
if ($line.Trim() -eq '' -or $line.StartsWith('#')) { continue }
$a = $line -split ' ',4
switch ($a[0]) {
'fg' { [L2]::ShowWindow($hw,9)|Out-Null; [L2]::BringWindowToTop($hw)|Out-Null; [L2]::SetForegroundWindow($hw)|Out-Null; Start-Sleep -m 900 }
'move' { $mx=[int]$a[1]; $my=[int]$a[2]
[L2]::SetCursorPos($mx-3,$my-3); Start-Sleep -m 150
# real relative motion so DirectInput-style polling sees a move
[L2]::mouse_event(1,[uint32]1,[uint32]1,0,[UIntPtr]::Zero); Start-Sleep -m 120
[L2]::SetCursorPos($mx,$my); Start-Sleep -m 150
[L2]::mouse_event(1,[uint32]1,[uint32]0,0,[UIntPtr]::Zero); Start-Sleep -m 120
[L2]::mouse_event(1,[uint32]4294967295,[uint32]0,0,[UIntPtr]::Zero); Start-Sleep -m 400 }
'click' { [L2]::SetCursorPos([int]$a[1]-2,[int]$a[2]); Start-Sleep -m 200;
[L2]::mouse_event(1,[uint32]2,[uint32]0,0,[UIntPtr]::Zero); Start-Sleep -m 200;
[L2]::SetCursorPos([int]$a[1],[int]$a[2]); Start-Sleep -m 500; [L2]::mouse_event(2,0,0,0,[UIntPtr]::Zero); Start-Sleep -m 300; [L2]::mouse_event(4,0,0,0,[UIntPtr]::Zero); Start-Sleep -m 900 }
'rclick' { [L2]::SetCursorPos([int]$a[1],[int]$a[2]); Start-Sleep -m 700; [L2]::mouse_event(8,0,0,0,[UIntPtr]::Zero); Start-Sleep -m 300; [L2]::mouse_event(16,0,0,0,[UIntPtr]::Zero); Start-Sleep -m 900 }
'dbl' { [L2]::SetCursorPos([int]$a[1],[int]$a[2]); Start-Sleep -m 700; for($i=0;$i -lt 2;$i++){[L2]::mouse_event(2,0,0,0,[UIntPtr]::Zero);Start-Sleep -m 60;[L2]::mouse_event(4,0,0,0,[UIntPtr]::Zero);Start-Sleep -m 60}; Start-Sleep -m 900 }
# wheel <x> <y> <notches, +in/-out>
'wheel' { [L2]::SetCursorPos([int]$a[1],[int]$a[2]); Start-Sleep -m 400
$n=[int]$a[3]; $d = if($n -ge 0){120}else{-120}; $c=[Math]::Abs($n)
for($i=0;$i -lt $c;$i++){ [L2]::mouse_event(0x0800,0,0,[uint32]$d,[UIntPtr]::Zero); Start-Sleep -m 120 }
Start-Sleep -m 700 }
'key' { [System.Windows.Forms.SendKeys]::SendWait($a[1]); Start-Sleep -m 500 }
'type' { [System.Windows.Forms.SendKeys]::SendWait(($line.Substring(5))); Start-Sleep -m 500 }
'sleep' { Start-Sleep -m ([int]$a[1]) }
}
}
$sb = New-Object System.Text.StringBuilder 256; [L2]::GetWindowTextW([L2]::GetForegroundWindow(),$sb,256)|Out-Null
$out += "done $(Get-Date -Format HH:mm:ss) fg='$($sb.ToString())'"
$out
Stop-Transcript | Out-Null