diff --git a/src/bin/minedmap/common.rs b/src/bin/minedmap/common.rs index 44c7ae4..a063bf8 100644 --- a/src/bin/minedmap/common.rs +++ b/src/bin/minedmap/common.rs @@ -85,6 +85,11 @@ impl Config { } } + pub fn region_path(&self, coords: TileCoords) -> PathBuf { + let filename = coord_filename(coords, "mca"); + [&self.region_dir, Path::new(&filename)].iter().collect() + } + pub fn processed_path(&self, coords: TileCoords) -> PathBuf { let filename = coord_filename(coords, "bin"); [&self.processed_dir, Path::new(&filename)].iter().collect() diff --git a/src/bin/minedmap/main.rs b/src/bin/minedmap/main.rs index 838728c..8d3bbea 100644 --- a/src/bin/minedmap/main.rs +++ b/src/bin/minedmap/main.rs @@ -10,7 +10,7 @@ use std::path::PathBuf; use anyhow::Result; use clap::Parser; -use common::{Config, TileCoords}; +use common::Config; use metadata_writer::MetadataWriter; use region_processor::RegionProcessor; use tile_mipmapper::TileMipmapper; @@ -28,11 +28,7 @@ fn main() -> Result<()> { let args = Args::parse(); let config = Config::new(args); - let mut regions = RegionProcessor::new(&config).run()?; - - // Sort regions in a zig-zag pattern to optimize cache usage - regions.sort_unstable_by_key(|&TileCoords { x, z }| (x, if x % 2 == 0 { z } else { -z })); - + let regions = RegionProcessor::new(&config).run()?; TileRenderer::new(&config).run(®ions)?; let tiles = TileMipmapper::new(&config).run(®ions)?; MetadataWriter::new(&config).run(tiles)?; diff --git a/src/bin/minedmap/region_processor.rs b/src/bin/minedmap/region_processor.rs index 34c9555..639dac7 100644 --- a/src/bin/minedmap/region_processor.rs +++ b/src/bin/minedmap/region_processor.rs @@ -1,4 +1,4 @@ -use std::{path::Path, time::SystemTime}; +use std::{ffi::OsStr, path::Path, time::SystemTime}; use anyhow::{Context, Result}; @@ -16,9 +16,8 @@ use minedmap::{ use super::common::*; /// Parses a filename in the format r.X.Z.mca into the contained X and Z values -fn parse_region_filename(path: &Path) -> Option { - let file_name = path.file_name()?.to_str()?; - let parts: Vec<_> = file_name.split('.').collect(); +fn parse_region_filename(file_name: &OsStr) -> Option { + let parts: Vec<_> = file_name.to_str()?.split('.').collect(); let &["r", x, z, "mca"] = parts.as_slice() else { return None; }; @@ -45,6 +44,29 @@ impl<'a> RegionProcessor<'a> { } } + fn collect_regions(&self) -> Result> { + Ok(self + .config + .region_dir + .read_dir() + .with_context(|| { + format!( + "Failed to read directory {}", + self.config.region_dir.display() + ) + })? + .filter_map(|entry| entry.ok()) + .filter(|entry| { + // We are only interested in regular files + matches!( + entry.file_type().map(|file_type| file_type.is_file()), + Ok(true) + ) + }) + .filter_map(|entry| parse_region_filename(&entry.file_name())) + .collect()) + } + /// Processes a single chunk fn process_chunk( &self, @@ -91,13 +113,14 @@ impl<'a> RegionProcessor<'a> { } /// Processes a single region file - fn process_region(&self, path: &Path, coords: TileCoords) -> Result<()> { + fn process_region(&self, coords: TileCoords) -> Result<()> { const N: u32 = (BLOCKS_PER_CHUNK * CHUNKS_PER_REGION) as u32; let mut processed_region = ProcessedRegion::default(); let mut lightmap = image::GrayAlphaImage::new(N, N); - let input_timestamp = fs::modified_timestamp(path)?; + let path = self.config.region_path(coords); + let input_timestamp = fs::modified_timestamp(&path)?; let output_path = self.config.processed_path(coords); let output_timestamp = fs::read_timestamp(&output_path, FILE_META_VERSION); @@ -152,41 +175,20 @@ impl<'a> RegionProcessor<'a> { /// /// Returns a list of the coordinates of all processed regions pub fn run(self) -> Result> { - let read_dir = self.config.region_dir.read_dir().with_context(|| { - format!( - "Failed to read directory {}", - self.config.region_dir.display() - ) - })?; + let mut regions = self.collect_regions()?; + + // Sort regions in a zig-zag pattern to optimize cache usage + regions.sort_unstable_by_key(|&TileCoords { x, z }| (x, if x % 2 == 0 { z } else { -z })); fs::create_dir_all(&self.config.processed_dir)?; fs::create_dir_all(&self.config.tile_dir(TileKind::Lightmap, 0))?; - let mut ret = Vec::new(); - - for entry in read_dir.filter_map(|entry| entry.ok()).filter(|entry| { - // We are only interested in regular files - entry - .file_type() - .map(|file_type| file_type.is_file()) - .unwrap_or_default() - }) { - let path = entry.path(); - let Some(coords) = parse_region_filename(&path) else { - continue; - }; - - if let Err(err) = self.process_region(&path, coords) { - eprintln!( - "Failed to process region {}: {:?}", - path.file_name().unwrap_or_default().to_string_lossy(), - err, - ); + for &coords in ®ions { + if let Err(err) = self.process_region(coords) { + eprintln!("Failed to process region {:?}: {:?}", coords, err); } - - ret.push(coords); } - Ok(ret) + Ok(regions) } }