@echo off
setlocal EnableExtensions EnableDelayedExpansion

:: ==============================================================
::   RETOPIA - Connect Tool
::   ID: Mengarahkan domain Growtopia ke ReTopia lewat file
::       hosts. Menu Restore mengembalikan seperti semula.
::   EN: Points the Growtopia domains at ReTopia through the
::       hosts file. The Restore menu puts everything back.
:: ==============================================================

title ReTopia - Connect Tool
color 0B

:: ---------------- KONFIGURASI / CONFIG ----------------
set "PS_IP=15.235.204.35"
set "HOSTS=%SystemRoot%\System32\drivers\etc\hosts"
set "BACKUP=%SystemRoot%\System32\drivers\etc\hosts.piter.bak"
set "TMP_OUT=%TEMP%\ps_hosts_%RANDOM%.tmp"
set "MARK_A=# === PITER SERVER redirect BEGIN ==="
set "MARK_B=# === PITER SERVER redirect END ==="

:: Domain yang di-resolve client -> semua diarahkan ke PS_IP.
set "DOM.1=www.growtopia1.com"
set "DOM.2=www.growtopia2.com"
set "DOM.3=growtopia1.com"
set "DOM.4=growtopia2.com"
set "DOM.5=growtopiagame.com"
set "DOM.6=login.growtopiagame.com"
set "DOM_COUNT=6"

:: ---------------- MINTA HAK ADMIN / ELEVATION ----------------
net session >nul 2>&1
if not "%errorlevel%"=="0" (
    echo Butuh hak administrator untuk mengubah file hosts.
    echo Administrator rights are needed to edit the hosts file.
    echo Meminta izin elevasi... / Requesting elevation...
    powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs" >nul 2>&1
    exit /b
)

:: ---------------- PILIH BAHASA / LANGUAGE ----------------
:LANGSEL
cls
echo ================================================================
echo                 RETOPIA  -  CONNECT TOOL
echo ================================================================
echo.
echo   [1] Bahasa Indonesia
echo   [2] English
echo.
set "LANGPICK="
set /p "LANGPICK=Pilih bahasa / Choose language (1/2), lalu Enter: "
if "!LANGPICK!"=="1" goto SET_ID
if "!LANGPICK!"=="2" goto SET_EN
goto LANGSEL

:SET_ID
set "L_TARGET=Target server :"
set "L_HOSTSF=File hosts    :"
set "L_NOTE1=Catatan: kalau antivirus memblokir perubahan hosts, izinkan"
set "L_NOTE2=         dulu atau matikan sementara."
set "L_M1=Connect      - arahkan Growtopia ke ReTopia"
set "L_M2=Restore      - hapus pengarahan, balik ke semula"
set "L_M3=Cek status   - lihat entri yang sedang aktif"
set "L_M4=Keluar"
set "L_M5=Change language - ganti ke bahasa Inggris"
set "L_PICK=Pilih (1/2/3/4/5), lalu Enter: "
set "L_BACKUP=Backup hosts asli disimpan:"
set "L_CLEAN=Membersihkan entri lama..."
set "L_ADD=Menambahkan entri ReTopia..."
set "L_FLUSH=Membersihkan cache DNS..."
set "L_DONE= SELESAI. Tutup total aplikasi Growtopia, buka lagi, lalu main."
set "L_RESTORED=Pengarahan ReTopia sudah dihapus. Hosts kembali normal."
set "L_ST_HDR=Entri terkait Growtopia yang aktif sekarang:"
set "L_ST_NONE=  [tidak ada - client akan masuk ke server resmi]"
set "L_ERR=GAGAL membaca file hosts - hosts TIDAK diubah. Cek antivirus."
goto MENU

:SET_EN
set "L_TARGET=Target server :"
set "L_HOSTSF=Hosts file    :"
set "L_NOTE1=Note: if your antivirus blocks hosts-file changes, allow the"
set "L_NOTE2=      change first or disable it for a moment."
set "L_M1=Connect      - point Growtopia at ReTopia"
set "L_M2=Restore      - remove the redirect, back to normal"
set "L_M3=Status       - show the entries currently active"
set "L_M4=Exit"
set "L_M5=Ganti bahasa - switch to Indonesian"
set "L_PICK=Choose (1/2/3/4/5), then Enter: "
set "L_BACKUP=Original hosts backed up to:"
set "L_CLEAN=Removing old entries..."
set "L_ADD=Adding ReTopia entries..."
set "L_FLUSH=Flushing the DNS cache..."
set "L_DONE= DONE. Fully close Growtopia, open it again, and play."
set "L_RESTORED=The ReTopia redirect has been removed. Hosts is back to normal."
set "L_ST_HDR=Growtopia-related entries currently active:"
set "L_ST_NONE=  [none - the client will join the official server]"
set "L_ERR=FAILED to read the hosts file - hosts NOT changed. Check your antivirus."
goto MENU

:: ---------------- MENU ----------------
:MENU
cls
echo ================================================================
echo                 RETOPIA  -  CONNECT TOOL
echo ================================================================
echo.
echo   %L_TARGET% %PS_IP%
echo   %L_HOSTSF% %HOSTS%
echo.
echo   %L_NOTE1%
echo   %L_NOTE2%
echo.
echo   [1] %L_M1%
echo   [2] %L_M2%
echo   [3] %L_M3%
echo   [4] %L_M4%
echo   [5] %L_M5%
echo.
set "PICK="
set /p "PICK=%L_PICK%"
if "!PICK!"=="1" goto CONNECT
if "!PICK!"=="2" goto RESTORE
if "!PICK!"=="3" goto STATUS
if "!PICK!"=="4" exit /b
if "!PICK!"=="5" goto LANGSEL
goto MENU

:: ---------------- CONNECT ----------------
:CONNECT
echo.
call :BackupOnce
call :StripBlock || goto MENU
echo %L_ADD%

>>"%HOSTS%" echo.
>>"%HOSTS%" echo %MARK_A%
for /L %%i in (1,1,%DOM_COUNT%) do (
    >>"%HOSTS%" echo %PS_IP% !DOM.%%i!
    echo   + %PS_IP% !DOM.%%i!
)
>>"%HOSTS%" echo %MARK_B%

echo.
echo %L_FLUSH%
ipconfig /flushdns >nul
echo.
echo ================================================================
echo %L_DONE%
echo ================================================================
echo.
pause
goto MENU

:: ---------------- RESTORE ----------------
:RESTORE
echo.
call :StripBlock || goto MENU
echo %L_FLUSH%
ipconfig /flushdns >nul
echo.
echo %L_RESTORED%
echo.
pause
goto MENU

:: ---------------- STATUS ----------------
:STATUS
echo.
echo %L_ST_HDR%
echo ----------------------------------------------------------------
findstr /I "growtopia" "%HOSTS%"
if errorlevel 1 echo %L_ST_NONE%
echo ----------------------------------------------------------------
echo.
pause
goto MENU

:: ================= SUBRUTIN / SUBROUTINES =================

:: Backup file hosts asli SEKALI saja (yang pertama = kondisi bersih).
:BackupOnce
if not exist "%BACKUP%" (
    copy /Y "%HOSTS%" "%BACKUP%" >nul
    echo %L_BACKUP% %BACKUP%
)
goto :eof

:: Buang semua baris pengarahan Growtopia (baris kita + penanda).
:: findstr /V = ambil baris yang TIDAK mengandung pola-pola ini.
:: errorlevel 2 = findstr gagal BACA (mis. diblokir AV) -> JANGAN
:: timpa hosts dengan file kosong, batalkan saja.
:StripBlock
echo %L_CLEAN%
findstr /V /I /C:"growtopia" /C:"PITER SERVER redirect" "%HOSTS%" > "%TMP_OUT%"
if errorlevel 2 (
    del "%TMP_OUT%" >nul 2>&1
    echo %L_ERR%
    pause
    exit /b 1
)
copy /Y "%TMP_OUT%" "%HOSTS%" >nul
del "%TMP_OUT%" >nul 2>&1
exit /b 0
