@echo off
rem Learn or Die voice helper for Windows 10 and 11 (64-bit). Double-click it before you play; keep it for next time.
rem It gets the helper from learnordie.me and starts it. Everything stays in one folder:
rem   %LOCALAPPDATA%\LearnOrDie\voice-helper   (delete that folder to remove it all)
rem No administrator rights are needed.
setlocal
title Learn or Die - voice helper
set "BASE=https://learnordie.me/voice"
if defined LOD_HELPER_BASE set "BASE=%LOD_HELPER_BASE%"
set "DIR=%LOCALAPPDATA%\LearnOrDie\voice-helper"
if defined LOD_HELPER_DIR set "DIR=%LOD_HELPER_DIR%"
set "UV_VERSION=0.11.23"
set "UV_SHA256=02ad29f07e674d68726ba3bb1ff25b335d83515756e2b1a194bb56c3cc30e07c"

echo Learn or Die voice helper
echo Keep this window open while you play. Close it to turn voice answers off.
echo The first start downloads about 1.5 GB: Python, PyTorch and the speech model of your language. Please wait.
echo.

if /i not "%PROCESSOR_ARCHITECTURE%"=="AMD64" goto unsupported
where curl.exe >nul 2>nul || goto oldwindows
where tar.exe >nul 2>nul || goto oldwindows

if not exist "%DIR%" mkdir "%DIR%"
cd /d "%DIR%" || goto nofolder

rem the helper's own files (a few small text files), checked before they replace the ones already here
curl.exe -fsSL -m 60 -o voice-helper.tar.gz.new "%BASE%/voice-helper.tar.gz"
if errorlevel 1 goto try_local
tar.exe -tzf voice-helper.tar.gz.new >nul 2>nul
if errorlevel 1 goto try_local
move /y voice-helper.tar.gz.new voice-helper.tar.gz >nul
goto unpack

:try_local
if exist voice-helper.tar.gz.new del voice-helper.tar.gz.new
rem no download: a copy of the helper's files lying next to this file is used, else the files already here
if not exist "%~dp0voice-helper.tar.gz" goto offline
echo Using the helper files next to this file.
copy /y "%~dp0voice-helper.tar.gz" voice-helper.tar.gz >nul

:unpack
tar.exe -xzf voice-helper.tar.gz
if errorlevel 1 goto badfiles
goto files_ready

:offline
if not exist server.py goto nointernet
echo No internet: starting the helper that is already here.

:files_ready
if exist "uv\uv.exe" goto run

rem uv installs Python and the packages; a fixed release from its own GitHub page, checked against its published SHA-256
echo Downloading uv %UV_VERSION% (it installs Python for the helper)...
curl.exe -fL --progress-bar -o uv.zip "https://github.com/astral-sh/uv/releases/download/%UV_VERSION%/uv-x86_64-pc-windows-msvc.zip"
if errorlevel 1 goto nouv
set "GOT="
for /f "skip=1 delims=" %%h in ('certutil -hashfile uv.zip SHA256') do if not defined GOT set "GOT=%%h"
set "GOT=%GOT: =%"
if /i not "%GOT%"=="%UV_SHA256%" goto baduv
rem unpacked next to its place and renamed when whole: a window closed half-way leaves nothing that looks finished
if exist uv.tmp rmdir /s /q uv.tmp
mkdir uv.tmp
tar.exe -xf uv.zip -C uv.tmp
if errorlevel 1 goto baduv
del uv.zip
if not exist "uv.tmp\uv.exe" goto baduv
if exist uv rmdir /s /q uv
ren uv.tmp uv
if not exist "uv\uv.exe" goto baduv

:run
set "UV_CACHE_DIR=%DIR%\cache\uv"
set "UV_PYTHON_INSTALL_DIR=%DIR%\python"
set "UV_PYTHON_PREFERENCE=only-managed"
set "HF_HOME=%DIR%\cache\huggingface"
set "HF_HUB_DISABLE_TELEMETRY=1"
set "HF_HUB_DISABLE_XET=1"
if not defined ZH_HELPER_PORT set "ZH_HELPER_PORT=17645"
rem none: no speech model until the game says which language you learn
if not defined ZH_HELPER_LANG set "ZH_HELPER_LANG=none"
"uv\uv.exe" run --frozen python server.py
if not errorlevel 1 exit /b 0
rem no brackets around these lines: a folder name with a bracket in it would end the block early
echo.
echo The voice helper stopped with an error, see the messages above.
echo Start this file again. If the error stays, delete the folder "%DIR%" and start this file once more.
pause
exit /b 1

:unsupported
echo Voice answers need 64-bit Windows 10 or 11 on an Intel or AMD processor.
goto stop
:oldwindows
echo This Windows is too old for the voice helper: it needs Windows 10 (version 1803 or newer) or Windows 11.
goto stop
:nofolder
echo Could not make the folder %DIR%
goto stop
:nointernet
echo Could not download the helper. Check your internet connection and start this file again.
goto stop
:badfiles
echo Could not unpack the helper. Start this file again.
goto stop
:nouv
echo Could not download uv. Check your internet connection and start this file again.
goto stop
:baduv
if exist uv.zip del uv.zip
echo The uv download is damaged. Start this file again.
goto stop
:stop
echo.
pause
exit /b 1
