mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-05 17:44:52 +01:00
minedmap: use fs helpers
This commit is contained in:
parent
587db0464c
commit
17a02dc74c
3 changed files with 34 additions and 74 deletions
|
@ -21,6 +21,10 @@ pub struct Config {
|
||||||
pub map_dir: PathBuf,
|
pub map_dir: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn coord_filename(coords: RegionCoords, ext: &str) -> String {
|
||||||
|
format!("r.{}.{}.{}", coords.0, coords.1, ext)
|
||||||
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn new(args: super::Args) -> Self {
|
pub fn new(args: super::Args) -> Self {
|
||||||
let region_dir = [&args.input_dir, Path::new("region")].iter().collect();
|
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 {
|
pub fn processed_path(&self, coords: RegionCoords) -> PathBuf {
|
||||||
let filename = format!(
|
let filename = coord_filename(coords, "bin");
|
||||||
"r.{}.{}.bin{}",
|
|
||||||
coords.0,
|
|
||||||
coords.1,
|
|
||||||
if temp { ".tmp" } else { "" },
|
|
||||||
);
|
|
||||||
[&self.processed_dir, Path::new(&filename)].iter().collect()
|
[&self.processed_dir, Path::new(&filename)].iter().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn light_path(&self, coords: RegionCoords, temp: bool) -> PathBuf {
|
pub fn light_path(&self, coords: RegionCoords) -> PathBuf {
|
||||||
let filename = format!(
|
let filename = coord_filename(coords, "png");
|
||||||
"r.{}.{}.png{}",
|
|
||||||
coords.0,
|
|
||||||
coords.1,
|
|
||||||
if temp { ".tmp" } else { "" },
|
|
||||||
);
|
|
||||||
[&self.light_dir, Path::new(&filename)].iter().collect()
|
[&self.light_dir, Path::new(&filename)].iter().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn map_path(&self, coords: RegionCoords, temp: bool) -> PathBuf {
|
pub fn map_path(&self, coords: RegionCoords) -> PathBuf {
|
||||||
let filename = format!(
|
let filename = coord_filename(coords, "png");
|
||||||
"r.{}.{}.png{}",
|
|
||||||
coords.0,
|
|
||||||
coords.1,
|
|
||||||
if temp { ".tmp" } else { "" },
|
|
||||||
);
|
|
||||||
[&self.map_dir, Path::new(&filename)].iter().collect()
|
[&self.map_dir, Path::new(&filename)].iter().collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use std::{fs, path::Path};
|
use std::path::Path;
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
|
||||||
use minedmap::{
|
use minedmap::{
|
||||||
io::storage,
|
io::{fs, storage},
|
||||||
resource,
|
resource,
|
||||||
types::*,
|
types::*,
|
||||||
world::{
|
world::{
|
||||||
|
@ -63,26 +63,16 @@ impl<'a> RegionProcessor<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_region(&self, coords: RegionCoords, processed_region: &ProcessedRegion) -> Result<()> {
|
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)
|
storage::write(&output_path, processed_region)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_lightmap(&self, coords: RegionCoords, lightmap: &image::GrayAlphaImage) -> Result<()> {
|
fn save_lightmap(&self, coords: RegionCoords, lightmap: &image::GrayAlphaImage) -> Result<()> {
|
||||||
let tmp_path = self.config.light_path(coords, true);
|
fs::create_with_tmpfile(&self.config.light_path(coords), |file| {
|
||||||
lightmap
|
lightmap
|
||||||
.save_with_format(&tmp_path, image::ImageFormat::Png)
|
.write_to(file, image::ImageFormat::Png)
|
||||||
.context("Failed to save image")?;
|
.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(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Processes a single region file
|
/// Processes a single region file
|
||||||
|
@ -132,18 +122,8 @@ impl<'a> RegionProcessor<'a> {
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
fs::create_dir_all(&self.config.processed_dir).with_context(|| {
|
fs::create_dir_all(&self.config.processed_dir)?;
|
||||||
format!(
|
fs::create_dir_all(&self.config.light_dir)?;
|
||||||
"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(),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let mut ret = Vec::new();
|
let mut ret = Vec::new();
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
use std::fs;
|
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
|
||||||
use minedmap::{io::storage, resource::block_color, types::*};
|
use minedmap::{
|
||||||
|
io::{fs, storage},
|
||||||
|
resource::block_color,
|
||||||
|
types::*,
|
||||||
|
};
|
||||||
|
|
||||||
use super::common::*;
|
use super::common::*;
|
||||||
|
|
||||||
|
@ -16,7 +18,7 @@ impl<'a> TileRenderer<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_region(&self, coords: RegionCoords) -> Result<ProcessedRegion> {
|
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")
|
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<()> {
|
fn render_tile(&self, coords: RegionCoords) -> Result<()> {
|
||||||
const N: u32 = (BLOCKS_PER_CHUNK * CHUNKS_PER_REGION) as u32;
|
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);
|
||||||
let output_path = self.config.map_path(coords, false);
|
|
||||||
println!(
|
println!(
|
||||||
"Rendering tile {}",
|
"Rendering tile {}",
|
||||||
output_path
|
output_path
|
||||||
|
@ -70,27 +72,16 @@ impl<'a> TileRenderer<'a> {
|
||||||
let region = self.load_region(coords)?;
|
let region = self.load_region(coords)?;
|
||||||
let mut image = image::RgbaImage::new(N, N);
|
let mut image = image::RgbaImage::new(N, N);
|
||||||
Self::render_region(&mut image, ®ion);
|
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<()> {
|
pub fn run(self, regions: &[RegionCoords]) -> Result<()> {
|
||||||
fs::create_dir_all(&self.config.map_dir).with_context(|| {
|
fs::create_dir_all(&self.config.map_dir)?;
|
||||||
format!(
|
|
||||||
"Failed to create directory {}",
|
|
||||||
self.config.map_dir.display(),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
for &coords in regions {
|
for &coords in regions {
|
||||||
if let Err(err) = self.render_tile(coords) {
|
if let Err(err) = self.render_tile(coords) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue