# Stability prep before launch — forces settings the frozen v43 DLL cannot enforce alone.
$ErrorActionPreference = 'Stop'
$ExpectedMenu = 8261120  # v43 patched; fresh recompiled is ~8273920 — both OK
$runtime = Join-Path $env:APPDATA 'AKMenu\runtime'
# Always wipe runtime so patched menu DLL is re-extracted (all patches are same byte size).
if (Test-Path $runtime) {
    Remove-Item $runtime -Recurse -Force -ErrorAction SilentlyContinue
    Write-Host '[OK] Cleared AKMenu runtime (forces fresh patched DLL extract)'
}

$settings = Join-Path $env:APPDATA 'AKMenu\settings.json'
if (Test-Path $settings) {
    $json = Get-Content $settings -Raw | ConvertFrom-Json

    if ($json.spoofing) {
        $json.spoofing.multiplex_session = $false
        $json.spoofing.increase_player_limit = $false
        $json.spoofing.multiplex_count = 1
    }
    if ($json.session) {
        $json.session.advertise_multiplex_count = 1
        $json.session.join_friend_mode = $false
        $json.session.fast_join = $false
        $json.session.lock_session = $false
        $json.session.hosting_mode = $false
        $json.session.player_magnet_enabled = $false
        $json.session.advertise_session_enabled = $false
    }
    if ($json.debug) {
        $json.debug.battleye_server = $false
    }

    $json | ConvertTo-Json -Depth 100 | Set-Content $settings -Encoding UTF8
    Write-Host '[OK] Stability settings: Multiplex OFF, Magnet OFF, Advertise OFF, BE emulation OFF'
}

$bridgeIni = Join-Path $env:APPDATA 'AKBridge\settings.ini'
if (-not (Test-Path (Split-Path $bridgeIni -Parent))) {
    New-Item -ItemType Directory -Force -Path (Split-Path $bridgeIni -Parent) | Out-Null
}
if (Test-Path $bridgeIni) { attrib -R $bridgeIni 2>$null }
@'
[Global]
BlockTelemetry = true

[GTA]
UseLocalSaves = false
EnableMoneyCheatCodes = false
UnlockGTAPlus = false
BypassBattlEye = true
UnlockCESP = false
EnableBackups = false
'@ | Set-Content -Path $bridgeIni -Encoding ASCII -NoNewline
Add-Content -Path $bridgeIni -Value '' -Encoding ASCII
Write-Host '[OK] AKBridge settings.ini rewritten (BypassBattlEye=true)'
