# reader

> **Tool `tools/formats/reader.py`** (library, Formats). A binary reader (little-endian, with big-endian and variable-length reads) for prototyping decoders of a game's data files.

A library: other tools and your own scripts import it. [Source on GitHub](https://github.com/tanepiper/game-frame/blob/main/tools/formats/reader.py).

## Its own help

This is the help text in the tool itself, as `./gameframe tool reader` prints it.

```text
A binary reader (little-endian, with big-endian and variable-length reads) for prototyping decoders of a game's data files.

    from reader import Reader
    r = Reader(data)
    magic = r.bytes(4); count = r.u32()
    for _ in range(count):
        rid, weight, name = r.u32(), r.f32(), r.pstr8()
    r.done()   # raises if any byte is left: a field you have not found yet

Every read is bounds-checked and says where it failed. `r.block(size)` returns a sub-reader for
a length-prefixed section, so a decoder can check that each section is consumed exactly. Write
the decoder here first, run it over every file of its type with validate.py, and only then
port it to the reimplementation's language.
```

## Named by

Skills: [re-format](/skills/re-format).

Agents: [re-decoder](/agents/re-decoder).
