pokemon/tools/parse/tileset_headers.py
Zack Buhman a921a44b66 parse: move tokenize_line/tokenize_lines to a new module
At times I forget what I have already written. I hope that this might help keep
things more organized.
2023-07-31 16:23:55 -07:00

42 lines
1.1 KiB
Python

from dataclasses import dataclass
from functools import partial
from parse import number
from parse.generic import macro_line
@dataclass
class TilesetHeader:
name: str
counters: list[str]
grass_tile: str
animations: str
def blockset(self):
# renamed from "block" to better disambiguate from Map blocks
return f"{self.name}_Block"
def gfx(self):
return f"{self.name}_GFX"
def coll(self):
return f"{self.name}_Coll"
tokenize_lines = partial(macro_line.tokenize_lines, prefix="tileset")
def flatten(tokens):
for ts in tokens:
assert ts[0] == 'tileset'
_, (name, c0, c1, c2, grass_tile, animations) = ts
yield TilesetHeader(
name=name,
counters=tuple(map(number.parse, [c0, c1, c2])),
grass_tile=number.parse(grass_tile),
animations=animations
)
def parse(prefix):
path = prefix / 'data/tilesets/tileset_headers.asm'
with open(path) as f:
tokens = tokenize_lines(f.read().split('\n'))
return list(flatten(tokens))