<#
  deploy-editor.ps1 - install the MW4GameEd2 editor INTO the game-data tree (c:\VWE\firestorm\Gameleap\mw4).

  The editor's entire job is editing the source content + resources, which live in Gameleap\mw4.
  So instead of a separate deploy dir that just junctions back here, we drop the editor binaries
  directly into the data tree and run it in place. The dev tree already carries the full runtime +
  editor tool DLLs (mfc42, ctcl, ijl10, Resources, Language.oeg, blade, ctcls, MissionLang,
  ScriptStrings, ...); we only need to:
    - add the editor exe (+ pdb) and DDrawCompat (ddraw.dll, for the windowed 3D viewport on Win11),
    - neutralize the two 2003 DLLs that shadow Windows' system copies and break DirectDraw init
      (dbghelp.dll / imagehlp.dll),
    - set the DirectDraw 16-bit compat shim on the exe path and write a launcher.

  NOTE: ddraw.dll (DDrawCompat) lands in the SAME dir the resource builder (MW4pro.exe) runs from.
  deploy-mw4.ps1 excludes ddraw.dll from the game deploy so the fullscreen game keeps native DDraw.
  Editor launches via run-editor.bat (just -armorlevel 3; joystick disabled in code, no /gos switch).
#>
param(
  [string]$Data   = "c:\VWE\firestorm\Gameleap\mw4",            # game-data tree = the editor's working dir
  [string]$BinDir = "c:\VWE\firestorm\Gameleap\code\pro.bin"    # our built MW4Ed2.exe (PROFILE = working config)
)
$ErrorActionPreference = "Stop"
Write-Host "== Install MW4 editor into the data tree ==" -ForegroundColor Cyan
Write-Host "  data : $Data"
Write-Host "  bin  : $BinDir`n"
foreach ($p in $Data,$BinDir) { if (-not (Test-Path $p)) { throw "missing source: $p" } }

# 1) editor exe (+ pdb for cdb symbol resolution)
Write-Host "[1/4] MW4Ed2.exe (Profile build) + pdb ..." -ForegroundColor Yellow
Copy-Item (Join-Path $BinDir "MW4Ed2.exe") (Join-Path $Data "MW4Ed2.exe") -Force
if (Test-Path (Join-Path $BinDir "MW4Ed2.pdb")) { Copy-Item (Join-Path $BinDir "MW4Ed2.pdb") (Join-Path $Data "MW4Ed2.pdb") -Force }

# 2) DDrawCompat ddraw.dll (narzoul v0.7.1) - Win11's legacy DirectDraw rejects windowed Direct3D7
#    CreateDevice (DDERR_INVALIDOBJECT); DDrawCompat reimplements ddraw/d3d7 so the Game View /
#    Overview render. deploy-mw4.ps1 keeps this OUT of the fullscreen game deploy.
Write-Host "[2/4] DDrawCompat ddraw.dll ..." -ForegroundColor Yellow
$ddc = Join-Path $PSScriptRoot "ddrawcompat\ddraw.dll"
if (Test-Path $ddc) { Copy-Item $ddc (Join-Path $Data "ddraw.dll") -Force; Write-Host "      + ddraw.dll (DDrawCompat v0.7.1)" }
else { Write-Host "      WARNING: $ddc missing -> 3D viewport will fail (DDERR_INVALIDOBJECT)" -ForegroundColor Red }

# 3) Neutralize the two 2003 DLLs that shadow Windows' system copies and break DirectDraw init on
#    Win11 (DDERR_NODIRECTDRAWHW). Renaming lets the app load the correct system versions. They were
#    never copied into the game deploy either; harmless to MW4pro.exe (which also runs from here).
#    Idempotent.
Write-Host "[3/4] neutralizing DDraw-breaking DLLs (dbghelp/imagehlp) ..." -ForegroundColor Yellow
foreach ($d in "dbghelp.dll","imagehlp.dll") {
  $f = Join-Path $Data $d
  if (Test-Path $f) { Move-Item $f "$f.disabled" -Force; Write-Host "      renamed $d -> $d.disabled" }
  elseif (Test-Path "$f.disabled") { Write-Host "      $d already disabled" }
  else { Write-Host "      $d not present (ok)" }
}
# editor tool DLLs (ctcl/ijl10/Resources/Language.oeg) + runtime DLLs are already in the dev tree.

# 4) DirectDraw 16-bit compat shim on the exe path + launcher
Write-Host "[4/4] DDraw 16-bit compat shim + launcher ..." -ForegroundColor Yellow
$exe = Join-Path $Data "MW4Ed2.exe"
$layers = "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
if (-not (Test-Path $layers)) { New-Item -Path $layers -Force | Out-Null }
Set-ItemProperty $layers -Name $exe -Value "DWM8And16BitMitigation HIGHDPIAWARE" -Type String
Write-Host "      shim set for $exe"

@"
@echo off
rem MechWarrior 4 Mission Editor (FireStorm) - runs IN PLACE in the game-data tree.
rem Edits the source Content\ and (re)builds resource\*.mw4 right here. Joystick disabled in code
rem (no /gos switch needed); DDrawCompat (ddraw.dll) enables the windowed 3D viewport on Win11.
cd /d "%~dp0"
start "" "%~dp0MW4Ed2.exe" -armorlevel 3
"@ | Set-Content (Join-Path $Data "run-editor.bat") -Encoding ASCII
Write-Host "      wrote run-editor.bat"

# PageHeap was a debug aid only; with it active the exe AVs on the modal-STOP path. Warn if present.
$ifeo = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\MW4Ed2.exe'
if (Test-Path $ifeo) {
  Write-Host "  WARNING: PageHeap IFEO for MW4Ed2.exe is set. Remove it (ELEVATED):" -ForegroundColor Red
  Write-Host "           reg import C:\VWE\firestorm\build-env\mw4ed2-pageheap-off.reg" -ForegroundColor Red
}

Write-Host "`nEditor installed into the data tree: $Data" -ForegroundColor Green
"  launch: $Data\run-editor.bat   (or MW4Ed2.exe -armorlevel 3 from $Data)"
"  it edits Content\ and builds resource\*.mw4 in place -- no separate editor directory."
