#!/usr/bin/env python3
"""
probe_laye.py — find where 9024 and 5120 are actually stored in a pm4n file,
and dump LAYE + Mode sections in detail.
Run: python3 probe_laye.py Dummy.pm4n
"""
import struct, sys

path = sys.argv[1]
data = open(path, 'rb').read()
fsize = len(data)
print(f"File: {path}  ({fsize} bytes)\n")

# Search for 9024 and 5120 as u32 LE, u16 LE, and as floats
targets = {
    'u32 9024':  struct.pack('<I', 9024),
    'u32 5120':  struct.pack('<I', 5120),
    'u16 9024':  struct.pack('<H', 9024),
    'u16 5120':  struct.pack('<H', 5120),
    'f32 9024':  struct.pack('<f', 9024.0),
    'f32 5120':  struct.pack('<f', 5120.0),
    'f32 153.408': struct.pack('<f', 153.408),
    'f32 87.040':  struct.pack('<f', 87.040),
}
for label, needle in targets.items():
    pos = 0
    while True:
        idx = data.find(needle, pos)
        if idx < 0:
            break
        print(f"  {label} found at 0x{idx:06X} ({idx})")
        pos = idx + 1

print()

# Dump LAYE section
laye_off = data.find(b'LAYE')
if laye_off >= 0:
    print(f"LAYE raw (first 160 bytes from 0x{laye_off:06X}):")
    for i in range(0, 160, 16):
        row = data[laye_off+i : laye_off+i+16]
        if not row: break
        hex_p = ' '.join(f'{b:02X}' for b in row)
        interp = []
        for j in range(0, len(row)-3, 4):
            u = struct.unpack_from('<I', row, j)[0]
            f = struct.unpack_from('<f', row, j)[0]
            interp.append(f"0x{u:08X}/f={f:.5g}")
        print(f"  +0x{i:02X}  {hex_p:<48}  {' | '.join(interp)}")
    print()

# Dump Mode section
mode_off = data.find(b'Mode')
if mode_off >= 0:
    print(f"Mode raw (first 96 bytes from 0x{mode_off:06X}):")
    for i in range(0, 96, 16):
        row = data[mode_off+i : mode_off+i+16]
        if not row: break
        hex_p = ' '.join(f'{b:02X}' for b in row)
        interp = []
        for j in range(0, len(row)-3, 4):
            u = struct.unpack_from('<I', row, j)[0]
            f = struct.unpack_from('<f', row, j)[0]
            interp.append(f"0x{u:08X}/f={f:.5g}")
        print(f"  +0x{i:02X}  {hex_p:<48}  {' | '.join(interp)}")
    print()