@echo off

cls
echo ***********************************************************************************************************************
echo *** Minecraft vanilla textures upscaler - HardTopNet 2019-2020                                                      ***
echo *** Version 0.91b                                                                                                   ***
echo ***********************************************************************************************************************


REM -=- CHANGE HERE -=-=-=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=-
REM paths should end with \
REM leave jarpath blank to use the default JAR set in path
set minecraftfolder=%appdata%\.minecraft\
set jarpath=
REM -=- END CHANGE HERE -=--=-=-=--=-=-=--=-=-=--=-=-=--=-=-=-

set ver=%1
set upscale=%2

REM CHECK ARGS & PREREQUS ****************************************************************************************

if [%ver%]==[] goto NOARG
if [%upscale%]==[] goto NOARGSCALE

set upscale=%upscale%x
for /F "tokens=2 delims=." %%a in ("%ver%") do set subver=%%a

set packformat=2
if %subver% geq 11 set packformat=3

if %subver% geq 13 goto POSTFLATTENING
:PREFLATTENING
set block=blocks
goto FLATTENINGDONE
:POSTFLATTENING
set block=block
set packformat=4
:FLATTENINGDONE
if %subver% geq 15 set packformat=5

echo.

REM magick
where /R "c:\Program Files" magick.exe /Q
if %errorlevel%==0 goto MAGICKISINSTALLED
where /R "c:\Program Files (x86)" magick.exe /Q
if %errorlevel%==0 goto MAGICKISINSTALLED
where magick.exe /Q
if %errorlevel%==0 goto MAGICKISINSTALLED
goto MAGICKISNOTINSTALLED

:MAGICKISINSTALLED
for /f "tokens=*" %%a in ('where magick.exe') do (
	set magickinstallpath=%%a
	goto MAGICKDONE
)
:MAGICKDONE
echo Found ImageMagick: %magickinstallpath%

REM 7zip
where /R "c:\Program Files" 7z.exe /Q
if %errorlevel%==0 goto ZIPISINSTALLED
where /R "c:\Program Files (x86)" 7z.exe /Q
if %errorlevel%==0 goto ZIPISINSTALLED
where 7z.exe /Q
if %errorlevel%==0 goto ZIPISINSTALLED
goto ZIPISNOTINSTALLED

:ZIPISINSTALLED
for /f "tokens=*" %%a in ('where /R "c:\Program Files" 7z.exe') do (	
	set zipinstallpath=%%a
	goto ZIPDONE
)
:ZIPDONE
echo Found 7-Zip: %zipinstallpath%

REM minecraft
if exist %minecraftfolder% goto MINECRAFTFOUND
set minecraftfolder=c:\users\%username%\appdata\roaming\.minecraft
if exist %minecraftfolder% goto MINECRAFTFOUND
goto MINECRAFTNOTFOUND
:MINECRAFTFOUND
echo Found Minecraft installation: %minecraftfolder%

REM temp
set tempfolder=%temp%
if [%tempfolder%]==[] goto TEMPNOTFOUND
goto TEMPFOUND
:TEMPNOTFOUND
set tempfolder=%localappdata%\temp
if [%tempfolder%]==[] goto TEMPNOTFOUNDB
goto TEMPFOUND
:TEMPNOTFOUNDB
set tempfolder=C:\TEMP
:TEMPFOUND
echo Temp folder: %tempfolder%

REM CHECK FOLDERS ****************************************************************************************
if exist "%tempfolder%" goto TEMPFOLDEREXISTS
md "%tempfolder%"

:TEMPFOLDEREXISTS
if exist "%tempfolder%\mcvanillaupscale" goto TEMPMCFOLDEREXISTS
goto CHECKVERSIONJAR

:TEMPMCFOLDEREXISTS
echo Cleaning up temp dir...
rmdir "%tempfolder%\mcvanillaupscale" /S /Q

:CHECKVERSIONJAR
if exist "%minecraftfolder%\versions\%ver%\%ver%.jar" goto EXTRACT
goto VERSIONJARDOESNTEXIST

