upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/scripts/windows-boot/boot.ps1
blob: 4681e741e0158d9935e3c670430aaa89422d4de0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
<#
.SYNOPSIS
    Boot the retail executable under gta6rt on native Windows, one game process at a time, and print a digest.

.DESCRIPTION
    The native counterpart of scripts/linux-boot/: sets the switches both machines boot with (see
    docs/re/howto-blank-to-story-load.md, section 1), runs `gta6rt --run` with --trace and --trace-d3d12 for
    -Seconds, stops it, runs tools/boot_trace.py and prints a short digest: exit, game/session state over time,
    PresentX count, device-fault lines, capture means. Paste numbers from the digest into a RESULT on the
    coordination board, never the logs: they can quote the game (AGENTS.md, "No game data").

    Only one game process runs at a time: the script holds a named mutex and also waits while any gta6rt.exe is
    running (another agent's boot, or one started by hand).

    Every default switch is used only when the variable is not already set in this shell; other LEONIDA_*
    variables of the shell pass through; -SetEnv overrides all of them. -DryRun lists the result. Outputs go to -OutDir (never inside the repository):
        <OutDir>\<Name>.log          stderr of gta6rt: [rt] diagnostics and [d3d12-trace] lines
        <OutDir>\<Name>.stdout.log   stdout of gta6rt
        <OutDir>\<Name>.jsonl        startup trace (--trace)
        <OutDir>\<Name>.summary.txt  tools/boot_trace.py report
        <OutDir>\<Name>.digest.txt   the digest printed at the end
        <OutDir>\cap_<Name>\         swapchain captures (PPM) and index.txt with mean RGB per frame

.PARAMETER Name
    Run name; output files are named after it. Default: boot-<yyyyMMdd-HHmmss>.

.PARAMETER Seconds
    How long the game runs before it is stopped. Default 180.

.PARAMETER GameDir
    Your package folder (MicrosoftGame.Config and the .rpf archives as plain files). Default: $env:LEONIDA_GAME_DIR,
    else game_path.txt beside gta6rt.exe (written by make_game.py's runtime stage).

.PARAMETER Exe
    game_scarlett_master.exe. Default: $env:LEONIDA_EXE, else <GameDir>\game_scarlett_master.exe.

.PARAMETER Runtime
    gta6rt.exe (gta6rt_tls_host.dll must sit beside it). Default: $env:GTA6RT, else
    <repo>\runtime\build\RelWithDebInfo\gta6rt.exe.

.PARAMETER Shaders
    The translated library, <work>\shaders\spirv. Default: $env:LEONIDA_SHADERS; when neither is given the
    runtime reads shader_path.txt beside gta6rt.exe.

.PARAMETER OutDir
    Folder for logs, traces and captures. Default: $env:LEONIDA_RUNS, else $HOME\leonida-runs.

.PARAMETER Overlay
    Writable folder for everything the title writes (saves, caches). Default <OutDir>\overlay.

.PARAMETER InputScript
    LEONIDA_INPUT_SCRIPT: timed pad presses, seconds since the pad's first poll (button@seconds, comma-separated;
    buttons a b x y start back lb rb up down left right). Each press is held 300 ms; button@seconds:hold holds it
    longer, e.g. b@1100:3 for a title that reads its pad only every few seconds. Sets LEONIDA_KEYBOARD_PAD=always. Builds older than
    main @ 8146ef690 ignore a script longer than 63 characters. Default 'a@45,a@60,a@75,a@90'.

.PARAMETER NoInput
    No input script (LEONIDA_KEYBOARD_PAD then stays as this shell has it: LEONIDA_KEYBOARD_PAD=1 to play by
    hand with the window focused).

.PARAMETER Stacks
    LEONIDA_STACKS: whole seconds at which every thread's call chain is printed to the log, e.g. '30,60'.

.PARAMETER SetEnv
    Extra or overriding environment variables as a hashtable, when called from a PowerShell prompt:
    & .\scripts\windows-boot\boot.ps1 -SetEnv @{ LEONIDA_PEEK_SECONDS = '2' }. A value of '' removes the variable.

.PARAMETER EnvPairs
    Any further arguments, each KEY=VALUE, as scripts/linux-boot/run-boot.sh takes them. These work with
    powershell -File too:  ... -Name x LEONIDA_PRESENT_PLANE1=0 "LEONIDA_PEEK=<rva>,<rva>".
    Applied after -SetEnv. KEY= removes the variable for this boot.

.PARAMETER NoCapture
    Do not set LEONIDA_CAPTURE.

.PARAMETER ExtraArgs
    More gta6rt arguments, appended after the standard ones.

.PARAMETER FirstRunUi
    Launch with the offline first-run UI profile instead of the generic runtime profile.

.PARAMETER Python
    Python 3.12+ used for tools/boot_trace.py. Default: python, else py -3, the first that starts and is 3.12+.

.PARAMETER SummaryLines
    How many lines of the boot_trace.py report to print. Default 40; 0 prints none.

.PARAMETER LockWaitSeconds
    How long to wait for another boot to finish. Default 3600.

.PARAMETER DryRun
    Print the environment and the command line, check the paths, launch nothing.

.PARAMETER CrashDump
    Run under the native Windows crash monitor. A crash dump and stack go into a fresh private folder under -OutDir.

.PARAMETER DigestOnly
    Launch nothing; write the summary and digest again from an earlier run's files (-Name, -OutDir).

.EXAMPLE
    powershell -NoProfile -File scripts\windows-boot\boot.ps1 -DryRun -GameDir D:\GTA6pkg -Shaders E:\leonida-work\shaders\spirv

.EXAMPLE
    powershell -NoProfile -File scripts\windows-boot\boot.ps1 -Name first -Seconds 200 -GameDir D:\GTA6pkg -Shaders E:\leonida-work\shaders\spirv

.EXAMPLE
    powershell -NoProfile -File scripts\windows-boot\boot.ps1 -Name stall -Seconds 90 -Stacks 40,80 "LEONIDA_PEEK=<rva>,<rva>"

.EXAMPLE
    powershell -NoProfile -File scripts\windows-boot\boot.ps1 -DigestOnly -Name first
#>
[CmdletBinding(PositionalBinding = $false)]
param(
    [string]$Name = "",
    [int]$Seconds = 180,
    [string]$GameDir = "",
    [string]$Exe = "",
    [string]$Runtime = "",
    [string]$Shaders = "",
    [string]$OutDir = "",
    [string]$Overlay = "",
    [string]$InputScript = "a@45,a@60,a@75,a@90",
    [string]$Stacks = "",
    [hashtable]$SetEnv = @{},
    [switch]$NoCapture,
    [switch]$CrashDump,
    [switch]$NoInput,
    [string[]]$ExtraArgs = @(),
    [switch]$FirstRunUi,
    [string]$Python = "",
    [int]$SummaryLines = 40,
    [int]$LockWaitSeconds = 3600,
    [switch]$DryRun,
    [switch]$DigestOnly,
    [Parameter(ValueFromRemainingArguments = $true)]
    [string[]]$EnvPairs = @()
)

Set-StrictMode -Version 2.0
$ErrorActionPreference = "Stop"

$RepoRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "..\.."))
$ExeName = "game_scarlett_master.exe"

function Get-FirstValue {
    param([object[]]$Candidates)
    foreach ($c in $Candidates) { if ($c -and ([string]$c).Trim()) { return ([string]$c).Trim() } }
    return ""
}

function Get-BootPath {
    param([string]$Path)
    if ($Path) { return $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) }
    return ""
}

function Read-SideFile {
    # One line from a text file make_game.py writes beside gta6rt.exe (game_path.txt, shader_path.txt).
    param([string]$Path)
    if (Test-Path -LiteralPath $Path -PathType Leaf) {
        $line = Get-Content -LiteralPath $Path -TotalCount 1
        if ($line) { return ([string]$line).Trim() }
    }
    return ""
}

function ConvertTo-ProcessArgument {
    # Quote one argument the way the MSVC CRT splits a command line (backslashes before a quote are doubled).
    param([string]$Value)
    if ($Value -ne "" -and $Value -notmatch '[\s"]') { return $Value }
    $sb = New-Object System.Text.StringBuilder
    [void]$sb.Append('"')
    $slashes = 0
    foreach ($ch in $Value.ToCharArray()) {
        if ($ch -eq '\') { $slashes++; continue }
        if ($ch -eq '"') { [void]$sb.Append('\', 2 * $slashes + 1) } elseif ($slashes) { [void]$sb.Append('\', $slashes) }
        $slashes = 0
        [void]$sb.Append($ch)
    }
    if ($slashes) { [void]$sb.Append('\', 2 * $slashes) }
    [void]$sb.Append('"')
    return $sb.ToString()
}

function Test-InsideFolder {
    param([string]$Path, [string]$Folder)
    $p = [System.IO.Path]::GetFullPath($Path).TrimEnd('\') + '\'
    $f = [System.IO.Path]::GetFullPath($Folder).TrimEnd('\') + '\'
    return $p.StartsWith($f, [System.StringComparison]::OrdinalIgnoreCase)
}

function Test-Python {
    # Get-Command also finds the Microsoft Store's python.exe alias in WindowsApps, which only says how to install
    # Python and exits 9009, and the repository needs 3.12+ (README.md): take a candidate that starts and is 3.12+,
    # within 15 s, so that one that hangs cannot hold the script after the game has stopped.
    param([string[]]$Command)
    $ErrorActionPreference = "Continue"
    $exe = Get-Command $Command[0] -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1
    if (-not $exe) { return $false }
    $arguments = @($Command | Select-Object -Skip 1) + @("-I", "-c", '"import sys; sys.exit(sys.version_info < (3, 12))"')
    try { $p = Start-Process -FilePath $exe.Source -ArgumentList $arguments -WindowStyle Hidden -PassThru } catch { return $false }
    $null = $p.Handle  # Windows PowerShell 5.1 reports no ExitCode unless the handle was opened while it ran
    if (-not $p.WaitForExit(15000)) {
        & taskkill.exe /T /F /PID $p.Id 2>&1 | Out-Null
        return $false
    }
    return ($p.ExitCode -eq 0)
}

function Resolve-Python {
    param([string]$Requested)
    if ($Requested) { return @($Requested) }
    foreach ($candidate in @(@("python"), @("py", "-3"))) {
        if (Test-Python $candidate) { return $candidate }
    }
    return @()
}

# ------------------------------------------------------------------------------------------------ paths

if (-not $Name) { $Name = "boot-" + (Get-Date -Format "yyyyMMdd-HHmmss") }
if ($NoInput) { $InputScript = "" }
if ($Name -notmatch '^[A-Za-z0-9._-]+$') { throw "-Name may hold letters, digits, '.', '_' and '-' only: $Name" }

$Runtime = Get-BootPath (Get-FirstValue @($Runtime, $env:GTA6RT, (Join-Path $RepoRoot "runtime\build\RelWithDebInfo\gta6rt.exe")))
$RuntimeDir = Split-Path -Parent $Runtime
$GameDir = Get-BootPath (Get-FirstValue @($GameDir, $env:LEONIDA_GAME_DIR, (Read-SideFile (Join-Path $RuntimeDir "game_path.txt"))))
$Exe = Get-BootPath (Get-FirstValue @($Exe, $env:LEONIDA_EXE, $(if ($GameDir) { Join-Path $GameDir $ExeName } else { "" })))
$Shaders = Get-BootPath (Get-FirstValue @($Shaders, $env:LEONIDA_SHADERS))
$OutDir = Get-BootPath (Get-FirstValue @($OutDir, $env:LEONIDA_RUNS, (Join-Path $HOME "leonida-runs")))
$Overlay = Get-BootPath (Get-FirstValue @($Overlay, (Join-Path $OutDir "overlay")))
$RunDirectory = Join-Path $OutDir ("work_" + $Name)
$Capture = Join-Path $OutDir ("cap_" + $Name)
$LogFile = Join-Path $OutDir ($Name + ".log")
$StdoutFile = Join-Path $OutDir ($Name + ".stdout.log")
$TraceFile = Join-Path $OutDir ($Name + ".jsonl")
$SummaryFile = Join-Path $OutDir ($Name + ".summary.txt")
$DigestFile = Join-Path $OutDir ($Name + ".digest.txt")

$problems = New-Object System.Collections.Generic.List[string]
if (Test-InsideFolder $OutDir $RepoRoot) { $problems.Add("-OutDir $OutDir is inside the repository; logs and captures must stay outside it") }
if (-not (Test-Path -LiteralPath $Runtime -PathType Leaf)) { $problems.Add("no gta6rt.exe at $Runtime (build it, or pass -Runtime / set GTA6RT)") }
elseif (-not (Test-Path -LiteralPath (Join-Path $RuntimeDir "gta6rt_tls_host.dll") -PathType Leaf)) {
    $problems.Add("gta6rt_tls_host.dll is missing beside $Runtime (build the gta6rt target; without it the title exits at once)")
}
if ($CrashDump) {
    $monitor = Join-Path $RuntimeDir "gta6rt_crash_monitor.exe"
    if (-not (Test-Path -LiteralPath $monitor -PathType Leaf)) {
        $problems.Add("-CrashDump needs the gta6rt_crash_monitor build target beside the runtime")
    }
}
if (-not $GameDir) { $problems.Add("no game folder: pass -GameDir or set LEONIDA_GAME_DIR") }
elseif (-not (Test-Path -LiteralPath (Join-Path $GameDir "MicrosoftGame.Config") -PathType Leaf)) {
    $problems.Add("$GameDir has no MicrosoftGame.Config: -GameDir is the folder with it and the .rpf archives")
}
if (-not $Exe -or -not (Test-Path -LiteralPath $Exe -PathType Leaf)) { $problems.Add("no $ExeName at '$Exe' (pass -Exe)") }
if ($Shaders) {
    if (-not (Test-Path -LiteralPath (Join-Path $Shaders "index.json") -PathType Leaf)) {
        $problems.Add("$Shaders has no index.json: -Shaders is <work>\shaders\spirv after make_game.py --stages check,shaders")
    } elseif (-not (Test-Path -LiteralPath (Join-Path $Shaders "exe\index.json") -PathType Leaf)) {
        Write-Warning "$Shaders has no exe\index.json: the title's 10 embedded shaders are missing and frames cannot draw (JOIN.md, 'The executable trap')"
    }
} elseif (-not (Read-SideFile (Join-Path $RuntimeDir "shader_path.txt"))) {
    $problems.Add("no shader library: pass -Shaders <work>\shaders\spirv or set LEONIDA_SHADERS")
}

# ------------------------------------------------------------------------------------------ environment

# Defaults: used only when this shell has not set the variable. Some are the Linux harness's too (scripts/linux-boot/); it
# does not set LEONIDA_DEVICE_FAULT.
$defaults = [ordered]@{
    LEONIDA_D3D12                     = "vulkan"
    LEONIDA_DEVICE_TYPE               = "6"            # Series S identity; the runtime answers the ConsoleMode registry read with it
    LEONIDA_ARENA_MB                  = "2048"
    LEONIDA_PIPELINE_CACHE_BYTES      = "4294967296"
    LEONIDA_STARTUP_CACHE_CHECKPOINTS = "1"
    LEONIDA_PIPELINE_CACHE_DIR        = (Join-Path $OutDir "pipeline-cache")
    # LEONIDA_PEEK: the game-state and session-state RVAs (measured locally, not kept in the repository) when
    # LEONIDA_GAME_STATE_RVA and LEONIDA_SESSION_STATE_RVA name them; see below.
    LEONIDA_PEEK_SECONDS              = "5"
    # 2: the device-loss report and checkpoints without NVIDIA's diagnostics config. =1 adds that config, which is part
    # of every compute journal file name, so a journal pre-compiled without it would not match. LEONIDA_AFTERMATH=1
    # gets 1 (below).
    LEONIDA_DEVICE_FAULT              = "2"            # device-loss report (VK_EXT_device_fault, checkpoints)
    LEONIDA_CAPTURE_EVERY             = "60"
    LEONIDA_CAPTURE_LIMIT             = "40"
    DISABLE_RTSS_LAYER                = "1"            # third-party Vulkan overlay layers off
    DISABLE_VULKAN_OBS_CAPTURE        = "1"
}
if ($InputScript) { $defaults["LEONIDA_KEYBOARD_PAD"] = "always" }
$gameStateRva = "$env:LEONIDA_GAME_STATE_RVA".ToLowerInvariant() -replace '^0x', ''
$sessionStateRva = "$env:LEONIDA_SESSION_STATE_RVA".ToLowerInvariant() -replace '^0x', ''
if ($gameStateRva -and $sessionStateRva) { $defaults["LEONIDA_PEEK"] = "0x$gameStateRva,0x$sessionStateRva" }
else {
    # The two RVAs were built in until they were taken out of the repository; say so rather than report "?" silently.
    Write-Warning ("LEONIDA_GAME_STATE_RVA and LEONIDA_SESSION_STATE_RVA are not both set: no LEONIDA_PEEK by default, " +
                   "so the RESULT game and session states read '?' (scripts/windows-boot/README.md)")
}
# Per-run values: always this script's (from its parameters), whatever the shell has.
$perRun = [ordered]@{
    LEONIDA_CAPTURE      = $(if ($NoCapture) { "" } else { $Capture })
    LEONIDA_INPUT_SCRIPT = $InputScript
}
if ($Shaders) { $perRun["LEONIDA_SHADERS"] = $Shaders }
if ($Stacks) { $perRun["LEONIDA_STACKS"] = $Stacks }

$bootEnv = [ordered]@{}
$given = @{}   # the variables this shell, -SetEnv or KEY=VALUE set, as opposed to the defaults above
foreach ($k in $defaults.Keys) {
    $cur = [Environment]::GetEnvironmentVariable($k)
    if ($cur) { $bootEnv[$k] = $cur; $given[$k] = $true } else { $bootEnv[$k] = $defaults[$k] }
}
foreach ($k in $perRun.Keys) { $bootEnv[$k] = $perRun[$k] }
# Other LEONIDA_* switches already set in this shell are passed through as they are; they are listed below.
foreach ($item in (Get-ChildItem Env: | Where-Object { $_.Name -like "LEONIDA_*" })) {
    if (-not $bootEnv.Contains($item.Name)) { $bootEnv[$item.Name] = $item.Value }
}
foreach ($k in $SetEnv.Keys) { $bootEnv[[string]$k] = [string]$SetEnv[$k]; $given[[string]$k] = $true }
foreach ($pair in $EnvPairs) {
    if ($pair -notmatch '^([A-Za-z_][A-Za-z0-9_]*)=(.*)$') { throw "not a parameter and not KEY=VALUE: '$pair' (Get-Help $PSCommandPath -Full)" }
    $bootEnv[$Matches[1]] = $Matches[2]; $given[$Matches[1]] = $true
}
# LEONIDA_AFTERMATH=1 wants NVIDIA's diagnostics config (shader debug info in the crash dump). The runtime reads
# LEONIDA_DEVICE_FAULT first and leaves the config out for 2 (d3d12_vulkan_device.cpp), so the default becomes 1 then;
# a value the user gave is kept.
if ($bootEnv["LEONIDA_AFTERMATH"] -eq "1" -and -not $given["LEONIDA_DEVICE_FAULT"]) { $bootEnv["LEONIDA_DEVICE_FAULT"] = "1" }
if ($bootEnv["LEONIDA_CAPTURE"]) { $Capture = $bootEnv["LEONIDA_CAPTURE"] }
# The journal `gta6rt --precompile-pipelines` fills beside gta6rt.exe (when it runs without LEONIDA_PIPELINE_CACHE_DIR)
# is read here as the runtime's read-only seed (LEONIDA_PIPELINE_JOURNAL_SEED_DIR, docs/d3d12_vulkan.md), so the pipelines
# it holds are not compiled again in the boot's own cache folder. Only when this shell or -SetEnv/KEY= did not decide, and
# when the two journals are distinct: the runtime turns its journal off for a seed that is, holds or sits in its writable
# journal (pipeline_cache_journal.h, resolve_roots). A relative or unresolvable cache folder gets no automatic seed.
if (-not $bootEnv.Contains("LEONIDA_PIPELINE_JOURNAL_SEED_DIR")) {
    # Windows' final path of a folder (GetFinalPathNameByHandle) sees through junctions, symbolic links, substituted
    # and mapped drives, short 8.3 names and the \\?\ and \\.\ prefixes, as the runtime's std::filesystem checks do.
    # A folder that does not exist yet is named from its deepest existing parent. Without the helper (a compile error,
    # a restricted session), the comparison falls back to the spelled paths.
    function Get-FinalFolderPath([string]$path) {
        $plain = ($path -replace '^\\\\[?.]\\UNC\\', '\\' -replace '^\\\\[?.]\\', '').TrimEnd('\', '/')
        try {
            if (-not ('LeonidaFinalPath' -as [type])) {
                Add-Type -ErrorAction Stop -TypeDefinition @'
using System; using System.Runtime.InteropServices; using System.Text; using Microsoft.Win32.SafeHandles;
public static class LeonidaFinalPath {
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    static extern SafeFileHandle CreateFileW(string name, uint access, uint share, IntPtr sa, uint disposition, uint flags, IntPtr template);
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    static extern uint GetFinalPathNameByHandleW(SafeFileHandle handle, StringBuilder buffer, uint length, uint flags);
    public static string Of(string path) {
        // No access asked, all sharing, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS (needed to open a folder).
        using (SafeFileHandle h = CreateFileW(path, 0, 7, IntPtr.Zero, 3, 0x02000000, IntPtr.Zero)) {
            if (h.IsInvalid) return null;
            StringBuilder b = new StringBuilder(32768);
            uint n = GetFinalPathNameByHandleW(h, b, (uint)b.Capacity, 0);
            return (n == 0 || n >= b.Capacity) ? null : b.ToString();
        }
    }
}
'@
            }
            $existing = $path.TrimEnd('\', '/'); $tail = @()
            while ($existing -and -not [System.IO.Directory]::Exists($existing)) {
                $tail = @([System.IO.Path]::GetFileName($existing)) + $tail
                $existing = [System.IO.Path]::GetDirectoryName($existing)
            }
            if (-not $existing) { return $plain }
            $final = [LeonidaFinalPath]::Of($existing)
            if (-not $final) { return $plain }
            $final = $final -replace '^\\\\\?\\UNC\\', '\\' -replace '^\\\\\?\\', ''
            foreach ($part in $tail) { $final = [System.IO.Path]::Combine($final, $part) }
            return $final.TrimEnd('\', '/')
        } catch { return $plain }
    }
    $seed = $null
    try {
        $runtimeFull = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($RuntimeDir)
        $precompiled = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($runtimeFull, "pipeline_cache.d")).TrimEnd('\', '/')
        if (Test-Path -LiteralPath $precompiled -PathType Container) {
            $cacheDir = $bootEnv["LEONIDA_PIPELINE_CACHE_DIR"]
            $ownJournal = $null
            if (-not $cacheDir) { $ownJournal = $precompiled }   # the runtime then keeps its journal beside gta6rt.exe
            elseif ($cacheDir -match '^([A-Za-z]:[\\/]|[\\/]{2})') {
                $ownJournal = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($cacheDir, "pipeline_cache.d")).TrimEnd('\', '/')
            }
            if ($ownJournal) {
                $a = Get-FinalFolderPath $ownJournal
                $b = Get-FinalFolderPath $precompiled
                $c = [System.StringComparison]::OrdinalIgnoreCase
                $overlap = $a.Equals($b, $c) -or $a.StartsWith($b + '\', $c) -or $b.StartsWith($a + '\', $c)
                if (-not $overlap) { $seed = $precompiled }
            }
        }
    } catch { $seed = $null }
    if ($seed) { $bootEnv["LEONIDA_PIPELINE_JOURNAL_SEED_DIR"] = $seed }
}

foreach ($key in @("LEONIDA_SHADERS", "LEONIDA_PIPELINE_CACHE_DIR")) {
    if ($bootEnv[$key]) { $bootEnv[$key] = Get-BootPath $bootEnv[$key] }
}
foreach ($key in @("LEONIDA_COMPAT_DIR", "LEONIDA_DSTORAGE_HISTORY", "LEONIDA_SAVE_DIR",
                  "LEONIDA_WRITE_DIR", "LEONIDA_XMEM_BACKING_DIR")) {
    $value = $bootEnv[$key]
    if ($value -and -not [System.IO.Path]::IsPathRooted($value)) {
        $bootEnv[$key] = Get-BootPath (Join-Path $RepoRoot $value)
    }
}
$Capture = Get-BootPath $Capture
if ($bootEnv["LEONIDA_CAPTURE"]) { $bootEnv["LEONIDA_CAPTURE"] = $Capture }

$launchMode = if ($FirstRunUi) { "--first-run-ui" } else { "--run" }
$gameArgs = @($launchMode, $Exe, "--game-dir", $GameDir, "--overlay", $Overlay, "--trace", $TraceFile, "--trace-d3d12") + $ExtraArgs
$argLine = ($gameArgs | ForEach-Object { ConvertTo-ProcessArgument ([string]$_) }) -join " "

if (-not $DigestOnly) {
    Write-Output "[boot] $Name  ${Seconds}s  repo $RepoRoot"
    Write-Output "[boot] gta6rt  $Runtime"
    Write-Output "[boot] out     $OutDir"
    Write-Output "[boot] cwd     $RunDirectory"
    foreach ($k in $bootEnv.Keys) { if ($bootEnv[$k] -ne "") { Write-Output ("[boot] env     {0}={1}" -f $k, $bootEnv[$k]) } }
    Write-Output "[boot] command $(ConvertTo-ProcessArgument $Runtime) $argLine"
}

if ($DigestOnly) {
    if (-not (Test-Path -LiteralPath $LogFile)) { Write-Output "[boot] no $LogFile to digest"; exit 2 }
    $problems.Clear()
}
if ($problems.Count) {
    foreach ($p in $problems) { Write-Output "[boot] problem: $p" }
    if (-not $DryRun) { exit 2 }
}
if ($DryRun) { Write-Output "[boot] dry run: nothing launched"; exit $(if ($problems.Count) { 2 } else { 0 }) }

$verdict = "digest of an earlier run (-DigestOnly)"
$exitCode = 0
if (-not $DigestOnly) {

    # --------------------------------------------------------------------------------------------- the lock

    $mutex = New-Object System.Threading.Mutex($false, "Local\LeonidaGameBoot")
    $owned = $false
    $saved = @{}
    $exitCode = 0
    try {
        $deadline = (Get-Date).AddSeconds($LockWaitSeconds)
        $said = $false
        while (-not $owned) {
            try { $owned = $mutex.WaitOne(5000) } catch [System.Threading.AbandonedMutexException] { $owned = $true }
            if ($owned -and (Get-Process -Name gta6rt -ErrorAction SilentlyContinue)) {
                # A game started by hand or by another script that does not take this lock: wait for it too.
                $mutex.ReleaseMutex(); $owned = $false; Start-Sleep -Seconds 5
            }
            if (-not $owned) {
                if (-not $said) { Write-Output "[boot] waiting: another game process is running (one at a time)"; $said = $true }
                if ((Get-Date) -gt $deadline) { Write-Output "[boot] gave up waiting after $LockWaitSeconds s"; exit 3 }
            }
        }

        foreach ($d in @($OutDir, $Overlay, $RunDirectory)) { New-Item -ItemType Directory -Force -Path $d | Out-Null }
        if ($bootEnv["LEONIDA_PIPELINE_CACHE_DIR"]) {
            New-Item -ItemType Directory -Force -Path $bootEnv["LEONIDA_PIPELINE_CACHE_DIR"] | Out-Null
        }
        if ($bootEnv["LEONIDA_CAPTURE"]) {
            # A capture folder reused by the same -Name: drop the earlier run's frames and index, nothing else.
            New-Item -ItemType Directory -Force -Path $Capture | Out-Null
            Get-ChildItem -LiteralPath $Capture -File | Where-Object { $_.Name -like "frame_*.ppm" -or $_.Name -like "*index.txt" } |
                Remove-Item -Force -Confirm:$false
        }
        foreach ($f in @($LogFile, $StdoutFile, $TraceFile, $SummaryFile, $DigestFile)) {
            if (Test-Path -LiteralPath $f) { Remove-Item -LiteralPath $f -Force -Confirm:$false }
        }

        foreach ($k in $bootEnv.Keys) {
            $saved[$k] = [Environment]::GetEnvironmentVariable($k)
            $v = $bootEnv[$k]; if ($v -eq "") { $v = $null }
            [Environment]::SetEnvironmentVariable($k, $v)
        }

        # ------------------------------------------------------------------------------------------- the boot
        $started = Get-Date
        $launchFile = $Runtime
        $launchArgs = $argLine
        if ($CrashDump) {
            $crashDir = Join-Path $OutDir ("{0}-crash-{1}" -f $Name, [guid]::NewGuid().ToString("N"))
            $runtimePath = (Get-Item -LiteralPath $Runtime).FullName
            $childCommand = "$(ConvertTo-ProcessArgument $runtimePath) $argLine"
            $launchFile = $monitor
            $launchArgs = (@($RepoRoot, $crashDir, $Seconds, $runtimePath, $childCommand) |
                ForEach-Object { ConvertTo-ProcessArgument ([string]$_) }) -join " "
        }
        $proc = Start-Process -FilePath $launchFile -ArgumentList $launchArgs -WorkingDirectory $RunDirectory -NoNewWindow -PassThru `
            -RedirectStandardError $LogFile -RedirectStandardOutput $StdoutFile
        $handle = $proc.Handle   # keeps the exit code readable after the process ends
        $stopped = $false
        if ($CrashDump) {
            # The monitor owns the deadline so a dump already in progress can finish.
            $proc.WaitForExit()
            $stopped = Test-Path -LiteralPath (Join-Path $crashDir "timed-out")
            if (Test-Path -LiteralPath (Join-Path $crashDir "crash.dmp")) {
                Write-Output "[boot] private crash diagnostics: $crashDir"
            } elseif ($proc.ExitCode -ne 0) {
                Write-Warning "No crash dump was saved. See the monitor diagnostics in $LogFile"
            }
        } elseif (-not $proc.WaitForExit($Seconds * 1000)) {
            $stopped = $true
            try { Stop-Process -Id $proc.Id -Force -Confirm:$false } catch { }
            [void]$proc.WaitForExit(20000)
        }
        $ran = [int]((Get-Date) - $started).TotalSeconds
        $rc = $null
        try { $rc = $proc.ExitCode } catch { }
        if ($stopped) { $verdict = "ran its full time limit ($ran s), stopped"; $exitCode = 0 }
        elseif ($rc -eq $null) { $verdict = "exited by itself after $ran s, exit code unknown"; $exitCode = 1 }
        else { $verdict = ("exited by itself after {0} s, exit code {1} (0x{2:x8})" -f $ran, $rc, ([int64]$rc -band 0xffffffffL)); $exitCode = [int]$rc }
    } finally {
        foreach ($k in $saved.Keys) { [Environment]::SetEnvironmentVariable($k, $saved[$k]) }
        if ($owned) { $mutex.ReleaseMutex() }
        $mutex.Dispose()
    }
}

# -------------------------------------------------------------------------------------------- the digest

function Get-FaultContext {
    param([string]$Path)
    $apis = @("vkAcquireNextImageKHR", "vkBeginCommandBuffer", "vkCreateSwapchainKHR", "vkDeviceWaitIdle",
              "vkEndCommandBuffer", "vkGetFenceStatus", "vkGetQueryPoolResults", "vkGetSemaphoreCounterValue",
              "vkQueueBindSparse", "vkQueuePresentKHR", "vkQueueSubmit", "vkQueueWaitIdle", "vkResetFences",
              "vkSignalSemaphore", "vkWaitForFences", "vkWaitSemaphores")
    $api = "not observed"
    $query = "not observed"
    $lost = $false
    [long]$queues = 0
    [long]$reported = 0
    [long]$decoded = 0
    [long]$unrecognized = 0

    # Select-String streams matches, including a report that arrives after a long trace.
    $prefix = '^\s*\[rt\] (device-fault: |vulkan: the device was lost \()'
    Select-String -LiteralPath $Path -CaseSensitive -Pattern $prefix | ForEach-Object {
        $tag = $_.Matches[0]
        $record = $_.Line.Substring($tag.Length).TrimEnd()
        if ($tag.Groups[1].Value -eq "vulkan: the device was lost (") {
            $lost = $true
            if ($api -in @("not observed", "other")) {
                if ($record -cmatch '^(vkWaitSemaphores|vkGetSemaphoreCounterValue) in the fence dispatcher returned VK_ERROR_DEVICE_LOST\)') {
                    $api = $Matches[1]
                } elseif ($record.StartsWith("a fence's counter read UINT64_MAX and vkDeviceWaitIdle then returned VK_ERROR_DEVICE_LOST)")) {
                    $api = "vkDeviceWaitIdle"
                }
            }
        } elseif ($record.StartsWith("VK_ERROR_DEVICE_LOST from ")) {
            $lost = $true
            if ($api -in @("not observed", "other")) {
                $api = "other"
                if ($record -cmatch '^VK_ERROR_DEVICE_LOST from ([^\s(]+)' -and $apis -ccontains $Matches[1]) {
                    $api = $Matches[1]
                }
            }
        } elseif ($record -cmatch '^queue \S+ checkpoints=([0-9]+)$') {
            [uint32]$count = 0
            if ([uint32]::TryParse($Matches[1], [ref]$count)) {
                $queues++
                $reported += $count
            }
        } elseif ($record -cmatch '^stage=0x[0-9a-fA-F]+ submission=[0-9]+ list=[0-9]+ command=[0-9]+ kind=\S+$') {
            $decoded++
        } elseif ($record -cmatch '^stage=0x[0-9a-fA-F]+ marker=\S+ \(unrecognized\)$') {
            $unrecognized++
        } elseif ($query -eq "not observed") {
            if ($record.StartsWith("description:")) {
                $query = if ($record.EndsWith(" (partial report)")) { "partial" } else { "complete" }
            } elseif ($record -cmatch '^vkGetDeviceFaultInfoEXT (counts|data) failed \(-?[0-9]+\)$') {
                $query = "$($Matches[1]) failed"
            } elseif ($record -ceq "fault info collection failed") {
                $query = "collection failed"
            }
        }
    }
    if ($lost -or $query -ne "not observed" -or $queues -or $decoded -or $unrecognized) {
        "fault context: api=$api, query=$query, queues=$queues, reported=$reported, decoded=$decoded, unrecognized=$unrecognized"
    }
}

$py = @(Resolve-Python $Python)
if ($py.Count) {
    $pyArgs = @()
    if ($py.Count -gt 1) { $pyArgs += $py[1..($py.Count - 1)] }
    $pyArgs += @("-I", (Join-Path $RepoRoot "tools\boot_trace.py"), $TraceFile, "--d3d12", $LogFile)
    # Windows PowerShell 5.1 turns a native program's stderr into error records; with "Stop" that would end the script.
    $ErrorActionPreference = "Continue"
    & $py[0] @pyArgs 2>&1 | ForEach-Object { "$_" } | Out-File -FilePath $SummaryFile -Encoding utf8
    $ErrorActionPreference = "Stop"
} else {
    "no usable Python 3.12+ found for tools/boot_trace.py (checked 'python' and 'py -3'; specify -Python to use another interpreter)" | Out-File -FilePath $SummaryFile -Encoding utf8
}

$peekRx = [regex]'\[rt\] peek t=(\d+)s:(.*)$'
$valRx = [regex]'0x([0-9a-f]+)=([0-9a-f?]+)'
$states = New-Object System.Collections.Generic.List[string]
$lastPeek = ""; $prevPair = ""
$presentOk = 0; $presentFailed = 0; $presses = 0
$shaderLine = ""
$deviceLost = $false
# VkDeviceFaultAddressTypeEXT, in enum order. Keep only counters, including one bucket for newer types.
$faultNames = @("NONE", "READ_INVALID", "WRITE_INVALID", "EXECUTE_INVALID", "INSTRUCTION_POINTER_UNKNOWN",
                "INSTRUCTION_POINTER_INVALID", "INSTRUCTION_POINTER_FAULT", "OTHER")
$faultCounts = New-Object 'long[]' $faultNames.Length
$faultAddressRx = [regex]'^\s*\[rt\] device-fault: address\[\d+\] type=(-?\d+)\s+address='
$wrapped = 0
if (Test-Path -LiteralPath $LogFile) {
    foreach ($line in [System.IO.File]::ReadLines($LogFile)) {
        if ($line.Contains("[rt] device-fault:")) {
            if ($line.Contains("[rt] device-fault: VK_ERROR_DEVICE_LOST from ")) { $deviceLost = $true }
            $m = $faultAddressRx.Match($line)
            if ($m.Success) {
                $type = 0
                if (-not [int]::TryParse($m.Groups[1].Value, [ref]$type) -or $type -lt 0 -or $type -ge ($faultNames.Length - 1)) {
                    $type = $faultNames.Length - 1
                }
                $faultCounts[$type]++
            }
            continue
        }
        if ($line.Contains("[rt] vulkan: the device was lost (") -or $line.Contains("VK_ERROR_DEVICE_LOST")) {
            $deviceLost = $true
        }
        if ($line.Contains("XboxPresentation::PresentX")) {
            if ($line.EndsWith(" ok")) { $presentOk++ } elseif ($line.EndsWith(" failed")) { $presentFailed++ }
            continue
        }
        if ($line.Contains("[rt] peek t=")) {
            $m = $peekRx.Match($line)
            if ($m.Success) {
                $lastPeek = $line.Substring($line.IndexOf("[rt] peek"))
                $game = "?"; $session = "?"
                foreach ($v in $valRx.Matches($m.Groups[2].Value)) {
                    $rva = $v.Groups[1].Value; $val = $v.Groups[2].Value
                    if ($gameStateRva -and $rva -eq $gameStateRva) { $game = $val }
                    elseif ($sessionStateRva -and $rva -eq $sessionStateRva -and $val -ne "?") {
                        $low = $val.PadLeft(16, '0').Substring(8).TrimStart('0')   # the session state is the low dword
                        $session = $(if ($low) { $low } else { "0" })
                    }
                }
                $pair = "game=$game session=$session"
                if ($pair -ne $prevPair) { $states.Add("t=$($m.Groups[1].Value)s $pair"); $prevPair = $pair }
            }
            continue
        }
        if ($line.Contains("[rt] gameinput: press ")) {
            # Buttons only: with the keyboard pad, mouse movement is logged as right-stick presses (rsup, rsdown...).
            if ($line -notmatch 'gameinput: press [lr]s') { $presses++ }
            continue
        }
        if (-not $shaderLine -and $line.Contains(" translated shaders (")) { $shaderLine = $line.Substring([Math]::Max(0, $line.IndexOf("[rt]"))).Trim() }
        if ($line.Contains("wrapped V#")) { $wrapped++ }
    }
}

$capText = "no captures"
$capIndex = Join-Path $Capture "index.txt"
if (Test-Path -LiteralPath $capIndex) {
    $frames = 0; $nonBlack = 0; $last = ""; $maxSum = -1.0; $maxText = ""
    $meanRx = [regex]'mean rgb ([0-9.]+) ([0-9.]+) ([0-9.]+)'
    foreach ($line in [System.IO.File]::ReadLines($capIndex)) {
        $m = $meanRx.Match($line)
        if (-not $m.Success) { continue }
        $frames++
        $r = [double]::Parse($m.Groups[1].Value, [Globalization.CultureInfo]::InvariantCulture)
        $g = [double]::Parse($m.Groups[2].Value, [Globalization.CultureInfo]::InvariantCulture)
        $b = [double]::Parse($m.Groups[3].Value, [Globalization.CultureInfo]::InvariantCulture)
        $sum = $r + $g + $b
        if ($sum -gt 0) { $nonBlack++ }
        $last = "$($m.Groups[1].Value) $($m.Groups[2].Value) $($m.Groups[3].Value)"
        if ($sum -gt $maxSum) { $maxSum = $sum; $maxText = $last }
    }
    $capText = "$frames captured, $nonBlack non-black; last mean rgb $last; brightest $maxText"
}

$digest = New-Object System.Collections.Generic.List[string]
$digest.Add("== boot ${Name}: $verdict")
$digest.Add("shaders:  $(if ($shaderLine) { $shaderLine } else { 'no translated-shaders line (Vulkan device not reached?)' })")
$digest.Add("states:   $(if ($states.Count) { $states -join ' | ' } else { 'no peek lines' })")
$digest.Add("presents: PresentX ok $presentOk, failed $presentFailed")
$digest.Add("input:    $presses presses logged")
$faultClasses = @(for ($i = 0; $i -lt $faultNames.Length; $i++) {
    if ($faultCounts[$i]) { "$($faultNames[$i])=$($faultCounts[$i])" }
})
$faultText = "none"
if ($deviceLost -or $faultClasses.Count) {
    $loss = if ($deviceLost) { "device loss reported" } else { "no device-loss marker" }
    $records = if ($faultClasses.Count) { $faultClasses -join ", " } else { "none" }
    $faultText = "$loss, address records: $records"
}
$digest.Add("faults:   $faultText")
if ($wrapped) { $digest.Add("wrapped:  $wrapped lines mention wrapped V#s") }
if (Test-Path -LiteralPath $LogFile) {
    $faultContext = Get-FaultContext $LogFile
    if ($faultContext) { $digest.Add($faultContext) }
}
$digest.Add("frames:   $capText")
$digest.Add("files:    $LogFile, $TraceFile, $SummaryFile")
$digest | Out-File -FilePath $DigestFile -Encoding utf8
$digest
if ($SummaryLines -gt 0 -and (Test-Path -LiteralPath $SummaryFile)) {
    Write-Output "-- boot_trace.py (first $SummaryLines lines of $SummaryFile)"
    Get-Content -LiteralPath $SummaryFile -TotalCount $SummaryLines
}
exit $exitCode