From 1136eb4d05a5b14cd98cc8559a9284404f5a9c25 Mon Sep 17 00:00:00 2001 From: RundownRhino <52856631+RundownRhino@users.noreply.github.com> Date: Wed, 29 Nov 2023 21:43:02 +0300 Subject: [PATCH] Refactor, moving chunks to another file and using it everywhere --- src/lib.rs | 24 ++++++++++-------------- src/utils.rs | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 src/utils.rs diff --git a/src/lib.rs b/src/lib.rs index b5f458d..747efa9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,20 +1,19 @@ +mod utils; + use std::{ collections::{hash_map::Entry, HashMap, HashSet}, fmt::Write, fs::File, path::{Path, PathBuf}, }; + +use utils::*; #[macro_use] extern crate log; use fastanvil::{Chunk, JavaChunk, RCoord, Region, RegionFileLoader, RegionLoader}; use itertools::iproduct; use serde::{Deserialize, Serialize}; -pub fn chunks(region: &mut Region) -> impl Iterator>> + '_ { - // x should be the first-changing index - see header_pos in fastanvil - iproduct!(0..32, 0..32).map(|(chunk_z, chunk_x)| region.read_chunk(chunk_x, chunk_z).unwrap()) -} - pub fn count_blocks(region: &mut Region, verbose: bool, dimension: &str) -> BlockCounts { let mut chunks_counted: usize = 0; let mut blocks_counted: u64 = 0; @@ -40,14 +39,11 @@ pub fn count_blocks(region: &mut Region, verbose: bool, dimension: &str) - } chunks_counted += 1; }; - for (x, z) in iproduct!(0..32, 0..32) { - if let Some(c) = region - .read_chunk(x, z) - .unwrap() - // This silently skips chunks that fail to deserialise. - .and_then(|data| JavaChunk::from_bytes(&data).ok()) - { - closure(x, z, c); + + for data in chunks(region).flatten() { + // This silently skips chunks that fail to deserialise. + if let Ok(c) = JavaChunk::from_bytes(&data.data) { + closure(data.x, data.z, c); } } BlockCounts { @@ -106,7 +102,7 @@ pub fn determine_version(regions: &mut RegionFileLoader, zone: Zone) -> RegionVe regions.region(RCoord(reg_x), RCoord(reg_z)).ok().flatten()) { if let Some(c) = chunks(&mut region) - .find_map(|data| data.and_then(|x| JavaChunkEnum::from_bytes(&x).ok())) + .find_map(|data| data.and_then(|x| JavaChunkEnum::from_bytes(&x.data).ok())) { return match c { JavaChunkEnum::Post18(_) => RegionVersion::AtLeast118, diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..0e900e2 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,19 @@ +use std::fs::File; + +use fastanvil::{ChunkData, Region}; +use itertools::iproduct; + +/// Used instead of Region.iter(), which skips over missing chunks +pub fn chunks(region: &mut Region) -> impl Iterator> + '_ { + // x should be the first-changing index - see header_pos in fastanvil + iproduct!(0..32, 0..32).map(|(chunk_z, chunk_x)| { + region + .read_chunk(chunk_x, chunk_z) + .unwrap() + .map(|data| ChunkData { + x: chunk_x, + z: chunk_z, + data, + }) + }) +}