diff --git a/src/bin/regiondump.rs b/src/bin/regiondump.rs index 1a3b216..3b68881 100644 --- a/src/bin/regiondump.rs +++ b/src/bin/regiondump.rs @@ -12,7 +12,7 @@ struct Args { fn main() -> Result<()> { let args = Args::parse(); - minedmap::io::region::from_file(&args.file)?.foreach_chunk(|x, z, value: fastnbt::Value| { - println!("Chunk({}, {}): {:#x?}", x.0, z.0, value); + minedmap::io::region::from_file(&args.file)?.foreach_chunk(|coords, value: fastnbt::Value| { + println!("Chunk {:?}: {:#x?}", coords, value); }) } diff --git a/src/io/region.rs b/src/io/region.rs index f2347bb..ec42d3c 100644 --- a/src/io/region.rs +++ b/src/io/region.rs @@ -15,8 +15,7 @@ const BLOCKSIZE: usize = 4096; #[derive(Debug)] struct ChunkDesc { - x: ChunkX, - z: ChunkZ, + coords: ChunkCoords, len: u8, } @@ -38,8 +37,10 @@ fn parse_header(header: &[u8; BLOCKSIZE]) -> HashMap { map.insert( offset, ChunkDesc { - x: ChunkX(x), - z: ChunkZ(z), + coords: ChunkCoords { + x: ChunkX(x), + z: ChunkZ(z), + }, len, }, ); @@ -85,7 +86,7 @@ impl Region { where R: Read + Seek, T: DeserializeOwned, - F: FnMut(ChunkX, ChunkZ, T), + F: FnMut(ChunkCoords, T), { let Region { mut reader } = self; @@ -102,13 +103,13 @@ impl Region { let mut seen = [[false; CHUNKS_PER_REGION as usize]; CHUNKS_PER_REGION as usize]; while !chunk_map.is_empty() { - let Some(ChunkDesc { x, z, len }) = chunk_map.remove(&index) else { + let Some(ChunkDesc { coords, len }) = chunk_map.remove(&index) else { reader.seek(SeekFrom::Current(BLOCKSIZE as i64)).context("Failed to seek chunk data")?; index += 1; continue; }; - let chunk_seen = &mut seen[x.0 as usize][z.0 as usize]; + let chunk_seen = &mut seen[coords.x.0 as usize][coords.z.0 as usize]; if *chunk_seen { bail!("Duplicate chunk"); } @@ -119,7 +120,7 @@ impl Region { .read_exact(&mut buffer[..]) .context("Failed to read chunk data")?; - f(x, z, decode_chunk(&buffer[..])?); + f(coords, decode_chunk(&buffer[..])?); index += len as u32; } diff --git a/src/types.rs b/src/types.rs index 54593a4..7fb8649 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,3 +1,5 @@ +use std::fmt::Debug; + pub const CHUNKS_PER_REGION: u8 = 32; /// A chunk X coordinate relative to a region @@ -7,3 +9,16 @@ pub struct ChunkX(pub u8); /// A chunk Z coordinate relative to a region #[derive(Debug, Clone, Copy)] pub struct ChunkZ(pub u8); + +/// A pair of chunk coordinates relative to a region +#[derive(Clone, Copy)] +pub struct ChunkCoords { + pub x: ChunkX, + pub z: ChunkZ, +} + +impl Debug for ChunkCoords { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "({}, {})", self.x.0, self.z.0) + } +}