Add version field to file metadata

This commit is contained in:
Matthias Schiffer 2023-07-30 21:48:58 +02:00
parent 4d6644f427
commit 6077138292
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
6 changed files with 26 additions and 8 deletions

View file

@ -6,7 +6,10 @@ use std::{
use serde::{Deserialize, Serialize}; 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)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct TileCoords { pub struct TileCoords {

View file

@ -72,7 +72,7 @@ impl<'a> RegionProcessor<'a> {
timestamp: SystemTime, timestamp: SystemTime,
) -> Result<()> { ) -> Result<()> {
let output_path = self.config.processed_path(coords); 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( fn save_lightmap(
@ -83,6 +83,7 @@ impl<'a> RegionProcessor<'a> {
) -> Result<()> { ) -> Result<()> {
fs::create_with_timestamp( fs::create_with_timestamp(
&self.config.tile_path(TileKind::Lightmap, 0, coords), &self.config.tile_path(TileKind::Lightmap, 0, coords),
FILE_META_VERSION,
timestamp, timestamp,
|file| { |file| {
lightmap lightmap

View file

@ -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 image
.write_to(file, image::ImageFormat::Png) .write_to(file, image::ImageFormat::Png)
.context("Failed to save image") .context("Failed to save image")

View file

@ -78,7 +78,7 @@ impl<'a> TileRenderer<'a> {
let mut image = image::RgbaImage::new(N, N); let mut image = image::RgbaImage::new(N, N);
Self::render_region(&mut image, &region); Self::render_region(&mut image, &region);
fs::create_with_timestamp(&output_path, timestamp, |file| { fs::create_with_timestamp(&output_path, FILE_META_VERSION, timestamp, |file| {
image image
.write_to(file, image::ImageFormat::Png) .write_to(file, image::ImageFormat::Png)
.context("Failed to save image") .context("Failed to save image")

View file

@ -8,8 +8,12 @@ use std::{
use anyhow::{Context, Ok, Result}; use anyhow::{Context, Ok, Result};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Clone, Copy, Serialize)]
pub struct FileMetaVersion(pub u32);
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct FileMeta { struct FileMeta {
version: FileMetaVersion,
timestamp: SystemTime, timestamp: SystemTime,
} }
@ -111,7 +115,12 @@ pub fn modified_timestamp(path: &Path) -> Result<SystemTime> {
}) })
} }
pub fn create_with_timestamp<T, F>(path: &Path, timestamp: SystemTime, f: F) -> Result<T> pub fn create_with_timestamp<T, F>(
path: &Path,
version: FileMetaVersion,
timestamp: SystemTime,
f: F,
) -> Result<T>
where where
F: FnOnce(&mut BufWriter<File>) -> Result<T>, F: FnOnce(&mut BufWriter<File>) -> Result<T>,
{ {
@ -119,7 +128,7 @@ where
let meta_path = metafile_name(path); let meta_path = metafile_name(path);
create(&meta_path, |file| { create(&meta_path, |file| {
serde_json::to_writer(file, &FileMeta { timestamp })?; serde_json::to_writer(file, &FileMeta { version, timestamp })?;
Ok(()) Ok(())
})?; })?;

View file

@ -10,8 +10,13 @@ use serde::{de::DeserializeOwned, Serialize};
use super::fs; use super::fs;
pub fn write<T: Serialize>(path: &Path, value: &T, timestamp: SystemTime) -> Result<()> { pub fn write<T: Serialize>(
fs::create_with_timestamp(path, timestamp, |file| { path: &Path,
value: &T,
version: fs::FileMetaVersion,
timestamp: SystemTime,
) -> Result<()> {
fs::create_with_timestamp(path, version, timestamp, |file| {
let data = bincode::serialize(value)?; let data = bincode::serialize(value)?;
let len = u32::try_from(data.len())?; let len = u32::try_from(data.len())?;
let compressed = zstd::bulk::compress(&data, 1)?; let compressed = zstd::bulk::compress(&data, 1)?;