diff --git a/Cargo.lock b/Cargo.lock index 8368bc88..5a2b05d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -815,6 +815,17 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-mcmat" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=b6d9feed23ceae249b40593ed84a668343091627#b6d9feed23ceae249b40593ed84a668343091627" +dependencies = [ + "craft-zip", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-menu" version = "0.1.0" @@ -913,6 +924,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=b6d9feed23ceae249b40593ed84a668343091627#b6d9feed23ceae249b40593ed84a668343091627" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip", +] + [[package]] name = "crc32fast" version = "1.5.2" @@ -4394,7 +4415,9 @@ version = "0.6.1" dependencies = [ "base64 0.22.1", "craft-materialx", + "craft-mcmat", "craft-mesh", + "craft-zip", "exr", "flate2", "gltf", diff --git a/Cargo.toml b/Cargo.toml index 09fada6a..28c1404c 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-mesh = { git = "https://github.com/storytold/craft-libs", rev = "d6ed8c20f12188c1f39c89b3a8436bd0d09c3748", default-features = false, features = ["stl"] } +craft-mcmat = { git = "https://github.com/storytold/craft-libs", rev = "b6d9feed23ceae249b40593ed84a668343091627" } +craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "b6d9feed23ceae249b40593ed84a668343091627" } # Who draws the window frame, and the app-drawn strip as the title bar (crates/ui-egui/src/titlebar.rs). craft-window = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["egui"] } # Release checks, the update prompt and the update helper (crates/ui-egui/src/updates_ui.rs). diff --git a/crates/engine/src/link_cmds.rs b/crates/engine/src/link_cmds.rs index e5122cd9..3a77ae50 100644 --- a/crates/engine/src/link_cmds.rs +++ b/crates/engine/src/link_cmds.rs @@ -56,7 +56,7 @@ pub fn specs() -> Vec { "Save MaterialCraft package", [], None, - "{layer?, path} — writes the MaterialCraft package (.mcraft) behind a material layer, to open in MaterialCraft", + "{layer?, path} — writes the MaterialCraft source (.mcraft or .sbs) behind a material layer", has_layer, save_source ), @@ -178,7 +178,9 @@ fn save_source(s: &mut Session, p: &Value) -> Result { const CMD: &str = "material.link.saveSource"; let id = layer_param(s, CMD, p)?; let path = strp(p, "path").ok_or_else(|| bad(CMD, "`path` is required"))?; - let path = if path.to_ascii_lowercase().ends_with(&format!(".{MCRAFT_EXT}")) { path.to_string() } else { format!("{path}.{MCRAFT_EXT}") }; + let link = link_of(s, CMD, id)?; + let ext = source_extension(&link.package); + let path = if path.to_ascii_lowercase().ends_with(&format!(".{ext}")) { path.to_string() } else { format!("{path}.{ext}") }; let bytes = s.link_package(id)?; texturecraft_formats::write_atomic(std::path::Path::new(&path), &bytes).map_err(|e| bad(CMD, e.to_string()))?; Ok(json!({"path": path})) @@ -195,6 +197,10 @@ fn save_source(_: &mut Session, _: &Value) -> Result { pub struct LinkJob { pub layer: LayerId, pub package: Arc>, + /// The source package extension (`mcraft` or `sbs`). + pub source_ext: &'static str, + /// The named graph in a linked material; direct package imports use its default. + pub graph: Option, /// Pixels across (the texture set's resolution). pub size: u32, /// Every parameter's value, by id, in MaterialCraft's typed form. @@ -226,7 +232,7 @@ fn quiet(program: &std::path::Path) -> std::process::Command { c } -/// Open `package` (a `.mcraft` file) in the MaterialCraft app (`$MATERIALCRAFT`, next to this +/// Open `package` (a `.mcraft` or `.sbs` file) in the MaterialCraft app (`$MATERIALCRAFT`, next to this /// program, or on `PATH`). #[cfg(not(target_arch = "wasm32"))] pub fn open_in_materialcraft(package: &std::path::Path) -> Result<()> { @@ -236,11 +242,21 @@ pub fn open_in_materialcraft(package: &std::path::Path) -> Result<()> { }) } +/// Source file extensions are chosen from the linked resource name, never an untrusted path. +pub fn source_extension(name: &str) -> &'static str { + if name.to_ascii_lowercase().ends_with(".sbs") { "sbs" } else { MCRAFT_EXT } +} + impl LinkJob { /// Cook through `materialcraft-cli` (`$MATERIALCRAFT_CLI`, next to this program, or on /// `PATH`); returns `.mcmat` bytes. Blocks for the cook (about a second at 2K). #[cfg(not(target_arch = "wasm32"))] pub fn run(&self) -> Result> { + self.run_with_cli(&materialcraft_tool("materialcraft-cli", "MATERIALCRAFT_CLI")) + } + + #[cfg(not(target_arch = "wasm32"))] + fn run_with_cli(&self, cli: &std::path::Path) -> Result> { let other = |e: String| EngineError::Other(e); let n = NEXT_JOB.fetch_add(1, std::sync::atomic::Ordering::Relaxed); let dir = std::env::temp_dir().join(format!("texturecraft-link-{}-{n}", std::process::id())); @@ -252,15 +268,27 @@ impl LinkJob { std::fs::write(&p, bytes).map_err(|e| other(format!("{name}: {e}")))?; utf8(&p) }; - let src = write("source.mcraft", &self.package)?; + let ext = if self.source_ext == "sbs" { "sbs" } else { MCRAFT_EXT }; + let src = write(&format!("source.{ext}"), &self.package)?; let mut inputs = Map::new(); for (i, (id, png)) in self.inputs.iter().enumerate() { inputs.insert(id.clone(), json!(write(&format!("input{i}.png"), png)?)); } let out = dir.join(format!("out.{MCMAT_EXT}")); let params = json!({"path": utf8(&out)?, "size": self.size, "values": self.values, "inputs": inputs}); - let cli = materialcraft_tool("materialcraft-cli", "MATERIALCRAFT_CLI"); - let run = quiet(&cli).arg("run").arg(&src).arg("file.exportMaterial").arg(params.to_string()).output().map_err(|e| { + let mut command = quiet(cli); + if let Some(graph) = &self.graph { + // Opening the source starts on its first graph; select the graph saved in the link. + let script = json!({"open": src, "steps": [ + {"id": "graph.open", "params": {"graph": graph}}, + {"id": "file.exportMaterial", "params": params}, + ]}); + let script_path = write("cook.json", script.to_string().as_bytes())?; + command.arg("batch").arg(script_path); + } else { + command.arg("run").arg(&src).arg("file.exportMaterial").arg(params.to_string()); + } + let run = command.output().map_err(|e| { other(format!( "MaterialCraft's command line ({}) did not start: {e}. Install MaterialCraft next to TextureCraft or set MATERIALCRAFT_CLI", cli.display() @@ -301,7 +329,15 @@ impl Session { let bytes = if is_mcraft(bytes) { let ts = self.ts(); let size = self.doc.project.texture_set(ts)?.resolution; - let job = LinkJob { layer: LayerId(0), package: Arc::new(bytes.to_vec()), size, values: Map::new(), inputs: Vec::new() }; + let job = LinkJob { + layer: LayerId(0), + package: Arc::new(bytes.to_vec()), + source_ext: MCRAFT_EXT, + graph: None, + size, + values: Map::new(), + inputs: Vec::new(), + }; cooked = job.run()?; cooked.as_slice() } else { @@ -316,7 +352,7 @@ impl Session { let (id, names) = self.edit(&label, |s| { let ts = s.ts(); let stem = texturecraft_formats::sanitize_resource_name(&m.manifest.name); - let package = unique(s, &format!("{stem}.{MCRAFT_EXT}")); + let package = unique(s, &format!("{stem}.{}", source_extension(&m.manifest.source))); s.doc.resources.insert(package.clone(), Arc::new(m.source.clone())); let link = MaterialLink { name: m.manifest.name.clone(), @@ -345,7 +381,7 @@ impl Session { Ok(id) } - /// The `.mcraft` package behind linked layer `id`. + /// The source package behind linked layer `id`. pub fn link_package(&self, id: LayerId) -> Result>> { let link = link_of(self, "material.link", id)?; self.doc.resources.get(&link.package).cloned().ok_or_else(|| EngineError::Other(format!("the package of {} is missing", link.name))) @@ -376,7 +412,15 @@ impl Session { // UDIM tiles share one cook at the texture set's base resolution let size = self.doc.project.texture_set(texture_set)?.resolution; - Ok(LinkJob { layer: id, package: self.link_package(id)?, size, values, inputs }) + Ok(LinkJob { + layer: id, + package: self.link_package(id)?, + source_ext: source_extension(&link.package), + graph: Some(link.graph.clone()), + size, + values, + inputs, + }) } /// Swap in maps a [`LinkJob`] cooked (`.mcmat` bytes) and the values they were cooked with. @@ -456,3 +500,54 @@ fn put_maps(s: &mut Session, stem: &str, maps: &[(Channel, Vec, texturecraft } names } + +#[cfg(all(test, unix))] +mod subprocess_tests { + use super::*; + use std::os::unix::fs::PermissionsExt; + + #[test] + fn cook_uses_the_saved_graph_and_source_type_without_global_environment() { + let id = NEXT_JOB.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let dir = std::env::temp_dir().join(format!("texturecraft-cook-test-{}-{id}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let cli = dir.join("materialcraft-cli"); + let script = r#"#!/bin/sh +set -eu +case "$1" in + batch) + grep -F '"graph":"Second"' "$2" >/dev/null + grep -F '"id":"file.exportMaterial"' "$2" >/dev/null + test -f "$(dirname "$2")/source.sbs" + test -f "$(dirname "$2")/input0.png" + cp "$(dirname "$0")/result.mcmat" "$(dirname "$2")/out.mcmat" + ;; + run) + test "$(basename "$2")" = "source.mcraft" + cp "$(dirname "$0")/result.mcmat" "$(dirname "$2")/out.mcmat" + ;; + *) exit 1 ;; +esac +"#; + std::fs::write(&cli, script).unwrap(); + std::fs::set_permissions(&cli, std::fs::Permissions::from_mode(0o700)).unwrap(); + std::fs::write(dir.join("result.mcmat"), b"cooked").unwrap(); + let mut job = LinkJob { + layer: LayerId(1), + package: Arc::new(b"source".to_vec()), + source_ext: "sbs", + graph: Some("Second".into()), + size: 64, + values: Map::new(), + inputs: vec![("curvature".into(), b"png".to_vec())], + }; + assert_eq!(job.run_with_cli(&cli).unwrap(), b"cooked"); + job.graph = None; + job.source_ext = "mcraft"; + assert_eq!(job.run_with_cli(&cli).unwrap(), b"cooked"); + job.graph = Some("wrong".into()); + assert!(job.run_with_cli(&cli).is_err()); + assert!(job.run_with_cli(&dir.join("missing")).is_err()); + let _ = std::fs::remove_dir_all(dir); + } +} diff --git a/crates/engine/tests/it/materialcraft_link.rs b/crates/engine/tests/it/materialcraft_link.rs index ac86d775..cbe75532 100644 --- a/crates/engine/tests/it/materialcraft_link.rs +++ b/crates/engine/tests/it/materialcraft_link.rs @@ -21,9 +21,14 @@ fn session() -> Session { /// A `.mcmat` as MaterialCraft writes it: uniform maps, one float input `wear` cooked at `wear`. fn mcmat(roughness: f32, wear: f32) -> Vec { + mcmat_version(roughness, wear, 1) +} + +fn mcmat_version(roughness: f32, wear: f32, version: u32) -> Vec { + let source = if version == 2 { "source.sbs" } else { "source.mcraft" }; let gray = |v: f32| mesh_map_png(4, 1, &[v; 16]).unwrap(); let manifest = json!({ - "format": "craft-material", "version": 1, "name": "Brick wall", "graph": "Brick_wall", "size": [4, 4], + "format": "craft-material", "version": version, "name": "Brick wall", "graph": "Brick_wall", "size": [4, 4], "maps": [ {"usage": "baseColor", "file": "maps/baseColor.png", "colorSpace": "srgb"}, {"usage": "roughness", "file": "maps/roughness.png", "colorSpace": "linear"}, @@ -42,7 +47,7 @@ fn mcmat(roughness: f32, wear: f32) -> Vec { {"identifier": "bentNormal", "kind": "color"} ], "presets": [{"name": "Worn", "values": {"wear": {"t": "float", "v": 1.0}, "note": {"t": "text", "v": "x"}}}], - "source": "source.mcraft" + "source": source }); let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); let o = zip::write::SimpleFileOptions::default(); @@ -51,7 +56,7 @@ fn mcmat(roughness: f32, wear: f32) -> Vec { ("maps/baseColor.png", mesh_map_png(4, 3, &[0.5; 48]).unwrap()), ("maps/roughness.png", gray(roughness)), ("maps/ambientOcclusion.png", gray(1.0)), - ("source.mcraft", b"package bytes".to_vec()), + (source, b"package bytes".to_vec()), ] { z.start_file(name, o).unwrap(); z.write_all(&bytes).unwrap(); @@ -81,6 +86,24 @@ fn map_name(s: &Session, id: LayerId, c: Channel) -> String { } } +#[test] +fn version_two_preserves_the_sbs_source_for_future_cooks() { + let mut session = session(); + let id = session.add_linked_material_bytes_at(&mcmat_version(0.25, 0.5, 2), Place::Selection).unwrap(); + let linked = link(&session, id); + assert!(linked.package.ends_with(".sbs")); + let job = session.link_job(id, &serde_json::Map::new()).unwrap(); + assert_eq!(job.source_ext, "sbs"); + assert_eq!(job.graph.as_deref(), Some("Brick_wall")); + assert_eq!(job.package.as_slice(), b"package bytes"); + let base = std::env::temp_dir().join(format!("texturecraft-sbs-source-{}", std::process::id())); + let saved = execute(&mut session, "material.link.saveSource", &json!({"layer": id.0, "path": base.to_str().unwrap()})).unwrap(); + let path = saved["path"].as_str().unwrap(); + assert!(path.ends_with(".sbs")); + assert_eq!(std::fs::read(path).unwrap(), b"package bytes"); + let _ = std::fs::remove_file(path); +} + #[test] fn imports_as_a_linked_fill_with_its_maps() { let mut s = session(); @@ -199,7 +222,7 @@ fn cooks_through_materialcraft_cli_when_available() { let dir = std::env::temp_dir().join(format!("tc-mc-link-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let pkg = dir.join("bricks.mcraft"); - let ok = std::process::Command::new(&cli).args(["new", "bricks"]).arg(&pkg).args(["--size", "64"]).status().unwrap(); + let ok = std::process::Command::new(&cli).args(["new", "pbr"]).arg(&pkg).args(["--size", "64"]).status().unwrap(); assert!(ok.success()); let exposed = dir.join("exposed.mcraft"); let ok = std::process::Command::new(&cli) @@ -217,9 +240,24 @@ fn cooks_through_materialcraft_cli_when_available() { assert_eq!(link(&s, id).size, 128, "cooked at the texture set's resolution"); assert!(link(&s, id).params.iter().any(|p| p.id == "wear")); execute(&mut s, "material.link.set", &json!({"layer": id.0, "id": "wear", "value": 0.8})).unwrap(); - assert_eq!(link(&s, id).params.iter().find(|p| p.id == "wear").unwrap().value, json!({"t": "float", "v": 0.8})); - let out = dir.join("back.mcraft"); + let wear = link(&s, id).params.iter().find(|p| p.id == "wear").unwrap().value["v"].as_f64().unwrap(); + assert!((wear - 0.8).abs() < 1e-6); + let out = dir.join("back.sbs"); execute(&mut s, "material.link.saveSource", &json!({"layer": id.0, "path": out.to_str().unwrap()})).unwrap(); assert!(std::fs::metadata(&out).unwrap().len() > 0); + + // The portable material can name a graph other than the first in the embedded source. + let multi = dir.join("multi.mcraft"); + let ok = std::process::Command::new(&cli).arg("run").arg(&exposed).arg("graph.duplicate").arg("{}").arg("-o").arg(&multi).status().unwrap(); + assert!(ok.success()); + let portable = dir.join("second.mcmat"); + let params = json!({"graph": 1, "size": 64, "path": portable.to_str().unwrap()}); + let ok = std::process::Command::new(&cli).arg("run").arg(&multi).arg("file.exportMaterial").arg(params.to_string()).status().unwrap(); + assert!(ok.success()); + let second = s.add_linked_material_bytes_at(&std::fs::read(&portable).unwrap(), Place::Selection).unwrap(); + let expected_graph = link(&s, second).graph; + let job = s.link_job(second, &serde_json::Map::new()).unwrap(); + let result = texturecraft_engine::formats::mcmat::read_mcmat(&job.run().unwrap()).unwrap(); + assert_eq!(result.manifest.graph, expected_graph); let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/formats/Cargo.toml b/crates/formats/Cargo.toml index d78d4d7a..756b2a78 100644 --- a/crates/formats/Cargo.toml +++ b/crates/formats/Cargo.toml @@ -11,6 +11,8 @@ workspace = true [dependencies] craft-materialx = { workspace = true } craft-mesh = { workspace = true } +craft-mcmat = { workspace = true } +craft-zip = { workspace = true } texturecraft-core = { workspace = true } texturecraft-texel = { workspace = true } texturecraft-geom = { workspace = true } diff --git a/crates/formats/src/mcmat.rs b/crates/formats/src/mcmat.rs index 1976c73b..5040f320 100644 --- a/crates/formats/src/mcmat.rs +++ b/crates/formats/src/mcmat.rs @@ -1,11 +1,11 @@ //! MaterialCraft portable materials (`.mcmat`, read only): a zip with `material.json` (the //! manifest: maps by usage, exposed parameters, image inputs, presets), `maps/.png` -//! (8-bit sRGB for color usages, 16-bit linear otherwise; OpenGL normals) and the source -//! `.mcraft` package. TextureCraft keeps its own reader (the apps share the format, not code). +//! (8-bit sRGB for color usages, 16-bit linear otherwise; OpenGL normals) and the +//! source package. The shared reader handles the portable container and manifest. //! //! Also the 16-bit PNG encoder that hands baked mesh maps to MaterialCraft as graph inputs. -use std::io::{Cursor, Read}; +use std::io::Cursor; use serde::Deserialize; use texturecraft_core::{LinkParam, LinkPreset}; @@ -16,8 +16,7 @@ use crate::{FormatError, Result}; pub const MCMAT_EXT: &str = "mcmat"; /// MaterialCraft's package extension. pub const MCRAFT_EXT: &str = "mcraft"; -const FORMAT: &str = "craft-material"; -const VERSION: u32 = 1; +const VERSION: u32 = 2; /// Largest entry read (maps are at most 8192² × 16 bits, compressed). const MAX_ENTRY: u64 = 512 << 20; /// Most map entries one manifest may name (a material has at most nine usages). @@ -70,68 +69,76 @@ fn malformed(msg: impl Into) -> FormatError { FormatError::Malformed { kind: KIND, msg: msg.into() } } -fn read_entry(name: &str, entry: impl Read, total_bytes: &mut u64) -> Result> { - // Earlier maps remain live while the next entry is read - let remaining_bytes = MAX_TOTAL.saturating_sub(*total_bytes); - let entry_limit = MAX_ENTRY.min(remaining_bytes); - let mut bytes = Vec::new(); - entry.take(entry_limit + 1).read_to_end(&mut bytes).map_err(|error| malformed(format!("{name}: {error}")))?; - if bytes.len() as u64 > MAX_ENTRY { - return Err(FormatError::TooLarge(name.to_string())); +fn shared_error(error: craft_mcmat::Error) -> FormatError { + match error { + craft_mcmat::Error::Version(version) => FormatError::Unsupported(format!("{KIND} version {version} (this version reads up to {VERSION})")), + craft_mcmat::Error::Limit(message) => malformed(message), + craft_mcmat::Error::Archive(ref archive) if archive.is_limit() => FormatError::TooLarge(error.to_string()), + other => malformed(other.to_string()), } - - let next_total = total_bytes.saturating_add(bytes.len() as u64); - if next_total > MAX_TOTAL { - return Err(FormatError::TooLarge(format!("{KIND} payload (over {MAX_TOTAL} bytes)"))); - } - - // Keep spare read capacity from accumulating across retained map entries - bytes.shrink_to_fit(); - *total_bytes = next_total; - - Ok(bytes) } -/// Read `.mcmat` bytes. +/// Read `.mcmat` bytes within TextureCraft's existing per-entry and total payload limits. pub fn read_mcmat(bytes: &[u8]) -> Result { - let mut archive = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|error| malformed(error.to_string()))?; - let mut total_bytes = 0u64; - let mut read = |name: &str| -> Result> { - let entry = archive.by_name(name).map_err(|_| malformed(format!("{name} is missing")))?; - read_entry(name, entry, &mut total_bytes) + let limits = craft_mcmat::Limits { + min_version: 0, + max_version: VERSION, + max_entries: 100_000, + max_maps: MAX_MAPS, + max_manifest_bytes: MAX_ENTRY, + max_entry_bytes: MAX_ENTRY, + max_total_bytes: MAX_TOTAL, }; - - let manifest: Manifest = serde_json::from_slice(&read("material.json")?).map_err(|error| malformed(format!("material.json: {error}")))?; - if manifest.format != FORMAT { - return Err(malformed(format!("format `{}`", manifest.format))); - } - - if manifest.version > VERSION { - return Err(FormatError::Unsupported(format!("{KIND} version {} (this version reads up to {VERSION})", manifest.version))); - } - - if manifest.maps.len() > MAX_MAPS { - return Err(malformed(format!("{} maps, over the {MAX_MAPS} map limit", manifest.maps.len()))); - } - - let maps = manifest.maps.iter().map(|map| Ok((map.usage.clone(), read(&map.file)?))).collect::>>()?; - let source = read(&manifest.source)?; - + let mut archive = craft_mcmat::Material::open(bytes, limits).map_err(shared_error)?; + // Consume the JSON values rather than cloning the entire untrusted manifest into a second tree. + let parameters = std::mem::take(&mut archive.manifest.parameters) + .into_iter() + .map(serde_json::from_value) + .collect::>() + .map_err(|e| malformed(format!("material.json: {e}")))?; + let inputs = std::mem::take(&mut archive.manifest.inputs) + .into_iter() + .map(serde_json::from_value) + .collect::>() + .map_err(|e| malformed(format!("material.json: {e}")))?; + let presets = std::mem::take(&mut archive.manifest.presets) + .into_iter() + .map(serde_json::from_value) + .collect::>() + .map_err(|e| malformed(format!("material.json: {e}")))?; + let shared = &archive.manifest; + let manifest = Manifest { + format: shared.format.clone(), + version: shared.version, + name: shared.name.clone(), + graph: shared.graph.clone(), + size: shared.size, + maps: shared.maps.iter().map(|m| MapEntry { usage: m.usage.clone(), file: m.file.clone() }).collect(), + parameters, + inputs, + presets, + source: shared.source.clone(), + }; + let maps = manifest.maps.iter().map(|map| Ok((map.usage.clone(), archive.read(&map.file).map_err(shared_error)?))).collect::>>()?; + let source = archive.read_source().map_err(shared_error)?; Ok(PortableMaterial { manifest, maps, source }) } -/// Whether `bytes` are a zip holding `entry` (`material.json`: a portable material, -/// `package.json`: a MaterialCraft package). -fn zip_has(bytes: &[u8], entry: &str) -> bool { - zip::ZipArchive::new(Cursor::new(bytes)).is_ok_and(|mut z| z.by_name(entry).is_ok()) -} - +/// Whether `bytes` contain a portable material manifest within the ZIP entry limit. pub fn is_mcmat(bytes: &[u8]) -> bool { - zip_has(bytes, "material.json") + craft_mcmat::is_material(bytes, 100_000) } pub fn is_mcraft(bytes: &[u8]) -> bool { - zip_has(bytes, "package.json") + let Ok(mut archive) = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: 100_000, max_total_bytes: 0 }) else { return false }; + // A portable material may itself contain source.sbs, so it is not a source archive. + if archive.has_duplicate_names() || archive.contains("material.json") { + return false; + } + if archive.contains("package.json") { + return true; + } + (0..archive.len()).any(|i| archive.entry_name(i).ok().flatten().is_some_and(|name| !name.contains('/') && name.to_ascii_lowercase().ends_with(".sbs"))) } /// A baked mesh map (`components` floats per texel, 0..1, rows top first) as a 16-bit PNG @@ -161,13 +168,14 @@ mod tests { /// A `.mcmat` as MaterialCraft writes it (see its `formats/src/material.rs`). pub(crate) fn sample(version: u32) -> Vec { + let source = if version >= 2 { "source.sbs" } else { "source.mcraft" }; let manifest = serde_json::json!({ "format": "craft-material", "version": version, "name": "Brick wall", "graph": "Brick_wall", "size": [4, 4], "maps": [{"usage": "baseColor", "file": "maps/baseColor.png", "colorSpace": "srgb"}], "parameters": [{"id": "wear", "label": "Wear", "default": {"t": "float", "v": 0.5}, "min": 0.0, "max": 1.0, "value": {"t": "float", "v": 0.25}}], "inputs": [{"identifier": "curvature", "kind": "gray"}], "presets": [{"name": "Worn", "values": {"wear": {"t": "float", "v": 1.0}}}], - "source": "source.mcraft", "futureField": true + "source": source, "futureField": true }); let mut z = zip::ZipWriter::new(Cursor::new(Vec::new())); let o = zip::write::SimpleFileOptions::default(); @@ -175,7 +183,7 @@ mod tests { z.write_all(manifest.to_string().as_bytes()).unwrap(); z.start_file("maps/baseColor.png", o).unwrap(); z.write_all(&mesh_map_png(4, 3, &[0.5; 48]).unwrap()).unwrap(); - z.start_file("source.mcraft", o).unwrap(); + z.start_file(source, o).unwrap(); z.write_all(b"pkg").unwrap(); z.finish().unwrap().into_inner() } @@ -195,15 +203,29 @@ mod tests { assert_eq!(material.maps[0].0, "baseColor"); assert_eq!(crate::decode_image(&material.maps[0].1).unwrap().width, 4); assert_eq!(material.source, b"pkg"); - assert!(matches!(read_mcmat(&sample(2)), Err(FormatError::Unsupported(_)))); + let current = read_mcmat(&sample(2)).unwrap(); + assert_eq!(current.manifest.source, "source.sbs"); + assert_eq!(current.source, b"pkg"); + assert!(matches!(read_mcmat(&sample(3)), Err(FormatError::Unsupported(_)))); assert!(read_mcmat(b"junk").is_err()); } + #[test] + fn recognizes_both_generations_of_source_archive_without_confusing_portable_materials() { + for entry in ["package.json", "package.sbs"] { + let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new())); + zip.start_file(entry, zip::write::SimpleFileOptions::default()).unwrap(); + zip.write_all(b"source").unwrap(); + let bytes = zip.finish().unwrap().into_inner(); + assert!(is_mcraft(&bytes), "{entry}"); + } + assert!(!is_mcraft(&sample(2)), "a portable material contains source.sbs too"); + } + #[test] fn an_unbounded_map_list_is_refused() { let maps: Vec<_> = (0..=MAX_MAPS).map(|_| serde_json::json!({"usage": "baseColor", "file": "maps/baseColor.png"})).collect(); - let manifest = - serde_json::json!({"format": FORMAT, "version": VERSION, "name": "n", "graph": "g", "size": [4, 4], "maps": maps, "source": "source.mcraft"}); + let manifest = serde_json::json!({"format": craft_mcmat::FORMAT, "version": VERSION, "name": "n", "graph": "g", "size": [4, 4], "maps": maps, "source": "source.mcraft"}); let mut archive = zip::ZipWriter::new(Cursor::new(Vec::new())); let options = zip::write::SimpleFileOptions::default(); archive.start_file("material.json", options).unwrap(); @@ -218,31 +240,6 @@ mod tests { assert!(matches!(result, Err(FormatError::Malformed { .. }))); } - #[test] - fn entry_read_stops_at_the_remaining_payload_budget() { - let payload = vec![7u8; 1024]; - let mut input = Cursor::new(&payload); - let mut total_bytes = MAX_TOTAL - 8; - - let result = read_entry("maps/extra.png", &mut input, &mut total_bytes); - - assert!(matches!(result, Err(FormatError::TooLarge(_)))); - assert_eq!(input.position(), 9, "only one byte beyond the remaining budget is needed to reject the entry"); - assert_eq!(total_bytes, MAX_TOTAL - 8, "a rejected entry does not consume the payload budget"); - } - - #[test] - fn payload_budget_accepts_the_exact_limit_and_empty_entries() { - let mut total_bytes = MAX_TOTAL - 4; - - let bytes = read_entry("map.png", Cursor::new([1, 2, 3, 4]), &mut total_bytes).unwrap(); - - assert_eq!(bytes, [1, 2, 3, 4]); - assert_eq!(total_bytes, MAX_TOTAL); - assert!(read_entry("empty.mcraft", Cursor::new([]), &mut total_bytes).unwrap().is_empty()); - assert!(matches!(read_entry("extra.png", Cursor::new([1]), &mut total_bytes), Err(FormatError::TooLarge(_)))); - } - #[test] fn mesh_maps_encode_as_16_bit() { let png = mesh_map_png(2, 1, &[0.0, 0.25, 0.5, 1.0]).unwrap(); diff --git a/crates/ui-egui/src/dock.rs b/crates/ui-egui/src/dock.rs index 1d266ee1..fcce9071 100644 --- a/crates/ui-egui/src/dock.rs +++ b/crates/ui-egui/src/dock.rs @@ -1828,7 +1828,8 @@ fn link_properties(app: &mut TextureCraftApp, ui: &mut Ui, layer: LayerId, link: app.cook_link(layer); } if ui.button(tr("Open in MaterialCraft")).clicked() { - let path = std::env::temp_dir().join(format!("{}.mcraft", texturecraft_engine::formats::export::sanitize_file_stem(&link.name))); + let ext = texturecraft_engine::link_cmds::source_extension(&link.package); + let path = std::env::temp_dir().join(format!("{}.{ext}", texturecraft_engine::formats::export::sanitize_file_stem(&link.name))); let opened = app .session .link_package(layer) diff --git a/crates/ui-egui/src/preview_worker.rs b/crates/ui-egui/src/preview_worker.rs index c979e3e4..e58901b4 100644 --- a/crates/ui-egui/src/preview_worker.rs +++ b/crates/ui-egui/src/preview_worker.rs @@ -206,7 +206,13 @@ mod tests { let mut answers = HashMap::new(); let start = Instant::now(); - while answers.len() < requests_sent && start.elapsed() < Duration::from_secs(120) { + // Instrumented orb baking can take several minutes on coverage runners. + let timeout = if std::env::var_os("LLVM_PROFILE_FILE").is_some() || std::env::var("RUSTFLAGS").is_ok_and(|f| f.contains("instrument-coverage")) { + Duration::from_secs(600) + } else { + Duration::from_secs(120) + }; + while answers.len() < requests_sent && start.elapsed() < timeout { answers.extend(worker.poll()); std::thread::sleep(Duration::from_millis(10)); } diff --git a/docs/architecture.md b/docs/architecture.md index 2ddcd76b..8d5deb43 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -121,10 +121,10 @@ on the CPU by `render::displace` from the composited height. A fill can carry a `MaterialLink` (`core::layer`): a MaterialCraft graph whose cooked maps are the fill's image sources. `engine::link_cmds` imports `.mcmat` files (`formats::mcmat`), keeps the -`.mcraft` package as a resource, and re-cooks by running `materialcraft-cli` with the link's +`.mcraft` or `.sbs` source as a resource, and re-cooks its named graph by running `materialcraft-cli` with the link's values, the texture set's resolution and its baked mesh maps (`LinkJob::run`, on a worker thread in the UI). Every cook stores its maps under fresh resource names, so undo, redo and the decoded -image cache never disagree. The apps share the file format and the command line, never code. +image cache never disagree. The apps share the `craft-mcmat` format library and the command line; neither depends on the other app. ## Commands and automation diff --git a/docs/control-protocol.md b/docs/control-protocol.md index f255a509..0ead3445 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -325,8 +325,9 @@ of a second. A `.mcmat` portable material (MaterialCraft's format, read here by `formats::mcmat`; the manifest is documented in MaterialCraft's `docs/control-protocol.md` § Portable materials) becomes a fill layer whose channel sources are its cooked maps and whose `link` (shown by `project.inspect`) holds the -graph's exposed inputs, presets and image inputs. The apps share the format and the command line, -not code. +graph's exposed inputs, presets and image inputs. Both apps use the shared `craft-mcmat` container. +Versions 1 and 2 are accepted; version 1 contains a legacy JSON `.mcraft` source that current +MaterialCraft cannot reopen, so its cooked maps remain usable but re-cooking needs a compatible older version. | Command | Params | |---|---| @@ -334,9 +335,9 @@ not code. | `material.link.set` | `{layer?, id, value, cook?: true}`: a number, `[..]`, a bool, `"#rrggbb"` or a typed `{"t", "v"}`; re-cooks unless `cook: false` | | `material.link.preset` | `{layer?, name, cook?: true}` | | `material.link.cook` | `{layer?}`: re-cook at the texture set's resolution with its baked mesh maps (after a bake or a resize) | -| `material.link.saveSource` | `{layer?, path}`: the `.mcraft` package, to open in MaterialCraft | +| `material.link.saveSource` | `{layer?, path}`: the `.mcraft` or `.sbs` source, to open in MaterialCraft | -Re-cooks run `materialcraft-cli run file.exportMaterial {path, size, values, inputs}`: +Re-cooks use `materialcraft-cli batch` to open the saved graph by name and run `file.exportMaterial` with its size, values and inputs: `$MATERIALCRAFT_CLI`, else `materialcraft-cli` next to the TextureCraft executable, else on `PATH` (the app is `$MATERIALCRAFT` / `materialcraft`). Baked maps feed graph input nodes whose identifier matches the map's shared id (`ambientOcclusion`, `curvature`, `thickness`, `position`,