Skip to content

Instantly share code, notes, and snippets.

@mnml

mnml/pcmpatch.py Secret

Last active June 10, 2020 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mnml/4c5b53ccb4fbb48a7ce12fa3174cda49 to your computer and use it in GitHub Desktop.
Save mnml/4c5b53ccb4fbb48a7ce12fa3174cda49 to your computer and use it in GitHub Desktop.
patch PCM sections in Wii VC ROMs with original data
#!/usr/bin/env python3
import io
import re
import sys
import Wii # https://pypi.org/project/Wii.py/
import lzss3 # https://github.com/magical/nlzss
import lzh83 # https://gist.github.com/mnml/1b61ddf0e8141a99018d327dc9e5f21b
if len(sys.argv) <= 2:
sys.exit(f"usage: {sys.argv[0]} <WAD> <original ROM> <output>")
filename, rom = next(f for f in Wii.U8.load(Wii.WAD.loadFile(sys.argv[1])[5]).files if f[0].endswith(b".rom"))
if filename.startswith(b"LZ77"):
rom = lzss3.decompress(rom)
elif filename.startswith(b"LZH8"):
rom = lzh83.decompress(io.BytesIO(rom))
rom = bytearray(rom)
with open(sys.argv[2], "rb") as origf:
orig = origf.read()
for m in re.finditer(rb"PCMF.{5}", rom, re.DOTALL):
rom[slice(*m.span())] = orig[slice(*m.span())]
with open(sys.argv[3], "wb") as outf:
outf.write(rom)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment