update_minecraft_bedrock.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/bin/bash
  2. # LinuxGSM update_minecraft_bedrock.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Minecraft Bedrock servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_dl() {
  9. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "bedrock_server.${remotebuildversion}.zip" "nochmodx" "norun" "noforce" "nohash"
  10. echo -e "Extracting to ${serverfiles}...\c"
  11. if [ "${firstcommandname}" == "INSTALL" ]; then
  12. unzip -oq "${tmpdir}/bedrock_server.${remotebuildversion}.zip" -x "server.properties" -d "${serverfiles}"
  13. else
  14. unzip -oq "${tmpdir}/bedrock_server.${remotebuildversion}.zip" -x "permissions.json" "server.properties" "allowlist.json" -d "${serverfiles}"
  15. fi
  16. local exitcode=$?
  17. if [ "${exitcode}" != 0 ]; then
  18. fn_print_fail_eol_nl
  19. fn_script_log_fatal "Extracting ${local_filename}"
  20. if [ -f "${lgsmlog}" ]; then
  21. echo -e "${extractcmd}" >> "${lgsmlog}"
  22. fi
  23. echo -e "${extractcmd}"
  24. core_exit.sh
  25. else
  26. fn_print_ok_eol_nl
  27. fn_script_log_pass "Extracting ${local_filename}"
  28. fi
  29. }
  30. fn_update_localbuild() {
  31. # Gets local build info.
  32. fn_print_dots "Checking local build: ${remotelocation}"
  33. # Uses log file to get local build.
  34. localbuild=$(grep Version "${consolelogdir}"/* 2> /dev/null | tail -1 | sed 's/.*Version: //' | tr -d '\000-\011\013-\037')
  35. if [ -z "${localbuild}" ]; then
  36. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  37. fn_script_log_error "Missing local build info"
  38. fn_script_log_error "Set localbuild to 0"
  39. localbuild="0"
  40. else
  41. fn_print_ok "Checking local build: ${remotelocation}"
  42. fn_script_log_pass "Checking local build"
  43. fi
  44. }
  45. fn_update_remotebuild() {
  46. # Random number for userAgent
  47. randnum=$((1 + RANDOM % 5000))
  48. # Get remote build info.
  49. if [ "${mcversion}" == "latest" ]; then
  50. remotebuildversion=$(curl -H "Accept-Encoding: identity" -H "Accept-Language: en" -Ls -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.${randnum}.212 Safari/537.36" "https://www.minecraft.net/en-us/download/server/bedrock/" | grep -o 'https://minecraft.azureedge.net/bin-linux/[^"]*' | sed 's/.*\///' | grep -Eo "[.0-9]+[0-9]")
  51. else
  52. remotebuildversion="${mcversion}"
  53. fi
  54. remotebuildurl="https://minecraft.azureedge.net/bin-linux/bedrock-server-${remotebuildversion}.zip"
  55. if [ "${firstcommandname}" != "INSTALL" ]; then
  56. fn_print_dots "Checking remote build: ${remotelocation}"
  57. # Checks if remotebuildversion variable has been set.
  58. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  59. fn_print_fail "Checking remote build: ${remotelocation}"
  60. fn_script_log_fatal "Checking remote build"
  61. core_exit.sh
  62. else
  63. fn_print_ok "Checking remote build: ${remotelocation}"
  64. fn_script_log_pass "Checking remote build"
  65. fi
  66. else
  67. # Checks if remotebuild variable has been set.
  68. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  69. fn_print_failure "Unable to get remote build"
  70. fn_script_log_fatal "Unable to get remote build"
  71. core_exit.sh
  72. fi
  73. fi
  74. }
  75. fn_update_compare() {
  76. fn_print_dots "Checking for update: ${remotelocation}"
  77. # Update has been found or force update.
  78. if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
  79. # Create update lockfile.
  80. date '+%s' > "${lockdir:?}/update.lock"
  81. fn_print_ok_nl "Checking for update: ${remotelocation}"
  82. echo -en "\n"
  83. echo -e "Update available"
  84. echo -e "* Local build: ${red}${localbuild}${default}"
  85. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  86. if [ -n "${branch}" ]; then
  87. echo -e "* Branch: ${branch}"
  88. fi
  89. if [ -f "${rootdir}/.dev-debug" ]; then
  90. echo -e "Remote build info"
  91. echo -e "* apiurl: ${apiurl}"
  92. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  93. echo -e "* remotebuildurl: ${remotebuildurl}"
  94. echo -e "* remotebuildversion: ${remotebuildversion}"
  95. fi
  96. echo -en "\n"
  97. fn_script_log_info "Update available"
  98. fn_script_log_info "Local build: ${localbuild}"
  99. fn_script_log_info "Remote build: ${remotebuildversion}"
  100. if [ -n "${branch}" ]; then
  101. fn_script_log_info "Branch: ${branch}"
  102. fi
  103. fn_script_log_info "${localbuild} > ${remotebuildversion}"
  104. if [ "${commandname}" == "UPDATE" ]; then
  105. unset updateonstart
  106. check_status.sh
  107. # If server stopped.
  108. if [ "${status}" == "0" ]; then
  109. fn_update_dl
  110. if [ "${localbuild}" == "0" ]; then
  111. exitbypass=1
  112. command_start.sh
  113. fn_firstcommand_reset
  114. exitbypass=1
  115. sleep 5
  116. command_stop.sh
  117. fn_firstcommand_reset
  118. fi
  119. # If server started.
  120. else
  121. fn_print_restart_warning
  122. exitbypass=1
  123. command_stop.sh
  124. fn_firstcommand_reset
  125. exitbypass=1
  126. fn_update_dl
  127. exitbypass=1
  128. command_start.sh
  129. fn_firstcommand_reset
  130. fi
  131. unset exitbypass
  132. date +%s > "${lockdir}/last-updated.lock"
  133. alert="update"
  134. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  135. alert="check-update"
  136. fi
  137. alert.sh
  138. else
  139. fn_print_ok_nl "Checking for update: ${remotelocation}"
  140. echo -en "\n"
  141. echo -e "No update available"
  142. echo -e "* Local build: ${green}${localbuild}${default}"
  143. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  144. if [ -n "${branch}" ]; then
  145. echo -e "* Branch: ${branch}"
  146. fi
  147. echo -en "\n"
  148. fn_script_log_info "No update available"
  149. fn_script_log_info "Local build: ${localbuild}"
  150. fn_script_log_info "Remote build: ${remotebuildversion}"
  151. if [ -n "${branch}" ]; then
  152. fn_script_log_info "Branch: ${branch}"
  153. fi
  154. if [ -f "${rootdir}/.dev-debug" ]; then
  155. echo -e "Remote build info"
  156. echo -e "* apiurl: ${apiurl}"
  157. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  158. echo -e "* remotebuildurl: ${remotebuildurl}"
  159. echo -e "* remotebuildversion: ${remotebuildversion}"
  160. fi
  161. fi
  162. }
  163. # The location where the builds are checked and downloaded.
  164. remotelocation="minecraft.net"
  165. if [ "${firstcommandname}" == "INSTALL" ]; then
  166. fn_update_remotebuild
  167. fn_update_dl
  168. else
  169. fn_print_dots "Checking for update"
  170. fn_print_dots "Checking for update: ${remotelocation}"
  171. fn_script_log_info "Checking for update: ${remotelocation}"
  172. fn_update_localbuild
  173. fn_update_remotebuild
  174. fn_update_compare
  175. fi