1
0
Fork 0
mirror of https://github.com/notwa/mm synced 2024-11-05 02:49:02 -08:00
mm/heuristics.py

62 lines
1.7 KiB
Python
Raw Normal View History

2016-02-02 05:29:57 -08:00
from util import *
2015-03-08 09:52:19 -07:00
def try_makerom(f):
2015-03-04 04:37:37 -08:00
f.seek(0)
2015-03-08 09:52:19 -07:00
if f.read(12) == b'\x80\x37\x12\x40\x00\x00\x00\x0f\x80\x00\x04\x00':
return True
return False
def try_dmadata(f):
f.seek(0)
if f.read(20) == b"\x00\x00\x00\x00\x00\x00\x10\x60\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x60":
return True
return False
def try_actor(f):
2016-02-02 05:29:57 -08:00
f.seek(-4, 2)
section_size = R4(f.read(4))
file_size = f.tell()
if section_size > 0x4000 or section_size == 0:
# assume this section isn't larger than 16KiB
# (that's 4 times larger than the largest one in OoT)
return False
f.seek(-section_size, 2)
section_start = f.tell()
data_offset = R4(f.read(4))
data_size = R4(f.read(4))
misc_size = R4(f.read(4))
if section_start == data_offset + data_size + misc_size:
return True
2015-03-13 12:16:26 -07:00
return False
2015-03-04 04:37:37 -08:00
2015-03-08 09:52:19 -07:00
def try_scene_or_room(f, bank=2):
2015-03-04 04:37:37 -08:00
# 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
2015-03-08 09:52:19 -07:00
if command[4] != bank and command[4] != 0x00:
2015-03-04 04:37:37 -08:00
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):
2015-03-08 09:52:19 -07:00
if try_makerom(f):
return None
if try_dmadata(f):
return None
if try_actor(f):
return 'actor'
if try_scene_or_room(f, 2):
2015-03-04 04:37:37 -08:00
return 'scene'
2015-03-08 09:52:19 -07:00
if try_scene_or_room(f, 3):
2015-03-04 04:37:37 -08:00
return 'room'
return None