@echo off
@setlocal EnableExtensions EnableDelayedExpansion
@title PESBUL 2021 SERVER EMULATOR
@mode con: cols=70 lines=23
@color 0B

@set "HOSTS_FILE=%SystemRoot%\System32\drivers\etc\hosts"
@set "BACKUP_FILE=%SystemRoot%\System32\drivers\etc\hosts.pesbul-backup"
@set "TEMP_FILE=%TEMP%\pesbul_hosts_%RANDOM%_%RANDOM%.tmp"

@set "P1=150"
@set "P2=230"
@set "P3=148"
@set "P4=219"
@set "TARGET=%P1%.%P2%.%P3%.%P4%"

@fltmc >nul 2>&1
@if errorlevel 1 (
    @powershell.exe -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    @exit /b
)

:MENU
@cls
@echo.
@echo     +--------------------------------------------------------+
@echo     ^|                                                        ^|
@echo     ^|              P E S B U L   2 0 2 1                     ^|
@echo     ^|                                                        ^|
@echo     ^|                 SERVER EMULATOR                        ^|
@echo     ^|                                                        ^|
@echo     +--------------------------------------------------------+
@echo.
@echo.
@echo                  [ 1 ]  CONNECT
@echo.
@echo                  [ 2 ]  DISCONNECT
@echo.
@echo.
@echo     ----------------------------------------------------------
@choice /c 12 /n /m "     Select option: "

@if errorlevel 2 goto DISCONNECT
@if errorlevel 1 goto CONNECT
@goto MENU

:CONNECT
@cls
@call :HEADER
@echo.
@echo                  Preparing connection...
@echo.

@if not exist "%HOSTS_FILE%" goto WRITE_ERROR

@if not exist "%BACKUP_FILE%" (
    @copy /y "%HOSTS_FILE%" "%BACKUP_FILE%" >nul 2>&1
    @if errorlevel 1 goto WRITE_ERROR
)

@findstr /V /I ^
 /C:"pes21-x64-gate.cs.konami.net" ^
 /C:"pes21-x64-stun.cs.konami.net" ^
 /C:"pesam.stun.service.konami.net" ^
 /C:"info.service.konami.net" ^
 /C:"cs.konami.net" ^
 /C:"ntl.service.konami.net" ^
 /C:"ntleu.service.konami.net" ^
 "%HOSTS_FILE%" > "%TEMP_FILE%" 2>nul

@if not exist "%TEMP_FILE%" goto WRITE_ERROR

@>>"%TEMP_FILE%" echo.
@>>"%TEMP_FILE%" echo # PESBUL 2021
@>>"%TEMP_FILE%" echo %TARGET% pes21-x64-gate.cs.konami.net
@>>"%TEMP_FILE%" echo %TARGET% pes21-x64-stun.cs.konami.net
@>>"%TEMP_FILE%" echo %TARGET% pesam.stun.service.konami.net
@>>"%TEMP_FILE%" echo %TARGET% info.service.konami.net
@>>"%TEMP_FILE%" echo %TARGET% cs.konami.net
@>>"%TEMP_FILE%" echo %TARGET% ntl.service.konami.net
@>>"%TEMP_FILE%" echo %TARGET% ntleu.service.konami.net

@copy /y "%TEMP_FILE%" "%HOSTS_FILE%" >nul 2>&1
@if errorlevel 1 goto WRITE_ERROR
@del /q "%TEMP_FILE%" >nul 2>&1

@findstr /L /C:"%TARGET% pes21-x64-gate.cs.konami.net" "%HOSTS_FILE%" >nul 2>&1
@if errorlevel 1 goto VERIFY_ERROR

@findstr /L /C:"%TARGET% ntleu.service.konami.net" "%HOSTS_FILE%" >nul 2>&1
@if errorlevel 1 goto VERIFY_ERROR

@ipconfig /flushdns >nul 2>&1
@call :CONNECT_PROGRESS
@call :MEASURE_PING

@cls
@call :HEADER
@echo.
@echo.
@echo                  [ OK ] CONNECTED SUCCESSFULLY
@echo.
@echo                  SERVER PING: !PING_TEXT!
@echo.
@echo                  Start eFootball PES 2021.
@echo.
@echo.
@echo     ----------------------------------------------------------
@echo                  Press any key to continue.
@echo     ----------------------------------------------------------
@pause >nul
@goto MENU

