update_gi.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #!/usr/bin/env bash
  2. # Checker: shellcheck --shell=sh --exclude=SC2006,SC3043 update_gi.sh | less
  3. # Exit on error.
  4. set -e
  5. fatal() {
  6. echo
  7. for arg in "$@"; do
  8. echo " * $arg" >&2
  9. done
  10. echo
  11. exit 1
  12. }
  13. # ======== Global constants
  14. UPDATE_URL_OS='https://sdk-os-static.mihoyo.com/hk4e_global/mdk/launcher/api/resource?key=gcStgarh&launcher_id=10'
  15. UPDATE_URL_CN='https://sdk-static.mihoyo.com/hk4e_cn/mdk/launcher/api/resource?key=eYd89JmJ&launcher_id=18'
  16. #UPDATE_URL_OS='http://127.0.0.1:8000/dummy/resource.json?arg1=foo&arg2=bar'
  17. CONFIG_FILE='config.ini'
  18. ANCHOR_FILE='UnityPlayer.dll'
  19. # ======== Voice pack constants
  20. LANG_PACKS_PATH_OS='GenshinImpact_Data/StreamingAssets/Audio/GeneratedSoundBanks/Windows'
  21. LANG_PACKS_PATH_CN='YuanShen_Data/StreamingAssets/Audio/GeneratedSoundBanks/Windows'
  22. LANG_MAP_ENGLISHUS='en-us'
  23. LANG_MAP_JAPANESE='ja-jp'
  24. LANG_MAP_KOREAN='ko-kr'
  25. LANG_MAP_CHINESE='zh-cn'
  26. # ======== Evaluated variables
  27. THIS_FILE=`basename "$0"`
  28. THIS_PATH=`realpath "$(dirname "$0")"`
  29. REPO_PATH=`dirname "$THIS_PATH"` # parent
  30. reltype="" # OS or CN, filled later.
  31. remove_archives=1 # 0 or 1, filled later.
  32. # ======== Dependency checks
  33. # Check is all required tools installed.
  34. for appname in jq bash unzip xdelta3; do
  35. exepath=`command -v "$appname" | tee`
  36. [ ! -e "$exepath" ] && fatal \
  37. "Required tool not found!" \
  38. "Please install: ${appname}."
  39. done
  40. # ======== Download tool setup
  41. DOWNLOAD_PATH="../_update_gi_download"
  42. #DOWNLOAD_PATH="../download with spaces"
  43. DL_APP_ARGS_axel='-n 15'
  44. DL_APP_ARGS_aria2c='--no-conf -c'
  45. DL_APP_ARGS_fetch='--force-restart --no-mtime --retry --keep-output --restart'
  46. DL_APP_ARGS_wget='-c'
  47. DL_APP_ARGS_curl='--disable -C -'
  48. # Find first available download tool.
  49. for appname in axel aria2c fetch wget curl; do
  50. # Pipe to "tee" overwrites the exit status
  51. exepath=`command -v "$appname" | tee`
  52. if [ -e "$exepath" ]; then
  53. DL_APP_BIN="$exepath"
  54. eval DL_APP_ARGS="\$DL_APP_ARGS_${appname}"
  55. break
  56. fi
  57. done
  58. [ ! -e "$DL_APP_BIN" ] && fatal \
  59. "No downloader application found." \
  60. "Please install one of: ${DL_APPS_LIST}."
  61. echo "--- Using download application: ${DL_APP_BIN} ${DL_APP_ARGS}"
  62. # ======== Functions
  63. # MacOS and *BSD do not have md5sum: use md5 instead
  64. exepath=$(command -v md5 | tee)
  65. if [ -e "$exepath" ]; then
  66. md5check() {
  67. # 1 = Checksum, 2 = File
  68. md5 -q -c "$1" "$2" >/dev/null 2>&1
  69. }
  70. else
  71. md5check() {
  72. # 1 = Checksum, 2 = File
  73. local input=`echo "$1" | tr 'A-Z' 'a-z'`
  74. local filesum=`md5sum "$2" | cut -d ' ' -f1`
  75. if [ "$input" != "$filesum" ]; then
  76. echo "Mismatch!"
  77. exit 1
  78. fi
  79. }
  80. fi
  81. download_file() {
  82. local url="$1"
  83. local dst_path="$2"
  84. local md5="$3"
  85. local filename_args="${dst_path}/${url##*/}"
  86. local filename="${filename_args%%\?*}"
  87. local filename_completed="${filename}.completed"
  88. echo
  89. mkdir -p "$dst_path"
  90. if [ -f "$filename_completed" ]; then
  91. echo "--> Skipping: Already downloaded."
  92. else
  93. (cd "$dst_path" && "$DL_APP_BIN" $DL_APP_ARGS "$url")
  94. if [ -n "$md5" ]; then
  95. echo -n '--- Verifying MD5 checksum ... '
  96. md5check "$md5" "$filename"
  97. echo 'OK'
  98. touch "$filename_completed"
  99. fi
  100. fi
  101. }
  102. # Approximate size of the installed archive
  103. download_json_size() {
  104. local size=`echo "$1" | jq -r -M ".size"`
  105. size="$((($size + 1048576) / 1024 / 1024 / 2))"
  106. echo "~${size} MiB"
  107. }
  108. download_json_section() {
  109. local json_text="$1"
  110. local url=`echo "$json_text" | jq -r -M ".path"`
  111. local md5=`echo "$json_text" | jq -r -M ".md5"`
  112. download_file "$url" "$DOWNLOAD_PATH" "$md5"
  113. }
  114. get_ini_value() {
  115. local filename="${1}"
  116. local variable="${2}"
  117. grep "${variable}=" "${filename}" | tr -d '\r\n' | sed -e 's|.*=||g'
  118. }
  119. # ======== Path sanity checks
  120. # There is a good reason for this check. Do not pollute the game directory.
  121. [ -e "${THIS_PATH}/${ANCHOR_FILE}" ] && fatal \
  122. "Please move this script outside the game directory prior executing." \
  123. " -> See README.md for proper installation instructions"
  124. # In case people accidentally want to install the game into the launcher directory.
  125. [ `find ./*.dll 2>/dev/null | wc -l` -gt 2 ] && fatal \
  126. "This script is likely run in the wrong directory." \
  127. "Found more than two DLL files. (expected: 0...2)" \
  128. "Please run this script in a proper/clean game directory."
  129. # ======== Command line processing
  130. # some help
  131. if [ "$1" = '-h' -o "${1#--}" = 'help' ]; then
  132. cat << HELP
  133. ${THIS_FILE} [-h|help] [install] [nodelete]
  134. This script will modify the current working directory.
  135. See README.md for details and examples.
  136. "install" : new game installation from scratch
  137. "nodelete" : whether to keep the downloaded archives
  138. HELP
  139. exit 0
  140. fi
  141. if [ "$1" = "nodelete" -o "$2" = "nodelete" ]; then
  142. remove_archives=0
  143. echo "--- Archives will not be deleted after download"
  144. fi
  145. # Option to install.
  146. if [ "$1" = 'install' ]; then
  147. if [ `find ./* 2>/dev/null | wc -l` -gt 0 ]; then
  148. fatal "'${PWD}' contains files and/or subdirectories." \
  149. "Please use an empty directory for a new installation." \
  150. "To update or resume an installtion, rerun this script without the 'install' argument."
  151. fi
  152. echo "Which game build would you like to install?"
  153. echo " 0 -> Genshin Impact [America/Europe/Asia/TW,HK,MO]"
  154. echo " 1 -> YuanShen [Mainland China]"
  155. read -p "Install [0/1] (0): " choice
  156. if [[ -z "$choice" || "$choice" == "0" ]]; then
  157. reltype="OS"
  158. echo -ne '[General]\r\nchannel=1\r\ncps=mihoyo\r\ngame_version=0.0.0\r\nsdk_version=\r\nsub_channel=0\r\n' >"$CONFIG_FILE"
  159. elif [ "$choice" = "1" ]; then
  160. reltype="CN"
  161. echo -ne '[General]\r\nchannel=1\r\ncps=mihoyo\r\ngame_version=0.0.0\r\nsdk_version=\r\nsub_channel=1\r\n' >"$CONFIG_FILE"
  162. else
  163. exit 1
  164. fi
  165. else
  166. # Check for existing installation
  167. if [ -e "GenshinImpact.exe" ]; then
  168. reltype="OS"
  169. elif [ -e "YuanShen.exe" ]; then
  170. reltype="CN"
  171. fi
  172. fi
  173. # ======== Configuration file parsing
  174. game_not_found_message=(
  175. "Make sure 'Genshin Impact Game' is the current working directory."
  176. "If you would like to install the game append the 'install' option:"
  177. "bash '${THIS_PATH}/${THIS_FILE}' install"
  178. )
  179. [ -z "$reltype" ] && fatal \
  180. "Cannot determine the installed game type." \
  181. "${game_not_found_message[@]}"
  182. eval LANG_PACKS_PATH="\$LANG_PACKS_PATH_${reltype}"
  183. eval UPDATE_URL="\$UPDATE_URL_${reltype}"
  184. # $reltype is no longer used
  185. [ ! -e "$CONFIG_FILE" ] && fatal \
  186. "Game information file ${CONFIG_FILE} not found." \
  187. "${game_not_found_message[@]}"
  188. installed_ver=`get_ini_value "${CONFIG_FILE}" 'game_version'`
  189. [ -z "$installed_ver" ] && fatal \
  190. "Cannot read game_version from ${CONFIG_FILE}. File corrupt?"
  191. echo "--- Installed version: ${installed_ver}"
  192. # ======== Update information download + meta checks
  193. # WARNING: File cannot be downloaded to NTFS/FAT* partitions due to special characters
  194. tmp_path=`mktemp -d`
  195. trap "rm -rf '$tmp_path'" EXIT
  196. download_file "$UPDATE_URL" "$tmp_path"
  197. RESOURCE_FILE=`find "$tmp_path" -name 'resource*' | tee`
  198. [ ! -f "${RESOURCE_FILE}" ] && fatal \
  199. "Failed to download version info. Check your internet connection."
  200. upstream_ver=`jq -r -M '.data .game .latest .version' "$RESOURCE_FILE" | tee`
  201. echo "--- Latest version: ${upstream_ver}"
  202. if [ "$upstream_ver" = "$installed_ver" ]; then
  203. echo
  204. echo "==> Client is up to date."
  205. exit 0
  206. fi
  207. # Check whether this version can be patched
  208. patcher_dir="$REPO_PATH"/`echo "$upstream_ver" | tr -d .`
  209. [ ! -d "$patcher_dir" ] && fatal \
  210. "No suitable patch script found. Please check the bug tracker for details about the progress."
  211. # ======== Select update type
  212. # Check is diff update possible.
  213. archive_json=`jq -r -M ".data .game .diffs[] | select(.version==\"${installed_ver}\")" "$RESOURCE_FILE"`
  214. if [ -z "$archive_json" ]; then
  215. # Fallback to full download.
  216. archive_json=`jq -r -M '.data .game .latest' "$RESOURCE_FILE"`
  217. dl_type="new installation"
  218. else
  219. dl_type="incremental update"
  220. fi
  221. size=`download_json_size "$archive_json"`
  222. echo "Download type: ${dl_type} (${size})"
  223. # Confirm install/update.
  224. while :; do
  225. read -r -p "Start/continue update? [Y/n]: " input
  226. #input="y"
  227. case "$input" in
  228. Y|y|'')
  229. echo
  230. break
  231. ;;
  232. n|N)
  233. exit 0
  234. ;;
  235. esac
  236. done
  237. # Download main game archive
  238. download_json_section "$archive_json"
  239. # ======== Locate and install voiceover packs
  240. if [ -d "$LANG_PACKS_PATH" ]; then
  241. # WARNING: Does not handle spaces. Do not add $PWD.
  242. lang_dir_names=`find "$LANG_PACKS_PATH"/* -type d | xargs basename | tr -d '()' | tr 'a-z' 'A-Z'`
  243. fi
  244. # Download langs packs.
  245. for dir_name in ${lang_dir_names}; do
  246. eval lang_code="\$LANG_MAP_${dir_name}"
  247. lang_archive_json=`echo ${archive_json} | jq -r -M ".voice_packs[] | select(.language==\"${lang_code}\")"`
  248. if [ "$lang_archive_json" = 'null' ]; then
  249. echo "Cannot find update for language: ${lang_code}"
  250. continue
  251. fi
  252. size=`download_json_size "$lang_archive_json"`
  253. echo "--- Voiceover pack: ${lang_code} (${size})"
  254. download_json_section "$lang_archive_json"
  255. done
  256. # ======== Revert patch & apply update
  257. # Run 'patch_revert.sh' on update existing installation.
  258. if [ -e "$ANCHOR_FILE" ]; then
  259. echo
  260. echo "============== Reverting previous Wine patch ==============="
  261. bash "${patcher_dir}/patch_revert.sh"
  262. echo "============================================================"
  263. echo
  264. fi
  265. # Unpack the game files and remove old ones according to deletefiles.txt
  266. echo "--- Updating game files...."
  267. find "${DOWNLOAD_PATH}" -name "*.zip" | while read archive_name; do
  268. bash "${THIS_PATH}/perform_update.sh" "$archive_name"
  269. done
  270. if [ "$remove_archives" -eq 1 ]; then
  271. # Remove downloads when all updates succeeded
  272. # otherwise keep the archives to fix with "perform_update.sh" manually
  273. find "${DOWNLOAD_PATH}" -name "*.zip" | while read archive_name; do
  274. rm -f "${archive_name}" "${archive_name}.completed"
  275. done
  276. fi
  277. # ======== Config update and game patching
  278. # Update version in config file.
  279. sed -i "s/game_version=${installed_ver}/game_version=${upstream_ver}/" "$CONFIG_FILE"
  280. # The directory should be empty now. Remove.
  281. rm -r "$DOWNLOAD_PATH"
  282. echo
  283. echo "==> Update to version ${upstream_ver} completed"
  284. # Run wine compatibility patch script.
  285. echo
  286. echo "================= Applying new Wine patch =================="
  287. bash "${patcher_dir}/patch.sh"
  288. echo "============================================================"
  289. echo "==> Update script completed successfully"
  290. exit 0