@echo off
title ClassicKBD Launcher - New Pre-Alpha
cd /d "%~dp0"

REM Prefer the portable Java shipped in runtime\ (players do not need to install Java)
set "JAVA_EXE="
if exist "%~dp0runtime\bin\java.exe" (
  REM Skip zeroed/corrupt runtime java.exe (MZ header missing)
  powershell -NoProfile -Command "if ((Get-Content -LiteralPath '%~dp0runtime\bin\java.exe' -Encoding Byte -TotalCount 2)[0] -eq 77) { exit 0 } else { exit 1 }" >nul 2>&1
  if not errorlevel 1 set "JAVA_EXE=%~dp0runtime\bin\java.exe"
)
if not defined JAVA_EXE if exist "%~dp0jre\bin\java.exe" set "JAVA_EXE=%~dp0jre\bin\java.exe"
if not defined JAVA_EXE if exist "C:\Java8\bin\java.exe" set "JAVA_EXE=C:\Java8\bin\java.exe"
if not defined JAVA_EXE set "JAVA_EXE=java"

"%JAVA_EXE%" -jar "%~dp0ClassicKBD_Launcher.jar"
if errorlevel 1 (
  echo.
  echo If Java failed to start, re-download ClassicKBD_Launcher.zip and Extract All
  echo so Launch.bat, the jar, payload\, and runtime\ are in the same folder.
  echo Or install/use Java 8 at C:\Java8.
  pause
)
