patch.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env bash
  2. # DESCRIPTION
  3. # This is the main patch script to ensure basic game functionality in Wine.
  4. #
  5. # EXIT STATUS
  6. # 0 : The patch was applied successfully.
  7. # other : Patch failure. See stdout or stderr output for details.
  8. # Templates for the next patch testing phase
  9. #echo " === !! UNTESTED PATCH. CHECK FOR BANS USING A TRASH ACCOUNT !! ==="
  10. #echo " === REPORT RESULTS TO THE MAIN REPOSITORY. THANK YOU. ==="
  11. #echo ""
  12. #echo "If you would like to test this patch, modify this script and remove the line below this one."
  13. #exit 1
  14. echo "[NOTE] As of game version 3.7.0, it is assumed that binary patches are no longer"
  15. echo " needed for upcoming game versions. If you face issues while starting the"
  16. echo " game or logging in, please consult TROUBLESHOOTING.md for workarounds."
  17. echo ""
  18. # MacOS and *BSD do not have md5sum: use md5 instead
  19. if [[ $(uname) == "Darwin" || $(uname) == *"BSD" ]]; then
  20. md5sum() {
  21. md5 -q $@
  22. }
  23. fi
  24. DIR=$(dirname "${BASH_SOURCE[0]}")
  25. DATADIR=$(find . -maxdepth 1 -type d -name "*_Data")
  26. FILE="UnityPlayer.dll"
  27. sum=($(md5sum $FILE))
  28. reltype="" # os, cn, bb
  29. block_analytics=1
  30. # original hashes
  31. # TODO: Switch back to file hashes on occasion
  32. if [ -e "GenshinImpact.exe" ]; then
  33. reltype="os"
  34. echo "--- Applying for: International version"
  35. fi
  36. if [ -e "YuanShen.exe" ]; then
  37. reltype="bb"
  38. echo "--- Applying for: bilibili/miHoYo CN version"
  39. fi
  40. if [ -z "$reltype" ]; then
  41. # The patch might corrupt invalid/outdated files if this check is skippd.
  42. echo "[ERROR] Wrong file version or the patch is already applied"
  43. echo " -> md5sum: ${sum}" && exit 1
  44. fi
  45. # =========== DO NOT REMOVE START ===========
  46. if [[ -e "$DIR/$FILE" ]]; then
  47. # There is a good reason for this check. Do not pollute the game directory.
  48. echo "[ERROR] Invalid patch download directory. Please move all"
  49. echo " patch files outside the game directory prior executing."
  50. echo " -> See README.md for proper installation instructions" && exit 1
  51. fi
  52. # =========== DO NOT REMOVE END ===========
  53. echo
  54. echo "--- Setting up blocked servers"
  55. # START OF SUDO DANGER ZONE
  56. etc_hosts="$(cat /etc/hosts)"
  57. # See dev_tools/network.md (up-to-date as of 5.2.0)
  58. declare -a domains
  59. if [[ "$reltype" == "os" ]]; then
  60. domains+=("overseauspider.yuanshen.com")
  61. domains+=("osuspider.yuanshen.com")
  62. domains+=("ys-log-upload-os.hoyoverse.com")
  63. domains+=("sg-public-data-api.hoyoverse.com")
  64. # v-- unused as of 2024-10-09
  65. domains+=("sdk-log-upload-os.hoyoverse.com")
  66. else
  67. # Possibly outdated as of 4.6.0
  68. domains+=("log-upload.mihoyo.com")
  69. domains+=("uspider.yuanshen.com")
  70. domains+=("ys-log-upload.mihoyo.com")
  71. domains+=("sdk-log-upload.mihoyo.com")
  72. fi
  73. servers="# Genshin logging servers (do not remove!)"
  74. for i in "${domains[@]}"; do
  75. servers=$(echo -e "${servers}\n0.0.0.0 $i")
  76. done
  77. if [[ ! "$etc_hosts" == *"$servers"* ]]; then
  78. echo "[Optional] Block analytics domains via /etc/hosts"
  79. echo " The game collects uniquely identifiable information about your system."
  80. echo " For details, refer to the official privacy page or the file"
  81. echo " '../static/analytics_data.md' contained in this repository."
  82. echo "$servers"
  83. read -r -p "Would you like to block the analytics servers above? [Y/n]: " choice
  84. if [[ ! "$choice" == [Nn0]* ]]; then
  85. echo "-- Adding analytics servers"
  86. echo "$servers" | sudo tee -a /etc/hosts
  87. if [ $? -ne 0 ]; then
  88. read -p "--- Failed. Please edit /etc/hosts manually. Enter to continue."
  89. fi
  90. else
  91. block_analytics=0
  92. fi
  93. else
  94. echo "--- Analytics servers are already blocked. Skip."
  95. fi
  96. etc_hosts=""
  97. # END OF SUDO DANGER ZONE
  98. # See issue #471 and #490
  99. if [[ $reltype == "bb" ]]; then
  100. echo "[Info] Detected BiliBili variant. If the game suddenly refuses to start, it is"
  101. echo " recommended to use the 'nmcli' workaround documented in '../TROUBLESHOOTING.md'."
  102. fi
  103. # ===========================================================
  104. echo "--- Adding launcher script"
  105. cp -f "$DIR/patch_files/launcher.bat" .
  106. if [ "$block_analytics" -eq 0 ]; then
  107. # Remove the lines related to domain checking
  108. sed -i "/REM DOMAIN_CHECK_BEGIN/,/REM DOMAIN_CHECK_END/d" "launcher.bat"
  109. fi
  110. if [[ "$reltype" != "os" ]]; then
  111. # Same thing but different
  112. sed -i "s/GenshinImpact/YuanShen/g" "launcher.bat"
  113. fi
  114. n=0
  115. placeholder="UNKNOWN_DOMAIN"
  116. while true; do
  117. [ "$n" -eq "${#domains[@]}" ] && break
  118. grep --quiet "$placeholder" -- "launcher.bat"
  119. if [ $? -eq 1 ]; then
  120. # TODO: There are currently only 5 placeholders
  121. echo "[ERROR] Domain blocking code template is too short."
  122. break
  123. fi
  124. sed -i "1,// s/${placeholder}/${domains[${n}]}/" "launcher.bat"
  125. n=$((n + 1))
  126. done
  127. sed -i "/REM SCRIPT_SANITY_BEGIN/,/REM SCRIPT_SANITY_END/d" "launcher.bat"
  128. # Done!
  129. echo "==> Patch applied! Enjoy the game."
  130. echo
  131. echo "[NOTICE] Please refrain from sharing this project in public. Thank you."
  132. exit 0