perform_update.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. unzip -o "${ZIP_FILE}"
  52. echo ""
  53. # ======== *.hdiff files
  54. set +e # to capture "$?"
  55. hdiff_files=$(find . -type f -name "*.hdiff")
  56. while read -r diffname; do
  57. if [ -z "$diffname" ]; then
  58. continue
  59. fi
  60. realname="${diffname::-6}" # trim ".hdiff"
  61. if [ -e "$realname" ]; then
  62. echo "--- Patching file ${realname}"
  63. output=$("$HDIFF_APP" -f "$realname" "$diffname" "${realname}.tmp_hdiff")
  64. if [ $? -eq 0 ]; then
  65. mv -f "${realname}.tmp_hdiff" "$realname"
  66. else
  67. echo "$output"
  68. rm -f "${realname}.tmp_hdiff" # just in case
  69. fi
  70. else
  71. # let the game re-download
  72. echo "--- [WARNING] HDiff source not found: ${realname}"
  73. fi
  74. rm "$diffname"
  75. done <<< "$hdiff_files"
  76. set -e
  77. # ======== deletefiles.txt
  78. if [ -f "deletefiles.txt" ]; then
  79. while read -r filename; do
  80. filename=$(echo "${filename}" | tr -d '\r\n')
  81. # Safety check. File must be within the game directory.
  82. [ ! -f "${filename}" ] && continue;
  83. filepath_abs=$(readlink -nf "${PWD}/${filename}")
  84. case "$filepath_abs" in
  85. ("$PWD"/*)
  86. echo "--- Removing old file: ${filename}"
  87. rm -f "$filepath_abs"
  88. continue
  89. ;;
  90. esac
  91. echo "--- [WARNING] Attempt to remove: ${filename}"
  92. done < "deletefiles.txt"
  93. fi
  94. # ======== Final cleanup
  95. do_cleanup
  96. exit 0