rhrtools/patch_roms.sh
agatha 32a3826139 feat: skip already patched ROMs
if a patch has already been applied it will be skipped.
2025-07-27 11:49:23 -04:00

121 lines
3.1 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
# Initialize counters
total_patches=0
existing_roms=0
new_patches=0
failed_patches=0
# Find all .bps files and extract week numbers
while IFS= read -r patch_file; do
((total_patches++))
# 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"
# Check if ROM already exists
if [ -f "$output_file" ]; then
echo "Skipping $padded_week (already exists)"
((existing_roms++))
continue
fi
if [ $DRY_RUN -eq 1 ]; then
echo "[DRY RUN] Would patch: $patch_file"
echo "[DRY RUN] Would create: $output_file"
echo
((new_patches++))
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"
((new_patches++))
else
echo "Error patching $patch_file"
((failed_patches++))
fi
echo
fi
else
echo "Warning: Could not extract week number from filename: $patch_file"
((failed_patches++))
fi
done < <(find "$PATCHES_DIR" -type f -name "Week*.bps" | sort -V)
# Print summary
echo
echo "$([ $DRY_RUN -eq 1 ] && echo '[DRY RUN] ')Summary:"
echo "Total patches found: $total_patches"
echo "Already patched ROMs: $existing_roms"
echo "New ROMs to create: $new_patches"
if [ $failed_patches -gt 0 ]; then
echo "Failed patches: $failed_patches"
fi
echo
echo "ROM status in $OUTPUT_DIR:"
echo "Total ROMs after operation: $(($existing_roms + $new_patches))"