@echo off
SET SDL_WINDOWS_DPI_SCALING=0
cd ..
set cnt=0
@set var=%cd%
for %%I in (.) do set GameDir=%%~nxI
for %%f in (*^).bat) do set GameName2=%%f
set GameName=%GameName2:~0,-4%
set IndexName=%GameName:~0,-7%
cd ..
cd ..
cd ..
set /p folderpath=<.\util\alt_launch.txt

IF NOT EXIST ".\eXoDOS\%GameDir%\" goto none
copy .\emulators\dosbox\Staging_crt.bak .\emulators\dosbox\Staging_crt.conf 
copy .\emulators\dosbox\Staging_crt_PP.bak .\emulators\dosbox\Staging_crt_PP.conf 
cls
:launch
.\util\setconsole.exe /reset
if not exist .\emulators\dosbox\alt_settings.txt goto next
for /F "delims=" %%a in ('findstr /C:"ppmode" .\emulators\dosbox\alt_settings.txt') do set ppmode1=%%a
set ppmode=%ppmode1:*:=%
for /F "delims=" %%a in ('findstr /C:"CRT:" .\emulators\dosbox\alt_settings.txt') do set crt1=%%a
set crt=%crt1:*:=%
for /F "delims=" %%a in ('findstr /C:"nslmode:" .\emulators\dosbox\alt_settings.txt') do set nsl1=%%a
set nsl=%nsl1:*:=%
for /F "delims=" %%a in ('findstr /C:"glshader" .\emulators\dosbox\alt_settings.txt') do set shader1=%%a
set shader=%shader1:*\=%
set shadert2=%shader1:*:=%
for /f "tokens=1 delims=\" %%a in ("%shadert2%") do set shadert=%%a
set filter=%shader1:*:=%

if [%shader%]==[] goto next


set dosbox="%folderpath%dosbox.exe"
if %crt%==On (
   if %ppmode%==On (
      set conf=".\emulators\dosbox\Staging_crt_PP.conf"
      goto ps))
if %crt%==On (
   if %ppmode%==Off (
      set conf=".\emulators\dosbox\Staging_crt.conf"
      goto ps))
if %nsl%==On (
   if %ppmode%==Off (
      set conf=".\emulators\dosbox\Staging_noline.conf"
      goto ps))
if %nsl%==On (
   if %ppmode%==On (
      set conf=".\emulators\dosbox\Staging_noline_PP.conf"
      goto ps))
if %crt%==Off (
   if %ppmode%==On (
      set conf=".\emulators\dosbox\Staging_PP.conf"
      goto ps))

:ps
if %crt%==Off set shader=Off
if %nsl%==On set shader=Off
cls
echo.
echo Previous settings have been found.
echo Integer Scaling:          %ppmode%
rem echo CRT:                      %crt%
echo No Scan Lines:            %nsl%
rem if %crt%==Off goto :blip
if %shader%==glshader:crt-auto goto blip2
echo Shader:                   %shader%
:blip2
if %nsl%==Off echo Shader Type:              %shadert%
:blip
echo.
echo Would you like to launch with these settings?
echo.
.\util\choice /C:YN /N [Y]es or [N]o to reconfigure

if errorlevel = 2 goto next
if errorlevel = 1 goto preinstall

:next
cls
echo.
echo Press 1 for Integer Scaling (Pixel Perfect mode)
echo Press 2 for GL Shader and Scaler selection
echo Press 3 for No Scan Lines mode
echo Press 4 to Quit 
echo.
echo Please note, not every game will work when using the above alternate launch options. Use
echo the standard game launch if issues occur.
echo.
echo Integer Scaling attempts to properly emulate pixel ratio. Choosing this option will provide
echo more info.
echo.
echo GL Shader and Scaler selection will allow your choice of Shader or Scaler to be applied
echo.
echo No Scan Line Mode removes scan lines from select FMV games
echo.
.\util\choice /C:1234 /N Please Choose:

if errorlevel = 4 goto quit
if errorlevel = 3 goto addNSLpp
if errorlevel = 2 goto CRTmode
if errorlevel = 1 goto PPmode

:PPMode
set dosbox="%folderpath%dosbox.exe"
set filter=Sharp
set conf=".\emulators\dosbox\Staging_PP.conf"
echo ppmode:On>.\emulators\dosbox\alt_settings.txt
echo CRT:Off>>.\emulators\dosbox\alt_settings.txt
echo nslmode:Off>>.\emulators\dosbox\alt_settings.txt
IF EXIST .\util\check.txt goto install
echo.
echo You have selected to play with Integer Scaling 
echo (Previously known as "pixel perfect mode").
echo This warning\explanation will only appears once.
echo Further info can be found in the read me file.
echo.
echo Integer Scaling emulates the pixel ratio
echo of DOS Games as accurately as can be managed
echo with DOSBox, however it is not compatible with
echo all games and may cause issues - in some cases
echo making the game unplayable. If this happens
echo restart the game using the standard launch method.
echo.
echo Other potential benefits of choosing this
echo mode is that in some cases DOSBox will be
echo using a more accurate OPL Audio emulation
echo [nuked OPL] and have more accurate PC Speaker
echo emulation.
echo.
pause
echo.>>.\util\check.txt
goto install

:CRTmode
set dosbox="%folderpath%dosbox.exe"
.\util\setconsole.exe /reset
cls
echo.
echo Available shaders come in three flavours:
echo. 
echo Press 1 for Adaptive CRT Shaders
echo Press 2 for Fixed CRT Shaders
echo Press 3 for Interpolation Shaders
echo Press 4 for Scalers
echo Press 5 to Quit 
echo.
.\util\choice /C:12345 /N Please Choose:

if errorlevel = 5 goto quit
if errorlevel = 4 goto Scaler
if errorlevel = 3 goto INTshader
if errorlevel = 2 goto CRTshader
if errorlevel = 1 goto aCRTshader

:aCRTshader
set filter=crt-auto
goto addpp

:CRTshader
SETLOCAL ENABLEDELAYEDEXPANSION
cd %folderpath%resources\glshaders\crt2
cls
echo.
for /F %%a in ('dir .\ /A /B') DO (
set /A cnt=!cnt!+1
set select!cnt!=%%a
set name=%%a
echo Type !cnt! for !name:~0,-5!
)
echo.
set /p crt="Type your chosen filter here: "
if !crt!==1 set filter=crt2\!select1!
if !crt!==2 set filter=crt2\!select2!
if !crt!==3 set filter=crt2\!select3!
if !crt!==4 set filter=crt2\!select4!
if !crt!==5 set filter=crt2\!select5!
if !crt!==6 set filter=crt2\!select6!
if !crt!==7 set filter=crt2\!select7!
if !crt!==8 set filter=crt2\!select8!
if !crt!==9 set filter=crt2\!select9!
if !crt!==10 set filter=crt2\!select10!
if !crt!==11 set filter=crt2\!select11!
if !crt!==12 set filter=crt2\!select12!
if !crt!==13 set filter=crt2\!select13!
if !crt!==14 set filter=crt2\!select14!
if !crt!==15 set filter=crt2\!select15!
if !crt!==16 set filter=crt2\!select16!
if !crt!==17 set filter=crt2\!select17!
if !crt!==18 set filter=crt2\!select18!
if !crt!==19 set filter=crt2\!select19!
if !crt!==20 set filter=crt2\!select20!
if !crt!==21 set filter=crt2\!select21!
if !crt!==22 set filter=crt2\!select22!
if !crt!==23 set filter=crt2\!select23!
if !crt!==24 set filter=crt2\!select24!
if !crt!==25 set filter=crt2\!select25!
if !crt!==26 set filter=crt2\!select26!
if !crt!==27 set filter=crt2\!select27!
if !crt!==28 set filter=crt2\!select28!
if !crt!==29 set filter=crt2\!select29!
if !crt!==30 set filter=crt2\!select30!
SETLOCAL DISABLEDELAYEDEXPANSION
cd ..\..\..\..\..\..\
goto addpp

:INTshader
SETLOCAL ENABLEDELAYEDEXPANSION
cd %folderpath%resources\glshaders\interpolation
cls
echo.
for /F %%a in ('dir .\ /A /B') DO (
set /A cnt=!cnt!+1
set select!cnt!=%%a
set name=%%a
echo Type !cnt! for !name:~0,-5!
)
echo.
set /p int="Type your chosen filter here: "
if !int!==1 set filter=interpolation\!select1!
if !int!==2 set filter=interpolation\!select2!
if !int!==3 set filter=interpolation\!select3!
if !int!==4 set filter=interpolation\!select4!
if !int!==5 set filter=interpolation\!select5!
if !int!==6 set filter=interpolation\!select6!
if !int!==7 set filter=interpolation\!select7!
if !int!==8 set filter=interpolation\!select8!
if !int!==9 set filter=interpolation\!select9!
if !int!==10 set filter=interpolation\!select10!
SETLOCAL DISABLEDELAYEDEXPANSION
cd ..\..\..\..\..\..\
goto addpp

