@echo off
setlocal EnableExtensions EnableDelayedExpansion
title D2LFG - Destiny 2 Steam Overlay Performance Fix

set "APPID=1085660"
set /a REMOVED=0
set /a MISSING=0
set /a FAILED=0

echo ============================================================
echo   D2LFG - Destiny 2 Steam Overlay Performance Fix
echo ============================================================
echo.
echo Community workaround credited to Dragwind on Steam Discussions.
echo This script removes ONLY seven known Steam overlay files from the
echo Steam ROOT folder, then launches Destiny 2.
echo.

rem --- Find Steam installation directory from registry ---
for /f "tokens=2,*" %%A in ('reg query "HKCU\Software\Valve\Steam" /v SteamPath 2^>nul ^| find /i "SteamPath"') do set "STEAMDIR=%%B"
if not defined STEAMDIR (
    for /f "tokens=2,*" %%A in ('reg query "HKLM\SOFTWARE\WOW6432Node\Valve\Steam" /v InstallPath 2^>nul ^| find /i "InstallPath"') do set "STEAMDIR=%%B"
)

if not defined STEAMDIR (
    echo [ERROR] Could not find the Steam installation directory.
    echo.
    echo Open Steam first, then run this file again.
    pause
    exit /b 1
)

set "STEAMDIR=%STEAMDIR:/=\%"
if not exist "%STEAMDIR%\steam.exe" (
    echo [ERROR] Registry path does not contain steam.exe:
    echo         %STEAMDIR%
    pause
    exit /b 1
)

echo Steam: %STEAMDIR%
echo.

rem --- Do not modify files while Destiny 2 is already running ---
tasklist /FI "IMAGENAME eq destiny2.exe" 2>nul | find /I "destiny2.exe" >nul
if not errorlevel 1 (
    echo [STOP] Destiny 2 is currently running.
    echo Exit Destiny 2 and fully restart Steam, but DO NOT launch a game.
    echo Then run this file again.
    pause
    exit /b 2
)

rem --- Workaround expects Steam to be open freshly, before launching a game ---
tasklist /FI "IMAGENAME eq steam.exe" 2>nul | find /I "steam.exe" >nul
if errorlevel 1 (
    echo [STOP] Steam is not running.
    echo.
    echo 1. Start Steam normally.
    echo 2. Do NOT launch Destiny 2 or another game.
    echo 3. Run this file again.
    echo.
    echo This matches the workaround's intended sequence and avoids
    echo deleting files while Steam is updating or starting.
    pause
    exit /b 3
)

echo Removing known overlay files...
echo.

call :RemoveOne "GameOverlayRenderer.dll"
call :RemoveOne "GameOverlayRenderer64.dll"
call :RemoveOne "gameoverlayui64.exe"
call :RemoveOne "SteamOverlayVulkanLayer.dll"
call :RemoveOne "SteamOverlayVulkanLayer.json"
call :RemoveOne "SteamOverlayVulkanLayer64.dll"
call :RemoveOne "SteamOverlayVulkanLayer64.json"

echo.
echo ------------------------------------------------------------
echo Removed: !REMOVED!   Already absent: !MISSING!   Failed: !FAILED!
echo ------------------------------------------------------------
echo.

if !FAILED! GTR 0 (
    echo [ACTION NEEDED] One or more files could not be removed.
    echo.
    echo Fully EXIT Steam, make sure no Steam processes remain, then start
    echo Steam again. Do NOT launch a game. Run this file again afterward.
    echo.
    echo If the failure is "Access is denied", Windows permissions may be
    echo blocking the change. You can right-click this BAT and choose
    echo "Run as administrator", but only if you obtained it from d2lfg.com
    echo and verified the source shown on the download page.
    pause
    exit /b 4
)

echo [OK] Overlay files are clear for this Steam session.
echo Launching Destiny 2...
start "" "steam://run/%APPID%"
exit /b 0

:RemoveOne
set "NAME=%~1"
set "TARGET=%STEAMDIR%\%NAME%"
if not exist "%TARGET%" (
    echo [SKIP] %NAME% - already absent
    set /a MISSING+=1
    goto :eof
)

del /f /q "%TARGET%" >nul 2>&1
if exist "%TARGET%" (
    echo [FAIL] %NAME% - still present / locked
    set /a FAILED+=1
) else (
    echo [ OK ] %NAME% - removed
    set /a REMOVED+=1
)
goto :eof
