check_integrity.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env bash
  2. # Utility script to check the game sanity
  3. # As of now, this script does not repair files.
  4. #
  5. # Usage: ./check_integrity.sh FILENAME quick
  6. # FILENAME : Checksum file e.g. "pkg_version" of the game or voiceover pack
  7. # "quick" : Skips md5 checks for more speed but is less reliable
  8. set -e
  9. # ======== Functions
  10. fatal() {
  11. echo
  12. for arg in "$@"; do
  13. echo " * $arg" >&2
  14. done
  15. echo
  16. exit 1
  17. }
  18. bad_filepaths=()
  19. on_file_mismatch() {
  20. # 1 = Error Msg, 2 = Filepath
  21. echo "$1 : $2"
  22. bad_filepaths+=("$2")
  23. }
  24. if [[ $(uname) == "Darwin" || $(uname) == *"BSD" ]]; then
  25. STATFLAGS="-f %z"
  26. md5sum() {
  27. md5 -q $@
  28. }
  29. else
  30. STATFLAGS="-c %s"
  31. fi
  32. # ======== Evaluated variables
  33. MASTER_FILE="$1"
  34. CHECK_MD5=1
  35. [ ! -e "$MASTER_FILE" ] && fatal \
  36. "Checksum file $MASTER_FILE not found."
  37. if [ "$2" == "quick" ]; then
  38. CHECK_MD5=0
  39. echo "--- Mode: Quick file size checking (fast, less reliable)"
  40. else
  41. echo "--- Mode: Detailed md5 checking (slow, most reliable)"
  42. fi
  43. # This is faster than running "jq" on each line
  44. # outputs a string in form of "Direcotry/File.ext|abc123|10001"
  45. per_line_data=$(jq -r "[.remoteName, .md5, .fileSize] | join(\"|\")" "$MASTER_FILE")
  46. i=0
  47. while IFS='|' read -r filepath checksum filesize; do
  48. i=$((i + 1))
  49. #echo "$test $filepath $checksum $filesize"
  50. if [ ! -e "$filepath" ]; then
  51. on_file_mismatch "Missing" "$filepath"
  52. continue
  53. fi
  54. size=$(stat $STATFLAGS "$filepath")
  55. if [ "$size" -ne "$filesize" ]; then
  56. on_file_mismatch "Size mismatch: $size != $filesize" "$filepath"
  57. continue
  58. fi
  59. if [ "$CHECK_MD5" -eq 1 ]; then
  60. sum=($(md5sum "$filepath"))
  61. if [ "$sum" != "$checksum" ]; then
  62. on_file_mismatch "Checksum error: $sum != $checksum" "$filepath"
  63. continue
  64. fi
  65. fi
  66. # Log file names every now and then
  67. if [ $((i % 300)) == 0 ]; then
  68. echo "--- Progress: File N°${i}"
  69. fi
  70. done <<< "$per_line_data"
  71. echo ""
  72. echo "==> List of files that do not correspond to the original game"
  73. echo " Certain files might be missing or modified due to patching"
  74. for filepath in "${bad_filepaths[@]}"; do
  75. echo -e "\t$filepath"
  76. done
  77. echo "==> END"
  78. exit 0