# Unity 脚本编译检查
$ErrorActionPreference = "Stop"
$projectRoot = Split-Path -Parent $PSScriptRoot
$unityEditor = "C:\Program Files\Unity\Hub\Editor\2022.3.62f3c1\Editor\Unity.exe"
$unityProject = "$projectRoot\src\Unity"

# 1. 重建 Core DLL
Write-Host "Building Core.dll..."
dotnet publish "$projectRoot\src\CounterDrone.Core\CounterDrone.Core.csproj" -c Release -o "$projectRoot\unity_plugins" 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) { Write-Host "Core.dll BUILD FAILED"; exit 1 }

# 2. 更新 Plugins + 清理缓存
Copy-Item "$projectRoot\unity_plugins\*.dll" "$unityProject\Assets\Plugins\CounterDrone.Core\" -Force
Remove-Item "$unityProject\Library\ScriptAssemblies" -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Compiling Unity scripts..."

# 3. 编译（CompileErrorDetector 捕获错误并 Exit(-1)）
& $unityEditor -batchmode -projectPath $unityProject -logFile - 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) { Write-Host "UNITY BUILD FAILED (exit code $LASTEXITCODE)"; exit 1 }

Write-Host "ALL CHECKS PASSED"
exit 0
