minedmap: use fs helpers

This commit is contained in:
Matthias Schiffer 2023-05-07 16:38:52 +02:00
parent 587db0464c
commit 17a02dc74c
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
3 changed files with 34 additions and 74 deletions

View file

@ -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<ProcessedRegion> {
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, &region);
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) {