update_gi.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. #!/usr/bin/env bash
  2. # Checker: shellcheck --shell=bash --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. DEBUG=${DEBUG:-0}
  15. declare -A UPDATE_URL_MAP
  16. UPDATE_URL_MAP[OS]='https://sdk-os-static.mihoyo.com/hk4e_global/mdk/launcher/api/resource?channel_id=1&key=gcStgarh&launcher_id=10'
  17. UPDATE_URL_MAP[CN]='https://sdk-static.mihoyo.com/hk4e_cn/mdk/launcher/api/resource?channel_id=1&key=eYd89JmJ&launcher_id=18'
  18. UPDATE_URL_MAP[BB]='https://sdk-static.mihoyo.com/hk4e_cn/mdk/launcher/api/resource?channel_id=14&key=KAtdSsoQ&launcher_id=17'
  19. [ "$DEBUG" -eq 1 ] && UPDATE_URL_MAP[OS]='http://127.0.0.1:8000/resource.json'
  20. CONFIG_FILE='config.ini'
  21. ANCHOR_FILE='UnityPlayer.dll'
  22. # ======== Voice pack constants
  23. declare -A LANG_PACKS_PATH_MAP LANG_MAP
  24. LANG_PACKS_PATH_MAP[OS]='GenshinImpact_Data/StreamingAssets/AudioAssets'
  25. LANG_PACKS_PATH_MAP[CN]='YuanShen_Data/StreamingAssets/AudioAssets'
  26. LANG_PACKS_PATH_MAP[BB]=${LANG_PACKS_PATH_MAP[CN]}
  27. LANG_MAP[ENGLISHUS]='en-us'
  28. LANG_MAP[JAPANESE]='ja-jp'
  29. LANG_MAP[KOREAN]='ko-kr'
  30. LANG_MAP[CHINESE]='zh-cn'
  31. # ======== Evaluated variables
  32. THIS_FILE=`basename "$0"`
  33. THIS_PATH=`realpath "$(dirname "$0")"`
  34. REPO_PATH=`dirname "$THIS_PATH"` # parent
  35. reltype="" # OS, CN or BB. filled later.
  36. remove_archives=1 # 0 or 1. filled later.
  37. # ======== Dependency checks
  38. # Check is all required tools installed.
  39. for appname in jq bash 7za xdelta3; do
  40. exepath=`command -v "$appname" | tee`
  41. [ ! -e "$exepath" ] && fatal \
  42. "Required tool not found!" \
  43. "Please install: ${appname}."
  44. done
  45. # ======== Download tool setup
  46. DOWNLOAD_PATH="../_update_gi_download"
  47. #DOWNLOAD_PATH="../download with spaces"
  48. declare -A DL_APP_ARGS_MAP
  49. DL_APP_ARGS_MAP[axel]="-n 15"
  50. DL_APP_ARGS_MAP[aria2c]="--no-conf -c"
  51. DL_APP_ARGS_MAP[fetch]="--force-restart --no-mtime --retry --keep-output --restart"
  52. DL_APP_ARGS_MAP[wget]="-c"
  53. DL_APP_ARGS_MAP[curl]="--disable -O -C -"
  54. # Find first available download tool.
  55. for appname in axel aria2c fetch wget curl; do
  56. # Pipe to "tee" overwrites the exit status
  57. exepath=`command -v "$appname" | tee`
  58. if [ -e "$exepath" ]; then
  59. DL_APP_BIN="$exepath"
  60. read -ra DL_APP_ARGS <<< "${DL_APP_ARGS_MAP[$appname]}"
  61. break
  62. fi
  63. done
  64. [ ! -e "$DL_APP_BIN" ] && fatal \
  65. "No downloader application found." \
  66. "Please install one of: ${DL_APPS_LIST}."
  67. echo "--- Using download application: ${DL_APP_BIN}" "${DL_APP_ARGS[@]}"
  68. # ======== Functions
  69. # MacOS and *BSD do not have md5sum: use md5 instead
  70. exepath=$(command -v md5 | tee)
  71. if [ -e "$exepath" ]; then
  72. md5check() {
  73. # 1 = Checksum, 2 = File
  74. md5 -q -c "$1" "$2" >/dev/null 2>&1
  75. }
  76. else
  77. md5check() {
  78. local input filesum
  79. # 1 = Checksum, 2 = File
  80. input=`<<< "$1" tr '[:upper:]' '[:lower:]'`
  81. filesum=`md5sum "$2" | cut -d ' ' -f1`
  82. if [ "$input" != "$filesum" ]; then
  83. echo "Mismatch!"
  84. exit 1
  85. fi
  86. }
  87. fi
  88. download_file() {
  89. local url="$1"
  90. local dst_path="$2"
  91. local md5="$3"
  92. local filename_args="${dst_path}/${url##*/}"
  93. local filename="${filename_args%%\?*}"
  94. local filename_completed="${filename}.completed"
  95. mkdir -p "$dst_path"
  96. if [ -f "$filename_completed" ]; then
  97. echo " -> Skipping: Already downloaded."
  98. else
  99. echo
  100. (cd "$dst_path" && "$DL_APP_BIN" "${DL_APP_ARGS[@]}" "$url")
  101. if [ -n "$md5" ]; then
  102. echo -n " -> Verifying MD5 checksum ... "
  103. md5check "$md5" "$filename"
  104. echo 'OK'
  105. touch "$filename_completed"
  106. fi
  107. fi
  108. }
  109. # Approximate size of the installed archive
  110. download_json_size() {
  111. local size
  112. size=`<<< "$1" jq -r -M ".size"`
  113. echo "~$(((size + 1048576) / 1024 / 1024 / 2)) MiB"
  114. }
  115. # 1: { "path": "URL", "md5": "hash" }
  116. download_json_section() {
  117. local json_text
  118. local url md5
  119. json_text="$1"
  120. url=`<<< "$json_text" jq -r -M ".path"`
  121. md5=`<<< "$json_text" jq -r -M ".md5"`
  122. download_file "$url" "$DOWNLOAD_PATH" "$md5"
  123. }
  124. get_ini_value() {
  125. local filename variable
  126. filename="$1"
  127. variable="$2"
  128. grep "${variable}=" "${filename}" | tr -d '\r\n' | sed -e 's|.*=||g'
  129. }
  130. # ======== Path sanity checks
  131. # There is a good reason for this check. Do not pollute the game directory.
  132. [ -e "${THIS_PATH}/${ANCHOR_FILE}" ] && fatal \
  133. "Please move this script outside the game directory prior executing." \
  134. " -> See README.md for proper installation instructions"
  135. # In case people accidentally want to install the game into the launcher directory.
  136. dllcount=`find ./*.dll 2>/dev/null | wc -l`
  137. [ "$dllcount" -gt 2 ] && fatal \
  138. "This script is likely run in the wrong directory." \
  139. "Found more than two DLL files. (expected: 0...2)" \
  140. "Please run this script in a proper/clean game directory."
  141. # ======== At Exit cleanups
  142. tmp_path="" # json download path
  143. do_remove_config_ini=0 # when the install fails
  144. atexit() {
  145. if [[ -n "$tmp_path" && -d "$tmp_path" ]]; then
  146. rm -r "$tmp_path"
  147. fi
  148. if [ "$do_remove_config_ini" -gt 0 ]; then
  149. rm -f "$CONFIG_FILE"
  150. fi
  151. echo " -- exit cleanup done -- "
  152. }
  153. trap 'atexit' EXIT
  154. # ======== Command line processing
  155. cli_help=0
  156. cli_install=0
  157. cli_predownload=0
  158. json_path=".game"
  159. for arg in "$@"; do
  160. case "$arg" in
  161. -h|--help|help)
  162. cli_help=1
  163. ;;
  164. install)
  165. cli_install=1
  166. ;;
  167. nodelete)
  168. remove_archives=0
  169. echo "--- Archives will not be deleted after download"
  170. ;;
  171. predownload)
  172. cli_predownload=1
  173. json_path=".pre_download_game"
  174. echo "--- Checking for predownload versions"
  175. ;;
  176. *)
  177. fatal "Unknown option: ${arg}"
  178. esac
  179. done
  180. if [ "$cli_help" -eq 1 ]; then
  181. cat << HELP
  182. ${THIS_FILE} [-h|help] [install] [nodelete]
  183. This script will modify the current working directory.
  184. See README.md for details and examples.
  185. "install" : new game installation from scratch
  186. "nodelete" : whether to keep the downloaded archives
  187. "predownload" : Checks and downloads pre-download-game archives
  188. HELP
  189. exit 0
  190. fi
  191. # New game installation option
  192. if [ "$cli_install" -eq 1 ]; then
  193. dircount=`find ./* 2>/dev/null | wc -l`
  194. if [ "$dircount" -gt 0 ]; then
  195. fatal "'${PWD}' contains files and/or subdirectories." \
  196. "Please use an empty directory for a new installation." \
  197. "To update or resume an installation, rerun this script without the 'install' argument."
  198. fi
  199. do_remove_config_ini=1
  200. echo ""
  201. echo " 0 -> Genshin Impact [America/Europe/Asia/TW,HK,MO]"
  202. echo " 1 -> YuanShen [Mainland China]"
  203. echo " 2 -> BiliBili [Mainland China]"
  204. read -rp "Which version shall be installed? [0]/1/2: " choice
  205. if [[ -z "$choice" || "$choice" == "0" ]]; then
  206. reltype="OS"
  207. 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"
  208. elif [ "$choice" = "1" ]; then
  209. reltype="CN"
  210. 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"
  211. elif [ "$choice" = "2" ]; then
  212. reltype="BB"
  213. echo -ne '[General]\r\nchannel=14\r\ncps=bilibili\r\ngame_version=0.0.0\r\nsdk_version=\r\nsub_channel=0\r\n' >"$CONFIG_FILE"
  214. else
  215. fatal "Invalid selection"
  216. fi
  217. else
  218. # Check for existing installation
  219. if [ -e "GenshinImpact.exe" ]; then
  220. reltype="OS"
  221. elif [ -e "YuanShen.exe" ]; then
  222. if [ -e "$DATADIR/Plugins/PCGameSDK.dll" ]; then
  223. reltype="BB"
  224. else
  225. reltype="CN"
  226. fi
  227. fi
  228. fi
  229. # ======== Configuration file parsing
  230. game_not_found_message=(
  231. "Make sure 'Genshin Impact Game' is the current working directory."
  232. "If you would like to install the game append the 'install' option:"
  233. "bash '${THIS_PATH}/${THIS_FILE}' install"
  234. )
  235. [ -z "$reltype" ] && fatal \
  236. "Cannot determine the installed game type." \
  237. "${game_not_found_message[@]}"
  238. LANG_PACKS_PATH=${LANG_PACKS_PATH_MAP[$reltype]}
  239. UPDATE_URL=${UPDATE_URL_MAP[$reltype]}
  240. unset reltype
  241. [ ! -e "$CONFIG_FILE" ] && fatal \
  242. "Game information file ${CONFIG_FILE} not found." \
  243. "${game_not_found_message[@]}"
  244. installed_ver=`get_ini_value "${CONFIG_FILE}" 'game_version'`
  245. [ -z "$installed_ver" ] && fatal \
  246. "Cannot read game_version from ${CONFIG_FILE}. File corrupt?"
  247. echo "--- Installed version: ${installed_ver}"
  248. # ======== Update information download + meta checks
  249. # WARNING: File cannot be downloaded to NTFS/FAT* partitions due to special characters
  250. tmp_path=`mktemp -d`
  251. download_file "$UPDATE_URL" "$tmp_path"
  252. RESOURCE_FILE=`find "$tmp_path" -name 'resource*' | tee`
  253. [ ! -f "${RESOURCE_FILE}" ] && fatal \
  254. "Failed to download version info. Check your internet connection."
  255. upstream_ver=`jq -r -M ".data ${json_path} .latest .version" "$RESOURCE_FILE" | tee`
  256. [ "$upstream_ver" = "null" ] && fatal "Could not find any matching update entry"
  257. echo "--- Latest version: ${upstream_ver}"
  258. if [ "$upstream_ver" = "$installed_ver" ]; then
  259. echo
  260. echo "==> Client is up to date."
  261. exit 0
  262. fi
  263. if [[ "$cli_predownload" -eq 0 && "$DEBUG" -eq 0 ]]; then
  264. # Check whether this version can be patched
  265. patcher_dir="$REPO_PATH"/`<<< "$upstream_ver" tr -d .`
  266. [ ! -d "$patcher_dir" ] && fatal \
  267. "No suitable patch script found. Please check the bug tracker for details about the progress."
  268. fi
  269. # ======== Select update type
  270. # Check is diff update possible.
  271. archive_json=`jq -r -M ".data ${json_path} .diffs[] | select(.version==\"${installed_ver}\")" "$RESOURCE_FILE"`
  272. if [ -z "$archive_json" ]; then
  273. # Fallback to full download.
  274. archive_json=`jq -r -M ".data ${json_path} .latest" "$RESOURCE_FILE"`
  275. dl_type="new installation"
  276. [ "$cli_install" -eq 0 ] && fatal "Cannot find an update for ${installed_ver} -> ${upstream_ver}." \
  277. "Please use a new directory to install the game from scatch."
  278. else
  279. dl_type="incremental update"
  280. fi
  281. size=`download_json_size "$archive_json"`
  282. echo "Download type: ${dl_type} (${size})"
  283. if [ "$cli_install" -eq 1 ]; then
  284. updated_ver="$upstream_ver"
  285. else
  286. # The version after updating might differ from the newest available version (bad package selection?)
  287. updated_ver=$(<<< "$archive_json" jq -r -M ".name")
  288. [[ "$updated_ver" =~ \.[0-9]+\.[0-9]+_([0-9]+\.[0-9]+\.[0-9]+)_ ]];
  289. updated_ver=${BASH_REMATCH[1]}
  290. if [ "$upstream_ver" != "$updated_ver" ]; then
  291. echo -e "=!= WARNING: Cannot find a direct update for ${installed_ver} -> ${upstream_ver}.\n" \
  292. " This script will first update to version ${updated_ver}.\n" \
  293. " Please re-run the update script after completing this update."
  294. fi
  295. fi
  296. # Confirm install/update.
  297. while :; do
  298. if [ "$DEBUG" -eq 0 ]; then
  299. read -rp "Start/continue update? [Y]/n: " input
  300. else
  301. input="y"
  302. fi
  303. case "$input" in
  304. Y|y|'')
  305. echo
  306. break
  307. ;;
  308. n|N)
  309. exit 0
  310. ;;
  311. esac
  312. done
  313. echo "--- Main game archive"
  314. # Download SDK if exists
  315. sdk_json=$(jq -r -M ".data .sdk | select(.!=null)" "$RESOURCE_FILE")
  316. if [ -n "$sdk_json" ]; then
  317. download_json_section "$sdk_json"
  318. fi
  319. # Get all segments (array) or empty string
  320. main_archive_segments=$(<<< "$archive_json" jq -r -M ".segments | select(.!=null)")
  321. # Download the main game or update archive(s)
  322. if [ -n "$main_archive_segments" ]; then
  323. i=0
  324. while :; do
  325. section=$(<<< "$main_archive_segments" jq -r -M ".[${i}] | select(.!=null)")
  326. [ -z "$section" ] && break
  327. download_json_section "$section"
  328. i=$((i + 1))
  329. done
  330. else
  331. download_json_section "$archive_json"
  332. fi
  333. # ======== Locate and install voiceover packs
  334. lang_dir_names() {
  335. if [ -d "$LANG_PACKS_PATH" ]; then
  336. # Get voiceover directory name in capitals. Does proper space handling.
  337. find "$LANG_PACKS_PATH" -mindepth 1 -type d -print0 \
  338. | xargs -0 -L1 -r basename \
  339. | tr -d '()' \
  340. | tr '[:lower:]' '[:upper:]'
  341. fi
  342. }
  343. lang_dir_names_str=$(lang_dir_names)
  344. if [[ -z "$lang_dir_names_str" && "$cli_install" -eq 1 ]]; then
  345. # TODO: make the default language(s) selectable
  346. lang_dir_names_str="ENGLISHUS"
  347. fi
  348. # Download langs packs.
  349. while read -r dir_name; do
  350. [ -z "$dir_name" ] && continue
  351. lang_code=${LANG_MAP[$dir_name]}
  352. lang_archive_json=`<<< "${archive_json}" jq -r -M ".voice_packs[] | select(.language==\"${lang_code}\")"`
  353. if [ "$lang_archive_json" = 'null' ] || [ "$lang_archive_json" = '' ]; then
  354. echo "--- Cannot find update for language: ${dir_name}"
  355. continue
  356. fi
  357. size=`download_json_size "$lang_archive_json"`
  358. echo "--- Voiceover pack: ${lang_code} (${size})"
  359. download_json_section "$lang_archive_json"
  360. done <<< "$lang_dir_names_str"
  361. # ======== Revert patch & apply update
  362. if [ "$cli_predownload" -eq 1 ]; then
  363. echo
  364. echo "==> Pre-download completed. The archives will be ready on release day."
  365. exit 0
  366. fi
  367. # Run 'patch_revert.sh' on update existing installation.
  368. if [ -e "$ANCHOR_FILE" ]; then
  369. echo
  370. echo "============== Reverting previous Wine patch ==============="
  371. [ "$DEBUG" -eq 0 ] && bash "${patcher_dir}/patch_revert.sh"
  372. echo "============================================================"
  373. echo
  374. fi
  375. # Unpack the game files and remove old ones according to deletefiles.txt
  376. echo "--- Updating game files ..."
  377. # -L to allow symlinking the download directory to another drive
  378. zip_files=$(find -L "$DOWNLOAD_PATH" \( -name "*.zip" -o -name "*.zip.001" \))
  379. while read -r archive_name; do
  380. [ ! -f "${archive_name}.completed" ] && fatal \
  381. "Archive '${archive_name}' is not marked as complete!"
  382. bash "${THIS_PATH}/perform_update.sh" "$archive_name"
  383. done <<< "$zip_files"
  384. if [ "$remove_archives" -eq 1 ]; then
  385. # Remove downloads when all updates succeeded
  386. # otherwise keep the archives to fix with "perform_update.sh" manually
  387. echo "--- Removing downloaded archives ..."
  388. while read -r archive_name; do
  389. rm -f "${archive_name}" "${archive_name}.completed"
  390. done <<< "$zip_files"
  391. fi
  392. # ======== Config update and game patching
  393. do_remove_config_ini=0
  394. # Update version in config file.
  395. sed -i "s/game_version=${installed_ver}/game_version=${updated_ver}/" "$CONFIG_FILE"
  396. if [ -n "$sdk_json" ]; then
  397. sdk_version=`<<< "$sdk_json" jq -r -M ".version"`
  398. sed -i "s/^sdk_version=.*/sdk_version=${sdk_version}/" "$CONFIG_FILE"
  399. fi
  400. if [ "$remove_archives" -eq 1 ]; then
  401. # The directory should be empty now. Remove.
  402. rm -r "$DOWNLOAD_PATH"
  403. fi
  404. echo
  405. echo "==> Update to version ${updated_ver} completed"
  406. # Run wine compatibility patch script.
  407. echo
  408. echo "================= Applying new Wine patch =================="
  409. [ "$DEBUG" -eq 0 ] && bash "${patcher_dir}/patch.sh"
  410. echo "============================================================"
  411. echo "==> Update script completed successfully"
  412. exit 0