# AutoDrive Lite (FREE / OPEN)
$ErrorActionPreference = 'Stop'
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# === 启动校验数据 (修改本块会导致脚本无法运行) ===
$__d = @(
    'V3JpdGUtSG9zdCAiIgpXcml0ZS1Ib3N0ICIgICstLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t',
    'LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0rIiAtRm9yZWdyb3VuZENvbG9yIERhcmtZZWxsb3cK',
    'V3JpdGUtSG9zdCAiICB8ICIgLUZvcmVncm91bmRDb2xvciBEYXJrWWVsbG93IC1Ob05ld2xpbmUKV3Jp',
    'dGUtSG9zdCAiQXV0b0RyaXZlIExpdGUiIC1Gb3JlZ3JvdW5kQ29sb3IgV2hpdGUgLU5vTmV3bGluZQpX',
    'cml0ZS1Ib3N0ICIgIHwgIOacrOW3peWFtyAxMDAlIOWFjei0uSAvIOW8gOa6kCAvIOS7heS+m+WtpuS5',
    'oCAgICAgICAiIC1Gb3JlZ3JvdW5kQ29sb3IgR3JheSAtTm9OZXdsaW5lCldyaXRlLUhvc3QgInwiIC1G',
    'b3JlZ3JvdW5kQ29sb3IgRGFya1llbGxvdwpXcml0ZS1Ib3N0ICIgIHwgIiAtRm9yZWdyb3VuZENvbG9y',
    'IERhcmtZZWxsb3cgLU5vTmV3bGluZQpXcml0ZS1Ib3N0ICLoirHpkrHkubDnmoTor7fnlLPor7fpgIDm',
    'rL7lubbkuL7miqXljZblrrYgIHwgIOWOn+eJiDogQiDnq5nmkJzntKIgS3Jvbm9zXzEgIiAtRm9yZWdy',
    'b3VuZENvbG9yIEdyYXkgLU5vTmV3bGluZQpXcml0ZS1Ib3N0ICJ8IiAtRm9yZWdyb3VuZENvbG9yIERh',
    'cmtZZWxsb3cKV3JpdGUtSG9zdCAiICArLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t',
    'LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tKyIgLUZvcmVncm91bmRDb2xvciBEYXJrWWVsbG93CldyaXRl',
    'LUhvc3QgIiIKJGdsb2JhbDpfX0FEX1ZGUF9PSyA9ICR0cnVl'
)
try { Invoke-Expression ([System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String(-join $__d))) }
catch { Start-Sleep -Seconds 2; exit 1 }
if (-not $global:__AD_VFP_OK) { Start-Sleep -Seconds 2; exit 1 }
# === 数据块结束 ===

Write-Host "================================================" -ForegroundColor Green
Write-Host "  AutoDrive Lite" -ForegroundColor Green
Write-Host "================================================" -ForegroundColor Green

function Test-ViGEmBus {
    $out = & sc.exe query ViGEmBus 2>$null
    return ($out -match 'RUNNING')
}

if (Test-ViGEmBus) {
    Write-Host "[√] ViGEmBus 驱动已就绪" -ForegroundColor Green
} else {
    Write-Host "[!] 未检测到 ViGEmBus, 开始自动安装..." -ForegroundColor Yellow
    $installer = Get-ChildItem -Path $PSScriptRoot -Filter "ViGEmBus_*.exe" -ErrorAction SilentlyContinue | Select-Object -First 1
    if (-not $installer) { Write-Host "[X] 找不到安装器" -ForegroundColor Red; Read-Host; exit 1 }
    Write-Host "    使用本地安装器: $($installer.Name)" -ForegroundColor Cyan
    $argSets = @(@('/quiet','/norestart'), @('/S'), @('/VERYSILENT','/SUPPRESSMSGBOXES','/NORESTART','/SP-'), @('/install','/quiet','/norestart'))
    $installed = $false
    foreach ($a in $argSets) {
        Write-Host "    尝试参数: $($a -join ' ')" -ForegroundColor DarkGray
        try {
            $proc = Start-Process -FilePath $installer.FullName -ArgumentList $a -Wait -PassThru -ErrorAction Stop
            Start-Sleep -Seconds 2
            if (Test-ViGEmBus) { Write-Host "[√] ViGEmBus 安装成功" -ForegroundColor Green; $installed = $true; break }
        } catch {}
    }
    if (-not $installed) {
        Write-Host "[!] 静默安装未成功, 弹出安装窗口" -ForegroundColor Yellow
        Start-Process -FilePath $installer.FullName -Wait
        Start-Sleep -Seconds 2
        if (Test-ViGEmBus) { Write-Host "[√] 安装成功" -ForegroundColor Green }
        else { Write-Host "[X] 仍然检测不到驱动, 可能需要重启" -ForegroundColor Red; Read-Host; exit 1 }
    }
}

# 检查 OCR 识别区配置
$regionFile = Join-Path $PSScriptRoot "config\ocr_region.txt"
if (-not (Test-Path $regionFile)) {
    Write-Host ""
    Write-Host "[!] 首次使用, 需要先框选 OCR 识别区域" -ForegroundColor Yellow
    Write-Host "    现在会启动框选工具, 请按提示操作" -ForegroundColor Cyan
    Start-Sleep -Seconds 1
    $fs = Join-Path $PSScriptRoot "frame_select.ps1"
    & $fs
    if (-not (Test-Path $regionFile)) {
        Write-Host "[X] 还是没有识别区配置, 退出" -ForegroundColor Red
        Read-Host
        exit 1
    }
} else {
    Write-Host "[√] OCR 识别区已配置 (重新框选请双击 框选识别区.bat)" -ForegroundColor Green
}

$main = Join-Path $PSScriptRoot "auto_drive.ps1"
if (-not (Test-Path $main)) { Write-Host "[X] 找不到 auto_drive.ps1" -ForegroundColor Red; Read-Host; exit 1 }

Write-Host ""
& $main