#!/usr/bin/env python3
"""Apply ONLY the guarded diving-catch possession timer change to a copy.
Example: patch_diving_recovery.py input.bin output.bin
The offset is verified for the project's v5.1W base. Other ROMs are accepted
only if the exact original instruction is present; layouts are not guessed.
"""
import argparse,hashlib,struct
from pathlib import Path
p=argparse.ArgumentParser(description=__doc__);p.add_argument('input',type=Path);p.add_argument('output',type=Path);a=p.parse_args()
if a.input.resolve()==a.output.resolve():p.error('use a different output path')
if a.output.exists():p.error('output already exists')
d=bytearray(a.input.read_bytes());at=0x1531c;old=bytes.fromhex('357c00050048');new=bytes.fromhex('357c00b40048')
if d[at:at+6]!=old:p.error(f'expected {old.hex()} at {at:06X}; no file written')
if len(d)<0x200 or len(d)%2 or d[0x100:0x104]!=b'SEGA':p.error('expected a raw, even-sized SEGA .bin ROM')
d[at:at+6]=new;struct.pack_into('>H',d,0x18e,sum(struct.unpack(f'>{(len(d)-0x200)//2}H',d[0x200:]))&65535)
a.output.write_bytes(d);print('Only diving-catch timer 5 -> 180 ticks and ROM checksum changed. SHA256',hashlib.sha256(d).hexdigest())
