Refactor descendant search functions for WeakDom and RojoTree

- Renamed `find_by_name` to `find_descendant` for clarity and updated its implementation to use recursion for improved search capabilities in `WeakDom`.
- Introduced a new function `find_descendant_by_name` for recursively finding descendants in `RojoTree`, enhancing the matching logic for nested structures.
- Updated references in the `syncback_matching_ufowave_textures` and `forward_matching_ufowave_textures` functions to utilize the new descendant search functions, improving code readability and maintainability.
This commit is contained in:
Joe 2026-02-17 18:33:16 -06:00
parent b6e34b9eb7
commit 21b3ee45fd

View file

@ -94,14 +94,32 @@ fn rojo_tree_to_weak_dom(tree: &librojo::RojoTree) -> WeakDom {
dom dom
} }
/// Find a descendant by path (e.g., "TsunamiWave") in a WeakDom. /// Recursively find a descendant by name in a WeakDom.
fn find_by_name(dom: &WeakDom, parent: Ref, name: &str) -> Option<Ref> { fn find_descendant(dom: &WeakDom, parent: Ref, name: &str) -> Option<Ref> {
let parent_inst = dom.get_by_ref(parent)?; let parent_inst = dom.get_by_ref(parent)?;
for &child in parent_inst.children() { for &child in parent_inst.children() {
let child_inst = dom.get_by_ref(child)?; let child_inst = dom.get_by_ref(child)?;
if child_inst.name == name { if child_inst.name == name {
return Some(child); return Some(child);
} }
if let Some(found) = find_descendant(dom, child, name) {
return Some(found);
}
}
None
}
/// Recursively find a descendant by name in a RojoTree.
fn find_descendant_by_name(tree: &librojo::RojoTree, parent: Ref, name: &str) -> Option<Ref> {
let parent_inst = tree.get_instance(parent)?;
for &child in parent_inst.children() {
let child_inst = tree.get_instance(child)?;
if child_inst.name() == name {
return Some(child);
}
if let Some(found) = find_descendant_by_name(tree, child, name) {
return Some(found);
}
} }
None None
} }
@ -151,35 +169,11 @@ fn syncback_matching_ufowave_textures() {
// Find TsunamiWave in both trees // Find TsunamiWave in both trees
let input_root = input_dom.root_ref(); let input_root = input_dom.root_ref();
let input_tsunami = find_by_name(&input_dom, input_root, "TsunamiWave") let input_tsunami = find_descendant(&input_dom, input_root, "TsunamiWave")
.or_else(|| {
// input.rbxm root might be UFOWave, with TsunamiWave as child
let ufo = input_dom.root_ref();
let ufo_children = input_dom.get_by_ref(ufo).unwrap().children();
for &c in ufo_children {
if let Some(found) = find_by_name(&input_dom, c, "TsunamiWave") {
return Some(found);
}
let inst = input_dom.get_by_ref(c).unwrap();
if inst.name == "TsunamiWave" {
return Some(c);
}
}
None
})
.expect("TsunamiWave not found in input.rbxm"); .expect("TsunamiWave not found in input.rbxm");
let expected_root = expected_dom.root_ref(); let expected_root = expected_dom.root_ref();
let expected_tsunami = find_by_name(&expected_dom, expected_root, "TsunamiWave") let expected_tsunami = find_descendant(&expected_dom, expected_root, "TsunamiWave")
.or_else(|| {
let root_children = expected_dom.get_by_ref(expected_root).unwrap().children();
for &c in root_children {
if let Some(found) = find_by_name(&expected_dom, c, "TsunamiWave") {
return Some(found);
}
}
None
})
.expect("TsunamiWave not found in expected/"); .expect("TsunamiWave not found in expected/");
let input_textures = get_texture_children(&input_dom, input_tsunami); let input_textures = get_texture_children(&input_dom, input_tsunami);
@ -276,18 +270,9 @@ fn forward_matching_ufowave_textures() {
.find(|c| c.name == "TsunamiWave") .find(|c| c.name == "TsunamiWave")
.expect("TsunamiWave not found in snapshot"); .expect("TsunamiWave not found in snapshot");
// Find TsunamiWave in the tree // Find TsunamiWave in the tree (may be nested under root → UFOWave → TsunamiWave)
let tree_tsunami = { let tree_tsunami = find_descendant_by_name(&tree, tree_root, "TsunamiWave")
let root = tree.get_instance(tree_root).unwrap(); .expect("TsunamiWave not found in tree");
root.children()
.iter()
.find(|&&r| {
tree.get_instance(r)
.map_or(false, |i| i.name() == "TsunamiWave")
})
.copied()
.expect("TsunamiWave not found in tree")
};
// Extract Texture children from snapshot (filesystem side, sparse) // Extract Texture children from snapshot (filesystem side, sparse)
let snap_textures: Vec<librojo::InstanceSnapshot> = snap_tsunami let snap_textures: Vec<librojo::InstanceSnapshot> = snap_tsunami