From 9d7077601b6170700a74c67f12ec06d3824a3175 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 23:26:09 -0500 Subject: [PATCH] Use shared aligned stored ZIP entries for USDZ export Co-authored-by: bflatastic --- Cargo.lock | 11 ++++++ Cargo.toml | 2 ++ crates/formats/Cargo.toml | 1 + crates/formats/src/mesh_export/usd.rs | 49 +++++++++++++++++++++++---- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 54f6ef82..a939100a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -957,6 +957,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=6128eabbfb98e5fe486fa7ee4413724e2a4ca908#6128eabbfb98e5fe486fa7ee4413724e2a4ca908" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip", +] + [[package]] name = "craft-zip" version = "0.1.0" @@ -4473,6 +4483,7 @@ dependencies = [ "craft-mesh", "craft-tcraft", "craft-usd", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=6128eabbfb98e5fe486fa7ee4413724e2a4ca908)", "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83)", "exr", "flate2", diff --git a/Cargo.toml b/Cargo.toml index 88936e5f..11a6b539 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,6 +70,8 @@ craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0 craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["serde"] } craft-materialx = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585" } craft-tcraft = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" } +# Isolated aligned writer; existing archive readers retain their pin and behavior. +craft-zip-usdz = { package = "craft-zip", git = "https://github.com/storytold/craft-libs", rev = "6128eabbfb98e5fe486fa7ee4413724e2a4ca908" } craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" } craft-mesh = { git = "https://github.com/storytold/craft-libs", rev = "b952ca0231867c8ad6749ac44cab5fc903c3ad52", default-features = false, features = ["stl", "ply", "fbx", "obj"] } craft-mcmat = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" } diff --git a/crates/formats/Cargo.toml b/crates/formats/Cargo.toml index f7053917..a70198cf 100644 --- a/crates/formats/Cargo.toml +++ b/crates/formats/Cargo.toml @@ -17,6 +17,7 @@ craft-mesh = { workspace = true } craft-mcmat = { workspace = true } craft-tcraft = { workspace = true } craft-zip = { workspace = true } +craft-zip-usdz = { workspace = true } texturecraft-core = { workspace = true } texturecraft-texel = { workspace = true } texturecraft-geom = { workspace = true } diff --git a/crates/formats/src/mesh_export/usd.rs b/crates/formats/src/mesh_export/usd.rs index 2f57f7aa..9c737f16 100644 --- a/crates/formats/src/mesh_export/usd.rs +++ b/crates/formats/src/mesh_export/usd.rs @@ -6,7 +6,6 @@ //! UsdUVTexture shaders. Meters, Y up. use std::fmt::Write as _; -use std::io::Write as _; use texturecraft_core::{Channel, NormalFormat}; use texturecraft_geom::Mesh; @@ -344,14 +343,12 @@ pub(crate) fn material_maps(mesh: &Mesh, stem: &str, textures: &[Vec)]) -> Result> { - let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored).with_alignment(64); - let err = |e: zip::result::ZipError| FormatError::Invalid(format!("usdz package: {e}")); + let mut zip = craft_zip_usdz::Writer::new(); + let err = |e| FormatError::Invalid(format!("usdz package: {e}")); for (name, bytes) in std::iter::once((layer_name, layer)).chain(files.iter().map(|(n, b)| (n.as_str(), b.as_slice()))) { - zip.start_file(name, opts).map_err(err)?; - zip.write_all(bytes)?; + zip.add_stored_aligned(name, bytes, 64).map_err(err)?; } - Ok(zip.finish().map_err(err)?.into_inner()) + zip.finish().map_err(err) } /// Text of a value as usda writes it. @@ -439,3 +436,41 @@ pub(crate) fn usda(layer: &UsdLayer) -> String { usda_prim(&mut s, &layer.root, 0); s } + +#[cfg(test)] +mod aligned_package_tests { + use super::*; + use std::io::{Cursor, Read, Write}; + + #[test] + fn shared_usdz_keeps_layer_first_and_exact_archive_bytes() { + let files = vec![("textures/color.png".to_owned(), vec![0, 1, 255]), ("empty".to_owned(), Vec::new())]; + let bytes = usdz("scene.usdc", b"PXR-USDC", &files).unwrap(); + let mut previous = zip::ZipWriter::new(Cursor::new(Vec::new())); + let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored).with_alignment(64); + for (name, data) in std::iter::once(("scene.usdc", b"PXR-USDC".as_slice())).chain(files.iter().map(|(n, b)| (n.as_str(), b.as_slice()))) { + previous.start_file(name, opts).unwrap(); + previous.write_all(data).unwrap(); + } + assert_eq!(bytes, previous.finish().unwrap().into_inner()); + let mut archive = zip::ZipArchive::new(Cursor::new(bytes)).unwrap(); + assert_eq!(archive.len(), 3); + for (i, (name, data)) in + std::iter::once(("scene.usdc", b"PXR-USDC".as_slice())).chain(files.iter().map(|(n, b)| (n.as_str(), b.as_slice()))).enumerate() + { + let mut entry = archive.by_index(i).unwrap(); + assert_eq!(entry.name().unwrap(), name); + assert_eq!(entry.compression(), zip::CompressionMethod::Stored); + assert_eq!(entry.data_start().unwrap() % 64, 0); + let mut actual = Vec::new(); + entry.read_to_end(&mut actual).unwrap(); + assert_eq!(actual, data); + } + } + + #[test] + fn shared_usdz_rejects_duplicate_layer_name() { + let err = usdz("scene.usdc", b"PXR-USDC", &[("scene.usdc".to_owned(), Vec::new())]).unwrap_err(); + assert!(matches!(err, FormatError::Invalid(ref why) if why.starts_with("usdz package:"))); + } +}