:Scaler
SETLOCAL ENABLEDELAYEDEXPANSION
cd %folderpath%resources\glshaders\scaler
cls
echo.
for /F %%a in ('dir .\ /A /B') DO (
set /A cnt=!cnt!+1
set select!cnt!=%%a
set name=%%a
echo Type !cnt! for !name:~0,-5!
)
echo.
set /p sca="Type your chosen scaler here: "
if !sca!==1 set filter=scaler\!select1!
if !sca!==2 set filter=scaler\!select2!
if !sca!==3 set filter=scaler\!select3!
if !sca!==4 set filter=scaler\!select4!
if !sca!==5 set filter=scaler\!select5!
if !sca!==6 set filter=scaler\!select6!
if !sca!==7 set filter=scaler\!select7!
if !sca!==8 set filter=scaler\!select8!
if !sca!==9 set filter=scaler\!select9!
if !sca!==10 set filter=scaler\!select10!
SETLOCAL DISABLEDELAYEDEXPANSION
cd ..\..\..\..\..\..\
goto addpp

:addpp
cls
echo.
echo Would you like to use Pixel Perfect mode at the same time?
echo.
.\util\choice /C:YN /N [Y]es or [N]o

If errorlevel = 2 goto crtnopp
If errorlevel = 1 goto crtpp

:crtpp
echo ppmode:On>.\emulators\dosbox\alt_settings.txt
echo CRT:On>>.\emulators\dosbox\alt_settings.txt
echo nslmode:Off>>.\emulators\dosbox\alt_settings.txt
set conf=".\emulators\dosbox\Staging_crt_PP.conf"
.\util\ssr 0 glshader= glshader=%filter% .\emulators\dosbox\Staging_crt_PP.conf
goto install

:crtnopp
echo ppmode:Off>.\emulators\dosbox\alt_settings.txt
echo CRT:On>>.\emulators\dosbox\alt_settings.txt
echo nslmode:Off>>.\emulators\dosbox\alt_settings.txt
set conf=".\emulators\dosbox\Staging_crt.conf"
.\util\ssr 0 glshader= glshader=%filter% .\emulators\dosbox\Staging_crt.conf
goto install

:addNSLpp
cls
echo.
echo Would you like to use Pixel Perfect mode at the same time?
echo.
.\util\choice /C:YN /N [Y]es or [N]o

If errorlevel = 2 goto NSLmodenopp
If errorlevel = 1 goto NSLmodepp

:NSLmodenopp
set filter=None
echo ppmode:Off>.\emulators\dosbox\alt_settings.txt
echo CRT:Off>>.\emulators\dosbox\alt_settings.txt
echo nslmode:On>>.\emulators\dosbox\alt_settings.txt
set dosbox="%folderpath%dosbox.exe"
set conf=".\emulators\dosbox\Staging_noline.conf"
goto install

:NSLmodepp
set filter=None
echo ppmode:On>.\emulators\dosbox\alt_settings.txt
echo CRT:Off>>.\emulators\dosbox\alt_settings.txt
echo nslmode:On>>.\emulators\dosbox\alt_settings.txt
set dosbox="%folderpath%dosbox.exe"
set conf=".\emulators\dosbox\Staging_noline_PP.conf"
goto install

:preinstall
if %conf%==".\emulators\dosbox\Staging_PP.conf" goto install
if %conf%==".\emulators\dosbox\Staging_noline.conf" goto install
if %conf%==".\emulators\dosbox\Staging_noline_PP.conf" goto install
.\util\ssr 0 glshader= glshader=%filter% %conf%
goto postinstall

:install
echo glshader:%filter%>>.\emulators\dosbox\alt_settings.txt

:postinstall
.\util\setconsole.exe /minimize
if exist .\eXoDOS\!dos\%GameDir%\exception.bat goto exception

:normal
%dosbox% -conf "%var%\dosbox.conf" -conf ".\emulators\dosbox\options.conf" -conf %conf% -exit -nomenu -noconsole
goto finish

:exception
set dosbox=%folderpath:~19%dosbox.exe
cls
.\eXoDOS\!dos\%GameDir%\exception.bat

:finish
rem .\util\ssr 0 glshader=%filter% glshader= .\emulators\dosbox\Staging_crt.conf
rem .\util\ssr 0 glshader=%filter% glshader= .\emulators\dosbox\Staging_crt_PP.conf
del stdout.txt
del stderr.txt
if exist glide.* del glide.*
if exist .\eXoDOS\CWSDPMI.swp del .\eXoDOS\CWSDPMI.swp
goto end

:none
.\util\setconsole.exe /reset
cls
echo.
echo Game has not been installed
echo.
echo.
echo Would you like to install the game?
echo.
.\util\choice

if errorlevel = 2 goto no
if errorlevel = 1 goto yes
:yes
cd %VAR%
call install.bat
cd ..
.\util\setconsole.exe /minimize
goto launch
:no
goto end

:end
