diff --git a/src/bin/minedmap/common.rs b/src/bin/minedmap/common.rs index eda782d..764fe4c 100644 --- a/src/bin/minedmap/common.rs +++ b/src/bin/minedmap/common.rs @@ -21,6 +21,10 @@ pub struct Config { pub map_dir: PathBuf, } +fn coord_filename(coords: RegionCoords, ext: &str) -> String { + format!("r.{}.{}.{}", coords.0, coords.1, ext) +} + impl Config { pub fn new(args: super::Args) -> Self { let region_dir = [&args.input_dir, Path::new("region")].iter().collect(); @@ -36,33 +40,18 @@ impl Config { } } - pub fn processed_path(&self, coords: RegionCoords, temp: bool) -> PathBuf { - let filename = format!( - "r.{}.{}.bin{}", - coords.0, - coords.1, - if temp { ".tmp" } else { "" }, - ); + pub fn processed_path(&self, coords: RegionCoords) -> PathBuf { + let filename = coord_filename(coords, "bin"); [&self.processed_dir, Path::new(&filename)].iter().collect() } - pub fn light_path(&self, coords: RegionCoords, temp: bool) -> PathBuf { - let filename = format!( - "r.{}.{}.png{}", - coords.0, - coords.1, - if temp { ".tmp" } else { "" }, - ); + pub fn light_path(&self, coords: RegionCoords) -> PathBuf { + let filename = coord_filename(coords, "png"); [&self.light_dir, Path::new(&filename)].iter().collect() } - pub fn map_path(&self, coords: RegionCoords, temp: bool) -> PathBuf { - let filename = format!( - "r.{}.{}.png{}", - coords.0, - coords.1, - if temp { ".tmp" } else { "" }, - ); + pub fn map_path(&self, coords: RegionCoords) -> PathBuf { + let filename = coord_filename(coords, "png"); [&self.map_dir, Path::new(&filename)].iter().collect() } } diff --git a/src/bin/minedmap/region_processor.rs b/src/bin/minedmap/region_processor.rs index d81994f..03229be 100644 --- a/src/bin/minedmap/region_processor.rs +++ b/src/bin/minedmap/region_processor.rs @@ -1,9 +1,9 @@ -use std::{fs, path::Path}; +use std::path::Path; use anyhow::{Context, Result}; use minedmap::{ - io::storage, + io::{fs, storage}, resource, types::*, world::{ @@ -63,26 +63,16 @@ impl<'a> RegionProcessor<'a> { } fn save_region(&self, coords: RegionCoords, processed_region: &ProcessedRegion) -> Result<()> { - let output_path = self.config.processed_path(coords, false); + let output_path = self.config.processed_path(coords); storage::write(&output_path, processed_region) } fn save_lightmap(&self, coords: RegionCoords, lightmap: &image::GrayAlphaImage) -> Result<()> { - let tmp_path = self.config.light_path(coords, true); - lightmap - .save_with_format(&tmp_path, image::ImageFormat::Png) - .context("Failed to save image")?; - - let output_path = self.config.light_path(coords, false); - fs::rename(&tmp_path, &output_path).with_context(|| { - format!( - "Failed to rename {} to {}", - tmp_path.display(), - output_path.display(), - ) - })?; - - Ok(()) + fs::create_with_tmpfile(&self.config.light_path(coords), |file| { + lightmap + .write_to(file, image::ImageFormat::Png) + .context("Failed to save image") + }) } /// Processes a single region file @@ -132,18 +122,8 @@ impl<'a> RegionProcessor<'a> { ) })?; - fs::create_dir_all(&self.config.processed_dir).with_context(|| { - format!( - "Failed to create directory {}", - self.config.processed_dir.display(), - ) - })?; - fs::create_dir_all(&self.config.light_dir).with_context(|| { - format!( - "Failed to create directory {}", - self.config.light_dir.display(), - ) - })?; + fs::create_dir_all(&self.config.processed_dir)?; + fs::create_dir_all(&self.config.light_dir)?; let mut ret = Vec::new(); diff --git a/src/bin/minedmap/tile_renderer.rs b/src/bin/minedmap/tile_renderer.rs index 3ae51c6..b4f4a3a 100644 --- a/src/bin/minedmap/tile_renderer.rs +++ b/src/bin/minedmap/tile_renderer.rs @@ -1,8 +1,10 @@ -use std::fs; - use anyhow::{Context, Result}; -use minedmap::{io::storage, resource::block_color, types::*}; +use minedmap::{ + io::{fs, storage}, + resource::block_color, + types::*, +}; use super::common::*; @@ -16,7 +18,7 @@ impl<'a> TileRenderer<'a> { } fn load_region(&self, coords: RegionCoords) -> Result { - let processed_path = self.config.processed_path(coords, false); + let processed_path = self.config.processed_path(coords); storage::read(&processed_path).context("Failed to load processed region data") } @@ -57,8 +59,8 @@ impl<'a> TileRenderer<'a> { fn render_tile(&self, coords: RegionCoords) -> Result<()> { const N: u32 = (BLOCKS_PER_CHUNK * CHUNKS_PER_REGION) as u32; - let tmp_path = self.config.map_path(coords, true); - let output_path = self.config.map_path(coords, false); + let output_path = self.config.map_path(coords); + println!( "Rendering tile {}", output_path @@ -70,27 +72,16 @@ impl<'a> TileRenderer<'a> { let region = self.load_region(coords)?; let mut image = image::RgbaImage::new(N, N); Self::render_region(&mut image, ®ion); - image - .save_with_format(&tmp_path, image::ImageFormat::Png) - .context("Failed to save image")?; - fs::rename(&tmp_path, &output_path).with_context(|| { - format!( - "Failed to rename {} to {}", - tmp_path.display(), - output_path.display(), - ) - })?; - Ok(()) + fs::create_with_tmpfile(&output_path, |file| { + image + .write_to(file, image::ImageFormat::Png) + .context("Failed to save image") + }) } pub fn run(self, regions: &[RegionCoords]) -> Result<()> { - fs::create_dir_all(&self.config.map_dir).with_context(|| { - format!( - "Failed to create directory {}", - self.config.map_dir.display(), - ) - })?; + fs::create_dir_all(&self.config.map_dir)?; for &coords in regions { if let Err(err) = self.render_tile(coords) {