update_gi.sh 12 KB

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