patch-images.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #! /usr/bin/env bash
  2. # This script is a derivative work based on Wiimmfi Patcher and WIT
  3. # Copyright 2022 MH3SP Patcher Project
  4. # SPDX-License-Identifier: GPL-2.0-or-later
  5. #----- setup
  6. PRINT_TITLE=2
  7. . ./bin/setup.sh
  8. #----- for each source image
  9. "$WIT" --allow-nkit filelist | tr -d '\r' | while read src
  10. do
  11. [[ -f $src ]] || continue
  12. mkdir -p "$LOGDIR"
  13. chmod 777 "$LOGDIR" 2>/dev/null
  14. log="$LOGDIR/${src##*/}.txt"
  15. ana="$LOGDIR/${src##*/}.ana"
  16. (
  17. dest="$DESTDIR/${src##*/}"
  18. if [[ -a $dest ]]
  19. then
  20. printf '%b Already exists: %s %b\n' "$COL_ERROR" "$dest" "$COL0"
  21. exit 1
  22. fi
  23. res_file=
  24. res_file_type=
  25. "$WIT" analyze --bash "$src" -d "$ana" --var res_
  26. . "$ana"
  27. if [[ ! $res_file || ! $res_file_type || $res_file_type = OTHER ]]
  28. then
  29. printf '%b Not a Wii image: %s %b\n' "$COL_ERROR" "$src" "$COL0"
  30. exit 1
  31. fi
  32. if [[ $res_file_type =~ ^NK ]]
  33. then
  34. printf '%b NKIT images not supported: %s %b\n' "$COL_ERROR" "$src" "$COL0"
  35. exit 1
  36. fi
  37. if [[ $res_dol_avail = 0 ]]
  38. then
  39. printf '%b Invalid Wii image: %s %b\n' "$COL_ERROR" "$src" "$COL0"
  40. exit 1
  41. fi
  42. config_path="config/${res_id6}.xml"
  43. if [[ ! -f "$config_path" ]]
  44. then
  45. printf '%b Unsupported game: %s %b\n' "$COL_ERROR" "$res_id6" "$COL0"
  46. exit 1
  47. fi
  48. #--- patch image
  49. mkdir -p "$DESTDIR"
  50. chmod 777 "$DESTDIR" 2>/dev/null
  51. rm -rf "$WORKDIR"
  52. "$WIT" extract -vv -1p "$src" --links --DEST="$WORKDIR" --psel data
  53. if [ $? -ne 0 ]
  54. then
  55. printf '%b Failed to extract game: %s %b\n' "$COL_ERROR" "$src" "$COL0"
  56. exit 1
  57. fi
  58. "$WIT" dolpatch "${WORKDIR}/sys/main.dol" xml="$config_path"
  59. if [ $? -ne 0 ]
  60. then
  61. printf '%b Failed to patch game: %s %b\n' "$COL_ERROR" "$src" "$COL0"
  62. exit 1
  63. fi
  64. src_name="${src##*/}"
  65. "$WIT" copy -vv --links "$WORKDIR" --DEST="$DESTDIR/${src_name%.*}.iso"
  66. if [ $? -ne 0 ]
  67. then
  68. printf '%b Failed to repack game: %s %b\n' "$COL_ERROR" "$src" "$COL0"
  69. exit 1
  70. fi
  71. ) 2>&1 | tee "$log"
  72. done