minedmap: separate collection for region list from preprocessing

Preparation for parallel processing, as well as a fix for regions
missing from later steps when the initial processing failed (rather than
using the processed data from a previous run).
This commit is contained in:
Matthias Schiffer 2023-08-14 14:27:52 +02:00
parent d5ac38ed9b
commit c1260a63b5
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
3 changed files with 44 additions and 41 deletions

View file

@ -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()

View file

@ -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(&regions)?;
let tiles = TileMipmapper::new(&config).run(&regions)?;
MetadataWriter::new(&config).run(tiles)?;

View file

@ -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<TileCoords> {
let file_name = path.file_name()?.to_str()?;
let parts: Vec<_> = file_name.split('.').collect();
fn parse_region_filename(file_name: &OsStr) -> Option<TileCoords> {
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<Vec<TileCoords>> {
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<Vec<TileCoords>> {
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,
);
}
ret.push(coords);
}
Ok(ret)
for &coords in &regions {
if let Err(err) = self.process_region(coords) {
eprintln!("Failed to process region {:?}: {:?}", coords, err);
}
}
Ok(regions)
}
}