Skip to content

Instantly share code, notes, and snippets.

@PikalaxALT
Created December 26, 2023 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PikalaxALT/7f2daef0e35f258d292c886fedac45b8 to your computer and use it in GitHub Desktop.
Save PikalaxALT/7f2daef0e35f258d292c886fedac45b8 to your computer and use it in GitHub Desktop.
random heartgold dumping tools
import re
import sys
label_pat = re.compile('(?P<name>\w+):')
data_pat = re.compile('\s+\.byte (?P<data>(?:(?:0x[0-9a-fA-F]{2})(?:, )?){6})')
name = None
nrow = 0
with open('.vscode/scratch.s') as fp:
for line in fp:
line = line.rstrip()
if m := label_pat.fullmatch(line):
if name is not None:
assert nrow == 6
print('};\n')
name = m['name']
nrow = 0
print(f'static const AlphPuzzle {name}[ALPH_PUZZLE_TILES_HIGH] = {{')
elif m := data_pat.fullmatch(line):
data = m['data']
print(' {')
for i, byte in enumerate(data.split(', '), 1):
value = int(byte, 0)
print(f' {{ {value & 31}, {(value >> 5) & 3}, {"TRUE" if (value >> 7) & 1 else "FALSE"} }},')
assert i == 6
print(' },')
nrow += 1
assert nrow == 6
print('};')
#!/usr/bin/env python3
import struct
import pathlib
import json
resdat_kind = [
'char',
'pltt',
'cell',
'anim',
'multicell',
'multianim',
]
for file in pathlib.Path('files/data/resdat').glob('resdat_*.bin'):
obj = None
with open(file, 'rb') as fp:
data = fp.read()
try:
if (kind := int.from_bytes(data[:4], 'little')) < len(resdat_kind):
reslist = list(struct.iter_unpack('<llllll', data[4:]))
obj = {
'kind': resdat_kind[kind],
'graphics': [
{
'narc': x[0],
'file': x[1],
'compressed': bool(x[2]),
'id': x[3],
'extra': [x[4], x[5]]
} for x in reslist if x[0] != -2
]
}
except struct.error:
pass
if obj is None:
try:
reslist = list(struct.iter_unpack('<llllllll', data))
obj = {
'kind': None,
'entries': [
{
'graphics': x[0],
'palette': x[1],
'cell': x[2],
'anim': x[3],
'transfer': x[6],
'priority': x[7],
} | (
{
'multicell': x[4],
'multianim': x[5],
} if x[4] != -1 else {}
) for x in reslist if x[0] != -2
]
}
except struct.error:
print(file, 'does not match template')
with open(file.with_suffix('.json'), 'w') as ofp:
json.dump(obj, ofp, indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment