upleb.uk

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

summaryrefslogtreecommitdiff
path: root/scripts/first_look.ps1
blob: 78ebc758bda8463ff0e52ce61b51fe3e3dd2b09f (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
# Day-one report for the game executable: PE inspection, encryption check, import surface and a loader dry run.
# Reads the exe only; no game code runs.
param(
    [Parameter(Mandatory)][string]$Exe,
    [string]$GameDir
)
$ErrorActionPreference = "Stop"
$root = Split-Path $PSScriptRoot -Parent
$Exe = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Exe)
if (-not (Test-Path -LiteralPath $Exe -PathType Leaf)) { throw "$Exe not found" }
$image = Get-Item -LiteralPath $Exe
if ($GameDir) {
    $GameDir = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($GameDir)
    if (-not (Test-Path -LiteralPath $GameDir -PathType Container)) { throw "$GameDir not found or not a directory" }
} else {
    $GameDir = $image.DirectoryName
}
$name = [IO.Path]::GetFileNameWithoutExtension($Exe)
$out = Join-Path $root "reports\exe\$name"
if (Test-Path -LiteralPath $out -PathType Leaf) { throw "$out is a file, not a directory" }
[void][IO.Directory]::CreateDirectory($out)

$fs = [IO.File]::OpenRead($Exe)
try {
    $mz = New-Object byte[] 2
    $read = $fs.Read($mz, 0, 2)
} finally {
    $fs.Dispose()
}
if ($read -ne 2 -or [Text.Encoding]::ASCII.GetString($mz) -ne "MZ") {
    throw "$Exe doesn't start with an MZ header. It's still packaged or encrypted, so there's nothing to inspect."
}

$size = $image.Length
$hash = (Get-FileHash -LiteralPath $Exe -Algorithm SHA256).Hash.ToLower()
"sha256 $hash  size $size" | Tee-Object -LiteralPath (Join-Path $out "hash.txt")

Write-Host "`n== PE inspection"
$reportPath = Join-Path $out "$([IO.Path]::GetFileName($Exe)).json"
if (Test-Path -LiteralPath $reportPath -PathType Leaf) { Remove-Item -LiteralPath $reportPath -Force }
python -I (Join-Path $root "tools\pe_inspect.py") $Exe --game-dir $GameDir --out $out
if ($LASTEXITCODE -ne 0) { throw "pe_inspect failed" }

Write-Host "`n== GDK runtime classes and slots called through xgameruntime.lib (read from the file)"
python -I (Join-Path $root "tools\gdk_runtime_calls.py") $Exe --out $out
if ($LASTEXITCODE -ne 0) { Write-Warning "gdk_runtime_calls could not read all of $Exe; its report may be incomplete" }

Write-Host "`n== Archetype lookup candidates for mods (docs/modding.md; pass the top RVA to gta6rt --archetype-lookup)"
python -I (Join-Path $root "tools\find_hash_lookup.py") $Exe --json (Join-Path $out "hash_lookup.json")
if ($LASTEXITCODE -ne 0) { Write-Warning "find_hash_lookup could not scan $Exe" }

Write-Host "`n== What to confirm against this executable (every ASSUMPTION / UNVERIFIED / VERIFY note; called ones first)"
$calls = Join-Path $out ((Split-Path $Exe -Leaf) + ".gdk_calls.json")
$checklistArgs = @("--out", (Join-Path $out "checklist.md"))
if (Test-Path -LiteralPath $calls) { $checklistArgs += @("--gdk-calls", $calls) }
python -I (Join-Path $root "tools\exe_checklist.py") @checklistArgs
if ($LASTEXITCODE -ne 0) { Write-Warning "exe_checklist could not write the checklist" }

function Find-RuntimeLoader($build, $folders) {
    foreach ($folder in $folders) {
        $candidate = [IO.Path]::Combine($build, $folder, "gta6rt.exe")
        if (Test-Path -LiteralPath $candidate -PathType Leaf) { return $candidate }
    }
}

$build = Join-Path $root "runtime\build"
$configs = @("RelWithDebInfo", "Release", "MinSizeRel", "Debug")
$folders = @("") + $configs
$rt = Find-RuntimeLoader $build $folders
# A gta6rt.exe older than the runtime sources (a pull since the last build) would report the old shim table, so it
# is rebuilt then too, not only when missing.
$sources = @("runtime\src", "runtime\shims", "runtime\CMakeLists.txt") | ForEach-Object { Join-Path $root $_ } |
    Where-Object { Test-Path -LiteralPath $_ }
$newest = $sources | ForEach-Object { Get-ChildItem -LiteralPath $_ -Recurse -File -ErrorAction SilentlyContinue } |
    Sort-Object LastWriteTimeUtc -Descending | Select-Object -First 1
