rhrtools/patch_roms.sh
2025-07-25 16:11:17 -04:00

92 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
# Function to show usage
show_usage() {
echo "Usage: $0 [-d|--dry-run] <path_to_original_rom>"
echo "Options:"
echo " -d, --dry-run Show what would be patched without actually patching"
exit 1
}
# Parse command line arguments
DRY_RUN=0
ORIGINAL_ROM=""
while [[ $# -gt 0 ]]; do
case $1 in
-d|--dry-run)
DRY_RUN=1
shift
;;
*)
ORIGINAL_ROM="$1"
shift
;;
esac
done
# Check if original ROM was provided
if [ -z "$ORIGINAL_ROM" ]; then
show_usage
fi
PATCHES_DIR="./patches"
OUTPUT_DIR="./patched"
# Check if original ROM exists
if [ ! -f "$ORIGINAL_ROM" ]; then
echo "Error: Original ROM file not found: $ORIGINAL_ROM"
exit 1
fi
# Check if flips is installed (skip check in dry run)
if [ $DRY_RUN -eq 0 ]; then
if ! command -v flips &> /dev/null; then
echo "Error: flips is not installed or not in PATH"
exit 1
fi
fi
# Create output directory if it doesn't exist (skip in dry run)
if [ $DRY_RUN -eq 0 ]; then
mkdir -p "$OUTPUT_DIR"
fi
echo "$([ $DRY_RUN -eq 1 ] && echo '[DRY RUN] ')Processing patches..."
echo
# Find all .bps files and extract week numbers
while IFS= read -r patch_file; do
# Extract week number from filename
if [[ $patch_file =~ Week([0-9]+)\.bps$ ]]; then
week_num="${BASH_REMATCH[1]}"
# Create zero-padded week number
padded_week=$(printf "Week%03d.smc" "$week_num")
output_file="$OUTPUT_DIR/$padded_week"
if [ $DRY_RUN -eq 1 ]; then
echo "[DRY RUN] Would patch: $patch_file"
echo "[DRY RUN] Would create: $output_file"
echo
else
echo "Patching: $patch_file -> $padded_week"
# Apply patch using flips
flips -a "$patch_file" "$ORIGINAL_ROM" "$output_file"
# Check if patching was successful
if [ $? -eq 0 ]; then
echo "Successfully created $padded_week"
else
echo "Error patching $patch_file"
fi
echo
fi
else
echo "Warning: Could not extract week number from filename: $patch_file"
fi
done < <(find "$PATCHES_DIR" -type f -name "Week*.bps" | sort -V)
total_files=$(find "$PATCHES_DIR" -type f -name "Week*.bps" | wc -l)
echo "$([ $DRY_RUN -eq 1 ] && echo '[DRY RUN] ')Would create $total_files patched ROMs in $OUTPUT_DIR"