Advertisement
sintrode

Minecraft Server Updater

Jan 24th, 2021
3,350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 10.07 KB | None | 0 0
  1. ::------------------------------------------------------------------------------
  2. :: NAME
  3. ::     updatemcjar.bat
  4. ::
  5. :: USAGE
  6. ::     updatemcjar.bat [options]
  7. ::
  8. :: DESCRIPTION
  9. ::     A shell script for updating the Minecraft server jar file on Windows.
  10. ::     A batch port of updatemcjar.sh by Andrew Haskell.
  11. ::     (https://minecraft.gamepedia.com/Tutorials/Linux_server_update_script)
  12. ::
  13. :: OPTIONAL ARGUMENTS
  14. ::     -f, --force      If the Target Version SHA1 matches the current
  15. ::                      file's SHA1, force the update anyway, implies --yes
  16. ::     -h, --help       Print this help message
  17. ::     --jar-path       Specify a different final JAR path, default is
  18. ::                      %JAR_PATH%
  19. ::     --manifest       Specify a different version manifest URL, default
  20. ::                      is %VERSION_MANIFEST%
  21. ::     --no-err         Suppress error messages (Dangerous!)
  22. ::     -s, --silent     Suppress script output, implies --yes
  23. ::     -t, --test       Test the Target Version SHA1 against the current
  24. ::                      file's SHA1 without changing any files
  25. ::     --temp-dir       Specify a different temporary directory,
  26. ::                      default is %TEMP_DIR%
  27. ::     -v, --version    Specify a different target version; without this
  28. ::                      parameter, the latest release version is used
  29. ::     -y, --yes        Skip update confirmation
  30. ::
  31. :: REQUIREMENTS
  32. ::     cURL (https://curl.haxx.se/) - Included in Windows 10
  33. ::
  34. :: AUTHOR
  35. ::     sintrode#4642 (/u/Shadow_Thief)
  36. ::
  37. :: VERSION HISTORY
  38. ::     1.0 (2020-09-18) - Initial Version
  39. ::
  40. :: NOTES
  41. ::     - This was originally going to be distributed under the WTFPL license,
  42. ::       but curl requires the MIT license
  43. ::     - You will still have to manually start your server
  44. ::------------------------------------------------------------------------------
  45. :: Copyright (c) 2020 sintrode#4642
  46. :: Permission is hereby granted, free of charge, to any person obtaining a copy
  47. :: of this software and associated documentation files (the "Software"), to deal
  48. :: in the Software without restriction, including without limitation the rights
  49. :: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  50. :: copies of the Software, and to permit persons to whom the Software is
  51. :: furnished to do so, subject to the following conditions:
  52. ::
  53. :: The above copyright notice and this permission notice shall be included in
  54. :: all copies or substantial portions of the Software.
  55. ::
  56. :: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  57. :: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  58. :: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  59. :: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  60. :: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  61. :: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  62. :: SOFTWARE.
  63. ::------------------------------------------------------------------------------
  64. @echo off
  65. setlocal enabledelayedexpansion
  66.  
  67. set "print_n=<nul set /p ="
  68. :: Default settings (can be changed through parameters)
  69. set "TEMP_DIR=%TEMP%\updatemc"
  70. set "JAR_PATH=%USERPROFILE%\minecraft\server\server.jar"
  71. set "VERSION_MANIFEST=https://launchermeta.mojang.com/mc/game/version_manifest.json"
  72.  
  73. :: Output paths
  74. set "NORMAL_OUT="
  75. set "ERROR_OUT="
  76.  
  77. :: Parameter flags
  78. set "FLAG_FORCE="
  79. set "FLAG_VERSION="
  80. set "FLAG_CONFIRM="
  81. set "FLAG_TEST="
  82.  
  83. :: Check script parameters
  84. if "%~1"=="" goto :begin_script
  85. :get_opts
  86. if /i "%~1"=="-f" set /a FLAG_FORCE=1, FLAG_CONFIRM=1
  87. if /i "%~1"=="--force" set /a FLAG_FORCE=1, FLAG_CONFIRM=1
  88. if /i "%~1"=="-h" goto :usage
  89. if /i "%~1"=="--help" goto :usage
  90. if /i "%~1"=="--jar-path" (set "JAR_PATH=%2" & shift)
  91. if /i "%~1"=="--manifest" (set "VERSION_MANIFEST=%~2" & shift)
  92. if /i "%~1"=="--no-err" set "ERROR_OUT=2>nul"
  93. if /i "%~1"=="-s" set "NORMAL_OUT=1>nul"
  94. if /i "%~1"=="--silent" set "NORMAL_OUT=1>nul"
  95. if /i "%~1"=="-t" set "FLAG_TEST=1"
  96. if /i "%~1"=="--test" set "FLAG_TEST=1"
  97. if /i "%~1"=="-v" (set "FLAG_VERSION=%~2" & shift)
  98. if /i "%~1"=="--version" (set "FLAG_VERSION=%~2" & shift)
  99. if /i "%~1"=="-y" set "FLAG_CONFIRM=1"
  100. if /i "%~1"=="--yes" set "FLAG_CONFIRM=1"
  101. shift
  102. if not "%~1"=="" goto :get_opts
  103.  
  104. if not defined JAR_PATH goto :usage
  105. if not defined VERSION_MANIFEST goto :usage
  106.  
  107. :begin_script
  108. %NORMAL_OUT% echo Clearing temp directory: %TEMP_DIR%
  109. rd /s /q "%TEMP_DIR%"
  110. mkdir "%TEMP_DIR%"
  111. set "DOWNLOADED_JAR=%TEMP_DIR%\server.jar"
  112. if exist "%DOWNLOADED_JAR%" del "%DOWNLOADED_JAR%"
  113.  
  114. %NORMAL_OUT% %print_n% Downloading Version Manifest...
  115. curl -k -s %VERSION_MANIFEST% -o "%TEMP_DIR%\version_manifest.json"
  116. if not exist "%TEMP_DIR%\version_manifest.json" (
  117.     %ERROR_OUT% echo Download error. Exiting.
  118.     exit /b 1
  119. )
  120. %NORMAL_OUT% echo Done
  121.  
  122. %NORMAL_OUT% %print_n% Parsing Version Manifest...
  123. set "ps_parse_ver=powershell ^(Get-Content "%TEMP_DIR%\version_manifest.json""
  124. set "ps_parse_ver=!ps_parse_ver! ^^| ConvertFrom-Json^^).latest.release"
  125. for /f "delims=" %%A in ('!ps_parse_ver!') do set "VERSION_LATEST=%%A"
  126. set "VERSION_TARGET=%VERSION_LATEST%"
  127. %NORMAL_OUT% %print_n% Latest Version: [%VERSION_LATEST%]...
  128. if defined FLAG_VERSION (
  129.     set "VERSION_TARGET=%FLAG_VERSION%"
  130.     %NORMAL_OUT% %print_n% Target Version: [!VERSION_TARGET!]...
  131. )
  132. set "PACKAGE_MANIFEST="
  133. set "ps_pkg_manifest=powershell ^(^(Get-Content"
  134. set "ps_pkg_manifest=!ps_pkg_manifest! "%TEMP_DIR%\version_manifest.json" ^^|"
  135. set "ps_pkg_manifest=!ps_pkg_manifest! ConvertFrom-Json^^).versions ^^|"
  136. set "ps_pkg_manifest=!ps_pkg_manifest! where-object id -eq "!VERSION_TARGET!"^^).url"
  137. for /f "delims=" %%A in ('!ps_pkg_manifest!') do set "PACKAGE_MANIFEST=%%A"
  138. %NORMAL_OUT% echo Done
  139. if not defined PACKAGE_MANIFEST (
  140.     %ERROR_OUT% echo Could not find target version !VERSION_TARGET!. Double-check the version manifest.
  141.     exit /b 1
  142. )
  143.  
  144. %NORMAL_OUT% %print_n% Downloading Package Manifest [!PACKAGE_MANIFEST!]...
  145. curl -k -s !PACKAGE_MANIFEST! -o "%TEMP_DIR%\pkg_manifest.json"
  146. if not exist "%TEMP_DIR%\pkg_manifest.json" (
  147.     %ERROR_OUT% echo Download error. Exiting.
  148.     exit /b 1
  149. )
  150. %NORMAL_OUT% echo Done
  151.  
  152. %NORMAL_OUT% %print_n% Parsing Package Manifest...
  153. set "ps_parse_manifest=powershell ^(Get-Content "%TEMP_DIR%\pkg_manifest.json""
  154. set "ps_parse_manifest=!ps_parse_manifest! ^^| ConvertFrom-Json^^)"
  155. set "ps_parse_manifest=!ps_parse_manifest!.downloads.server"
  156. :: I have no idea why I need to declare variables for this
  157. set "ps_url=!ps_parse_manifest!.url"
  158. set "ps_sha1=!ps_parse_manifest!.sha1"
  159. for /f "delims=" %%A in ('!ps_url!') do set "SERVER_NEWJAR_URL=%%A"
  160. for /f "delims=" %%A in ('!ps_sha1!') do set "SERVER_NEWJAR_SHA1=%%A"
  161. %NORMAL_OUT% echo Done
  162.  
  163. %NORMAL_OUT% %print_n% Old JAR SHA1:
  164. for /f "delims=" %%A in ('certutil -hashfile "%JAR_PATH%" SHA1 ^| find /v "hash"') do set "OLD_SHA1=%%A"
  165. :: Probably isn't needed, but it's there for backwards compatibility
  166. set "SERVER_OLDJAR_SHA1=%OLD_SHA1: =%"
  167. %NORMAL_OUT% echo !SERVER_OLDJAR_SHA1!
  168. %NORMAL_OUT% echo New JAR SHA1: !SERVER_NEWJAR_SHA1!
  169.  
  170. if "!SERVER_OLDJAR_SHA1!"=="!SERVER_NEWJAR_SHA1!" (
  171.     %NORMAL_OUT% echo SHA1 sums match. %JAR_PATH% is already target version.
  172.     if defined FLAG_TEST exit /b
  173.    
  174.     if not defined FLAG_FORCE (
  175.         %NORMAL_OUT% echo Exiting.
  176.         exit /b
  177.     ) else (
  178.         %NORMAL_OUT% echo Forcing JAR update.
  179.     )
  180. ) else (
  181.     %NORMAL_OUT% echo SHA1 sums mismatched. %JAR_PATH% differs from target version.
  182.     if defined FLAG_TEST exit /b
  183.     if not defined FLAG_CONFIRM (
  184.         choice /M "Replace %JAR_PATH% with latest"
  185.         if "!errorlevel!"=="2"  exit /b
  186.     )
  187. )
  188.  
  189. %NORMAL_OUT% %print_n% Downloading new server.jar from !SERVER_NEWJAR_URL!...
  190. curl -k -s -L -f !SERVER_NEWJAR_URL! -o "%DOWNLOADED_JAR%"
  191. if not exist "%DOWNLOADED_JAR%" (
  192.     %ERROR_OUT% echo Download error. Exiting.
  193.     exit /b 1
  194. )
  195. %NORMAL_OUT% echo Done
  196.  
  197. %NORMAL_OUT% %print_n% Downloaded JAR SHA1:
  198. for /f "delims=" %%A in ('certutil -hashfile "%DOWNLOADED_JAR%" SHA1 ^| find /v "hash"') do set "DL_SHA1=%%A"
  199. set "SERVER_DLJAR_SHA1=%DL_SHA1: =%"
  200. %NORMAL_OUT% echo %SERVER_DLJAR_SHA1%
  201.  
  202. if "!SERVER_DLJAR_SHA1!"=="!SERVER_NEWJAR_SHA1!" (
  203.     %NORMAL_OUT% echo SHA1 sums match
  204. ) else (
  205.     %ERROR_OUT% echo SHA1 sums do not match. Exiting.
  206.     exit /b 1
  207. )
  208.  
  209. set "JAR_PATH_BACKUP=%JAR_PATH%.bak"
  210. %NORMAL_OUT% %print_n% Backing up server.jar...
  211. >nul move /Y "%JAR_PATH%" "%JAR_PATH_BACKUP%"
  212. %NORMAL_OUT% echo Done
  213.  
  214. %NORMAL_OUT% %print_n% Copying downloaded file into place...
  215. copy /V /Y "%DOWNLOADED_JAR%" "%JAR_PATH%" >nul
  216. if not "!errorlevel!"=="0" (
  217.     %ERROR_OUT% echo FAILURE. Rolling back.
  218.     >nul move /Y "%JAR_PATH_BACKUP%" "%JAR_PATH%"
  219.     exit /b 1
  220. ) else (
  221.     %NORMAL_OUT% echo Done
  222. )
  223.  
  224. exit /b
  225.  
  226. ::------------------------------------------------------------------------------
  227. :: Displays the help page for the script
  228. ::
  229. :: Arguments: None
  230. :: Returns:   None
  231. ::------------------------------------------------------------------------------
  232. :usage
  233. echo %~nx0 - Updates the minecraft server jar file
  234. echo A port of updatemcjar.sh by Andrew Haskell
  235. echo(
  236. echo USAGE: %~nx0 [options]
  237. echo    -f, --force      If the Target Version SHA1 matches the current
  238. echo                     file's SHA1, force the update anyway, implies --yes
  239. echo    -h, --help       Print this help message
  240. echo    --jar-path       Specify a different final JAR path, default is
  241. echo                     %JAR_PATH%
  242. echo    --manifest       Specify a different version manifest URL, default
  243. echo                     is %VERSION_MANIFEST%
  244. echo    --no-err         Suppress error messages (Dangerous!)
  245. echo    -s, --silent     Suppress script output, implies --yes
  246. echo    -t, --test       Test the Target Version SHA1 against the current
  247. echo                     file's SHA1 without changing any files
  248. echo    --temp-dir       Specify a different temporary directory,
  249. echo                     default is %TEMP_DIR%
  250. echo    -v, --version    Specify a different target version; without this
  251. echo                     parameter, the latest release version is used
  252. echo    -y, --yes        Skip update confirmationecho
  253. exit /b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement