# 将最新编译的 Simple3 部署到 bin\Debug 并提示重启。
# 用法：先关闭 Simple3（任务管理器或托盘退出），再运行本脚本。

$ErrorActionPreference = 'Stop'
$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path
$slProj = Join-Path $repoRoot 'Simple\Simple3\Simple3.csproj'
$outDir = Join-Path $repoRoot 'Simple\Simple3\bin\Debug'
$objDll = Join-Path $repoRoot 'Simple\Simple3\obj\Debug\Simple3.dll'

Write-Host '==> 编译 Simple3 (DisableFody)...' -ForegroundColor Cyan
dotnet build $slProj -c Debug -p:DisableFody=true
if ($LASTEXITCODE -ne 0) { throw "dotnet build 失败 (exit $LASTEXITCODE)" }

$builtDll = if (Test-Path $objDll) { $objDll } else { Join-Path $outDir 'Simple3.dll' }

if ((Get-Process Simple3 -ErrorAction SilentlyContinue) -or (Get-Process SimpleLite -ErrorAction SilentlyContinue)) {
    Write-Host ''
    Write-Host 'Simple3 仍在运行，无法覆盖 DLL。请先关闭 Simple3.exe，再重新运行本脚本。' -ForegroundColor Yellow
    exit 1
}

Copy-Item $builtDll (Join-Path $outDir 'Simple3.dll') -Force
$objExe = Join-Path $repoRoot 'Simple\Simple3\obj\Debug\Simple3.exe'
if (Test-Path $objExe) {
    Copy-Item $objExe (Join-Path $outDir 'Simple3.exe') -Force
}

Write-Host '==> 已更新' $outDir -ForegroundColor Green
Write-Host '请重新登录迷毂平台（或手动启动 Simple3.exe）以加载新 API。'
