mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-28 17:19:24 +01:00
Export: the desktop shader settings file runs the same name check as the web and headless ones
file.exportTextures wrote <project>_shaders.json without checking it against the export's texture paths, which the web download and the headless export do (review on #827). The check is one shared helper, check_shader_parameters_name, run here against this command's files and the earlier commands' reservedNames, so a clash is a warning and the file is not written.
This commit is contained in:
1 parent
abe73ab180
commit
2a79b229bb
3 files changed
+37
-5
No files matched your search
@@ -825,11 +825,16 @@ fn export_textures(session: &mut Session, params: &Value) -> Result<Value> {
|
||||
written.extend(write_usd_asset(session, dir, exports, &mut warnings));
|
||||
}
|
||||
if let Some(shader_sets) = shader_sets {
|
||||
let path = dir.join(crate::shader_parameters::shader_parameters_file(&session.doc.project.name));
|
||||
let name = crate::shader_parameters::shader_parameters_file(&session.doc.project.name);
|
||||
let path = dir.join(&name);
|
||||
let parameters = crate::shader_parameters::shader_parameters(&session.doc.project, &shader_sets);
|
||||
|
||||
// Check the name against this command's files and the earlier commands' `reservedNames`, as the web download and headless export do
|
||||
let roots = written.iter().filter_map(|written| written.strip_prefix(dir).ok()?.components().next()?.as_os_str().to_str());
|
||||
let checked = crate::shader_parameters::check_shader_parameters_name(&name, roots.chain(taken_names.keys().map(String::as_str)));
|
||||
|
||||
// The textures are written by now: a shaders file that fails is a warning, as a failed USD asset is
|
||||
let text = serde_json::to_vec_pretty(¶meters).map_err(|error| error.to_string());
|
||||
let text = checked.and_then(|()| serde_json::to_vec_pretty(¶meters).map_err(|error| error.to_string()));
|
||||
match text.and_then(|text| texturecraft_formats::write_atomic(&path, &text).map_err(|error| error.to_string())) {
|
||||
Ok(()) => written.push(path),
|
||||
Err(error) => warnings.push(format!("The shader settings file was not written: {error}")),
|
||||
|
||||
@@ -34,12 +34,19 @@ pub fn shader_parameters_file(name: &str) -> String {
|
||||
format!("{}_shaders.json", texturecraft_formats::export::sanitize_file_stem(name))
|
||||
}
|
||||
|
||||
/// Refuse the file name `name` when one of the export's texture paths (`/`-separated, relative to the export folder) is that file or lies in a folder of that name, ignoring case
|
||||
pub fn check_shader_parameters_name<'a>(name: &str, texture_paths: impl IntoIterator<Item = &'a str>) -> std::result::Result<(), String> {
|
||||
if texture_paths.into_iter().any(|path| path.split('/').next().is_some_and(|root| root.eq_ignore_ascii_case(name))) {
|
||||
return Err(format!("\"{name}\" is already used by a texture file or folder"));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Build the download or headless sidecar after checking its name against every texture path.
|
||||
pub fn shader_parameters_sidecar(project: &Project, sets: &[usize], textures: &[ExportedFile]) -> std::result::Result<ExportedFile, String> {
|
||||
let name = shader_parameters_file(&project.name);
|
||||
if textures.iter().any(|texture| texture.name.split('/').next().is_some_and(|root| root.eq_ignore_ascii_case(&name))) {
|
||||
return Err(format!("\"{name}\" is already used by a texture file or folder"));
|
||||
}
|
||||
check_shader_parameters_name(&name, textures.iter().map(|texture| texture.name.as_str()))?;
|
||||
|
||||
let bytes = serde_json::to_vec_pretty(&shader_parameters(project, sets)).map_err(|error| error.to_string())?;
|
||||
Ok(ExportedFile { name, bytes, width: 0, height: 0, warnings: Vec::new() })
|
||||
|
||||
@@ -96,3 +96,23 @@ fn a_shaders_json_that_cannot_be_written_is_a_warning_after_the_textures() {
|
||||
|
||||
assert!(warnings.iter().any(|w| w.as_str().is_some_and(|w| w.starts_with("The shader settings file was not written"))), "{result}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_shaders_json_named_like_an_earlier_commands_folder_is_a_warning() {
|
||||
let mut session = texturecraft_engine::sample_session(128, 32).unwrap();
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let name = texturecraft_engine::shader_parameters::shader_parameters_file(&session.doc.project.name);
|
||||
let reserved = format!("{}/Color.png", name.to_uppercase());
|
||||
|
||||
let result = execute(
|
||||
&mut session,
|
||||
"file.exportTextures",
|
||||
&json!({"dir": dir.path().display().to_string(), "ts": 0, "shaderParameters": true, "reservedNames": [reserved]}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert!(!dir.path().join(&name).exists(), "{result}");
|
||||
let warnings = result["warnings"].as_array().unwrap();
|
||||
|
||||
assert!(warnings.iter().any(|w| w.as_str().is_some_and(|w| w.contains("already used by a texture file or folder"))), "{result}");
|
||||
}
|
||||
Reference in new issue
Block a user