diff --git a/crates/effects/src/catalog.rs b/crates/effects/src/catalog.rs index 7fcb024bf..c7eecaaf3 100644 --- a/crates/effects/src/catalog.rs +++ b/crates/effects/src/catalog.rs @@ -147,7 +147,7 @@ pub fn effects_markdown() -> String { - **Status**: {} implemented in full, {partial} partial (what is missing is listed).\n\n", r.len() - partial )); - s.push_str("Effects are addressed by id (`ec..`) or display name in commands, scripts and the control channel, and every parameter by its id (`effects/#1/blurriness`).\n\n"); + s.push_str("Effects are addressed by id (`ec..`) or display name in commands, scripts and the control channel, and every parameter by its id (`effects/#1/blurriness`). A parameter that was renamed keeps its old name working in expressions of older projects: `effect(\"CC Ball Action\")(\"Instability State\")` still reads Scatter Motion.\n\n"); for cat in CATEGORIES { let list: Vec<_> = r.iter().filter(|e| e.category == *cat).collect(); if list.is_empty() { diff --git a/crates/expr/Cargo.toml b/crates/expr/Cargo.toml index 6f157698b..a0cb29945 100644 --- a/crates/expr/Cargo.toml +++ b/crates/expr/Cargo.toml @@ -11,6 +11,7 @@ workspace = true [dependencies] effectcraft-time = { workspace = true } +effectcraft-effects = { workspace = true } effectcraft-geom = { workspace = true } effectcraft-keyframe = { workspace = true } serde_json = { workspace = true } @@ -25,5 +26,4 @@ boa_engine = { workspace = true, features = ["js"] } [dev-dependencies] effectcraft-color = { workspace = true } -effectcraft-effects = { workspace = true } effectcraft-text = { workspace = true } diff --git a/crates/expr/src/host.rs b/crates/expr/src/host.rs index 43400ea1d..52dca5823 100644 --- a/crates/expr/src/host.rs +++ b/crates/expr/src/host.rs @@ -7,9 +7,10 @@ //! the answers and runs the script again. Expressions are pure functions of these answers, so //! re-running is exact, and the runtime stays free of borrowed state. +use effectcraft_effects::migrate::PARAM_ID_ALIASES; use effectcraft_geom::{Mat3, Mat4}; use effectcraft_keyframe::Value; -use effectcraft_project::{Comp, ItemId, ItemKind, Layer, LayerId, LayerSource, Node, PropGroup, Property}; +use effectcraft_project::{Comp, GroupKind, ItemId, ItemKind, Layer, LayerId, LayerSource, Node, PropGroup, Property}; use effectcraft_render::EvalCtx; use effectcraft_time::Tick; @@ -283,10 +284,24 @@ fn find_child<'g>(g: &'g PropGroup, key: &Key) -> Option<&'g Node> { .find(|n| n.match_id() == m) .or_else(|| g.children.iter().find(|n| n.name().eq_ignore_ascii_case(name))) .or_else(|| g.children.iter().find(|n| compact(n.name()) == c || compact(n.match_id()) == c)) + // Last, so a legacy name never shadows a current one. + .or_else(|| legacy_effect_child(g, &c)) } } } +/// A parameter of an effect instance addressed by a name it had when the project was saved +/// ([`PARAM_ID_ALIASES`]): `effect("CC Ball Action")("Instability State")` keeps reading the +/// parameter that is now Scatter Motion. `c` is the compacted (lowercase, no spaces) name. Only +/// renames inside the instance's own top-level group count; moves into twirl-downs do not. +fn legacy_effect_child<'g>(g: &'g PropGroup, c: &str) -> Option<&'g Node> { + let GroupKind::Effect { effect } = &g.kind else { return None }; + PARAM_ID_ALIASES + .iter() + .filter(|(e, old, new)| e == effect && !old.contains('/') && !new.contains('/') && compact(old) == c) + .find_map(|(_, _, new)| g.children.iter().find(|n| n.match_id() == *new)) +} + impl<'a> Resolver<'a> { fn comp(&self, c: u64) -> Option<&'a Comp> { if c == self.ctx.comp_id.0 { Some(self.ctx.comp) } else { self.ctx.project.comp(ItemId(c)) } diff --git a/crates/expr/src/tests.rs b/crates/expr/src/tests.rs index 050a5a550..22ce60ca0 100644 --- a/crates/expr/src/tests.rs +++ b/crates/expr/src/tests.rs @@ -2070,3 +2070,86 @@ fn bench_evaluation() { println!("BENCH {best:>9} ns/eval {label} (min of 3 x {iters})"); } } + +/// Layer A with an old-format CC Ball Action: Scatter Motion saved as `instabilityState` +/// ("Instability State"), opened through the migration (#3197). Scatter Motion is 37 and keyed +/// 10 at 0 s, 50 at 2 s. +fn old_ball_action() -> Fx { + let mut f = fx(); + let spec = effectcraft_effects::find("ec.sim.ccballaction").unwrap(); + let mut g = group_push(&mut f.p, |ids| effectcraft_effects::instantiate(spec, ids, "CC Ball Action", [20.0, 20.0])); + let sm = g.prop_mut("scatterMotion").unwrap(); + sm.value = Value::Scalar(37.0); + sm.keys = vec![Keyframe::new(Tick::ZERO, Value::Scalar(10.0)), Keyframe::new(secs(2.0), Value::Scalar(50.0))]; + sm.match_id = "instabilityState".into(); + sm.name = "Instability State".into(); + let mut next = f.p.next_id; + assert!(effectcraft_effects::migrate::upgrade_instance(spec, &mut g, &mut Ids(&mut next), [20.0, 20.0])); + f.p.next_id = next; + assert!(g.prop("scatterMotion").is_some(), "the migration renamed the parameter"); + f.p.comp_mut(f.cid).unwrap().layers[0].props.sub_mut("effects").unwrap().children.push(g.into()); + f +} + +#[test] +fn old_parameter_names_still_resolve_in_expressions() { + let f = old_ball_action(); + for name in ["Instability State", "instabilityState", "instability state", "InstabilityState"] { + // Keyframes still apply: the keyed value at 0 s, not the stored 37. + assert_close!(f.num(&format!("effect('CC Ball Action')('{name}')"), 0.0), 10.0); + assert_close!(f.num(&format!("effect('CC Ball Action')('{name}')"), 2.0), 50.0); + assert_close!(f.num(&format!("effect('CC Ball Action')('{name}').valueAtTime(2)"), 0.0), 50.0); + } + // Current names read the same property, unchanged. + for name in ["Scatter Motion", "scatterMotion"] { + assert_close!(f.num(&format!("effect('CC Ball Action')('{name}')"), 0.0), 10.0); + } +} + +#[test] +fn old_parameter_name_follows_the_parameter_expression() { + let mut f = old_ball_action(); + let p = f.p.comp_mut(f.cid).unwrap().layers[0].props.prop_mut("effects/#2/scatterMotion"); + let p = match p { + Some(p) => p, + None => panic!("scatterMotion at effects/#2"), + }; + p.expr = Some(Expression { text: "123".into(), enabled: true }); + assert_close!(f.num("effect('CC Ball Action')('Instability State')", 0.0), 123.0); +} + +#[test] +fn unknown_parameter_names_still_fail() { + let f = old_ball_action(); + assert!(f.ev(f.a, ROT, "effect('CC Ball Action')('No Such Thing')", 0.0).is_err()); + // Old names only exist on the effect they were renamed on. + assert!(f.ev(f.a, ROT, "effect('Slider Control')('Instability State')", 0.0).is_err()); +} + +/// Every in-group rename in the alias table keeps resolving by its old id and display name. +#[test] +fn every_in_group_alias_resolves_by_its_old_name() { + let mut n = 0; + for (effect, old, new) in effectcraft_effects::migrate::PARAM_ID_ALIASES { + if old.contains('/') || new.contains('/') { + continue; + } + let Some(spec) = effectcraft_effects::find(effect) else { continue }; + let mut f = fx(); + let g = group_push(&mut f.p, |ids| effectcraft_effects::instantiate(spec, ids, "Probe", [20.0, 20.0])); + f.p.comp_mut(f.cid).unwrap().layers[0].props.sub_mut("effects").unwrap().children.push(g.into()); + let (new_name, kind) = { + let l = &f.p.comp(f.cid).unwrap().layers[0]; + let p = l.props.prop(&format!("effects/#2/{new}")).unwrap(); + (p.name.clone(), p.value.clone()) + }; + if !matches!(kind, Value::Scalar(_)) { + continue; + } + let a = f.ev(f.a, ROT, &format!("effect('Probe')('{old}')"), 0.0).unwrap(); + let b = f.ev(f.a, ROT, &format!("effect('Probe')('{new_name}')"), 0.0).unwrap(); + assert_eq!(a, b, "{effect}: {old}"); + n += 1; + } + assert!(n >= 1); +} diff --git a/docs/effects.md b/docs/effects.md index 83ee80223..903d26113 100644 --- a/docs/effects.md +++ b/docs/effects.md @@ -9,7 +9,7 @@ EffectCraft ships 308 effects with 2992 parameters in total, grouped into the sa - **32**: 308 effects process 32-bit float (HDR, overbright) pixels without clamping. - **Status**: 308 implemented in full, 0 partial (what is missing is listed). -Effects are addressed by id (`ec..`) or display name in commands, scripts and the control channel, and every parameter by its id (`effects/#1/blurriness`). +Effects are addressed by id (`ec..`) or display name in commands, scripts and the control channel, and every parameter by its id (`effects/#1/blurriness`). A parameter that was renamed keeps its old name working in expressions of older projects: `effect("CC Ball Action")("Instability State")` still reads Scatter Motion. ## 3D Channel