:EXTRACT

REM PROCEEDING WITH UPSCALING **************************************************************************************
md "%tempfolder%\mcvanillaupscale\assets\minecraft\textures\%block%"
md "%tempfolder%\mcvanillaupscale\input"

echo Extracting version %ver%...

set TEMP_DRIVE=%temp:~0,2%
set CURRENT_DIR=%cd%
set CURRENT_DRIVE=%cd:~0,2%

%TEMP_DRIVE%
cd "%tempfolder%\mcvanillaupscale\input"

"%jarpath%jar" xf "%minecraftfolder%\versions\%ver%\%ver%.jar" assets/minecraft/textures/%block%
"%jarpath%jar" xf "%minecraftfolder%\versions\%ver%\%ver%.jar" pack.png

%CURRENT_DRIVE%
cd %CURRENT_DIR%

echo Converting blocks to %upscale%

"%magickinstallpath%" mogrify -resize %upscale% -filter Point -path "%tempfolder%\mcvanillaupscale\assets\minecraft\textures\%block%" "%tempfolder%\mcvanillaupscale\input\assets\minecraft\textures\%block%\*.png"
copy "%tempfolder%\mcvanillaupscale\input\pack.png" "%tempfolder%\mcvanillaupscale\" >NUL
copy "%tempfolder%\mcvanillaupscale\input\assets\minecraft\textures\%block%\*.mcmeta" "%tempfolder%\mcvanillaupscale\assets\minecraft\textures\%block%" >NUL

echo Generating pack.mcmeta file

set mcmetafile="%tempfolder%\mcvanillaupscale\pack.mcmeta"
echo {>>%mcmetafile%
echo  "pack": {>>%mcmetafile%
echo    "pack_format": %packformat%,>>%mcmetafile%
echo    "description": "Upscaled Vanilla pack %upscale%\nVersion %ver%">>%mcmetafile%
echo  }>>%mcmetafile%
echo }>>%mcmetafile%

echo Cleaning up...

rmdir "%tempfolder%\mcvanillaupscale\input" /Q /S

REM ZIP UPSCALED TEXTURE PACK **************************************************************************************
echo Zipping file UPSCALE_%ver%_%upscale%.zip to %minecraftfolder%\resourcepacks folder

"%zipinstallpath%" a -r "%minecraftfolder%\resourcepacks\UPSCALE_%ver%_%upscale%.zip" "%temp%\mcvanillaupscale\*.*"

echo Cleaning up...

rmdir "%tempfolder%\mcvanillaupscale" /Q /S

echo ALL DONE.

goto END

REM DONE **************************************************************************************

:NOARG
:NOARGSCALE
echo.
echo USAGE	vanilla_upscale_script [version] [res]
echo.
echo where	[version] is the Minecraft version (1.12.2, 1.14.4, ...)
echo 		[res] is the desired upscale resolution (64, 256, ...)
echo.
echo eg:	vanilla_upscale_script 1.13.2 128
echo		to create a new resource pack with blocks from version 1.13.2
echo		upscaled to 128x
goto END

:MAGICKISNOTINSTALLED
echo.
echo ImageMagick doesn't seem to be installed. Please install ImageMagick before running this script.
echo Official webpage: https://imagemagick.org/
goto END

:ZIPISNOTINSTALLED
echo.
echo 7-Zip doesn't seem to be installed. Please install 7-Zip before running this script.
echo Official webpage: https://www.7-zip.org/
goto END

:MINECRAFTNOTFOUND
echo.
echo Minecraft installation was not found at location: %minecraftfolder%
echo please edit this script file to change the Minecraft install folder path.
goto END

:VERSIONJARDOESNTEXIST
echo.
echo JAR file not found for the specified version. Please make sure version %ver% is installed.
echo Check folder %minecraftfolder%\versions\ for currently installed versions.
goto END

:END
echo.
pause
echo.

