From b99ebf73210efa7ae065ea5bfd7709f547da17d7 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Tue, 3 Nov 2026 22:17:57 +0100 Subject: [PATCH] Save size check: skip orphan baked sets, clearer message, wider test Co-Authored-By: Claude Sonnet 5.5 --- crates/formats/src/project_file/mod.rs | 7 ++++--- crates/formats/src/project_file/tests.rs | 12 ++++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/crates/formats/src/project_file/mod.rs b/crates/formats/src/project_file/mod.rs index 0b8ca754..e9513793 100644 --- a/crates/formats/src/project_file/mod.rs +++ b/crates/formats/src/project_file/mod.rs @@ -139,7 +139,8 @@ fn load_size(pf: &ProjectFile) -> Result { let tiles = |img: &TileImage| (img.allocated_count() as u64) * (TILE_AREA * img.comps() * 2) as u64; n += pf.pixels.layers.iter().filter(|((id, _), _)| texture_set_of(&pf.project, *id).is_some()).map(|(_, img)| tiles(img)).sum::(); n += pf.pixels.masks.iter().filter(|(id, _)| texture_set_of(&pf.project, **id).is_some()).map(|(_, img)| tiles(img)).sum::(); - for maps in pf.baked.values() { + // `load` skips the baked maps of a texture set that no longer exists. + for maps in pf.baked.iter().filter(|(ts, _)| **ts < pf.project.texture_sets.len()).map(|(_, m)| m) { for (m, data) in &maps.maps { let h = maps.height(*m); if h != 0 && data.len() == (maps.resolution as usize) * (h as usize) * m.components() { @@ -156,8 +157,8 @@ fn check_loadable(pf: &ProjectFile, limit: u64) -> Result<()> { if size > limit { let gib = |b: u64| b as f64 / (1u64 << 30) as f64; return Err(FormatError::TooLarge(format!( - "the project holds {:.1} GiB of data once unpacked, but TextureCraft opens project files only up to {:.1} GiB, so it was not saved and the file on disk is unchanged. \ - Remove paint layers or baked maps, or lower the texture resolution", + "the project holds {:.2} GiB of data once unpacked, but TextureCraft opens project files only up to {:.2} GiB, so nothing was saved and an existing file is unchanged. \ + Remove paint layers or baked maps to make it smaller", gib(size), gib(limit) ))); diff --git a/crates/formats/src/project_file/tests.rs b/crates/formats/src/project_file/tests.rs index 5ae008f8..44fd17cf 100644 --- a/crates/formats/src/project_file/tests.rs +++ b/crates/formats/src/project_file/tests.rs @@ -219,7 +219,15 @@ fn load_size_matches_what_load_counts() { tiled.project.texture_sets[0].uv_tiles = vec![1001, 1002]; tiled.pixels = PixelStore::default(); tiled.baked = [(0, BakedMaps { resolution: 32, maps: [(BakedMap::AmbientOcclusion, Arc::new(vec![0.25; 32 * 16]))].into() })].into(); - for pf in [&s.pf, &tiled] { + // The branches where the count could drift: pixels of a layer in no stack, a baked map of a + // set that does not exist, and a baked map whose size does not match (not written). + let mut odd = sample().pf; + odd.pixels.layer_mut(LayerId(999), Channel::BaseColor, (128, 128)).set(1, 1, &[1, 2, 3, 4]); + odd.pixels.mask_mut(LayerId(998), (64, 64)).set(1, 1, &[1, 2]); + odd.baked.insert(7, BakedMaps { resolution: 16, maps: [(BakedMap::AmbientOcclusion, Arc::new(vec![0.5; 256]))].into() }); + odd.baked.entry(1).or_default().resolution = 8; + odd.baked.get_mut(&1).unwrap().maps.insert(BakedMap::Curvature, Arc::new(vec![0.5; 13])); + for pf in [&s.pf, &tiled, &odd] { let bytes = save_project_bytes(pf).unwrap(); let (_, counted) = load_counted(Cursor::new(bytes)).unwrap(); assert!(counted > 1000); @@ -234,7 +242,7 @@ fn a_project_too_big_to_reopen_is_not_saved() { assert!(check_loadable(&s.pf, size).is_ok()); let err = check_loadable(&s.pf, size - 1).unwrap_err(); assert!(matches!(err, FormatError::TooLarge(_))); - assert!(err.to_string().contains("not saved"), "{err}"); + assert!(err.to_string().contains("nothing was saved"), "{err}"); } #[cfg(not(target_arch = "wasm32"))]