diff --git a/crates/engine/src/meshmap_cmds.rs b/crates/engine/src/meshmap_cmds.rs index ac9003b7..4524b361 100644 --- a/crates/engine/src/meshmap_cmds.rs +++ b/crates/engine/src/meshmap_cmds.rs @@ -60,21 +60,7 @@ pub(crate) fn use_images(s: &mut Session, cmd: &str, ts: usize, map: BakedMap, i return Err(bad(cmd, format!("no image for UV tile {tile}"))); } - // Every mesh map of a set shares one size: the other maps', or a bake's (a bake-size block per atlas tile) - let (columns, rows) = grid.map_or((1, 1), |grid| (grid.cols, grid.rows)); - let (width, height) = match s.doc.baked.get(&ts).filter(|baked| !baked.maps.is_empty()) { - Some(baked) => (baked.resolution, baked.maps.keys().next().map_or(baked.resolution, |other| baked.height(*other))), - None => { - let (width, height) = texture_set.bake.common.output_size(); - (width.saturating_mul(columns), height.saturating_mul(rows)) - } - }; - let empty = width == 0 || height == 0; - let too_many_texels = u64::from(width) * u64::from(height) > texturecraft_bake::MAX_TEXELS; - let too_wide = grid.is_none() && width > MAX_RESOLUTION; - if empty || too_many_texels || too_wide { - return Err(bad(cmd, format!("mesh map size {width} × {height} is too large or empty"))); - } + let (width, height) = image_map_size(s, cmd, ts)?; let flip_green = map == BakedMap::Normal && s.doc.project.normal_format == NormalFormat::DirectX; let data = match (grid, images.values().next()) { @@ -94,6 +80,30 @@ pub(crate) fn use_images(s: &mut Session, cmd: &str, ts: usize, map: BakedMap, i Ok(width) } +/// Dimensions shared by imported mesh maps, checked before allocating the resampled pixels +pub(crate) fn image_map_size(s: &Session, cmd: &str, ts: usize) -> Result<(u32, u32)> { + let texture_set = s.doc.project.texture_set(ts)?; + let grid = texture_set.tile_grid(); + + // Every mesh map of a set shares one size: the other maps', or a bake's (a bake-size block per atlas tile) + let (columns, rows) = grid.map_or((1, 1), |grid| (grid.cols, grid.rows)); + let (width, height) = match s.doc.baked.get(&ts).filter(|baked| !baked.maps.is_empty()) { + Some(baked) => (baked.resolution, baked.maps.keys().next().map_or(baked.resolution, |other| baked.height(*other))), + None => { + let (width, height) = texture_set.bake.common.output_size(); + (width.saturating_mul(columns), height.saturating_mul(rows)) + } + }; + let empty = width == 0 || height == 0; + let too_many_texels = u64::from(width) * u64::from(height) > texturecraft_bake::MAX_TEXELS; + let too_wide = grid.is_none() && width > MAX_RESOLUTION; + if empty || too_many_texels || too_wide { + return Err(bad(cmd, format!("mesh map size {width} × {height} is too large or empty"))); + } + + Ok((width, height)) +} + /// The image of every UV tile of a set: `name` with its tile number (`ao.1001.png`) swapped for each tile's fn tile_images(images: &ImageStore, cmd: &str, name: &str, tiles: &[u32]) -> Result>> { let tile_at = diff --git a/crates/engine/src/meshmap_import_cmds.rs b/crates/engine/src/meshmap_import_cmds.rs index f8ea8244..20913de2 100644 --- a/crates/engine/src/meshmap_import_cmds.rs +++ b/crates/engine/src/meshmap_import_cmds.rs @@ -7,7 +7,7 @@ use serde_json::{Value, json}; use texturecraft_core::BakedMap; use crate::commands::{CommandSpec, always, bad, strp}; -use crate::meshmap_cmds::{FIRST_TILE, tile_number, use_images}; +use crate::meshmap_cmds::{FIRST_TILE, image_map_size, tile_number, use_images}; use crate::meshmap_file_name::{baked_map_from_file_name, texture_set_from_file_name}; use crate::resource_cmds::read_image_file; use crate::{Result, Session, cmd}; @@ -17,7 +17,7 @@ const CMD: &str = "textureSet.meshMap.import"; /// Most files one import takes: every mesh map of dozens of sets, each split over a few UV tiles const MAX_FILES: usize = 512; -/// Most bytes one import reads, since every file stays decoded in memory until the import ends +/// Bound retained file bytes, decoded RGBA floats and resampled maps together, before allocating pixels const MAX_TOTAL_BYTES: u64 = 2 << 30; pub fn specs() -> Vec { @@ -26,13 +26,17 @@ pub fn specs() -> Vec { "Import baked maps", [], None, - "{files: [{path, map?: mesh map id}]} → {assigned: [{path, ts, map}], unmatched: [path]} — each image file becomes the mesh map its name names (`Body_ao.png`, `Body_world_normal.png`; or `map`) of the texture set its name starts with (any name when there is one set), read as by textureSet.meshMap.set. A UV-tiled set takes one file per tile, named with the tile number (`Body_ao.1001.png`, `Body_ao.1002.png`). Left out and listed in `unmatched`: files matching no set or map, a second file for one slot or tile, files of a UV-tiled set without one of its tile numbers, and every file of a map missing a tile. One undo step (none when nothing matches); desktop only", + "{files: [{path, map?: mesh map id}]} → {assigned: [{path, ts, map}], unmatched: [path]} — each image file becomes the mesh map its name names (`Body_ao.png`, `Body_world_normal.png`; or `map`) of the texture set its name starts with (any name when there is one set), read as by textureSet.meshMap.set. A UV-tiled set takes one file per tile, named with the tile number (`Body_ao.1001.png`, `Body_ao.1002.png`). Left out and listed in `unmatched`: files matching no set or map, a second file for one slot or tile, files of a UV-tiled set without one of its tile numbers, and every file of a map missing a tile. One undo step (none when nothing matches); at most 512 files and 2 GiB of file bytes, decoded images and mesh maps; desktop only", always, import )] } fn import(s: &mut Session, p: &Value) -> Result { + import_with_budget(s, p, MAX_TOTAL_BYTES) +} + +fn import_with_budget(s: &mut Session, p: &Value, max_bytes: u64) -> Result { let files = p.get("files").and_then(Value::as_array).ok_or_else(|| bad(CMD, "`files` (a list of {path, map?}) is required"))?; if files.len() > MAX_FILES { return Err(bad(CMD, format!("at most {MAX_FILES} files at once"))); @@ -43,7 +47,12 @@ fn import(s: &mut Session, p: &Value) -> Result { let mut unmatched = Vec::new(); for file in files { let path = strp(file, "path").ok_or_else(|| bad(CMD, "every file needs a `path`"))?; - let given_map = strp(file, "map").map(|id| BakedMap::parse(id).ok_or_else(|| bad(CMD, format!("unknown mesh map `{id}`")))).transpose()?; + let given_map = match file.get("map") { + None | Some(Value::Null) => None, + Some(Value::String(id)) => Some(BakedMap::parse(id).ok_or_else(|| bad(CMD, format!("unknown mesh map `{id}`")))?), + Some(_) => return Err(bad(CMD, "`map` must be a mesh map id string")), + }; + match slot_of(s, &set_names, path, given_map) { Some((slot, tile)) if !slots.get(&slot).is_some_and(|tiles| tiles.contains_key(&tile)) => { slots.entry(slot).or_default().insert(tile, path); @@ -67,20 +76,22 @@ fn import(s: &mut Session, p: &Value) -> Result { let result = s.edit("Import baked maps", |s| { let mut assigned = Vec::new(); - let mut total_bytes = 0; + let mut remaining_bytes = max_bytes; for ((ts, map), tiles) in &slots { + let (width, height) = image_map_size(s, CMD, *ts)?; + let map_bytes = u64::from(width) * u64::from(height) * map.components() as u64 * size_of::() as u64; + reserve_bytes(&mut remaining_bytes, map_bytes)?; + let mut images = BTreeMap::new(); for (tile, path) in tiles { - let bytes = read_image_file(CMD, path)?; - total_bytes += bytes.len() as u64; - if total_bytes > MAX_TOTAL_BYTES { - return Err(bad(CMD, format!("the files are larger than {} GB together", MAX_TOTAL_BYTES >> 30))); - } + let bytes = read_image_file(CMD, path, remaining_bytes)?; + reserve_bytes(&mut remaining_bytes, bytes.len() as u64)?; - let name = s.add_image_resource(path, bytes)?; - if let Some(image) = s.images.get(&name) { - images.insert(*tile, image.clone()); - } + let bytes_per_texel = size_of::<[f32; 4]>() as u64; + let name = s.add_image_resource(path, bytes, remaining_bytes / bytes_per_texel)?; + let image = s.images.get(&name).ok_or_else(|| bad(CMD, format!("no decoded image `{name}`")))?; + reserve_bytes(&mut remaining_bytes, u64::from(image.width) * u64::from(image.height) * bytes_per_texel)?; + images.insert(*tile, image.clone()); assigned.push(json!({"path": path, "ts": ts, "map": map.id()})); } use_images(s, CMD, *ts, *map, &images)?; @@ -95,6 +106,11 @@ fn import(s: &mut Session, p: &Value) -> Result { Ok(json!({ "assigned": result?, "unmatched": unmatched })) } +fn reserve_bytes(remaining_bytes: &mut u64, bytes: u64) -> Result<()> { + *remaining_bytes = remaining_bytes.checked_sub(bytes).ok_or_else(|| bad(CMD, "the files, decoded images and mesh maps exceed the import memory budget"))?; + Ok(()) +} + /// The texture set and mesh map a file is for, and its UV tile ([`FIRST_TILE`] on a set without UV tiles) fn slot_of(s: &Session, set_names: &[&str], path: &str, given_map: Option) -> Option<((usize, BakedMap), u32)> { let ts = texture_set_from_file_name(path, set_names)?; @@ -106,3 +122,41 @@ fn slot_of(s: &Session, set_names: &[&str], path: &str, given_map: Option Arc { impl Session { /// Add an image resource from bytes (one undo step); returns its unique, sanitized name pub fn import_resource_bytes(&mut self, file_name: &str, bytes: Vec) -> Result { - self.edit("Import resource", |s| s.add_image_resource(file_name, bytes)) + self.edit("Import resource", |s| s.add_image_resource(file_name, bytes, u64::MAX)) } - /// [`Session::import_resource_bytes`] without its own undo step, for imports inside a larger edit - pub(crate) fn add_image_resource(&mut self, file_name: &str, bytes: Vec) -> Result { + /// Add an image within a decoded texel budget, inside the caller's edit + pub(crate) fn add_image_resource(&mut self, file_name: &str, bytes: Vec, max_texels: u64) -> Result { if bytes.len() > MAX_BYTES { return Err(EngineError::Other(format!("{file_name} is larger than {} MB", MAX_BYTES >> 20))); } - let image = texturecraft_formats::decode_image(&bytes).map_err(|e| EngineError::Other(format!("{file_name}: {e}")))?; + let image = texturecraft_formats::decode_image_within(&bytes, max_texels).map_err(|e| EngineError::Other(format!("{file_name}: {e}")))?; let leaf = std::path::Path::new(file_name).file_name().and_then(|n| n.to_str()).unwrap_or("image"); let base = texturecraft_formats::sanitize_resource_name(leaf); let mut name = base.clone(); @@ -101,24 +101,41 @@ fn list(s: &Session) -> Value { #[cfg(not(target_arch = "wasm32"))] fn import(s: &mut Session, p: &Value) -> Result { let path = strp(p, "path").ok_or_else(|| bad("resource.import", "`path` is required"))?; - let bytes = read_image_file("resource.import", path)?; + let bytes = read_image_file("resource.import", path, MAX_BYTES as u64)?; let name = s.import_resource_bytes(path, bytes)?; Ok(json!({ "name": name })) } -/// The bytes of the image file at `path`, refused above [`MAX_BYTES`] +/// Read a regular image file within both the per-file cap and the caller's remaining byte budget #[cfg(not(target_arch = "wasm32"))] -pub(crate) fn read_image_file(cmd: &str, path: &str) -> Result> { - let meta = std::fs::metadata(path).map_err(|e| bad(cmd, format!("cannot read {path}: {e}")))?; - if meta.len() > MAX_BYTES as u64 { - return Err(bad(cmd, format!("{path} is too large"))); +pub(crate) fn read_image_file(cmd: &str, path: &str, max_bytes: u64) -> Result> { + use std::io::Read; + + let max_bytes = max_bytes.min(MAX_BYTES as u64); + let read_error = |error| bad(cmd, format!("cannot read {path}: {error}")); + let too_large = || bad(cmd, format!("{path} is too large (limit {max_bytes} bytes)")); + let metadata = std::fs::metadata(path).map_err(read_error)?; + // Pipes can block on open, and devices may never reach EOF + if !metadata.is_file() { + return Err(bad(cmd, format!("{path} is not a regular file"))); + } + if metadata.len() > max_bytes { + return Err(too_large()); } - std::fs::read(path).map_err(|e| bad(cmd, format!("cannot read {path}: {e}"))) + // Bound the read even if the file grows after the metadata check + let file = std::fs::File::open(path).map_err(read_error)?; + let mut bytes = Vec::with_capacity(metadata.len() as usize); + file.take(max_bytes + 1).read_to_end(&mut bytes).map_err(read_error)?; + if bytes.len() as u64 > max_bytes { + return Err(too_large()); + } + + Ok(bytes) } #[cfg(target_arch = "wasm32")] -pub(crate) fn read_image_file(cmd: &str, _: &str) -> Result> { +pub(crate) fn read_image_file(cmd: &str, _: &str, _: u64) -> Result> { Err(bad(cmd, "file paths are not available in the browser")) } @@ -180,3 +197,24 @@ fn add_image_fill(s: &mut Session, p: &Value) -> Result { })?; Ok(json!({ "layer": id.0 })) } + +#[cfg(all(test, not(target_arch = "wasm32")))] +mod tests { + use super::*; + + #[test] + fn file_reads_obey_the_remaining_byte_budget() { + let path = std::env::temp_dir().join(format!("texturecraft-read-image-{}.png", std::process::id())); + std::fs::write(&path, b"1234").unwrap(); + let name = path.to_str().unwrap(); + assert!(read_image_file("test", name, 3).unwrap_err().to_string().contains("too large")); + assert_eq!(read_image_file("test", name, 4).unwrap(), b"1234"); + std::fs::remove_file(path).unwrap(); + } + + #[cfg(unix)] + #[test] + fn devices_are_rejected_before_reading() { + assert!(read_image_file("test", "/dev/null", 16).unwrap_err().to_string().contains("not a regular file")); + } +} diff --git a/crates/engine/tests/meshmap_import.rs b/crates/engine/tests/meshmap_import.rs index d9ed53f1..33dc2ec8 100644 --- a/crates/engine/tests/meshmap_import.rs +++ b/crates/engine/tests/meshmap_import.rs @@ -170,3 +170,129 @@ fn nothing_matched_makes_no_undo_step() { assert!(!s.modified); std::fs::remove_dir_all(dir).ok(); } + +#[test] +fn an_invalid_map_override_does_not_fall_back_to_the_file_name() { + let dir = bake_folder("invalid-map", &["Ball_ao.png"]); + let mut session = session(); + for map in [json!(true), json!(42), json!([]), json!({}), json!("unknown")] { + let params = json!({"files": [{"path": dir.join("Ball_ao.png"), "map": map}]}); + let result = execute(&mut session, "textureSet.meshMap.import", ¶ms); + assert!(result.is_err(), "invalid map {map} must be rejected: {result:?}"); + assert!(session.doc.resources.is_empty()); + assert!(!session.modified); + } + std::fs::remove_dir_all(dir).unwrap(); +} + +#[test] +#[ignore = "release benchmark; run with --ignored --nocapture --test-threads=1"] +fn benchmark_baked_map_import() { + use std::time::Instant; + use texturecraft_engine::core::BakeResolution; + + let dir = std::env::temp_dir().join(format!("texturecraft-import-benchmark-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + for (size, tiled) in [(1024, false), (2048, false), (1024, true)] { + let mut files = Vec::new(); + let mut encoded_bytes = 0; + for map in ["normal", "ao", "curvature"] { + for tile in 1001..=if tiled { 1002 } else { 1001 } { + let path = dir.join(format!("mesh_{map}.{tile}.png")); + image::RgbaImage::from_fn(size, size, |x, y| { + let value = (x.wrapping_mul(73) ^ y.wrapping_mul(151) ^ tile) as u8; + image::Rgba([value, value.wrapping_add(37), 255, 255]) + }) + .save(&path) + .unwrap(); + encoded_bytes += std::fs::metadata(&path).unwrap().len(); + files.push(json!({"path": path})); + } + } + let params = json!({"files": files}); + let mut milliseconds = Vec::new(); + for iteration in 0..6 { + let mesh = if tiled { primitives::udim_plane() } else { primitives::cube(2.0) }; + let mut session = Session::new_project("benchmark", mesh, "builtin:cube", None, 128).unwrap(); + session.doc.project.texture_sets[0].bake.common.resolution = BakeResolution::from_pixels(size).unwrap(); + let started = Instant::now(); + let result = execute(&mut session, "textureSet.meshMap.import", ¶ms).unwrap(); + let elapsed = started.elapsed().as_secs_f64() * 1000.0; + assert_eq!(result["assigned"].as_array().unwrap().len(), files.len()); + assert_eq!(session.doc.baked[&0].maps.len(), 3); + if iteration > 0 { + milliseconds.push(elapsed); + } + } + milliseconds.sort_by(f64::total_cmp); + eprintln!( + "import size={size} tiles={} files={} encoded_bytes={encoded_bytes} median_ms={:.3} min_ms={:.3} max_ms={:.3}", + if tiled { 2 } else { 1 }, + files.len(), + milliseconds[2], + milliseconds[0], + milliseconds[4] + ); + } + std::fs::remove_dir_all(dir).unwrap(); +} + +#[test] +fn tiled_import_uses_new_images_when_resource_names_collide() { + let files = ["Plane_ao.1001.png", "Plane_ao.1002.png"]; + let dir = bake_folder("collisions", &files); + let mut session = tiled_session(); + let old_bytes = std::fs::read(dir.join(files[0])).unwrap(); + for name in files { + session.import_resource_bytes(name, old_bytes.clone()).unwrap(); + } + let revision_before = session.revision; + + import(&mut session, &dir, &files.map(|file| (file, None))); + + let baked = &session.doc.baked[&0]; + let ao = &baked.maps[&BakedMap::AmbientOcclusion]; + let width = baked.resolution as usize; + let row = ao.len() / width / 2 * width; + assert!((ao[row + width / 4] - 20.0 / 255.0).abs() < 1e-5); + assert!((ao[row + 3 * width / 4] - 40.0 / 255.0).abs() < 1e-5); + assert_eq!(session.revision, revision_before + 1); + assert_eq!(session.doc.resources.len(), 4); + + session.undo().unwrap(); + assert_eq!(session.doc.resources.len(), 2); + assert_eq!(session.images.len(), 2); + session.redo().unwrap(); + assert_eq!(session.images.len(), 4); + assert!(has_map(&session, 0, BakedMap::AmbientOcclusion)); + std::fs::remove_dir_all(dir).unwrap(); +} + +#[test] +fn too_many_files_are_rejected_before_reading_any() { + let mut session = session(); + let files = vec![json!({"path": "Ball_ao.png"}); 513]; + let error = execute(&mut session, "textureSet.meshMap.import", &json!({"files": files})).unwrap_err().to_string(); + assert!(error.contains("at most 512 files"), "{error}"); + assert!(session.doc.resources.is_empty()); + assert!(!session.modified); +} + +#[test] +fn tga_maps_survive_undo_redo_and_project_reload() { + let dir = bake_folder("tga", &["Ball_normal.tga", "Base_ao.png"]); + let mut session = session(); + let result = import(&mut session, &dir, &[("Ball_normal.tga", None), ("Base_ao.png", None)]); + assert_eq!(result["assigned"].as_array().unwrap().len(), 2); + assert!(has_map(&session, 0, BakedMap::Normal)); + let image = session.images["Ball_normal.tga"].clone(); + + session.undo().unwrap(); + assert!(session.images.is_empty()); + session.redo().unwrap(); + assert_eq!(session.images["Ball_normal.tga"].rgba, image.rgba); + let reloaded = Session::from_project_bytes(&session.to_project_bytes().unwrap()).unwrap(); + assert_eq!(reloaded.images["Ball_normal.tga"].rgba, image.rgba); + assert_eq!(reloaded.doc.baked, session.doc.baked); + std::fs::remove_dir_all(dir).unwrap(); +} diff --git a/crates/formats/src/image_res.rs b/crates/formats/src/image_res.rs index 49e1e014..85590d9d 100644 --- a/crates/formats/src/image_res.rs +++ b/crates/formats/src/image_res.rs @@ -21,20 +21,27 @@ pub fn decode_image(bytes: &[u8]) -> Result { /// [`decode_image`] for an image of at most `max_texels` (width × height), checked from the /// header, before any pixel is decoded or allocated. pub fn decode_image_within(bytes: &[u8], max_texels: u64) -> Result { - let reader = + let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() })?; - let (w, h) = reader.into_dimensions().map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() })?; - if w == 0 || h == 0 || w > MAX_IMAGE_SIZE || h > MAX_IMAGE_SIZE { - return Err(FormatError::TooLarge(format!("image is {w}x{h} (limit {MAX_IMAGE_SIZE})"))); + + // TGA has no magic signature; its decoder validates the header when no format was recognized + let format = reader.format().unwrap_or(image::ImageFormat::Tga); + reader.set_format(format); + + let (width, height) = reader.into_dimensions().map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() })?; + if width == 0 || height == 0 || width > MAX_IMAGE_SIZE || height > MAX_IMAGE_SIZE { + return Err(FormatError::TooLarge(format!("image is {width}x{height} (limit {MAX_IMAGE_SIZE})"))); } - if u64::from(w) * u64::from(h) > max_texels { - return Err(FormatError::TooLarge(format!("image is {w}x{h}, more than {max_texels} texels"))); + if u64::from(width) * u64::from(height) > max_texels { + return Err(FormatError::TooLarge(format!("image is {width}x{height}, more than {max_texels} texels"))); } - let img = crate::guard("image", || image::load_from_memory(bytes).map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() }))?; - let srgb = !matches!(img, image::DynamicImage::ImageRgb32F(_) | image::DynamicImage::ImageRgba32F(_)); - let f = img.into_rgba32f(); - let (width, height) = f.dimensions(); - let rgba = f.pixels().map(|p| p.0).collect(); + + let image = + crate::guard("image", || image::load_from_memory_with_format(bytes, format).map_err(|e| FormatError::Malformed { kind: "image", msg: e.to_string() }))?; + let srgb = !matches!(image, image::DynamicImage::ImageRgb32F(_) | image::DynamicImage::ImageRgba32F(_)); + let float_image = image.into_rgba32f(); + let (width, height) = float_image.dimensions(); + let rgba = float_image.pixels().map(|pixel| pixel.0).collect(); Ok(DecodedImage { width, height, rgba, srgb }) } @@ -69,6 +76,19 @@ mod tests { assert!(coverage_png(2, 2, &[0], [255; 3]).is_err()); } + #[test] + fn tga_without_a_magic_signature_decodes_within_the_texel_budget() { + let mut bytes = Vec::new(); + image::RgbaImage::from_pixel(4, 4, image::Rgba([128, 51, 255, 255])).write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Tga).unwrap(); + let image = decode_image_within(&bytes, 16).unwrap(); + assert_eq!((image.width, image.height), (4, 4)); + assert!((image.rgba[0][1] - 0.2).abs() < 1e-6); + assert!(image.srgb); + let header = &bytes[..18]; + assert!(matches!(decode_image_within(header, 15), Err(FormatError::TooLarge(_)))); + assert!(decode_image(header).is_err()); + } + #[test] fn decoding_within_a_texel_limit_refuses_big_images_from_the_header() { let png = { diff --git a/crates/ui-egui/src/new_project_advanced.rs b/crates/ui-egui/src/new_project_advanced.rs index ae1993d1..4d2915ef 100644 --- a/crates/ui-egui/src/new_project_advanced.rs +++ b/crates/ui-egui/src/new_project_advanced.rs @@ -5,7 +5,6 @@ use std::collections::BTreeSet; use egui::{RichText, Sense, Ui, vec2}; use serde_json::{Value, json}; use texturecraft_engine::core::NormalFormat; -use texturecraft_engine::template_cmds::builtin_templates; use crate::TextureCraftApp; use crate::i18n::{tr, trf}; @@ -65,13 +64,7 @@ pub(crate) fn advanced_settings(app: &mut TextureCraftApp, ui: &mut Ui) { fn normal_format_combo(app: &mut TextureCraftApp, ui: &mut Ui) { let new_project = &mut app.ui.new_project; - let user_templates = app.session.templates.list.iter().cloned(); - let template_format = builtin_templates() - .into_iter() - .chain(user_templates) - .find(|template| template.name == new_project.template) - .map(|template| template.normal_format) - .unwrap_or_default(); + let template_format = app.session.template(&new_project.template).map(|template| template.normal_format).unwrap_or_default(); let current = new_project.normal_format.unwrap_or(template_format); let current_label = NORMAL_FORMATS.iter().find(|(format, _, _)| *format == current).map_or("", |(_, _, label)| label); egui::ComboBox::from_id_salt("np_normal_format").width(COMBO_WIDTH).selected_text(current_label).show_ui(ui, |ui| { diff --git a/crates/ui-egui/tests/new_project_baked_maps.rs b/crates/ui-egui/tests/new_project_baked_maps.rs index 5b1d7615..39a7c813 100644 --- a/crates/ui-egui/tests/new_project_baked_maps.rs +++ b/crates/ui-egui/tests/new_project_baked_maps.rs @@ -80,3 +80,74 @@ fn create_imports_the_listed_maps_in_the_chosen_normal_format() { assert!(state.ui.new_project.baked_maps.is_empty(), "the next new project starts with no files"); assert_eq!(state.session.undo_label(), Some("Import baked maps")); } + +#[test] +fn a_failed_import_keeps_the_file_list_and_created_project() { + let mut h = harness(); + let paths = bake_files("failure", &["Ball_normal.png", "Base_ao.png"]); + std::fs::write(&paths[1], b"not an image").unwrap(); + add_baked_map_files(&mut h.state_mut().ui.new_project, paths.clone()); + h.state_mut().ui.new_project.name = "Import failure".to_string(); + h.run_steps(3); + + h.get_by_label("Create").click(); + h.run_steps(3); + + let app = h.state(); + assert!(!app.ui.windows.new_project); + assert_eq!(app.session.doc.project.name, "Import failure"); + assert_eq!(app.ui.new_project.baked_maps, paths); + assert!(app.session.doc.resources.is_empty() && app.session.images.is_empty()); + assert!(app.session.doc.baked.values().all(|baked| baked.maps.is_empty())); + let toast = app.toasts.last().unwrap(); + assert!(toast.error && toast.text.contains("Base_ao.png"), "{}", toast.text); +} + +#[test] +#[ignore = "offscreen GPU snapshots; set TC_UI_SNAPSHOTS to an output directory"] +fn new_project_baked_maps_snapshots() { + let output_dir = PathBuf::from(std::env::var_os("TC_UI_SNAPSHOTS").expect("set TC_UI_SNAPSHOTS")); + std::fs::create_dir_all(&output_dir).unwrap(); + let session = texturecraft_engine::sample_session(256, 64).unwrap(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 960.0)).with_pixels_per_point(1.0).wgpu().build_eframe(|cc| { + let adapter = cc.wgpu_render_state.as_ref().unwrap().adapter.get_info(); + eprintln!("snapshot adapter: {adapter:?}"); + TextureCraftApp::new(cc, session) + }); + h.state_mut().ui.windows.new_project = true; + h.state_mut().ui.new_project.resolution = 256; + h.run_steps(6); + h.get_by_label("Advanced settings").click(); + h.run_steps(6); + h.get_by_label("Import baked maps").click(); + h.run_steps(6); + let paths = bake_files("snapshots", &["Ball_normal.png", "Base_ao.png", "Ball_notes.png"]); + add_baked_map_files(&mut h.state_mut().ui.new_project, paths); + h.state_mut().ui.new_project.normal_format = Some(NormalFormat::DirectX); + h.run_steps(6); + h.render().unwrap().save(output_dir.join("new-project-baked-maps.png")).unwrap(); + h.set_size(egui::vec2(1024.0, 768.0)); + h.run_steps(6); + h.render().unwrap().save(output_dir.join("new-project-baked-maps-small.png")).unwrap(); + h.set_size(egui::vec2(1600.0, 960.0)); + h.run_steps(6); + h.get_by_label("Create").click(); + h.run_steps(3); + assert!(h.state().session.doc.baked[&0].maps.contains_key(&BakedMap::Normal)); + h.render().unwrap().save(output_dir.join("new-project-imported.png")).unwrap(); +} + +#[test] +fn normal_format_preview_uses_the_same_template_lookup_as_creation() { + let mut h = harness(); + h.state_mut().session.templates.dir = None; + h.state_mut().run("textureSet.settings", serde_json::json!({"normalFormat": "directX"})).unwrap(); + h.state_mut().run("project.template.save", serde_json::json!({"name": "Custom DirectX"})).unwrap(); + h.state_mut().ui.new_project.template = "custom directx".to_string(); + h.run_steps(3); + + h.get_by_value("DirectX"); + h.get_by_label("Create").click(); + h.run_steps(3); + assert_eq!(h.state().session.doc.project.normal_format, NormalFormat::DirectX); +}