update_gi.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. cli_help=0
  131. cli_install=0
  132. for arg in "$@"; do
  133. case "$arg" in
  134. -h|--help|help)
  135. cli_help=1
  136. ;;
  137. install)
  138. cli_install=1
  139. ;;
  140. nodelete)
  141. remove_archives=0
  142. echo "--- Archives will not be deleted after download"
  143. ;;
  144. *)
  145. fatal "Unknown option: ${arg}"
  146. esac
  147. done
  148. if [ "$cli_help" -eq 1 ]; then
  149. cat << HELP
  150. ${THIS_FILE} [-h|help] [install] [nodelete]
  151. This script will modify the current working directory.
  152. See README.md for details and examples.
  153. "install" : new game installation from scratch
  154. "nodelete" : whether to keep the downloaded archives
  155. HELP
  156. exit 0
  157. fi
  158. # New game installation option
  159. if [ "$cli_install" -eq 1 ]; then
  160. if [ `find ./* 2>/dev/null | wc -l` -gt 0 ]; then
  161. fatal "'${PWD}' contains files and/or subdirectories." \
  162. "Please use an empty directory for a new installation." \
  163. "To update or resume an installation, rerun this script without the 'install' argument."
  164. fi
  165. echo ""
  166. echo "Which game build would you like to install?"
  167. echo " 0 -> Genshin Impact [America/Europe/Asia/TW,HK,MO]"
  168. echo " 1 -> YuanShen [Mainland China]"
  169. read -p "Install [0/1] (0): " choice
  170. if [[ -z "$choice" || "$choice" == "0" ]]; then
  171. reltype="OS"
  172. 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"
  173. elif [ "$choice" = "1" ]; then
  174. reltype="CN"
  175. 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"
  176. else
  177. exit 1
  178. fi
  179. else
  180. # Check for existing installation
  181. if [ -e "GenshinImpact.exe" ]; then
  182. reltype="OS"
  183. elif [ -e "YuanShen.exe" ]; then
  184. reltype="CN"
  185. fi
  186. fi
  187. # ======== Configuration file parsing
  188. game_not_found_message=(
  189. "Make sure 'Genshin Impact Game' is the current working directory."
  190. "If you would like to install the game append the 'install' option:"
  191. "bash '${THIS_PATH}/${THIS_FILE}' install"
  192. )
  193. [ -z "$reltype" ] && fatal \
  194. "Cannot determine the installed game type." \
  195. "${game_not_found_message[@]}"
  196. eval LANG_PACKS_PATH="\$LANG_PACKS_PATH_${reltype}"
  197. eval UPDATE_URL="\$UPDATE_URL_${reltype}"
  198. # $reltype is no longer used
  199. [ ! -e "$CONFIG_FILE" ] && fatal \
  200. "Game information file ${CONFIG_FILE} not found." \
  201. "${game_not_found_message[@]}"
  202. installed_ver=`get_ini_value "${CONFIG_FILE}" 'game_version'`
  203. [ -z "$installed_ver" ] && fatal \
  204. "Cannot read game_version from ${CONFIG_FILE}. File corrupt?"
  205. echo "--- Installed version: ${installed_ver}"
  206. # ======== Update information download + meta checks
  207. # WARNING: File cannot be downloaded to NTFS/FAT* partitions due to special characters
  208. tmp_path=`mktemp -d`
  209. trap "rm -rf '$tmp_path'" EXIT
  210. download_file "$UPDATE_URL" "$tmp_path"
  211. RESOURCE_FILE=`find "$tmp_path" -name 'resource*' | tee`
  212. [ ! -f "${RESOURCE_FILE}" ] && fatal \
  213. "Failed to download version info. Check your internet connection."
  214. upstream_ver=`jq -r -M '.data .game .latest .version' "$RESOURCE_FILE" | tee`
  215. echo "--- Latest version: ${upstream_ver}"
  216. if [ "$upstream_ver" = "$installed_ver" ]; then
  217. echo
  218. echo "==> Client is up to date."
  219. exit 0
  220. fi
  221. # Check whether this version can be patched
  222. patcher_dir="$REPO_PATH"/`echo "$upstream_ver" | tr -d .`
  223. [ ! -d "$patcher_dir" ] && fatal \
  224. "No suitable patch script found. Please check the bug tracker for details about the progress."
  225. # ======== Select update type
  226. # Check is diff update possible.
  227. archive_json=`jq -r -M ".data .game .diffs[] | select(.version==\"${installed_ver}\")" "$RESOURCE_FILE"`
  228. if [ -z "$archive_json" ]; then
  229. # Fallback to full download.
  230. archive_json=`jq -r -M '.data .game .latest' "$RESOURCE_FILE"`
  231. dl_type="new installation"
  232. else
  233. dl_type="incremental update"
  234. fi
  235. size=`download_json_size "$archive_json"`
  236. echo "Download type: ${dl_type} (${size})"
  237. # Confirm install/update.
  238. while :; do
  239. read -r -p "Start/continue update? [Y/n]: " input
  240. #input="y"
  241. case "$input" in
  242. Y|y|'')
  243. echo
  244. break
  245. ;;
  246. n|N)
  247. exit 0
  248. ;;
  249. esac
  250. done
  251. # Download main game archive
  252. download_json_section "$archive_json"
  253. # ======== Locate and install voiceover packs
  254. if [ -d "$LANG_PACKS_PATH" ]; then
  255. # Get voiceover directory name in capitals. Does proper space handling.
  256. lang_dir_names=$(find "$LANG_PACKS_PATH" -mindepth 1 -type d | xargs -L1 -r basename | tr -d '()' | tr 'a-z' 'A-Z')
  257. fi
  258. # Download langs packs.
  259. echo "$lang_dir_names" | while read dir_name; do
  260. [ -z "$dir_name" ] && continue
  261. eval lang_code="\$LANG_MAP_${dir_name}"
  262. lang_archive_json=`echo ${archive_json} | jq -r -M ".voice_packs[] | select(.language==\"${lang_code}\")"`
  263. if [ "$lang_archive_json" = 'null' -o "$lang_archive_json" = '' ]; then
  264. echo "--- Cannot find update for language: ${dir_name}"
  265. continue
  266. fi
  267. size=`download_json_size "$lang_archive_json"`
  268. echo "--- Voiceover pack: ${lang_code} (${size})"
  269. download_json_section "$lang_archive_json"
  270. done
  271. # ======== Revert patch & apply update
  272. # Run 'patch_revert.sh' on update existing installation.
  273. if [ -e "$ANCHOR_FILE" ]; then
  274. echo
  275. echo "============== Reverting previous Wine patch ==============="
  276. bash "${patcher_dir}/patch_revert.sh"
  277. echo "============================================================"
  278. echo
  279. fi
  280. # Unpack the game files and remove old ones according to deletefiles.txt
  281. echo "--- Updating game files...."
  282. find "${DOWNLOAD_PATH}" -name "*.zip" | while read archive_name; do
  283. bash "${THIS_PATH}/perform_update.sh" "$archive_name"
  284. done
  285. if [ "$remove_archives" -eq 1 ]; then
  286. # Remove downloads when all updates succeeded
  287. # otherwise keep the archives to fix with "perform_update.sh" manually
  288. find "${DOWNLOAD_PATH}" -name "*.zip" | while read archive_name; do
  289. rm -f "${archive_name}" "${archive_name}.completed"
  290. done
  291. fi
  292. # ======== Config update and game patching
  293. # Update version in config file.
  294. sed -i "s/game_version=${installed_ver}/game_version=${upstream_ver}/" "$CONFIG_FILE"
  295. if [ "$remove_archives" -eq 1 ]; then
  296. # The directory should be empty now. Remove.
  297. rm -r "$DOWNLOAD_PATH"
  298. fi
  299. echo
  300. echo "==> Update to version ${upstream_ver} completed"
  301. # Run wine compatibility patch script.
  302. echo
  303. echo "================= Applying new Wine patch =================="
  304. bash "${patcher_dir}/patch.sh"
  305. echo "============================================================"
  306. echo "==> Update script completed successfully"
  307. exit 0