Skip to content

Instantly share code, notes, and snippets.

@NWPlayer123
Created August 30, 2017 05:06
Show Gist options
  • Save NWPlayer123/d5617e493fb56b4686f4c815a1170289 to your computer and use it in GitHub Desktop.
Save NWPlayer123/d5617e493fb56b4686f4c815a1170289 to your computer and use it in GitHub Desktop.
Sonic Mania music swap script
from struct import pack, unpack
from binascii import hexlify, unhexlify
from os.path import exists
from os import mkdir
mode = 1
filename = b"b15bad41162c48c8d5d3dbc533b0e1c8.ogg"
hashname = filename.split(".")[0]
if mode == 0: #extract
if not exists("music"):
mkdir("music")
with open("Data.rsdk", "rb") as f:
assert f.read(4) == b"RSDK"
assert f.read(2) == b"v5"
count = unpack("<H", f.read(2))[0]
datas = []
for i in range(count):
datas.append(unpack("<16sII", f.read(24)))
for data in datas:
f.seek(data[1])
size = data[2] & 0x7FFFFFFF
if f.read(4) == b"OggS":
with open("music/%s.ogg" % hexlify(data[0]), "wb") as o:
o.write(b"OggS" + f.read(size - 4)) #-4 = magic
print("%s %08X %08X" % (hexlify(data[0]), data[1], data[2]))
elif mode == 1: #rebuild
with open("Data.rsdk", "rb") as f:
assert f.read(4) == b"RSDK"
assert f.read(2) == b"v5"
count = unpack("<H", f.read(2))[0]
datas = []
for i in range(count):
datas.append(unpack("<16sII", f.read(24)))
datas2 = []
shift = 0
keep = []
for data in datas:
if hexlify(data[0]) == hashname:
keep = data
oldsize = data[2] & 0x7FFFFFFF
with open("music/%s.ogg" % hexlify(data[0]), "rb") as o:
o.seek(0, 2)
newsize = o.tell()
shift = newsize - oldsize
datas2.append([data[0], data[1], newsize])
else:
datas2.append([data[0], data[1] + shift, data[2]])
with open("Data1.rsdk", "wb") as o:
f.seek(0, 0)
o.write(f.read(8))
for data in datas2:
o.write(data[0] + pack("<II", data[1], data[2]))
f.seek(8 + (24 * len(datas2))) #9D40, not hardcoded, futureproof
size = keep[1] - f.tell()
o.write(f.read(size))
with open("music/%s.ogg" % hexlify(keep[0]), "rb") as q:
o.write(q.read())
f.seek(keep[1] + (keep[2] & 0x7FFFFFFF))
o.write(f.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment