1
0
Fork 0
mirror of https://github.com/notwa/mm synced 2024-06-28 12:57:12 -07:00

detect scene and room file types

This commit is contained in:
Connor Olding 2015-03-04 04:37:37 -08:00
parent 5742f395a9
commit 1b3458361c
2 changed files with 50 additions and 3 deletions

40
heuristics.py Normal file
View File

@ -0,0 +1,40 @@
def try_scene(f):
f.seek(0)
while True:
command = f.read(8)
if len(command) == 0:
return False
if command[2:4] != b'\x00\x00':
return False
if command[0] > 0x1e:
return False
if command[4] != 0x02 and command[4] != 0x00:
if command[0] not in (0x05, 0x10, 0x11, 0x12):
return False
if command == b'\x14\x00\x00\x00\x00\x00\x00\x00':
return True
def try_room(f):
# note: doesn't detect syotes_room_0 because it's missing a header
f.seek(0)
while True:
command = f.read(8)
if len(command) == 0:
return False
if command[2:4] != b'\x00\x00':
return False
if command[0] > 0x1e:
return False
if command[4] != 0x03 and command[4] != 0x00:
if command[0] not in (0x05, 0x10, 0x11, 0x12):
return False
if command == b'\x14\x00\x00\x00\x00\x00\x00\x00':
return True
def detect_format(f, fn=None):
if try_scene(f):
return 'scene'
if try_room(f):
return 'room'
return None

View File

@ -7,6 +7,7 @@ from io import BytesIO
from hashlib import sha1 from hashlib import sha1
from util import * from util import *
from heuristics import *
import n64 import n64
import Yaz0 import Yaz0
@ -15,6 +16,12 @@ lament = lambda *args, **kwargs: print(*args, file=sys.stderr, **kwargs)
# assume first entry is makerom (0x1060), and second entry begins from makerom # assume first entry is makerom (0x1060), and second entry begins from makerom
dma_sig = b"\x00\x00\x00\x00\x00\x00\x10\x60\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x60" dma_sig = b"\x00\x00\x00\x00\x00\x00\x10\x60\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x60"
def dump_wrap(data, fn, size):
kind = detect_format(BytesIO(data), fn)
if kind is not None:
fn += '.' + kind
dump_as(data, fn, size)
def z_dump_file(f, i=0, name=None): def z_dump_file(f, i=0, name=None):
vs = R4(f.read(4)) # virtual start vs = R4(f.read(4)) # virtual start
ve = R4(f.read(4)) # virtual end ve = R4(f.read(4)) # virtual end
@ -41,20 +48,20 @@ def z_dump_file(f, i=0, name=None):
pe = ps + size pe = ps + size
f.seek(ps) f.seek(ps)
data = f.read(pe - ps) data = f.read(pe - ps)
dump_as(data, fn, size) dump_wrap(data, fn, size)
else: else:
#lament('file is compressed') #lament('file is compressed')
f.seek(ps) f.seek(ps)
compressed = f.read(pe - ps) compressed = f.read(pe - ps)
if compressed[:4] == b'Yaz0': if compressed[:4] == b'Yaz0':
data = Yaz0.decode(compressed) data = Yaz0.decode(compressed)
dump_as(data, fn, size) dump_wrap(data, fn, size)
else: else:
lament('unknown compression; skipping:', fn) lament('unknown compression; skipping:', fn)
lament(compressed[:4]) lament(compressed[:4])
f.seek(here) f.seek(here)
return True, fn, vs, ve, ps, pe return True
def z_find_dma(f): def z_find_dma(f):
while True: while True: