perform_update.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env bash
  2. # Utility script to unzip an archive to $PWD and process HDiffPatch updates
  3. # md5 archive sanity checks must be done by the caller function/application
  4. #
  5. # Usage: ./perform_update.sh "/path/to/archive.zip"
  6. # Exit on error.
  7. set -e
  8. fatal() {
  9. echo
  10. for arg in "$@"; do
  11. echo " * $arg" >&2
  12. done
  13. echo
  14. exit 1
  15. }
  16. do_cleanup() {
  17. # Old patch files
  18. find . -type f -name "*.hdiff" | while read -r filename; do
  19. rm "$filename"
  20. done
  21. # Prevent deleting of the possibly necessary files in case of full update.
  22. rm -f "deletefiles.txt"
  23. rm -f "hdifffiles.txt"
  24. }
  25. # ======== Evaluated variables
  26. THIS_PATH=$(realpath "$(dirname "$0")")
  27. HDIFF_APP="${THIS_PATH}/HDiffPatch/hpatchz"
  28. ZIP_FILE="$1"
  29. # ======== Sanity checks
  30. [ ! -e "config.ini" ] && fatal \
  31. "Game information file not found."
  32. [ ! -e "$ZIP_FILE" ] && fatal \
  33. "Archive '${ZIP_FILE}' not found."
  34. chmod +x "$HDIFF_APP"
  35. # ======== Preparation
  36. do_cleanup
  37. # ======== Unzipping
  38. short_name=$(basename "${ZIP_FILE}")
  39. echo "--- Installing ${short_name} ... "
  40. # macOS has a different version of stat
  41. if [ $(uname) = "Darwin" ]; then
  42. SIZEFLAGS="-f %z"
  43. else
  44. SIZEFLAGS="-c %s"
  45. fi
  46. # unzip returns status code 1 for empty zip files
  47. if [ $(stat $SIZEFLAGS "${ZIP_FILE}") -lt 42 ]; then
  48. echo " Empty zip file. Skipping."
  49. exit 0
  50. fi
  51. # Overwrite + Progress bar
  52. 7za x -aoa -bsp1 -- "${ZIP_FILE}"
  53. echo ""
  54. # ======== *.hdiff files
  55. set +e # to capture "$?"
  56. hdiff_files=$(find . -type f -name "*.hdiff")
  57. while read -r diffname; do
  58. if [ -z "$diffname" ]; then
  59. continue
  60. fi
  61. realname="${diffname::-6}" # trim ".hdiff"
  62. if [ -e "$realname" ]; then
  63. echo "--- Patching file ${realname}"
  64. output=$("$HDIFF_APP" -f "$realname" "$diffname" "${realname}.tmp_hdiff")
  65. if [ $? -eq 0 ]; then
  66. mv -f "${realname}.tmp_hdiff" "$realname"
  67. else
  68. echo "$output"
  69. rm -f "${realname}.tmp_hdiff" # just in case
  70. fi
  71. else
  72. # let the game re-download
  73. echo "--- [WARNING] HDiff source not found: ${realname}"
  74. fi
  75. rm "$diffname"
  76. done <<< "$hdiff_files"
  77. set -e
  78. # ======== deletefiles.txt
  79. if [ -f "deletefiles.txt" ]; then
  80. while read -r filename; do
  81. filename=$(echo "${filename}" | tr -d '\r\n')
  82. # Safety check. File must be within the game directory.
  83. [ ! -f "${filename}" ] && continue;
  84. filepath_abs=$(readlink -nf "${PWD}/${filename}")
  85. case "$filepath_abs" in
  86. ("$PWD"/*)
  87. echo "--- Removing old file: ${filename}"
  88. rm -f "$filepath_abs"
  89. continue
  90. ;;
  91. esac
  92. echo "--- [WARNING] Attempt to remove: ${filename}"
  93. done < "deletefiles.txt"
  94. fi
  95. # ======== Final cleanup
  96. do_cleanup
  97. exit 0