@echo off
setlocal EnableExtensions
fltmc >nul 2>&1
if errorlevel 1 (
  echo Please right-click this file and select Run as administrator.
  pause
  exit /b 1
)

set "WORK=%TEMP%\frs-v14-runtime-fix"
if not exist "%WORK%" mkdir "%WORK%"

echo [1/3] Downloading Microsoft Visual C++ x64 runtime...
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$ProgressPreference='SilentlyContinue'; Invoke-WebRequest -UseBasicParsing 'https://aka.ms/vs/17/release/vc_redist.x64.exe' -OutFile '%WORK%\vc_redist.x64.exe'"
if errorlevel 1 goto :download_failed

echo [2/3] Downloading Microsoft Visual C++ x86 runtime...
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$ProgressPreference='SilentlyContinue'; Invoke-WebRequest -UseBasicParsing 'https://aka.ms/vs/17/release/vc_redist.x86.exe' -OutFile '%WORK%\vc_redist.x86.exe'"
if errorlevel 1 goto :download_failed

echo [3/3] Installing runtimes...
start /wait "" "%WORK%\vc_redist.x64.exe" /install /quiet /norestart
set "X64_CODE=%ERRORLEVEL%"
start /wait "" "%WORK%\vc_redist.x86.exe" /install /quiet /norestart
set "X86_CODE=%ERRORLEVEL%"

del /q "%WORK%\vc_redist.x64.exe" "%WORK%\vc_redist.x86.exe" >nul 2>&1
rmdir "%WORK%" >nul 2>&1

if not "%X64_CODE%"=="0" if not "%X64_CODE%"=="1638" if not "%X64_CODE%"=="3010" goto :install_failed
if not "%X86_CODE%"=="0" if not "%X86_CODE%"=="1638" if not "%X86_CODE%"=="3010" goto :install_failed

echo.
echo Repair completed. Restart Windows, then warm up the game shaders again.
pause
exit /b 0

:download_failed
echo.
echo Download failed. Check your network connection and try again.
pause
exit /b 2

:install_failed
echo.
echo Installation failed. x64 code: %X64_CODE%, x86 code: %X86_CODE%
echo Keep these codes and contact support.
pause
exit /b 3
