# 给 .ps1/.cs 文件补上唯一的 UTF-8 BOM (PS 5.1 与 csc 对无 BOM 文件按 ANSI 读取, 中文会乱)
# 幂等: 先剥离已有 BOM 再加一个。本脚本本身纯 ASCII。
param([string[]]$Files)
if (-not $Files) {
  $Files = @()
  $Files += Get-ChildItem (Join-Path $PSScriptRoot '*.ps1') | ForEach-Object { $_.FullName }
  $Files += Get-ChildItem (Join-Path $PSScriptRoot '..\src') -Filter '*.cs' -Recurse | ForEach-Object { $_.FullName }
}
foreach ($f in $Files) {
  $bytes = [System.IO.File]::ReadAllBytes($f)
  $i = 0
  while (($i + 2) -lt $bytes.Length -and $bytes[$i] -eq 0xEF -and $bytes[$i + 1] -eq 0xBB -and $bytes[$i + 2] -eq 0xBF) { $i += 3 }
  if ($i -gt 0) { $bytes = $bytes[$i..($bytes.Length - 1)] }
  $out = New-Object byte[] ($bytes.Length + 3)
  $out[0] = 0xEF; $out[1] = 0xBB; $out[2] = 0xBF
  [Array]::Copy($bytes, 0, $out, 3, $bytes.Length)
  [System.IO.File]::WriteAllBytes($f, $out)
  Write-Output ("BOM fixed: " + $f)
}
