Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / tilespill_part1_s1.rs
tilespill_part1_s1.rs231 lines · 9.9 KiBRaw
| 1 | fn encode_entry(tile: u64, layer_id: u8, entry: &ChunkEntry, out: &mut Vec<u8>) { |
| 2 | let layer = &entry.layer; |
| 3 | let base = out.len(); |
| 4 | out.resize(base + ENTRY_HEADER_BYTES, 0); |
| 5 | out[base..base + 8].copy_from_slice(&tile.to_le_bytes()); |
| 6 | out[base + 8..base + 12].copy_from_slice(&(layer.features.len() as u32).to_le_bytes()); |
| 7 | out[base + 12..base + 16].copy_from_slice(&(layer.parts.len() as u32).to_le_bytes()); |
| 8 | out[base + 16..base + 20].copy_from_slice(&(layer.coords.len() as u32).to_le_bytes()); |
| 9 | out[base + 20] = layer_id; |
| 10 | // Byte 21 flags a turn-lane section after the names; byte 22 a building section after that; |
| 11 | // byte 23 a carriageway section after that. The building section is `features` records (one per |
| 12 | // feature, dense), each a BUILDING_BYTES packed [`BuildingAttrs`]. Present only for a |
| 13 | // `buildings` tile that had S3DB attributes in it. |
| 14 | // |
| 15 | // The carriageway section is the same shape for the `roads` layer. It has to be here rather |
| 16 | // than reconstructed later: a side table that never reaches the spill is dropped on every |
| 17 | // build that spills a zoom, and the archive comes back with no lane markings and no error. |
| 18 | let has_turns = !entry.turn_lanes.is_empty(); |
| 19 | let has_buildings = !entry.buildings.is_empty(); |
| 20 | let has_carriageways = !entry.carriageways.is_empty(); |
| 21 | out[base + 21] = has_turns as u8; |
| 22 | out[base + 22] = has_buildings as u8; |
| 23 | out[base + 23] = has_carriageways as u8; |
| 24 | out[base + 24..base + 28].copy_from_slice(&(entry.names.len() as u32).to_le_bytes()); |
| 25 | out[base + 28..base + 32].copy_from_slice(&(entry.ids.len() as u32).to_le_bytes()); |
| 26 | for feature in &layer.features { |
| 27 | // The spill carries the v2 index, not the v1 wire: name_idx, transit_color and the three |
| 28 | // transit lane bytes ride the entry and are re-encoded by the body serializer, so the |
| 29 | // scratch format never lags the codec by a version. |
| 30 | out.extend_from_slice(&feature.kind.to_le_bytes()); |
| 31 | out.extend_from_slice(&feature.kind_detail.to_le_bytes()); |
| 32 | out.push(feature.geom_type); |
| 33 | out.push(feature.flags); |
| 34 | out.extend_from_slice(&feature.name_idx.to_le_bytes()); |
| 35 | out.extend_from_slice(&feature.parts_offset.to_le_bytes()); |
| 36 | out.extend_from_slice(&feature.part_count.to_le_bytes()); |
| 37 | out.extend_from_slice(&feature.transit_color.to_le_bytes()); |
| 38 | out.push(feature.transit_ordinal); |
| 39 | out.push(feature.transit_lanes); |
| 40 | out.push(feature.transit_taper); |
| 41 | out.push(feature.lane_count); |
| 42 | } |
| 43 | for part in &layer.parts { |
| 44 | out.extend_from_slice(&part.coord_start.to_le_bytes()); |
| 45 | out.extend_from_slice(&part.point_count.to_le_bytes()); |
| 46 | out.extend_from_slice(&part.winding.to_le_bytes()); |
| 47 | } |
| 48 | for (x, y) in &layer.coords { |
| 49 | out.extend_from_slice(&x.to_le_bytes()); |
| 50 | out.extend_from_slice(&y.to_le_bytes()); |
| 51 | } |
| 52 | for id in &entry.ids { |
| 53 | out.extend_from_slice(&id.to_le_bytes()); |
| 54 | } |
| 55 | for name in &entry.names { |
| 56 | out.extend_from_slice(&(name.len() as u32).to_le_bytes()); |
| 57 | out.extend_from_slice(name.as_bytes()); |
| 58 | } |
| 59 | // The turn-lane section, after the names, when present. One record per feature, dense. |
| 60 | if has_turns { |
| 61 | for turns in &entry.turn_lanes { |
| 62 | out.push(turns.forward.len() as u8); |
| 63 | out.push(turns.backward.len() as u8); |
| 64 | for m in turns.forward.iter().chain(&turns.backward) { |
| 65 | out.extend_from_slice(&m.to_le_bytes()); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | // The building section, after the turn-lane section, when present. One record per feature. |
| 70 | if has_buildings { |
| 71 | for a in &entry.buildings { |
| 72 | out.extend_from_slice(&a.height.to_le_bytes()); |
| 73 | out.extend_from_slice(&a.min_height.to_le_bytes()); |
| 74 | out.extend_from_slice(&a.roof_height.to_le_bytes()); |
| 75 | out.push(a.roof_shape); |
| 76 | out.push(a.roof_direction); |
| 77 | out.push(a.roof_orientation); |
| 78 | out.extend_from_slice(&a.building_colour.to_le_bytes()); |
| 79 | out.extend_from_slice(&a.roof_colour.to_le_bytes()); |
| 80 | } |
| 81 | } |
| 82 | // The carriageway section, last, when present. One record per feature, in the same field order |
| 83 | // the body's own [`CARRIAGEWAY_RECORD_LEN`] record uses. |
| 84 | if has_carriageways { |
| 85 | for c in &entry.carriageways { |
| 86 | out.push(c.forward); |
| 87 | out.push(c.backward); |
| 88 | out.extend_from_slice(&c.solid_dividers.to_le_bytes()); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | struct EntryHeader { |
| 94 | tile: u64, |
| 95 | layer_id: u8, |
| 96 | features: usize, |
| 97 | parts: usize, |
| 98 | coords: usize, |
| 99 | names: usize, |
| 100 | ids: usize, |
| 101 | has_turns: bool, |
| 102 | has_buildings: bool, |
| 103 | has_carriageways: bool, |
| 104 | } |
| 105 | |
| 106 | fn entry_header(head: &[u8; ENTRY_HEADER_BYTES]) -> Result<EntryHeader> { |
| 107 | // Bytes 21 (turn-lane flag), 22 (building flag) and 23 (carriageway flag) are 0 or 1; anything |
| 108 | // else means the reader is the wrong version for the file, because a newer writer would be |
| 109 | // using the spare values. Guessing would decode a field that moved. |
| 110 | if head[21] > 1 { |
| 111 | return err("a tile chunk entry has an unknown turn-lane flag"); |
| 112 | } |
| 113 | if head[22] > 1 { |
| 114 | return err("a tile chunk entry has an unknown building flag"); |
| 115 | } |
| 116 | if head[23] > 1 { |
| 117 | return err("a tile chunk entry has an unknown carriageway flag"); |
| 118 | } |
| 119 | let u32_at = |
| 120 | |o: usize| u32::from_le_bytes(head[o..o + 4].try_into().expect("4 bytes")) as usize; |
| 121 | let header = EntryHeader { |
| 122 | tile: u64::from_le_bytes(head[0..8].try_into().expect("8 bytes")), |
| 123 | features: u32_at(8), |
| 124 | parts: u32_at(12), |
| 125 | coords: u32_at(16), |
| 126 | layer_id: head[20], |
| 127 | names: u32_at(24), |
| 128 | ids: u32_at(28), |
| 129 | has_turns: head[21] == 1, |
| 130 | has_buildings: head[22] == 1, |
| 131 | has_carriageways: head[23] == 1, |
| 132 | }; |
| 133 | // A layer either has an id per feature or none at all. Checked before the length so a garbled |
| 134 | // count is refused as the desync it is rather than as a size that happens not to fit. |
| 135 | if header.ids != 0 && header.ids != header.features { |
| 136 | return err(format!( |
| 137 | "a tile chunk entry has {} id(s) for {} feature(s)", |
| 138 | header.ids, header.features, |
| 139 | )); |
| 140 | } |
| 141 | let len = ENTRY_HEADER_BYTES as u64 |
| 142 | + payload_bytes(header.features, header.parts, header.coords, header.ids); |
| 143 | if len > MAX_ENTRY_BYTES { |
| 144 | return err(format!("a tile chunk entry is {len} byte(s), which is corruption")); |
| 145 | } |
| 146 | Ok(header) |
| 147 | } |
| 148 | |
| 149 | /// Decode an entry's fixed arenas. `payload` is exactly the fixed bytes the header accounted |
| 150 | /// for; names are pulled separately by the reader, one inline length at a time. |
| 151 | /// |
| 152 | /// `spare` is a previously-yielded entry the merge handed back, its arenas emptied but their |
| 153 | /// capacity kept. Reusing it turns four `Vec` allocations per entry into four `clear`/`reserve` |
| 154 | /// pairs on the serial merge thread. The decoded values are identical either way: every arena is |
| 155 | /// cleared before it is refilled, so nothing of the old entry survives. |
| 156 | fn decode_fixed( |
| 157 | header: &EntryHeader, |
| 158 | payload: &[u8], |
| 159 | spare: Option<ChunkEntry>, |
| 160 | ) -> Result<ChunkEntry> { |
| 161 | let u16_at = |b: &[u8], o: usize| u16::from_le_bytes(b[o..o + 2].try_into().expect("2 bytes")); |
| 162 | let u32_at = |b: &[u8], o: usize| u32::from_le_bytes(b[o..o + 4].try_into().expect("4 bytes")); |
| 163 | let i16_at = |b: &[u8], o: usize| i16::from_le_bytes(b[o..o + 2].try_into().expect("2 bytes")); |
| 164 | |
| 165 | let mut entry = spare.unwrap_or_else(|| ChunkEntry::new(header.layer_id)); |
| 166 | entry.layer.layer_id = header.layer_id; |
| 167 | entry.layer.features.clear(); |
| 168 | entry.layer.features.reserve(header.features); |
| 169 | entry.layer.parts.clear(); |
| 170 | entry.layer.parts.reserve(header.parts); |
| 171 | entry.layer.coords.clear(); |
| 172 | entry.layer.coords.reserve(header.coords); |
| 173 | entry.ids.clear(); |
| 174 | entry.ids.reserve(header.ids); |
| 175 | // The reader fills these; clear so a reused entry starts empty. Names are reserved here (the |
| 176 | // count is in the header); the section arenas are reserved by the reader when their flag is set. |
| 177 | entry.names.clear(); |
| 178 | entry.names.reserve(header.names); |
| 179 | entry.turn_lanes.clear(); |
| 180 | entry.buildings.clear(); |
| 181 | entry.carriageways.clear(); |
| 182 | |
| 183 | let mut at = 0usize; |
| 184 | for _ in 0..header.features { |
| 185 | let b = payload |
| 186 | .get(at..at + FEATURE_BYTES) |
| 187 | .ok_or_else(|| Error("a tile chunk entry's features run past its payload".to_string()))?; |
| 188 | entry.layer.features.push(BodyFeature { |
| 189 | kind: u16_at(b, 0), |
| 190 | kind_detail: u16_at(b, 2), |
| 191 | geom_type: b[4], |
| 192 | flags: b[5], |
| 193 | name_idx: u16_at(b, 6), |
| 194 | parts_offset: u32_at(b, 8), |
| 195 | part_count: u32_at(b, 12), |
| 196 | transit_color: u32_at(b, 16), |
| 197 | transit_ordinal: b[20], |
| 198 | transit_lanes: b[21], |
| 199 | transit_taper: b[22], |
| 200 | lane_count: b[23], |
| 201 | }); |
| 202 | at += FEATURE_BYTES; |
| 203 | } |
| 204 | for _ in 0..header.parts { |
| 205 | let b = payload |
| 206 | .get(at..at + PART_BYTES) |
| 207 | .ok_or_else(|| Error("a tile chunk entry's parts run past its payload".to_string()))?; |
| 208 | entry.layer.parts.push(Part { |
| 209 | coord_start: u32_at(b, 0), |
| 210 | point_count: u32_at(b, 4), |
| 211 | winding: u16_at(b, 8), |
| 212 | }); |
| 213 | at += PART_BYTES; |
| 214 | } |
| 215 | for _ in 0..header.coords { |
| 216 | let b = payload |
| 217 | .get(at..at + COORD_BYTES) |
| 218 | .ok_or_else(|| Error("a tile chunk entry's coords run past its payload".to_string()))?; |
| 219 | entry.layer.coords.push((i16_at(b, 0), i16_at(b, 2))); |
| 220 | at += COORD_BYTES; |
| 221 | } |
| 222 | for _ in 0..header.ids { |
| 223 | let b = payload |
| 224 | .get(at..at + ID_BYTES) |
| 225 | .ok_or_else(|| Error("a tile chunk entry's ids run past its payload".to_string()))?; |
| 226 | entry.ids.push(u64::from_le_bytes(b.try_into().expect("8 bytes"))); |
| 227 | at += ID_BYTES; |
| 228 | } |
| 229 | debug_assert_eq!(at, payload.len(), "the fixed arenas must consume their payload exactly"); |
| 230 | Ok(entry) |
| 231 | } |