WIP DO NOT MERGE Fix: save_map_binary wrote scenario_id prefix that C never reads#374
WIP DO NOT MERGE Fix: save_map_binary wrote scenario_id prefix that C never reads#374eugenevinitsky wants to merge 1 commit into3.0from
Conversation
save_map_binary wrote 16 bytes of scenario_id before sdc_track_index, but load_map_binary in C starts reading at sdc_track_index with no scenario_id. This misaligned all subsequent reads, causing segfaults when loading converted maps. Also fixes Town10HD conversion failure (scenario_id was an int, not str).
There was a problem hiding this comment.
Pull request overview
Fixes a binary format mismatch between Python map conversion (save_map_binary) and the C loader (load_map_binary) that was misaligning reads and causing crashes when loading converted maps.
Changes:
- Removed writing a 16-byte
scenario_idstring prefix insave_map_binaryso the file starts withsdc_track_index, matching the C reader. - Avoided conversion-time failures when
scenario_idisn’t a string by eliminating the.encode()path entirely (since the prefix is no longer written).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Note: C load_map_binary does NOT read a scenario_id prefix. | ||
| # Do not write one here or the binary will be misaligned. | ||
|
|
||
| # Write sdc_track_index | ||
| f.write(struct.pack("i", sdc_track_index)) |
There was a problem hiding this comment.
This change alters the on-disk binary format (removing the 16-byte prefix). Please add a small regression test that writes a minimal map via save_map_binary and asserts the first bytes are sdc_track_index (i.e., no 16-byte offset) so this misalignment doesn’t get reintroduced later.
| # Note: C load_map_binary does NOT read a scenario_id prefix. | ||
| # Do not write one here or the binary will be misaligned. |
There was a problem hiding this comment.
The note here is easy to misread as “scenario_id isn’t part of the binary at all”, but load_map_binary does read an int scenario_id per-entity. Consider rewording to explicitly say “the file must start with sdc_track_index (no 16-byte string prefix)” and/or reference pufferlib/ocean/drive/drive.h:1060 to prevent confusion.
| # Note: C load_map_binary does NOT read a scenario_id prefix. | |
| # Do not write one here or the binary will be misaligned. | |
| # Binary layout note: | |
| # The file must start with sdc_track_index (an int32), with NO 16-byte | |
| # scenario_id string prefix. The corresponding C reader | |
| # (load_map_binary in pufferlib/ocean/drive/drive.h:1060) reads | |
| # scenario_id as an int per-entity, not as a file-level prefix. Writing | |
| # any extra prefix here will misalign all subsequent reads. |
Summary
save_map_binarywrote 16 bytes of scenario_id before sdc_track_index, butload_map_binaryin C starts reading at sdc_track_index with no scenario_id. This misaligned all subsequent reads, causing segfaults when loading converted maps.Also fixes Town10HD conversion failure (scenario_id was an int, not str).
Test plan
process_all_maps