# OnebotCatalog 编译脚本 (零安装: 使用 VS Build Tools 自带 Roslyn csc + Windows 自带 .NET Framework 4.8/WPF)
# 用法:
#   开发版:  powershell -NoProfile -ExecutionPolicy Bypass -File tools\build.ps1
#   客户版:  powershell -NoProfile -ExecutionPolicy Bypass -File tools\build.ps1 -Customer [-Opc 数据包.opc]
param(
  [string]$Out = (Join-Path $PSScriptRoot '..\bin\OnebotCatalog.exe'),
  [switch]$Customer,
  [string]$Opc = (Join-Path $PSScriptRoot '..\sample\OnebotCatalog_2026.08.opc'),
  [string]$Icon = (Join-Path $PSScriptRoot '..\assets\OneBot-logo.ico')
)
if ($Customer -and -not $PSBoundParameters.ContainsKey('Out')) {
  $Out = Join-Path $PSScriptRoot '..\bin\OnebotCatalog-Customer.exe'
}

$root = $PSScriptRoot
$fw   = 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319'
$wpf  = Join-Path $fw 'WPF'
$csc  = 'C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\Roslyn\csc.exe'

if (-not (Test-Path $csc)) { Write-Output '未找到 csc.exe (VS Build Tools)'; exit 1 }

$refs = @(
  (Join-Path $fw 'mscorlib.dll'),
  (Join-Path $fw 'System.dll'),
  (Join-Path $fw 'System.Core.dll'),
  (Join-Path $fw 'System.Data.dll'),
  (Join-Path $fw 'System.Xaml.dll'),
  (Join-Path $fw 'System.IO.Compression.dll'),
  (Join-Path $fw 'System.IO.Compression.FileSystem.dll'),
  (Join-Path $fw 'System.Web.Extensions.dll'),
  (Join-Path $fw 'System.Windows.Forms.dll'),
  (Join-Path $wpf 'WindowsBase.dll'),
  (Join-Path $wpf 'PresentationCore.dll'),
  (Join-Path $wpf 'PresentationFramework.dll')
)

# 开源 3D 渲染库 Helix Toolkit (MIT): 窗口内直渲 STL/OBJ 网格 (真实数模预览, 免浏览器)
$helixDir = Join-Path $PSScriptRoot '..\lib'
$helixWpf = Join-Path $helixDir 'HelixToolkit.Wpf.dll'
$helixCore = Join-Path $helixDir 'HelixToolkit.dll'
if ((Test-Path $helixWpf) -and (Test-Path $helixCore)) {
  $refs += @($helixWpf, $helixCore)
} else {
  Write-Output '警告: lib\ 缺 HelixToolkit DLL, 跳过 3D 窗口内渲染引用 (仅影响真实数模窗口内预览)'
}

# WebView2 控件 (微软, 窗口内嵌新迪查看器显示真实 STEP, 免弹浏览器; Win11 自带运行时)
$wv2Core = Join-Path $helixDir 'Microsoft.Web.WebView2.Core.dll'
$wv2Wpf = Join-Path $helixDir 'Microsoft.Web.WebView2.Wpf.dll'
$wv2Loader = Join-Path $helixDir 'WebView2Loader.dll'
if ((Test-Path $wv2Core) -and (Test-Path $wv2Wpf)) {
  $refs += @($wv2Core, $wv2Wpf)
} else {
  Write-Output '警告: lib\ 缺 WebView2 DLL, 跳过窗口内嵌 3D (真实数模将回退浏览器预览)'
}

# GenServer (NX 按需生成服务, 独立控制台 exe) 单独编译, 不进主程序
$srcs = Get-ChildItem (Join-Path $root '..\src') -Filter '*.cs' -Recurse |
  Where-Object { $_.FullName -notmatch '\\GenServer\\' } | ForEach-Object { $_.FullName }
if (-not $srcs) { Write-Output 'src 下无 .cs 文件'; exit 1 }

$outDir = Split-Path $Out
New-Item -ItemType Directory -Force -Path $outDir | Out-Null

$cscArgs = @('/nologo', '/target:winexe', '/platform:anycpu', '/optimize+', '/utf8output', ('/out:' + $Out))
# OneBot logo 嵌入资源 (窗口图标 + 顶栏 logo)
$logoPng = Join-Path $PSScriptRoot '..\assets\OneBot-logo.png'
if (Test-Path $logoPng) { $cscArgs += ('/resource:' + $logoPng + ',OneBot-logo.png') }
if ($Customer) {
  $cscArgs += '/define:CUSTOMER_BUILD'
  if (Test-Path $Opc) { $cscArgs += ('/resource:' + $Opc + ',catalog.opc') }
  else { Write-Output ("警告: 找不到数据包 " + $Opc + ", 客户版将无内置数据") }
  if (Test-Path $Icon) { $cscArgs += ('/win32icon:' + $Icon) }
}
foreach ($r in $refs) { if (Test-Path $r) { $cscArgs += ('/reference:' + $r) } }
$cscArgs += $srcs

& $csc @cscArgs
if ($LASTEXITCODE -eq 0) {
  # 第三方 DLL 与 exe 同目录, 运行时加载 (Helix + WebView2; WebView2Loader 为 native, 必须同目录)
  if (Test-Path $helixWpf) { Copy-Item $helixWpf $outDir -Force }
  if (Test-Path $helixCore) { Copy-Item $helixCore $outDir -Force }
  if (Test-Path $wv2Core) { Copy-Item $wv2Core $outDir -Force }
  if (Test-Path $wv2Wpf) { Copy-Item $wv2Wpf $outDir -Force }
  if (Test-Path $wv2Loader) { Copy-Item $wv2Loader $outDir -Force }
  Write-Output ("编译成功: " + $Out)
} else {
  Write-Output ("编译失败 (exit " + $LASTEXITCODE + ")")
  exit 1
}
