@echo off
setlocal enabledelayedexpansion

rem Check if the script is running with administrative privileges
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo Requesting administrative privileges...
    powershell -Command "Start-Process cmd -Argument '/c %~s0 %*' -Verb runAs"
    exit /b
)

Title Fixer

rem Define variables
set "ruleName=hola"
set "softwareName=HD-Player"
set "executablePath="
set "lastStatus="

rem Set path to store msi_path.txt in the Documents folder
set "docPath=%USERPROFILE%\Documents\msi_path.txt"

rem Check if the MSI App Player path is already set in the Documents folder
if exist "%docPath%" (
    for /f "delims=" %%a in (%docPath%) do set "MsiPath=%%a"
    set "executablePath=!MsiPath!"
    goto check_firewall
) else (
    set "MsiPath="
    set "executablePath="
)

:fixer_menu
cls
echo.
echo MSI App Player Path Manager
echo.
echo 1) Set MSI App Player Path
echo 2) Change Path
echo 3) Exit
echo.
set /p "choice=Select an option (1-3): "

if "%choice%"=="1" goto set_path
if "%choice%"=="2" goto change_path
if "%choice%"=="3" exit /b
goto fixer_menu

:set_path
cls
rem Default path to HD-Player.exe (usually C:\Program Files\BlueStacks_msi5\HD-Player.exe)
set "defaultMsiPath=C:\Program Files\BlueStacks_msi5\HD-Player.exe"
echo Enter the path to HD-Player.exe or press Enter to use the default path:
echo Default: !defaultMsiPath!
set /p "MsiPath=Path: "
if "!MsiPath!"=="" set "MsiPath=!defaultMsiPath!"
echo "!MsiPath!" > "%docPath%"
set "executablePath=!MsiPath!"
echo Path saved successfully in the Documents folder.
pause
goto check_firewall

:change_path
cls
if "!MsiPath!"=="" (
    echo No path is currently set.
) else (
    echo Current path: !MsiPath!
)
echo.
echo Enter the new path to HD-Player.exe:
set /p "MsiPath=New Path: "
if not "!MsiPath!"=="" (
    echo "!MsiPath!" > "%docPath%"
    set "executablePath=!MsiPath!"
)
echo Path updated successfully in the Documents folder.
pause
goto check_firewall

:check_firewall
rem Check if the firewall is enabled
netsh advfirewall show allprofiles state | find "State" | find "ON" >nul
if %errorlevel% neq 0 (
    cls
    echo Windows Firewall is disabled. Please enable the firewall to use this fixer.
    pause
    goto fixer_menu
)

:display_menu
cls
echo .............................................................................................................
echo BGMI FIXER
echo .............................................................................................................
echo.

call :check_rule_status
call :display_status
echo.
echo Select an option:
echo 1. Start Fixer [ALT + F]
echo 2. Stop Fixer [ALT + G]
echo 3. No match found fix [ALT + H]
echo 4. Ignore this option
echo 5. Go to MSI App player path manager

:wait_for_input
rem Wait for user input or check status every second
choice /C 12345 /N /T 1 /D 4 >nul
if errorlevel 5 (
    goto fixer_menu
)
if errorlevel 4 (
    call :check_rule_status
    if "!currentStatus!" neq "!lastStatus!" (
        goto display_menu
    )
    goto wait_for_input
)
if errorlevel 3 (
    call :no_match_fix
    goto display_menu
)
if errorlevel 2 (
    call :stop_fixer
    goto display_menu
)
if errorlevel 1 (
    call :start_fixer
    goto display_menu
)
goto wait_for_input

:check_rule_status
netsh advfirewall firewall show rule name="%ruleName%" >nul 2>&1
if %errorlevel% equ 0 (
    set "currentStatus=on"
) else (
    set "currentStatus=off"
)
exit /b

:display_status
rem Set color and display current status
if "%currentStatus%"=="on" (
    color 2F
    echo Current Status : Fixer on
) else (
    color 4F
    echo Current Status : Fixer off
)
set "lastStatus=%currentStatus%"
exit /b

:start_fixer
echo Starting Fixer..
echo Using executable path: "!executablePath!"  rem Debugging information
netsh advfirewall firewall add rule name="%ruleName%" protocol=TCP remoteport=80-17500 dir=out action=block program="!executablePath!" >nul 2>&1
if %errorlevel% neq 0 (
    echo There was an error adding the firewall rule. Please check the path and try again.
    pause
    goto display_menu
)
powershell -c "[console]::beep(1500,500)" >nul 2>&1
powershell -c "[console]::beep(1500,500)" >nul 2>&1
exit /b

:stop_fixer
echo Stopping Fixer....
netsh advfirewall firewall delete rule name="%ruleName%" >nul 2>&1
powershell -c "[console]::beep(500,1000)" >nul 2>&1
exit /b

:no_match_fix
echo Fixing...
netsh advfirewall firewall add rule name="%ruleName%" protocol=TCP remoteport=80-17500 dir=out action=block program="!executablePath!" >nul 2>&1
powershell -c "[console]::beep(1500,500)" >nul 2>&1
powershell -c "[console]::beep(1500,500)" >nul 2>&1

timeout /t 2 >nul

rem Delete the rules to clean up, combining commands into a single line
netsh advfirewall firewall delete rule name="%ruleName%" >nul 2>&1
powershell -c "[console]::beep(1500,500)" >nul 2>&1
exit /b
when i am running the code first time after setting the path then fixer works perfectly but when i close it and open the code next time the fixer doesnt work , i guess it has problem fetching the path from txt file