param(
    [string]$GameRoot = "D:\SteamLibrary\steamapps\common\ProjectZomboid"
)

$ErrorActionPreference = "Stop"

function Get-Sha256([string]$Path) {
    if (-not (Test-Path $Path)) { return $null }
    return (Get-FileHash $Path -Algorithm SHA256).Hash.ToLowerInvariant()
}

function Get-JarEntrySha256([string]$JarPath, [string]$EntryName) {
    if (-not (Test-Path $JarPath)) { return $null }

    Add-Type -AssemblyName System.IO.Compression.FileSystem
    $zip = [System.IO.Compression.ZipFile]::OpenRead($JarPath)
    try {
        $entry = $zip.GetEntry($EntryName)
        if ($null -eq $entry) { return $null }
        $stream = $entry.Open()
        $memory = New-Object System.IO.MemoryStream
        try {
            $stream.CopyTo($memory)
        } finally {
            $stream.Close()
        }
        $bytes = $memory.ToArray()
        $sha = [System.Security.Cryptography.SHA256]::Create()
        return (($sha.ComputeHash($bytes) | ForEach-Object { $_.ToString("x2") }) -join "")
    } finally {
        $zip.Dispose()
    }
}

$repoRoot = Split-Path -Parent $PSScriptRoot
$knownPatchedClass = Join-Path $repoRoot "zombie\vehicles\BaseVehicle.class"
$patchedHash = Get-Sha256 $knownPatchedClass
$patchedHashText = if ($null -eq $patchedHash) { "missing" } else { $patchedHash }
$knownHelperClass = Join-Path $repoRoot "zombie\vehicles\LandtrainConstraintAuthHelper.class"
$helperHash = Get-Sha256 $knownHelperClass
$helperHashText = if ($null -eq $helperHash) { "missing" } else { $helperHash }

$targets = @(
    @{
        Name = "Client override path"
        Path = Join-Path $GameRoot "zombie\vehicles\BaseVehicle.class"
    },
    @{
        Name = "Dedicated override path"
        Path = Join-Path $GameRoot "java\zombie\vehicles\BaseVehicle.class"
    }
)

$jarCandidates = @(
    (Join-Path $GameRoot "projectzomboid.jar"),
    (Join-Path $GameRoot "java\projectzomboid.jar")
)

Write-Output "GameRoot: $GameRoot"
Write-Output "Known patched hash (repo): $patchedHashText"
Write-Output "Known helper hash (repo): $helperHashText"
Write-Output ""

foreach ($target in $targets) {
    $path = $target.Path
    $hash = Get-Sha256 $path
    if ($null -eq $hash) {
        Write-Output "$($target.Name): MISSING ($path)"
        continue
    }

    $status = if ($patchedHash -and $hash -eq $patchedHash) { "PATCHED" } else { "NOT_MATCHING_PATCHED_HASH" }
    Write-Output "$($target.Name): $status"
    Write-Output "  path: $path"
    Write-Output "  hash: $hash"
}

Write-Output ""
foreach ($helperPath in @(
    (Join-Path $GameRoot "zombie\vehicles\LandtrainConstraintAuthHelper.class"),
    (Join-Path $GameRoot "java\zombie\vehicles\LandtrainConstraintAuthHelper.class")
) | Select-Object -Unique) {
    $hash = Get-Sha256 $helperPath
    if ($null -eq $hash) {
        Write-Output "Helper class: MISSING ($helperPath)"
        continue
    }
    $status = if ($helperHash -and $hash -eq $helperHash) { "PATCHED" } else { "NOT_MATCHING_HELPER_HASH" }
    Write-Output "Helper class: $status"
    Write-Output "  path: $helperPath"
    Write-Output "  hash: $hash"
}

Write-Output ""
foreach ($jar in $jarCandidates | Select-Object -Unique) {
    $jarHash = Get-JarEntrySha256 $jar "zombie/vehicles/BaseVehicle.class"
    if ($null -eq $jarHash) {
        Write-Output "Jar class: MISSING ($jar)"
        continue
    }
    Write-Output "Jar class hash:"
    Write-Output "  jar: $jar"
    Write-Output "  hash: $jarHash"
}
