123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- #!/usr/bin/sh
- # Checker: shellcheck --shell=sh --exclude=SC2006,SC3043 update_gi.sh | less
- # Exit on error.
- set -e
- fatal() {
- echo
- for arg in "$@"; do
- echo " * $arg" >&2
- done
- echo
- exit 1
- }
- # ======== Global constants
- UPDATE_URL='https://sdk-os-static.mihoyo.com/hk4e_global/mdk/launcher/api/resource?key=gcStgarh&launcher_id=10'
- #UPDATE_URL='http://127.0.0.1:8000/dummy/resource.json?arg1=foo&arg2=bar'
- CONFIG_FILE='config.ini'
- ANCHOR_FILE='UnityPlayer.dll'
- # ======== Voice pack constants
- LANG_PACKS_PATH='GenshinImpact_Data/StreamingAssets/Audio/GeneratedSoundBanks/Windows'
- LANG_MAP_ENGLISHUS='en-us'
- LANG_MAP_JAPANESE='ja-jp'
- LANG_MAP_KOREAN='ko-kr'
- LANG_MAP_CHINESE='zh-cn'
- # ======== Evaluated variables
- THIS_FILE=`basename "$0"`
- THIS_PATH=`realpath "$(dirname "$0")"`
- REPO_PATH=`dirname "$THIS_PATH"` # parent
- # ======== Dependency checks
- # Check is all required tools installed.
- for appname in jq bash unzip xdelta3; do
- exepath=`command -v "$appname" | tee`
- [ ! -e "$exepath" ] && fatal \
- "Required tool not found!" \
- "Please install: ${appname}."
- done
- # ======== Download tool setup
- DOWNLOAD_PATH="../_update_gi_download"
- #DOWNLOAD_PATH="../download with spaces"
- DL_APP_ARGS_axel='-n 15'
- DL_APP_ARGS_aria2c='--no-conf -c'
- DL_APP_ARGS_fetch='--force-restart --no-mtime --retry --keep-output --restart'
- DL_APP_ARGS_wget='-c'
- DL_APP_ARGS_curl='--disable -C -'
- # Find first available download tool.
- for appname in axel aria2c fetch wget curl; do
- # Pipe to "tee" overwrites the exit status
- exepath=`command -v "$appname" | tee`
- if [ -e "$exepath" ]; then
- DL_APP_BIN="$exepath"
- eval DL_APP_ARGS="\$DL_APP_ARGS_${appname}"
- break
- fi
- done
- [ ! -e "$DL_APP_BIN" ] && fatal \
- "No downloader application found." \
- "Please install one of: ${DL_APPS_LIST}."
- echo "--- Using download application: ${DL_APP_BIN} ${DL_APP_ARGS}"
- # ======== Functions
- # MacOS and *BSD do not have md5sum: use md5 instead
- exepath=$(command -v md5 | tee)
- if [ -e "$exepath" ]; then
- md5check() {
- # 1 = Checksum, 2 = File
- md5 -q -c "$1" "$2" >/dev/null 2>&1
- }
- else
- md5check() {
- # 1 = Checksum, 2 = File
- local input=`echo "$1" | tr 'A-Z' 'a-z'`
- local filesum=`md5sum "$2" | cut -d ' ' -f1`
- if [ "$input" != "$filesum" ]; then
- echo "Mismatch!"
- exit 1
- fi
- }
- fi
- download_file() {
- local url="$1"
- local dst_path="$2"
- local md5="$3"
- local filename_args="${dst_path}/${url##*/}"
- local filename="${filename_args%%\?*}"
- local filename_completed="${filename}.completed"
- echo
- mkdir -p "$dst_path"
- if [ -f "$filename_completed" ]; then
- echo "--> Skipping: Already downloaded."
- else
- (cd "$dst_path" && "$DL_APP_BIN" $DL_APP_ARGS "$url")
- if [ -n "$md5" ]; then
- echo -n '--- Verifying MD5 checksum ... '
- md5check "$md5" "$filename"
- echo 'OK'
- touch "$filename_completed"
- fi
- fi
- }
- # Approximate size of the installed archive
- download_json_size() {
- local size=`echo "$1" | jq -r -M ".size"`
- size="$((($size + 1048576) / 1024 / 1024 / 2))"
- echo "~${size} MiB"
- }
- download_json_section() {
- local json_text="$1"
- local url=`echo "$json_text" | jq -r -M ".path"`
- local md5=`echo "$json_text" | jq -r -M ".md5"`
- download_file "$url" "$DOWNLOAD_PATH" "$md5"
- }
- get_ini_value() {
- local filename="${1}"
- local variable="${2}"
- grep "${variable}=" "${filename}" | tr -d '\r\n' | sed -e 's|.*=||g'
- }
- # ======== Path sanity checks
- # There is a good reason for this check. Do not pollute the game directory.
- [ -e "${THIS_PATH}/${ANCHOR_FILE}" ] && fatal \
- "Please move this script outside the game directory prior executing." \
- " -> See README.md for proper installation instructions"
- # In case people accidentally want to install the game into the launcher directory.
- [ `find ./*.dll 2>/dev/null | wc -l` -gt 2 ] && fatal \
- "This script is likely run in the wrong directory." \
- "Found more than two DLL files. (expected: 0...2)" \
- "Please run this script in a proper/clean game directory."
- # ======== Command line processing
- # some help
- if [ "$1" = '-h' -o "${1#--}" = 'help' ]; then
- cat << HELP
- ${THIS_FILE} [-h|help] [install]
-
- This script will modify the current working directory.
- Specify "install" for a new installation, or nothing to perform an update.
- See README.md for details and examples.
- HELP
- exit 0
- fi
- # Option to install.
- if [ "$1" = 'install' ]; then
- if [ `find ./* 2>/dev/null | wc -l` -gt 0 ]; then
- fatal "'${PWD}' contains files and/or subdirectories." \
- "Please use an empty directory for a new installation." \
- "To update or resume an installtion, rerun this script without the 'install' argument."
- fi
- echo -e '[General]\r\nchannel=1\r\ncps=mihoyo\r\ngame_version=0.0.0\r\nsub_channel=0\r\n' >"$CONFIG_FILE"
- fi
- # ======== Configuration file parsing
- [ ! -e "$CONFIG_FILE" ] && fatal \
- "Game information file ${CONFIG_FILE} not found." \
- "Make sure 'Genshin Impact Game' is the current working directory." \
- "If you would like to install the game append the 'install' option:" \
- "sh '${THIS_PATH}/${THIS_FILE}' install"
- installed_ver=`get_ini_value "${CONFIG_FILE}" 'game_version'`
- [ -z "$installed_ver" ] && fatal \
- "Cannot read game_version from ${CONFIG_FILE}. File corrupt?"
- echo "--- Installed version: ${installed_ver}"
- # ======== Update information download + meta checks
- # WARNING: File cannot be downloaded to NTFS/FAT* partitions due to special characters
- tmp_path=`mktemp -d`
- trap "rm -rf '$tmp_path'" EXIT
- download_file "$UPDATE_URL" "$tmp_path"
- RESOURCE_FILE=`find "$tmp_path" -name 'resource*' | tee`
- [ ! -f "${RESOURCE_FILE}" ] && fatal \
- "Failed to download version info. Check your internet connection."
- upstream_ver=`jq -r -M '.data .game .latest .version' "$RESOURCE_FILE" | tee`
- echo "--- Latest version: ${upstream_ver}"
- if [ "$upstream_ver" = "$installed_ver" ]; then
- echo
- echo "==> Client is up to date."
- exit 0
- fi
- # Check whether this version can be patched
- patcher_dir="$REPO_PATH"/`echo "$upstream_ver" | tr -d .`
- [ ! -d "$patcher_dir" ] && fatal \
- "No suitable patch script found. Please check the bug tracker for details about the progress."
- # ======== Select update type
- # Check is diff update possible.
- archive_json=`jq -r -M ".data .game .diffs[] | select(.version==\"${installed_ver}\")" "$RESOURCE_FILE"`
- if [ -z "$archive_json" ]; then
- # Fallback to full download.
- archive_json=`jq -r -M '.data .game .latest' "$RESOURCE_FILE"`
- dl_type="new installation"
- else
- dl_type="incremental update"
- fi
- size=`download_json_size "$archive_json"`
- echo "Download type: ${dl_type} (${size})"
- # Confirm install/update.
- while :; do
- read -r -p "Start/continue update? [Y/n]: " input
- #input="y"
- case "$input" in
- Y|y|'')
- echo
- break
- ;;
- n|N)
- exit 0
- ;;
- esac
- done
- # Download main game archive
- download_json_section "$archive_json"
- # ======== Locate and install voiceover packs
- if [ -d "$LANG_PACKS_PATH" ]; then
- # WARNING: Does not handle spaces. Do not add $PWD.
- lang_dir_names=`find "$LANG_PACKS_PATH"/* -type d | xargs basename | tr -d '()' | tr 'a-z' 'A-Z'`
- fi
- # Download langs packs.
- for dir_name in ${lang_dir_names}; do
- eval lang_code="\$LANG_MAP_${dir_name}"
- lang_archive_json=`echo ${archive_json} | jq -r -M ".voice_packs[] | select(.language==\"${lang_code}\")"`
- if [ "$lang_archive_json" = 'null' ]; then
- echo "Cannot find update for language: ${lang_code}"
- continue
- fi
- size=`download_json_size "$lang_archive_json"`
- echo "--- Voiceover pack: ${lang_code} (${size})"
- download_json_section "$lang_archive_json"
- done
- # ======== Revert patch & apply update
- # Run 'patch_revert.sh' on update existing installation.
- if [ -e "$ANCHOR_FILE" ]; then
- echo
- echo "============== Reverting previous Wine patch ==============="
- bash "${patcher_dir}/patch_revert.sh"
- echo "============================================================"
- echo
- fi
- # Unpack the game files and remove old ones according to deletefiles.txt
- echo "--- Updating game files...."
- find "${DOWNLOAD_PATH}" -name "*.zip" | while read archive_name; do
- # Prevent deleting of the possibly necessary files in case of full update.
- rm -f "deletefiles.txt"
- short_name=`basename "${archive_name}"`
- echo "--- Installing ${short_name} ... "
- unzip -o "${archive_name}"
- rm -f "${archive_name}" "${archive_name}.completed"
- # Process new deletefiles
- [ ! -f "deletefiles.txt" ] && continue;
- while read -r line_full; do
- filename=`echo "${line_full}" | tr -d '\r\n'`
- # Safety check. File must be within the game directory.
- [ ! -f "${filename}" ] && continue;
- filepath_abs=`readlink -nf "${PWD}/${filename}"`
- case "$filepath_abs" in
- ("$PWD"/*)
- echo "--- Removing old file: ${filename}"
- rm -f "$filepath_abs"
- continue
- ;;
- esac
- echo "--- Attempt to remove: ${filename}"
- done < "deletefiles.txt"
- done
- rm -f "deletefiles.txt"
- # ======== Config update and game patching
- # Update version in config file.
- sed -i "s/game_version=${installed_ver}/game_version=${upstream_ver}/" "$CONFIG_FILE"
- # The directory should be empty now. Remove.
- rm -r "$DOWNLOAD_PATH"
- echo
- echo "==> Update to version ${upstream_ver} completed"
- # Run wine compatibility patch script.
- echo
- echo "================= Applying new Wine patch =================="
- bash "${patcher_dir}/patch.sh"
- echo "============================================================"
- echo "==> Update script completed successfully"
- exit 0
|