The Windows app builds the flash save when it runs, so the script reads that build and reports the method instead of guessing a packet. Co-authored-by: Cursor <cursoragent@cursor.com>
131 lines
3.8 KiB
Python
131 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Find how MidiSuite saves an SMC-PAD preset.
|
|
|
|
Downloads the current Windows MidiSuite and reads the compiled app.
|
|
It prints the save method names and any finished vendor packets stored
|
|
in the file. It does not connect to the pad.
|
|
|
|
python3 scripts/find_midisuite_save.py
|
|
python3 scripts/find_midisuite_save.py --zip ~/MidiSuite_new.zip
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import io
|
|
import sys
|
|
import urllib.request
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
MIDISUITE_URL = "https://yms-file-store.oss-cn-hongkong.aliyuncs.com/software/pc/MidiSuite_new.zip"
|
|
APP_SO = "Midi Suite/data/app.so"
|
|
|
|
# Names compiled into MidiSuite's SMC-PAD save path.
|
|
SAVE_NAMES = (
|
|
"flashSave0",
|
|
"save0Packet",
|
|
"flashWrite",
|
|
"flashRead",
|
|
"flashErase",
|
|
"smc_pad_desktop_save",
|
|
)
|
|
|
|
READ_COMMAND = 0x23
|
|
WRITE_COMMAND = 0x22
|
|
|
|
|
|
def strings(blob: bytes, shortest: int = 4) -> list[str]:
|
|
found: list[str] = []
|
|
current = bytearray()
|
|
for byte in blob:
|
|
if 32 <= byte < 127:
|
|
current.append(byte)
|
|
continue
|
|
if len(current) >= shortest:
|
|
found.append(current.decode())
|
|
current = bytearray()
|
|
if len(current) >= shortest:
|
|
found.append(current.decode())
|
|
return found
|
|
|
|
|
|
def save_names(blob: bytes) -> list[str]:
|
|
present = set(strings(blob))
|
|
return [name for name in SAVE_NAMES if name in present]
|
|
|
|
|
|
def literal_packets(blob: bytes) -> list[tuple[int, int]]:
|
|
"""Offsets of stored ``00 59`` read or write packets."""
|
|
found: list[tuple[int, int]] = []
|
|
start = 0
|
|
marker = b"\x00\x59"
|
|
while True:
|
|
index = blob.find(marker, start)
|
|
if index < 0 or index + 2 >= len(blob):
|
|
break
|
|
command = blob[index + 2]
|
|
if command in (READ_COMMAND, WRITE_COMMAND):
|
|
found.append((index, command))
|
|
start = index + 1
|
|
return found
|
|
|
|
|
|
def app_so(package: bytes) -> bytes:
|
|
with zipfile.ZipFile(io.BytesIO(package)) as archive:
|
|
try:
|
|
return archive.read(APP_SO)
|
|
except KeyError:
|
|
names = [name for name in archive.namelist() if name.endswith("app.so")]
|
|
if not names:
|
|
raise SystemExit("MidiSuite zip has no app.so")
|
|
return archive.read(names[0])
|
|
|
|
|
|
def report(blob: bytes) -> str:
|
|
names = save_names(blob)
|
|
packets = literal_packets(blob)
|
|
lines = ["MidiSuite save names:"]
|
|
if names:
|
|
lines.extend(f" {name}" for name in names)
|
|
else:
|
|
lines.append(" none")
|
|
lines.append("Stored 00 59 read/write packets:")
|
|
if packets:
|
|
for offset, command in packets:
|
|
kind = "write" if command == WRITE_COMMAND else "read"
|
|
lines.append(f" {offset:#x} command {command:#04x} {kind}")
|
|
else:
|
|
lines.append(" none")
|
|
if "flashSave0" in names and not packets:
|
|
lines.append(
|
|
"Save is the flashSave0 method. It builds save0Packet when MidiSuite runs. "
|
|
"The finished packet is not stored in the file."
|
|
)
|
|
lines.append("Nothing was sent to the pad.")
|
|
return "\n".join(lines)
|
|
|
|
|
|
def download(url: str = MIDISUITE_URL) -> bytes:
|
|
with urllib.request.urlopen(url, timeout=120) as response:
|
|
return response.read()
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
parser.add_argument("--zip", type=Path, help="MidiSuite_new.zip, downloaded when omitted")
|
|
parser.add_argument("--so", type=Path, help="an already extracted app.so")
|
|
args = parser.parse_args(argv)
|
|
|
|
if args.so:
|
|
blob = args.so.read_bytes()
|
|
else:
|
|
package = args.zip.read_bytes() if args.zip else download()
|
|
blob = app_so(package)
|
|
print(report(blob))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|