:DISCONNECT
@cls
@call :HEADER
@echo.
@echo                  Removing PESBUL connection...
@echo.

@if not exist "%HOSTS_FILE%" goto WRITE_ERROR

@findstr /V /I ^
 /C:"pes21-x64-gate.cs.konami.net" ^
 /C:"pes21-x64-stun.cs.konami.net" ^
 /C:"pesam.stun.service.konami.net" ^
 /C:"info.service.konami.net" ^
 /C:"cs.konami.net" ^
 /C:"ntl.service.konami.net" ^
 /C:"ntleu.service.konami.net" ^
 "%HOSTS_FILE%" > "%TEMP_FILE%" 2>nul

@if not exist "%TEMP_FILE%" goto WRITE_ERROR

@copy /y "%TEMP_FILE%" "%HOSTS_FILE%" >nul 2>&1
@if errorlevel 1 goto WRITE_ERROR
@del /q "%TEMP_FILE%" >nul 2>&1
@ipconfig /flushdns >nul 2>&1
@call :DISCONNECT_PROGRESS

@cls
@call :HEADER
@echo.
@echo.
@echo                  [ OK ] DISCONNECTED
@echo.
@echo          PESBUL entries were removed successfully.
@echo          All other hosts entries were preserved.
@echo.
@echo.
@pause >nul
@goto MENU

:MEASURE_PING
@set "PING_MS="
@set "PING_TEXT=Unavailable"
@for /f "usebackq delims=" %%P in (`powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$times=@(); 1..3|ForEach-Object{$c=New-Object Net.Sockets.TcpClient; $sw=[Diagnostics.Stopwatch]::StartNew(); try{$a=$c.BeginConnect('%TARGET%',5000,$null,$null); if($a.AsyncWaitHandle.WaitOne(3000,$false) -and $c.Connected){$c.EndConnect($a); $sw.Stop(); $times+=$sw.Elapsed.TotalMilliseconds}}catch{}finally{$sw.Stop();$c.Close()}}; if($times.Count -gt 0){[math]::Round(($times|Measure-Object -Average).Average)}" 2^>nul`) do @set "PING_MS=%%P"
@if defined PING_MS @set "PING_TEXT=!PING_MS! ms"
@exit /b

:CONNECT_PROGRESS
@cls
@call :HEADER
@echo.
@echo                  Connecting...
@echo.
@echo             [######------------------]  25%%
@timeout /t 1 /nobreak >nul

@cls
@call :HEADER
@echo.
@echo                  Connecting...
@echo.
@echo             [############------------]  50%%
@timeout /t 1 /nobreak >nul

@cls
@call :HEADER
@echo.
@echo                  Connecting...
@echo.
@echo             [##################------]  75%%
@timeout /t 1 /nobreak >nul

@cls
@call :HEADER
@echo.
@echo                  Connecting...
@echo.
@echo             [########################] 100%%
@timeout /t 1 /nobreak >nul
@exit /b

:DISCONNECT_PROGRESS
@cls
@call :HEADER
@echo.
@echo                  Disconnecting...
@echo.
@echo             [########----------------]  33%%
@timeout /t 1 /nobreak >nul

@cls
@call :HEADER
@echo.
@echo                  Disconnecting...
@echo.
@echo             [################--------]  66%%
@timeout /t 1 /nobreak >nul

@cls
@call :HEADER
@echo.
@echo                  Disconnecting...
@echo.
@echo             [########################] 100%%
@timeout /t 1 /nobreak >nul
@exit /b

:HEADER
@echo.
@echo     +--------------------------------------------------------+
@echo     ^|              PESBUL 2021 SERVER EMULATOR               ^|
@echo     +--------------------------------------------------------+
@exit /b

:WRITE_ERROR
@del /q "%TEMP_FILE%" >nul 2>&1
@cls
@call :HEADER
@echo.
@echo.
@echo                  [ ERROR ] UPDATE FAILED
@echo.
@echo          Windows could not update the hosts file.
@echo          Check Windows Security or antivirus protection.
@echo.
@echo.
@pause >nul
@goto MENU

:VERIFY_ERROR
@del /q "%TEMP_FILE%" >nul 2>&1
@cls
@call :HEADER
@echo.
@echo.
@echo                  [ ERROR ] VERIFY FAILED
@echo.
@echo          The connection entries were not written correctly.
@echo.
@echo.
@pause >nul
@goto MENU
