diff options
| author | NotRexed <therealnothingfn@gmail.com> | 2026-11-30 17:11:06 -0500 |
|---|---|---|
| committer | NotRexed <therealnothingfn@gmail.com> | 2026-11-30 17:11:06 -0500 |
| commit | b1f91c2f938fa9f4e082d612f412a0c57a8e3b87 (patch) | |
| tree | a11f74f14cb748725dab211a358da053428d6309 /scripts | |
| parent | 2c4f423ee565c73a3f9eb3f88870ba774ccb289a (diff) | |
| parent | 407f71a237de5293d1d373620d2173943ee1a499 (diff) | |
Merge remote-tracking branch 'origin/main' into push/main-1130
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/game-gate.ps1 | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/scripts/game-gate.ps1 b/scripts/game-gate.ps1 new file mode 100644 index 000000000..ad02a6c3b --- /dev/null +++ b/scripts/game-gate.ps1 | |||
| @@ -0,0 +1,215 @@ | |||
| 1 | # The in-game gate for the D3D12 runtime (leonida.exe): one run, measured the same way on every machine. | ||
| 2 | # | ||
| 3 | # pwsh -NoProfile -File scripts\game-gate.ps1 run -Game <dir> -Data <dir> -DataTemplate <dir> -Name <label> [options] | ||
| 4 | # pwsh -NoProfile -File scripts\game-gate.ps1 start <same options> # the same, detached: returns at once, prints the run folder | ||
| 5 | # pwsh -NoProfile -File scripts\game-gate.ps1 stop -RunDir <run folder> # asks a run to stop (a STOP file) | ||
| 6 | # | ||
| 7 | # It starts the game through this checkout's scripts\play-d3d12.ps1, so a run is what players run, and writes one | ||
| 8 | # folder per run under -RunRoot (default: <Data>\gate-runs): run.json, launch.ps1, settings.txt (the play script's | ||
| 9 | # -DryRun), leonida-settings.txt (the F1 menu file, when there is one), launcher.txt (the precompile lines), | ||
| 10 | # leonida.log (the runtime's log), events.jsonl (what this script did and saw), data\ (a copy of -DataTemplate, so | ||
| 11 | # the tester's own saves are never touched) and summary.json (tools\game_gate.py, schema in docs\game_gate.md). | ||
| 12 | # | ||
| 13 | # It stops the game with WM_SYSCOMMAND/SC_CLOSE (the runtime's own end: the title suspended, the pipeline cache | ||
| 14 | # saved) when -WorldSeconds were spent in the world, when a STOP file appears in the run folder, or after | ||
| 15 | # -MaxMinutes; it kills the game only if it has not ended 60 s later. Exit code: 0 PASS, 1 FAIL, 2 ERROR. | ||
| 16 | # Options: -WorldSeconds (300), -MaxMinutes (45), -More 'LEONIDA_X=1;LEONIDA_Y=2', -PlayArgs '-SeriesS -NoFrameGen'. | ||
| 17 | param( | ||
| 18 | [Parameter(Mandatory, Position = 0)][ValidateSet('run', 'start', 'stop')][string]$Verb, | ||
| 19 | [string]$Game = '', | ||
| 20 | [string]$Data = '', | ||
| 21 | [string]$DataTemplate = '', | ||
| 22 | [string]$Name = '', | ||
| 23 | [string]$RunRoot = '', | ||
| 24 | [string]$RunDir = '', | ||
| 25 | [int]$WorldSeconds = 300, | ||
| 26 | [int]$MaxMinutes = 45, | ||
| 27 | [string]$More = '', | ||
| 28 | [string]$PlayArgs = '', | ||
| 29 | [string]$Python = '' | ||
| 30 | ) | ||
| 31 | $ErrorActionPreference = 'Stop' | ||
| 32 | $repo = Split-Path -Parent $PSScriptRoot | ||
| 33 | $tool = Join-Path $repo 'tools\game_gate.py' | ||
| 34 | $play = Join-Path $repo 'scripts\play-d3d12.ps1' | ||
| 35 | $exe = Join-Path $repo 'd3d12\target\diag\leonida.exe' | ||
| 36 | $menuFile = Join-Path $repo 'd3d12\leonida-settings.txt' | ||
| 37 | if (-not $Python) { $Python = (Get-Command python -ErrorAction Stop).Source } | ||
| 38 | |||
| 39 | if ($Verb -eq 'stop') { | ||
| 40 | if (-not $RunDir -or -not (Test-Path (Join-Path $RunDir 'run.json'))) { throw "not a run folder: '$RunDir'" } | ||
| 41 | Set-Content (Join-Path $RunDir 'STOP') 'stop' -NoNewline | ||
| 42 | "asked $RunDir to stop" | ||
| 43 | exit 0 | ||
| 44 | } | ||
| 45 | |||
| 46 | foreach ($p in 'Game', 'Data', 'DataTemplate', 'Name') { | ||
| 47 | if (-not (Get-Variable $p -ValueOnly)) { throw "-$p is needed" } | ||
| 48 | } | ||
| 49 | $Game = (Resolve-Path $Game).Path | ||
| 50 | $Data = (Resolve-Path $Data).Path | ||
| 51 | $DataTemplate = (Resolve-Path $DataTemplate).Path | ||
| 52 | if (-not $RunRoot) { $RunRoot = Join-Path $Data 'gate-runs' } | ||
| 53 | New-Item -ItemType Directory -Force $RunRoot | Out-Null | ||
| 54 | $RunRoot = (Resolve-Path $RunRoot).Path | ||
| 55 | # A run writes gigabytes of logs and a copy of the saves: never into a checkout. | ||
| 56 | if (($RunRoot + '\').StartsWith($repo + '\', [StringComparison]::OrdinalIgnoreCase)) { throw "-RunRoot is inside the checkout: $RunRoot" } | ||
| 57 | if (-not (Test-Path $exe)) { throw "no ${exe}: build it first (d3d12\scripts\build-nvidia.ps1), the play script would build it inside the run" } | ||
| 58 | if (-not $RunDir) { $RunDir = Join-Path $RunRoot ('{0}-{1}' -f (Get-Date -Format 'yyyyMMdd-HHmmss'), ($Name -replace '[^\w.-]', '_')) } | ||
| 59 | |||
| 60 | function Get-RunningGame { | ||
| 61 | Get-CimInstance Win32_Process -Filter "Name='leonida.exe' OR Name='leonida_sm.exe'" | | ||
| 62 | Where-Object { $_.CommandLine -match '\srun\s' } | Select-Object -First 1 | ||
| 63 | } | ||
| 64 | |||
| 65 | if ($Verb -eq 'start') { | ||
| 66 | if (Get-RunningGame) { throw 'a leonida run is already running' } | ||
| 67 | $pwsh = (Get-Process -Id $PID).Path | ||
| 68 | $quote = { param($s) '"' + ($s -replace '"', '\"') + '"' } | ||
| 69 | $line = @(& $quote $pwsh; '-NoProfile'; '-ExecutionPolicy'; 'Bypass'; '-File'; & $quote $PSCommandPath; 'run' | ||
| 70 | '-Game'; & $quote $Game; '-Data'; & $quote $Data; '-DataTemplate'; & $quote $DataTemplate; '-Name'; & $quote $Name | ||
| 71 | '-RunRoot'; & $quote $RunRoot; '-RunDir'; & $quote $RunDir; '-WorldSeconds'; $WorldSeconds; '-MaxMinutes'; $MaxMinutes | ||
| 72 | '-More'; & $quote $More; '-PlayArgs'; & $quote $PlayArgs; '-Python'; & $quote $Python) -join ' ' | ||
| 73 | # Created by Windows, not as a child of this shell: an agent's or a console's end does not end the run. | ||
| 74 | $startup = New-CimInstance -ClassName Win32_ProcessStartup -ClientOnly -Property @{ ShowWindow = [uint16]0 } | ||
| 75 | $made = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{ | ||
| 76 | CommandLine = $line; CurrentDirectory = $repo; ProcessStartupInformation = $startup } | ||
| 77 | if ($made.ReturnValue -ne 0) { throw "starting the run failed ($($made.ReturnValue))" } | ||
| 78 | $RunDir | ||
| 79 | exit 0 | ||
| 80 | } | ||
| 81 | |||
| 82 | # ---- run: the supervisor, in the foreground. | ||
| 83 | if (Get-RunningGame) { throw 'a leonida run is already running' } | ||
| 84 | New-Item -ItemType Directory -Force $RunDir | Out-Null | ||
| 85 | # A detached run has no console: what this script says, and any error, goes to supervisor.log. | ||
| 86 | Start-Transcript -Path (Join-Path $RunDir 'supervisor.log') -Append | Out-Null | ||
| 87 | $events = Join-Path $RunDir 'events.jsonl' | ||
| 88 | function Add-Event([string]$event, [hashtable]$fields = @{}) { | ||
| 89 | $e = [ordered]@{ t = (Get-Date).ToUniversalTime().ToString('o'); event = $event } | ||
| 90 | foreach ($k in $fields.Keys) { $e[$k] = $fields[$k] } | ||
| 91 | Add-Content $events ($e | ConvertTo-Json -Compress) | ||
| 92 | } | ||
| 93 | |||
| 94 | # The tester's saves, copied: the run plays from its own data folder (-More wins over the play script's own). | ||
| 95 | Copy-Item -Path (Join-Path $DataTemplate '*') -Destination (New-Item -ItemType Directory -Force (Join-Path $RunDir 'data')).FullName -Recurse | ||
| 96 | $hadMenuFile = Test-Path $menuFile | ||
| 97 | if ($hadMenuFile) { Copy-Item $menuFile (Join-Path $RunDir 'leonida-settings.txt') } | ||
| 98 | |||
| 99 | $settings = [ordered]@{ | ||
| 100 | LEONIDA_DATA_DIR = (Join-Path $RunDir 'data') | ||
| 101 | LEONIDA_LOG = 'info,leonida_gpu::frame=debug' | ||
| 102 | } | ||
| 103 | foreach ($s in ($More -split ';' | Where-Object { $_ -match '=' })) { $k, $v = $s -split '=', 2; $settings[$k.Trim()] = $v.Trim() } | ||
| 104 | $moreText = ($settings.GetEnumerator() | ForEach-Object { " {0} = '{1}'" -f $_.Key, ($_.Value -replace "'", "''") }) -join "`n" | ||
| 105 | $launch = Join-Path $RunDir 'launch.ps1' | ||
| 106 | Set-Content $launch @" | ||
| 107 | # Written by scripts\game-gate.ps1: how this run started the game. | ||
| 108 | `$env:NO_COLOR = '1' | ||
| 109 | Set-Location '$repo' | ||
| 110 | `$settings = @{ | ||
| 111 | $moreText | ||
| 112 | } | ||
| 113 | & '$play' -Game '$Game' -Data '$Data' -More `$settings $PlayArgs @args | ||
| 114 | "@ | ||
| 115 | |||
| 116 | $gpu = Get-CimInstance Win32_VideoController | Sort-Object { [uint64]$_.AdapterRAM } -Descending | Select-Object -First 1 | ||
| 117 | $builtWith = Join-Path $repo 'd3d12\target\diag\built-with.txt' | ||
| 118 | [ordered]@{ | ||
| 119 | name = $Name | ||
| 120 | started_utc = (Get-Date).ToUniversalTime().ToString('o') | ||
| 121 | commit = (git -C $repo rev-parse --short HEAD) | ||
| 122 | dirty = [bool](git -C $repo status --porcelain --untracked-files=no) | ||
| 123 | untracked = @(git -C $repo status --porcelain --untracked-files=normal | Where-Object { $_.StartsWith('??') }).Count | ||
| 124 | exe_sha16 = (Get-FileHash $exe -Algorithm SHA256).Hash.Substring(0, 16).ToLower() | ||
| 125 | built_with = if (Test-Path $builtWith) { (Get-Content $builtWith -Raw).Trim() } else { $null } | ||
| 126 | gpu = $gpu.Name | ||
| 127 | driver = $gpu.DriverVersion | ||
| 128 | play_args = $PlayArgs | ||
| 129 | world_seconds = $WorldSeconds | ||
| 130 | max_minutes = $MaxMinutes | ||
| 131 | f1_settings_file = $hadMenuFile | ||
| 132 | } | ConvertTo-Json | Set-Content (Join-Path $RunDir 'run.json') | ||
| 133 | |||
| 134 | & pwsh -NoProfile -ExecutionPolicy Bypass -File $launch -DryRun *> (Join-Path $RunDir 'settings.txt') | ||
| 135 | $launcher = Start-Process pwsh -ArgumentList @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $launch) -WindowStyle Hidden -PassThru ` | ||
| 136 | -RedirectStandardOutput (Join-Path $RunDir 'launcher.txt') -RedirectStandardError (Join-Path $RunDir 'leonida.log') | ||
| 137 | Add-Event 'started' @{ launcher = $launcher.Id } | ||
| 138 | |||
| 139 | if (-not ('GateWindow' -as [type])) { | ||
| 140 | Add-Type -TypeDefinition @' | ||
| 141 | using System; | ||
| 142 | using System.Runtime.InteropServices; | ||
| 143 | public static class GateWindow { | ||
| 144 | delegate bool EnumProc(IntPtr h, IntPtr l); | ||
| 145 | [StructLayout(LayoutKind.Sequential)] public struct RECT { public int L, T, R, B; } | ||
| 146 | [DllImport("user32.dll")] static extern bool EnumWindows(EnumProc cb, IntPtr l); | ||
| 147 | [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid); | ||
| 148 | [DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr h); | ||
| 149 | [DllImport("user32.dll")] static extern bool GetClientRect(IntPtr h, out RECT r); | ||
| 150 | [DllImport("user32.dll")] static extern bool PostMessage(IntPtr h, uint msg, IntPtr w, IntPtr l); | ||
| 151 | // WM_SYSCOMMAND/SC_CLOSE to the process's largest visible window: the runtime's end_title path. (A plain | ||
| 152 | // WM_CLOSE goes to the title's own window procedure, which does not end the run.) | ||
| 153 | public static bool Close(int pid) { | ||
| 154 | IntPtr best = IntPtr.Zero; long area = 0; | ||
| 155 | EnumWindows((h, l) => { | ||
| 156 | uint p; GetWindowThreadProcessId(h, out p); | ||
| 157 | RECT r; | ||
| 158 | if (p == (uint)pid && IsWindowVisible(h) && GetClientRect(h, out r)) { | ||
| 159 | long a = (long)(r.R - r.L) * (r.B - r.T); | ||
| 160 | if (a > area) { area = a; best = h; } | ||
| 161 | } | ||
| 162 | return true; | ||
| 163 | }, IntPtr.Zero); | ||
| 164 | return best != IntPtr.Zero && PostMessage(best, 0x0112, new IntPtr(0xF060), IntPtr.Zero); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | '@ | ||
| 168 | } | ||
| 169 | |||
| 170 | $began = Get-Date | ||
| 171 | $title = $null # the game process ($Game, the same name in any case, is its folder) | ||
| 172 | $stopReason = $null | ||
| 173 | try { | ||
| 174 | while ($true) { | ||
| 175 | Start-Sleep -Seconds 5 | ||
| 176 | if (-not $title) { | ||
| 177 | $found = Get-RunningGame | ||
| 178 | if ($found) { | ||
| 179 | $title = Get-Process -Id $found.ProcessId | ||
| 180 | $null = $title.Handle # held now, so that the exit code can be read after the process ends | ||
| 181 | Add-Event 'game' @{ pid = $found.ProcessId; image = $found.Name } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | if ($title -and $title.HasExited) { break } | ||
| 185 | if (-not $title -and $launcher.HasExited) { break } | ||
| 186 | $state = & $Python -I $tool state $RunDir | ConvertFrom-Json | ||
| 187 | if ($state.relaunched) { $stopReason = 'relaunched' } | ||
| 188 | elseif (Test-Path (Join-Path $RunDir 'STOP')) { $stopReason = 'stop_file' } | ||
| 189 | elseif ($state.world_s -ge $WorldSeconds) { $stopReason = 'target' } | ||
| 190 | elseif (((Get-Date) - $began).TotalMinutes -ge $MaxMinutes) { $stopReason = 'timeout' } | ||
| 191 | if ($stopReason) { break } | ||
| 192 | } | ||
| 193 | |||
| 194 | if ($stopReason -and $title -and -not $title.HasExited) { | ||
| 195 | Add-Event 'stop_requested' @{ reason = $stopReason } | ||
| 196 | if ([GateWindow]::Close($title.Id)) { Add-Event 'close_sent' } | ||
| 197 | if (-not $title.WaitForExit(60000)) { | ||
| 198 | Stop-Process -Id $title.Id -Force | ||
| 199 | Add-Event 'killed' | ||
| 200 | $title.WaitForExit() | ||
| 201 | } | ||
| 202 | } elseif ($stopReason) { | ||
| 203 | Add-Event 'stop_requested' @{ reason = $stopReason } | ||
| 204 | } | ||
| 205 | if ($title) { Add-Event 'game_exited' @{ exit_code = $title.ExitCode } } | ||
| 206 | if (-not $launcher.WaitForExit(30000)) { Stop-Process -Id $launcher.Id -Force } | ||
| 207 | } catch { | ||
| 208 | Add-Event 'error' @{ message = $_.Exception.Message; line = $_.InvocationInfo.ScriptLineNumber } | ||
| 209 | Write-Host "gate error: $($_.Exception.Message) (line $($_.InvocationInfo.ScriptLineNumber))" | ||
| 210 | } finally { | ||
| 211 | & $Python -I $tool summary $RunDir | ||
| 212 | $code = $LASTEXITCODE | ||
| 213 | Stop-Transcript | Out-Null | ||
| 214 | } | ||
| 215 | exit $code | ||