mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-04 17:23:33 +01:00
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:
parent
d5ac38ed9b
commit
c1260a63b5
3 changed files with 44 additions and 41 deletions
|
@ -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 {
|
pub fn processed_path(&self, coords: TileCoords) -> PathBuf {
|
||||||
let filename = coord_filename(coords, "bin");
|
let filename = coord_filename(coords, "bin");
|
||||||
[&self.processed_dir, Path::new(&filename)].iter().collect()
|
[&self.processed_dir, Path::new(&filename)].iter().collect()
|
||||||
|
|
|
@ -10,7 +10,7 @@ use std::path::PathBuf;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
use common::{Config, TileCoords};
|
use common::Config;
|
||||||
use metadata_writer::MetadataWriter;
|
use metadata_writer::MetadataWriter;
|
||||||
use region_processor::RegionProcessor;
|
use region_processor::RegionProcessor;
|
||||||
use tile_mipmapper::TileMipmapper;
|
use tile_mipmapper::TileMipmapper;
|
||||||
|
@ -28,11 +28,7 @@ fn main() -> Result<()> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let config = Config::new(args);
|
let config = Config::new(args);
|
||||||
|
|
||||||
let mut regions = RegionProcessor::new(&config).run()?;
|
let 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 }));
|
|
||||||
|
|
||||||
TileRenderer::new(&config).run(®ions)?;
|
TileRenderer::new(&config).run(®ions)?;
|
||||||
let tiles = TileMipmapper::new(&config).run(®ions)?;
|
let tiles = TileMipmapper::new(&config).run(®ions)?;
|
||||||
MetadataWriter::new(&config).run(tiles)?;
|
MetadataWriter::new(&config).run(tiles)?;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{path::Path, time::SystemTime};
|
use std::{ffi::OsStr, path::Path, time::SystemTime};
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
|
||||||
|
@ -16,9 +16,8 @@ use minedmap::{
|
||||||
use super::common::*;
|
use super::common::*;
|
||||||
|
|
||||||
/// Parses a filename in the format r.X.Z.mca into the contained X and Z values
|
/// 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> {
|
fn parse_region_filename(file_name: &OsStr) -> Option<TileCoords> {
|
||||||
let file_name = path.file_name()?.to_str()?;
|
let parts: Vec<_> = file_name.to_str()?.split('.').collect();
|
||||||
let parts: Vec<_> = file_name.split('.').collect();
|
|
||||||
let &["r", x, z, "mca"] = parts.as_slice() else {
|
let &["r", x, z, "mca"] = parts.as_slice() else {
|
||||||
return None;
|
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
|
/// Processes a single chunk
|
||||||
fn process_chunk(
|
fn process_chunk(
|
||||||
&self,
|
&self,
|
||||||
|
@ -91,13 +113,14 @@ impl<'a> RegionProcessor<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Processes a single region file
|
/// 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;
|
const N: u32 = (BLOCKS_PER_CHUNK * CHUNKS_PER_REGION) as u32;
|
||||||
|
|
||||||
let mut processed_region = ProcessedRegion::default();
|
let mut processed_region = ProcessedRegion::default();
|
||||||
let mut lightmap = image::GrayAlphaImage::new(N, N);
|
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_path = self.config.processed_path(coords);
|
||||||
let output_timestamp = fs::read_timestamp(&output_path, FILE_META_VERSION);
|
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
|
/// Returns a list of the coordinates of all processed regions
|
||||||
pub fn run(self) -> Result<Vec<TileCoords>> {
|
pub fn run(self) -> Result<Vec<TileCoords>> {
|
||||||
let read_dir = self.config.region_dir.read_dir().with_context(|| {
|
let mut regions = self.collect_regions()?;
|
||||||
format!(
|
|
||||||
"Failed to read directory {}",
|
// Sort regions in a zig-zag pattern to optimize cache usage
|
||||||
self.config.region_dir.display()
|
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.processed_dir)?;
|
||||||
fs::create_dir_all(&self.config.tile_dir(TileKind::Lightmap, 0))?;
|
fs::create_dir_all(&self.config.tile_dir(TileKind::Lightmap, 0))?;
|
||||||
|
|
||||||
let mut ret = Vec::new();
|
for &coords in ®ions {
|
||||||
|
if let Err(err) = self.process_region(coords) {
|
||||||
for entry in read_dir.filter_map(|entry| entry.ok()).filter(|entry| {
|
eprintln!("Failed to process region {:?}: {:?}", coords, err);
|
||||||
// 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)
|
Ok(regions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue