@echo off
setlocal
chcp 65001 >nul
cd /d "%~dp0"
rem Scan this folder, .minecraft/minecraft, then their versions/*; only existing mods directories.
if not defined EXTRA_HTTP set "EXTRA_HTTP=http://sn.zer0m1nd.me:42003"
set "EXTRA_TEMP="
for /f %%G in ('powershell -NoProfile -Command "[Guid]::NewGuid().ToString()"') do set "EXTRA_TEMP=%TEMP%\ATM9Extra-%%G"
if not defined EXTRA_TEMP goto failed
mkdir "%EXTRA_TEMP%"
if errorlevel 1 goto failed
curl.exe -fS --connect-timeout 15 "%EXTRA_HTTP%/hash/files/extra/EXTRA-UPDATER.jar" -o "%EXTRA_TEMP%\hash.json"
if errorlevel 1 goto failed
curl.exe -fS --connect-timeout 15 "%EXTRA_HTTP%/download/files/extra/EXTRA-UPDATER.jar" -o "%EXTRA_TEMP%\updater.jar"
if errorlevel 1 goto failed
powershell -NoProfile -Command "$ErrorActionPreference='Stop'; $p=$env:EXTRA_TEMP; $h=(Get-Content -LiteralPath (Join-Path $p 'hash.json') -Raw | ConvertFrom-Json).sha256; if ($h -notmatch '^[a-f0-9]{64}$' -or ([BitConverter]::ToString([Security.Cryptography.SHA256]::Create().ComputeHash([IO.File]::ReadAllBytes((Join-Path $p 'updater.jar'))))).Replace('-','') -ne $h) { throw 'Updater hash mismatch' }"
if errorlevel 1 goto failed
java -jar "%EXTRA_TEMP%\updater.jar" "%CD%" "atm9-tc4" "%EXTRA_HTTP%"
set "result=%ERRORLEVEL%"
goto finished
:failed
set "result=1"
echo Update failed. Close Minecraft and check the messages above.
:finished
if not defined EXTRA_NO_PAUSE pause
exit /b %result%
