From 60771382920b9178ef60d0f20c3c898a52f274de Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 30 Jul 2023 21:48:58 +0200 Subject: [PATCH] Add version field to file metadata --- src/bin/minedmap/common.rs | 5 ++++- src/bin/minedmap/region_processor.rs | 3 ++- src/bin/minedmap/tile_mipmapper.rs | 2 +- src/bin/minedmap/tile_renderer.rs | 2 +- src/io/fs.rs | 13 +++++++++++-- src/io/storage.rs | 9 +++++++-- 6 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/bin/minedmap/common.rs b/src/bin/minedmap/common.rs index 4aed1c0..a108e8d 100644 --- a/src/bin/minedmap/common.rs +++ b/src/bin/minedmap/common.rs @@ -6,7 +6,10 @@ use std::{ use serde::{Deserialize, Serialize}; -use minedmap::{types::*, world::layer}; +use minedmap::{io::fs::FileMetaVersion, types::*, world::layer}; + +// Increase to force regeneration of all output files +pub const FILE_META_VERSION: FileMetaVersion = FileMetaVersion(0); #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct TileCoords { diff --git a/src/bin/minedmap/region_processor.rs b/src/bin/minedmap/region_processor.rs index 2081785..d20f82b 100644 --- a/src/bin/minedmap/region_processor.rs +++ b/src/bin/minedmap/region_processor.rs @@ -72,7 +72,7 @@ impl<'a> RegionProcessor<'a> { timestamp: SystemTime, ) -> Result<()> { let output_path = self.config.processed_path(coords); - storage::write(&output_path, processed_region, timestamp) + storage::write(&output_path, processed_region, FILE_META_VERSION, timestamp) } fn save_lightmap( @@ -83,6 +83,7 @@ impl<'a> RegionProcessor<'a> { ) -> Result<()> { fs::create_with_timestamp( &self.config.tile_path(TileKind::Lightmap, 0, coords), + FILE_META_VERSION, timestamp, |file| { lightmap diff --git a/src/bin/minedmap/tile_mipmapper.rs b/src/bin/minedmap/tile_mipmapper.rs index 51eebd9..22b942d 100644 --- a/src/bin/minedmap/tile_mipmapper.rs +++ b/src/bin/minedmap/tile_mipmapper.rs @@ -109,7 +109,7 @@ impl<'a> TileMipmapper<'a> { ); } - fs::create_with_timestamp(&output_path, timestamp, |file| { + fs::create_with_timestamp(&output_path, FILE_META_VERSION, timestamp, |file| { image .write_to(file, image::ImageFormat::Png) .context("Failed to save image") diff --git a/src/bin/minedmap/tile_renderer.rs b/src/bin/minedmap/tile_renderer.rs index 0b804c8..0e91054 100644 --- a/src/bin/minedmap/tile_renderer.rs +++ b/src/bin/minedmap/tile_renderer.rs @@ -78,7 +78,7 @@ impl<'a> TileRenderer<'a> { let mut image = image::RgbaImage::new(N, N); Self::render_region(&mut image, ®ion); - fs::create_with_timestamp(&output_path, timestamp, |file| { + fs::create_with_timestamp(&output_path, FILE_META_VERSION, timestamp, |file| { image .write_to(file, image::ImageFormat::Png) .context("Failed to save image") diff --git a/src/io/fs.rs b/src/io/fs.rs index 0cb2a4d..dfb2e78 100644 --- a/src/io/fs.rs +++ b/src/io/fs.rs @@ -8,8 +8,12 @@ use std::{ use anyhow::{Context, Ok, Result}; use serde::Serialize; +#[derive(Debug, Clone, Copy, Serialize)] +pub struct FileMetaVersion(pub u32); + #[derive(Debug, Serialize)] struct FileMeta { + version: FileMetaVersion, timestamp: SystemTime, } @@ -111,7 +115,12 @@ pub fn modified_timestamp(path: &Path) -> Result { }) } -pub fn create_with_timestamp(path: &Path, timestamp: SystemTime, f: F) -> Result +pub fn create_with_timestamp( + path: &Path, + version: FileMetaVersion, + timestamp: SystemTime, + f: F, +) -> Result where F: FnOnce(&mut BufWriter) -> Result, { @@ -119,7 +128,7 @@ where let meta_path = metafile_name(path); create(&meta_path, |file| { - serde_json::to_writer(file, &FileMeta { timestamp })?; + serde_json::to_writer(file, &FileMeta { version, timestamp })?; Ok(()) })?; diff --git a/src/io/storage.rs b/src/io/storage.rs index b889231..4ba6050 100644 --- a/src/io/storage.rs +++ b/src/io/storage.rs @@ -10,8 +10,13 @@ use serde::{de::DeserializeOwned, Serialize}; use super::fs; -pub fn write(path: &Path, value: &T, timestamp: SystemTime) -> Result<()> { - fs::create_with_timestamp(path, timestamp, |file| { +pub fn write( + path: &Path, + value: &T, + version: fs::FileMetaVersion, + timestamp: SystemTime, +) -> Result<()> { + fs::create_with_timestamp(path, version, timestamp, |file| { let data = bincode::serialize(value)?; let len = u32::try_from(data.len())?; let compressed = zstd::bulk::compress(&data, 1)?;