mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-04 17:23:33 +01:00
io/region: use ChunkArray to parse region header
A layout of a ChunkArray::<u32> matches the binary format of the region header.
This commit is contained in:
parent
28b22ce423
commit
daa188eb1d
3 changed files with 15 additions and 9 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -20,6 +20,12 @@ version = "1.3.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "bytemuck"
|
||||
version = "1.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c041d3eab048880cb0b86b256447da3f18859a163c3b8d8893f4e6368abe6393"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.4.3"
|
||||
|
@ -202,6 +208,7 @@ name = "minedmap"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"bytemuck",
|
||||
"clap",
|
||||
"fastnbt",
|
||||
"flate2",
|
||||
|
|
|
@ -9,6 +9,7 @@ default-run = "minedmap"
|
|||
|
||||
[dependencies]
|
||||
anyhow = "1.0.68"
|
||||
bytemuck = "1.13.0"
|
||||
clap = { version = "4.1.4", features = ["derive"] }
|
||||
fastnbt = "2.3.2"
|
||||
flate2 = "1.0.25"
|
||||
|
|
|
@ -19,20 +19,18 @@ struct ChunkDesc {
|
|||
len: u8,
|
||||
}
|
||||
|
||||
fn parse_header(header: &[u8; BLOCKSIZE]) -> HashMap<u32, ChunkDesc> {
|
||||
fn parse_header(header: &ChunkArray<u32>) -> HashMap<u32, ChunkDesc> {
|
||||
let mut map = HashMap::new();
|
||||
|
||||
for coords in ChunkArray::<()>::keys() {
|
||||
let chunk = &header[(4
|
||||
* (usize::from(CHUNKS_PER_REGION) * usize::from(coords.z.0)
|
||||
+ usize::from(coords.x.0)))..];
|
||||
for (coords, &chunk) in header.iter() {
|
||||
let offset_len = u32::from_be(chunk);
|
||||
|
||||
let offset = u32::from(chunk[0]) << 16 | u32::from(chunk[1]) << 8 | u32::from(chunk[2]);
|
||||
let offset = offset_len >> 8;
|
||||
if offset == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let len = chunk[3];
|
||||
let len = offset_len as u8;
|
||||
|
||||
map.insert(offset, ChunkDesc { coords, len });
|
||||
}
|
||||
|
@ -81,9 +79,9 @@ impl<R: Read + Seek> Region<R> {
|
|||
let Region { mut reader } = self;
|
||||
|
||||
let mut chunk_map = {
|
||||
let mut header = [0u8; BLOCKSIZE];
|
||||
let mut header = ChunkArray::<u32>::default();
|
||||
reader
|
||||
.read_exact(&mut header)
|
||||
.read_exact(bytemuck::cast_mut::<_, [u8; BLOCKSIZE]>(&mut header.0))
|
||||
.context("Failed to read region header")?;
|
||||
|
||||
parse_header(&header)
|
||||
|
|
Loading…
Add table
Reference in a new issue