Replace the tracked JSON-in-header bake with a deterministic, bounds-checked CLB binary cooked by the build step. cntrl now loads only the binary; direct JSON loading is restricted to debug builds. Add codec/CLI tests, binary checker, editor bake-on-playtest, and pipeline docs.
4.5 KiB
Level data pipeline
Floor 01 is authored data, not C++ source. The pipeline has three stages and one source of truth:
- Author in JSON.
assets/levels/floor01.json(schemaversion: 1) is the only file designers, the editor and scripts edit. Plain text keeps git diffs readable and the level reviewable in a normal editor. - Cook to binary at build time. The CMake
bake_leveltarget runslevel_tool --bake <level.json> <level.bin>. It validates the level first and refuses to cook invalid data. The output is generated into the build tree (assets/levels/floor01.binrelative to the build directory); binaries are never hand-edited or committed. - Load only the binary at runtime.
cntrlreads the cooked.binand never parses JSON in a release build. Debug builds may pass--level <file.json>for hot-reload iteration; the standalone editor cooks its draft before it launchescntrl --level <file.bin>for playtest.
This keeps authoring friendly and diffs clean without shipping a JSON parser (or a JSON text asset) in the client. The binary is deterministic: cooking an unchanged JSON produces identical bytes.
Why a custom binary instead of FlatBuffers
The project deliberately has no third-party runtime dependencies beyond raylib
(see docs/TECHNICAL.md). A small, fixed-layout, bounds-checked codec in
src/level/level_binary.* gives the same benefits — one compact file, no parse
ambiguity, integrity checking — without pulling in a schema compiler and generated
headers. The JSON-to-LevelData conversion in src/level/level_data.cpp remains
the single definition of the authoring schema; the binary is just its cooked form.
Cooked binary layout (CLB v1)
All integers are little-endian. Floats are IEEE-754 bits. Strings are a u32
byte count followed by that many UTF-8 bytes (no terminator). Vectors are a
u32 count followed by that many records. There is no padding. Enums are stored
as their authoring names and validated against the same name tables the JSON
reader uses, so reordering an enum cannot silently reinterpret an old file.
header 8 bytes magic "CNTRLLVL" + u32 version (= 1)
level str name, f32 map_width, f32 map_height, f32 scale,
f32 spawn.x, f32 spawn.y
rooms u32 count; per room: str name, f32 x0,y0,x1,y1, str kind,
i32 flow, i32 side_index
corr. u32 count; per rect: f32 x0,y0,x1,y1
walls u32 count; per line: i32 axis, f32 coord,from,to,height,
u32 gap_count; per gap: f32 from,to
doors u32 count; per door: str id, f32 x0,y0,x1,y1, f32 height
props u32 count; per prop: str kind, f32 pos.x,pos.y, f32 w,d,h,
u8 solid, str model, f32 yaw, str room_kind, f32 y, f32 scale,
u8 destructible
ritual u32 count; per ritual: str type, f32 pos.x,pos.y,
f32 radius,duration,ring_radius, i32 ring_count,
f32 light_radius,light_damage_mult,light_duration
thermo u32 count; per door: f32 pos.x,pos.y, span, yaw
footer u32 FNV-1a checksum of every preceding byte
decode_binary checks the magic, version, exact length, per-string and per-vector
limits, enum names and the checksum before returning a LevelData. It never
modifies the caller's level on failure. level_tool --check-binary <file.bin>
performs the same decode and re-runs gameplay validation on the result.
Working with it
cmake --build build --target bake_level # cook JSON -> build/assets/levels/floor01.bin
cmake --build build --target check_level # JSON checks + cooked-binary check
./build/level_tool --bake assets/levels/floor01.json /tmp/floor01.bin
./build/level_tool --check-binary /tmp/floor01.bin
ctest --test-dir build --output-on-failure
Floor resolves the baked path from the working directory or the executable
directory, so both ./build/cntrl and cd build && ./cntrl find the same file.
If a cooked file cannot be loaded, the client warns and falls back to the
built-in Floor 01 so a missing asset never bricks a build.
Adding a field
- Add it to
LevelDatainsrc/level/level_data.hppwith a safe default. - Add JSON read/write in
src/level/level_data.cpp(unknown fields are already tolerated; new fields must default when absent). - Add it to
write_level/read_levelinsrc/level/level_binary.cpp, then bumpkBinaryVersioninsrc/level/level_binary.hpp. Old binaries are rejected rather than misread; the build step regenerates them from JSON. - Extend
tests/level_binary_test.cppand reruncheck_level+ CTest.