Guest User

Modified Tools/patchscript for AM2R

a guest
Oct 17th, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. #!/bin/bash
  2. # Set GAMEDIR to the current directory and set logfile
  3. GAMEDIR="$PWD"
  4. LOGFILE="patchlog.txt"
  5. LOGERR="patcherr.txt"
  6.  
  7. # Redirect output and error to the log file
  8. > "$GAMEDIR/$LOGFILE" && exec > >(tee "$GAMEDIR/$LOGFILE") 2>&1
  9. echo "GAMEDIR is set to: $GAMEDIR"
  10.  
  11. # Exports
  12. export DATADIR="$GAMEDIR/assets"
  13. export DATAFILE="am2r.port"
  14. export TMPDIR="$GAMEDIR/tmp"
  15.  
  16. prepare_system() {
  17. # Test for commands: python, zip
  18. PYTHON=$(which python3)
  19. if [ -z "$PYTHON" ]; then
  20. echo "Missing Python!"
  21. return 1
  22. fi
  23.  
  24. # Define 7zzs binary
  25. SEVENZIP="$controlfolder/7zzs.${DEVICE_ARCH}"
  26. if [ ! -x "$SEVENZIP" ]; then
  27. echo "7zzs binary not found at $SEVENZIP"
  28. return 1
  29. fi
  30.  
  31. # Mount the GMToolkit runtime
  32. TOOLKIT="$HOME/gmtoolkit"
  33. RUNTIME="$controlfolder/libs/gmtoolkit.squashfs"
  34. if [ -f "$RUNTIME" ]; then
  35. $ESUDO mkdir -p "$TOOLKIT"
  36. $ESUDO umount "$TOOLKIT" 2>/dev/null || true
  37. $ESUDO mount "$RUNTIME" "$TOOLKIT"
  38. else
  39. echo "This port requires the GMToolkit runtime. Please download it."
  40. return 1
  41. fi
  42.  
  43. # Attempt to get SAVEDIR from gmloader.json using jq
  44. if command -v jq >/dev/null 2>&1; then
  45. SAVEDIR=$(jq -r '.save_dir // empty' "$GAMEDIR/gmloader.json" 2>/dev/null)
  46. else
  47. SAVEDIR="$GAMEDIR/saves"
  48. fi
  49.  
  50. # Exports
  51. export PYTHON
  52. export SAVEDIR
  53. export SEVENZIP
  54. export PATH="$TOOLKIT/gmtools:$TOOLKIT/utmt-cli:$PATH"
  55. export LD_LIBRARY_PATH="/usr/lib:$TOOLKIT/libs:$LD_LIBRARY_PATH"
  56. }
  57.  
  58. unzip_archives() {
  59. # Create folders
  60. mkdir -p "$DATADIR/am2r" "$DATADIR/am2rpatch"
  61.  
  62. # Identify AM2R zip (data.win in root)
  63. AM2R_ZIP="$DATADIR/am2r-another-metroid-2-remake-1-1.zip"
  64.  
  65. # Identify autopatcher zip (has AM2R-Autopatcher-Windows-master/data/)
  66. AUTOPATCHER_ZIP="$DATADIR/AM2R-Autopatcher-Windows-master.zip"
  67.  
  68. AM2R16B_ZIP="AM2R_1.6b2_windows.zip"
  69.  
  70. if [ -z "$AM2R_ZIP" ]; then
  71. echo "Error: No valid AM2R zip file found."
  72. return 1
  73. fi
  74.  
  75. if [ -z "$AUTOPATCHER_ZIP" ]; then
  76. echo "Error: No valid Community Patch zip file found."
  77. return 1
  78. fi
  79.  
  80. if [ -z "$AM2R16B_ZIP" ]; then
  81. echo "Error: No valid AM2R 1.6B2 Patch zip file found."
  82. return 1
  83. fi
  84.  
  85. # Unzip AM2R and autopatcher
  86. "$SEVENZIP" x -y "$AM2R_ZIP" -o"$DATADIR/am2r" >/dev/null
  87. [ -n "$AUTOPATCHER_ZIP" ] && "$SEVENZIP" x -y "$AUTOPATCHER_ZIP" -o"$DATADIR/am2rpatch" >/dev/null
  88. }
  89.  
  90. prepare_files() {
  91. # Remove unnecessary files
  92. rm -rf "$DATADIR/am2r/"*.exe "$DATADIR/am2r/"*.dll "$DATADIR/.gitkeep" "$DATADIR/am2r/"*.txt
  93.  
  94. # Apply autopatcher files if present
  95. if [ -n "$AUTOPATCHER_ZIP" ]; then
  96. cp -rf "$DATADIR/am2rpatch/AM2R-Autopatcher-Windows-master/data/files_to_copy/"* "$DATADIR/am2r/"
  97. fi
  98.  
  99. # Unzip 1.6b patch
  100. "$SEVENZIP" x -y "$DATADIR/$AM2R16B_ZIP" -o"$DATADIR/am2r16b" >/dev/null
  101. cp -rf "$DATADIR/am2r16b/files_to_copy/"* "$DATADIR/am2r/"
  102. [ -n "$AUTOPATCHER_ZIP" ] && cp -rf "$DATADIR/am2rpatch/AM2R-Autopatcher-Windows-master/data/HDR_HQ_in-game_music/"* "$DATADIR/am2r/"
  103.  
  104. DATA="$DATADIR/am2r/data.win"
  105. PATCH="$DATADIR/am2r16b/data.xdelta"
  106. }
  107.  
  108. apply_xdelta() {
  109. # Check if the data.win file exists and apply xdelta
  110. if [ -f "$DATADIR/am2r/data.win" ]; then
  111. output=$($controlfolder/xdelta3 -d -s "$DATA" -f "$PATCH" "$DATADIR/am2r/game.droid" 2>&1)
  112. if [ $? -eq 0 ]; then
  113. echo "Patch applied successfully"
  114. echo "$output"
  115. rm -rf "$DATA" && rm -rf "$DATADIR/am2rpatch"
  116. else
  117. # If this happens, it's not AM2R 1.1
  118. echo "Failed to apply patch"
  119. echo "$output"
  120. return 1
  121. fi
  122. else
  123. # This shouldn't be reachable, but we'll do a safety check anyway
  124. echo "No data.win file found to patch!"
  125. return 1
  126. fi
  127. }
  128.  
  129. clean_files() {
  130. mkdir -p "$TMPDIR"
  131.  
  132. # Move everything from am2r into TMPDIR
  133. if [ -d "$DATADIR/am2r" ]; then
  134. mv "$DATADIR/am2r/"* "$TMPDIR/" 2>/dev/null
  135. mv "$DATADIR/am2r/".* "$TMPDIR/" 2>/dev/null || true # Hidden files (except . and ..)
  136. rmdir "$DATADIR/am2r" 2>/dev/null || true
  137. fi
  138.  
  139. # Clean all contents of assets directory (DATADIR)
  140. rm -rf "$DATADIR/"*
  141.  
  142. # Move everything back from TMPDIR to DATADIR
  143. mv "$TMPDIR/"* "$DATADIR/" 2>/dev/null
  144. mv "$TMPDIR/".* "$DATADIR/" 2>/dev/null || true # Hidden files again
  145.  
  146. # Clean TMPDIR
  147. rm -rf "$TMPDIR/"*
  148. }
  149.  
  150. # Zip it all into the .port file
  151. zip_archive() {
  152. if "$SEVENZIP" a -tzip -aoa -mx=0 "$GAMEDIR/$DATAFILE" "$DATADIR"; then
  153. rm -rf "$DATADIR"/*
  154. else
  155. echo "Failed to create $DATAFILE."
  156. return 1
  157. fi
  158. }
  159.  
  160. # Cleanup function
  161. cleanup() {
  162. rm -rf "$TMPDIR"
  163. $ESUDO umount "$DOTNETDIR" 2>/dev/null || true
  164. $ESUDO umount "$TOOLKIT" 2>/dev/null || true
  165. }
  166.  
  167. process_game() {
  168. echo "Checking for dependencies..."
  169. prepare_system || { cleanup; return 1; }
  170. echo "Unzipping archives..."
  171. unzip_archives || { cleanup; return 1; }
  172. echo "Preparing game..."
  173. prepare_files || { cleanup; return 1; }
  174. echo "Applying 1.6b2 patch..."
  175. apply_xdelta || { cleanup; return 1; }
  176. echo "Cleaning excess files..."
  177. clean_files || { cleanup; return 1; }
  178. echo "Finishing up..."
  179. zip_archive || { cleanup; return 1; }
  180. }
  181.  
  182. # Call the function
  183. if ! process_game; then
  184. echo "Patching process failed!"
  185. mv "$LOGFILE" "$LOGERR"
  186. else
  187. echo "Patching process complete!"
  188. fi
  189.  
  190. # Always run cleanup at the end
  191. cleanup
  192.  
Add Comment
Please, Sign In to add comment