From 3a716373db85fb4a5b703a5561213b430701847b Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 4 Oct 2024 20:48:47 -0700 Subject: [PATCH] FromLua type stuff + find_first_descendant_of_class --- src/runner/instance.rs | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/runner/instance.rs b/src/runner/instance.rs index 093897c..e52caf2 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -32,6 +32,9 @@ fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance)->S pub fn find_first_child_of_class<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,class:&str)->Option<&'a rbx_dom_weak::Instance>{ instance.children().iter().filter_map(|&r|dom.get_by_ref(r)).find(|inst|inst.class==class) } +pub fn find_first_descendant_of_class<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,class:&str)->Option<&'a rbx_dom_weak::Instance>{ + dom.descendants_of(instance.referent()).find(|&inst|inst.class==class) +} //workaround until I have an enum of classes struct Dereferent(Ref); @@ -162,17 +165,12 @@ impl Instance{ Ok(children) }) ); - let ffc=|lua,this:&T,(name,search_descendants):(mlua::String,mlua::Value)|{ + let ffc=|lua,this:&T,(name,search_descendants):(mlua::String,Option)|{ let name_str=name.to_str()?; - let search_descendants=match search_descendants{ - mlua::Value::Nil=>false, - mlua::Value::Boolean(b)=>b, - _=>Err(mlua::Error::runtime("Invalid argument #3 bool expected"))?, - }; dom(lua,|dom|{ let instance=this.get(dom)?; Ok( - match search_descendants{ + match search_descendants.unwrap_or(false){ true=>dom.descendants_of(this.referent()).find(|inst|inst.name==name_str), false=>instance.children().iter().filter_map(|&r| dom.get_by_ref(r) @@ -186,19 +184,15 @@ impl Instance{ }; methods.add_method("FindFirstChild",ffc); methods.add_method("WaitForChild",ffc); - methods.add_method("FindFirstChildOfClass",|lua,this,(class,search_descendants):(mlua::String,mlua::Value)|{ + methods.add_method("FindFirstChildOfClass",|lua,this,(class,search_descendants):(mlua::String,Option)|{ let class_str=class.to_str()?; - let search_descendants=match search_descendants{ - mlua::Value::Nil=>false, - mlua::Value::Boolean(b)=>b, - _=>Err(mlua::Error::runtime("Invalid argument #3 bool expected"))?, - }; - if search_descendants==true{ - return Err(mlua::Error::runtime("FFC of class searching descendants not supported get rekt")); - } dom(lua,|dom|{ - Ok(find_first_child_of_class(dom,this.get(dom)?,class_str) - .map(|inst|(Instance::new(inst.referent()))) + Ok( + match search_descendants.unwrap_or(false){ + true=>find_first_descendant_of_class(dom,this.get(dom)?,class_str), + false=>find_first_child_of_class(dom,this.get(dom)?,class_str), + } + .map(|inst|(Instance::new(inst.referent()))) ) }) });