$stale = $rt -and $newest -and ($newest.LastWriteTimeUtc -gt (Get-Item -LiteralPath $rt).LastWriteTimeUtc)
if ($stale) { Write-Host "$rt is older than $($newest.Name): rebuilding it first" }
$tlsName = "gta6rt_tls_host.dll"
$hasTls = $rt -and (Test-Path -LiteralPath (Join-Path (Split-Path -LiteralPath $rt) $tlsName) -PathType Leaf)
if (-not $hasTls -or $stale) {
    if (-not $hasTls) { Write-Host "Missing runtime files: gta6rt.exe and $tlsName must be in the same folder; rebuilding the runtime" }
    $cache = Join-Path $build "CMakeCache.txt"
    if ((Test-Path -LiteralPath $cache -PathType Leaf) -and
        (Select-String -LiteralPath $cache -Pattern '^CMAKE_GENERATOR:INTERNAL=Visual Studio ' -Quiet)) {
        # The wrapper configures Ninja and cannot reuse a Visual Studio build tree.
        $commandEntry = Select-String -LiteralPath $cache -Pattern '^CMAKE_COMMAND:INTERNAL=(.+)$'
        if (-not $commandEntry) { throw "Missing CMAKE_COMMAND in $cache. Reconfigure the Visual Studio build and retry." }
        $cmake = $commandEntry.Matches.Groups[1].Value
        $folders = $configs
        $types = Select-String -LiteralPath $cache -Pattern '^CMAKE_CONFIGURATION_TYPES:[^=]+=(.+)$'
        if ($types) {
            $available = $types.Matches.Groups[1].Value -split ';'
            $folders = @($configs | Where-Object { $_ -in $available })
        }
        $existing = Find-RuntimeLoader $build $folders
        $configArgs = @()
        if ($existing) { $configArgs = @("--config", (Split-Path (Split-Path $existing) -Leaf)) }
        elseif ($types -and $folders.Count) { $configArgs = @("--config", $folders[0]) }
        & $cmake --build $build --target gta6rt @configArgs
        if ($LASTEXITCODE -ne 0) { throw "Building gta6rt in $build failed ($LASTEXITCODE)" }
    } else {
        $global:LASTEXITCODE = 0
        & (Join-Path $PSScriptRoot "build_runtime.ps1")
        if ($LASTEXITCODE -ne 0) { throw "build_runtime.ps1 failed ($LASTEXITCODE)" }
    }
    $rt = Find-RuntimeLoader $build $folders
    if (-not $rt) { throw "runtime build did not produce gta6rt.exe in $build" }
    $tlsHost = Join-Path (Split-Path -LiteralPath $rt) $tlsName
    if (-not (Test-Path -LiteralPath $tlsHost -PathType Leaf)) { throw "runtime build did not produce $tlsHost" }
}
Write-Host "`n== Loader dry run (map, relocate, bind imports; nothing executes)"
& {
    # Keep native stderr in the report without letting Windows PowerShell stop the dry run on it.
    $ErrorActionPreference = "Continue"
    $PSNativeCommandUseErrorActionPreference = $false
    & $rt --dry-run $Exe --game-dir $GameDir 2>&1
} | Tee-Object -LiteralPath (Join-Path $out "dryrun.txt") | Select-Object -Last 40
$dryRunExitCode = $LASTEXITCODE

# Shim to-do list, grouped by DLL, most functions first.
if (-not (Test-Path -LiteralPath $reportPath -PathType Leaf)) { throw "pe_inspect did not write $reportPath" }
$report = Get-Content -LiteralPath $reportPath -Raw | ConvertFrom-Json
$todo = $report.imports | Where-Object { $_.where -eq "shim" } | Sort-Object { -$_.functions.Count }
$lines = foreach ($t in $todo) { "{0,-40} {1,5}" -f $t.dll, $t.functions.Count }
# "unverified" only comes from a host that isn't Windows and has no Wine prefix (pe_inspect.py, classify_dll): most
# are system DLLs, but a console DLL the shims don't serve yet is among them, so they follow the list, marked.
$unverified = @($report.imports | Where-Object { $_.where -eq "unverified" } | Sort-Object { -$_.functions.Count })
if ($unverified.Count -gt 0) {
    $lines = @(foreach ($t in $todo) { "{0,-40} {1,5}" -f $t.dll, $t.functions.Count }) +
        @(foreach ($t in $unverified) { "{0,-40} {1,5}  system (unverified)" -f $t.dll, $t.functions.Count })
}
Set-Content -LiteralPath (Join-Path $out "shim-todo.txt") -Value $lines
Write-Host "`n== Shim to-do (DLL, function count)"
$lines
if ($unverified.Count -gt 0) {
    Write-Warning "$($unverified.Count) DLLs could not be checked against system DLLs (no Wine prefix): set WINEPREFIX and run again"
}
$enc = $report.sections | Where-Object { $_.looks_encrypted } | ForEach-Object { $_.name }
if ($enc) { Write-Warning "High-entropy sections (still encrypted or packed): $($enc -join ', ')" }
Write-Host "`nReports: $out"
exit $dryRunExitCode