from math import pow from typing import BinaryIO def nds_get_info(opt: str, inf: BinaryIO): match opt.lower(): case 'title': inf.seek(0x000) return inf.read(12).decode() case 'gamecode': inf.seek(0x00C) return inf.read(4).decode() case 'makercode': inf.seek(0x010) return _maker_lookup(inf.read(2).decode()) case 'unitcode': inf.seek(0x012) return _unit_lookup(inf.read(1)) case 'encryptionseed': inf.seek(0x013) return str(int.from_bytes(inf.read(1), 'little')) case 'devicecapacity': inf.seek(0x014) return _capacity_lookup(int.from_bytes(inf.read(1), 'little')) case 'gamerevision': inf.seek(0x01C) return str(int.from_bytes(inf.read(2), 'little')) case 'region': inf.seek(0x01D) return _region_lookup(int.from_bytes(inf.read(1), 'little')) case _: return 'Unknown' def _maker_lookup(code: str) -> str: match code: case '01': return 'Nintendo ({})'.format(code) case '41': return 'Ubisoft ({})'.format(code) case 'FH': return 'Foreign Media Games ({})'.format(code) case 'GD': return 'Square-Enix ({})'.format(code) case _: return 'Unknown ({})'.format(code) def _unit_lookup(code: bytes) -> str: if code == b'\x00': return 'NDS (00h)' elif code == b'\x02': return 'DSi Enhanced (02h)' elif code == b'\x03': return 'DSi Exclusive (03h)' else: return 'Unknown ({})'.format(code) def _capacity_lookup(code: int) -> str: # 512Mbit (67108864 bytes) (09h) match code: case 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12: return '{} MB ({})'.format(round(128 * pow(2, code) / 1024), '{}h'.format(str(code).zfill(2))) case _: return 'Unknown ({})'.format(code) def _region_lookup(code: int) -> str: match code: case 0: return 'All (0x00 (0))' case 64: return 'Korea (0x40 (64))' case 128: return 'China (0x80 (128))' case _: return 'Unknown ({})'.format(code)