From def0d1a69ac8709e2b268806b7c81bb84bcd1fa3 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 11 Jan 2024 14:51:31 -0800 Subject: [PATCH] async decompile --- src/main.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1c9ef5a..882b266 100644 --- a/src/main.rs +++ b/src/main.rs @@ -427,8 +427,8 @@ fn sanitize<'a>(s:&'a str)->std::borrow::Cow<'a,str>{ lazy_regex::regex!(r"[^a-zA-Z0-9._-]").replace_all(s,"_") } -fn write_item(dom:&rbx_dom_weak::WeakDom,mut file:std::path::PathBuf,node:&TreeNode,node_name_override:&str,write_models:bool,write_scripts:bool)->AResult<()>{ - file.push(sanitize(node_name_override).as_ref()); +async fn write_item(dom:&rbx_dom_weak::WeakDom,mut file:std::path::PathBuf,node:&TreeNode,node_name_override:String,write_models:bool,write_scripts:bool)->AResult<()>{ + file.push(sanitize(node_name_override.as_str()).as_ref()); match node.class{ Class::Folder=>(), Class::ModuleScript|Class::LocalScript|Class::Script=>{ @@ -565,6 +565,9 @@ struct WriteConfig{ } async fn write_files(config:WriteConfig,mut context:DecompiledContext)->AResult<()>{ + let mut write_queue=Vec::new(); + let mut destroy_queue=Vec::new(); + let mut name_tally=std::collections::HashMap::::new(); let mut folder=config.output_folder.clone(); let mut stack=vec![WriteStackInstruction::Node(context.tree_refs.get(&context.dom.root_ref()).unwrap(),0)]; @@ -572,7 +575,7 @@ async fn write_files(config:WriteConfig,mut context:DecompiledContext)->AResult< match instruction{ WriteStackInstruction::PushFolder(component)=>folder.push(component), WriteStackInstruction::PopFolder=>assert!(folder.pop(),"weirdness"), - WriteStackInstruction::Destroy(referent)=>context.dom.destroy(referent), + WriteStackInstruction::Destroy(referent)=>destroy_queue.push(referent), WriteStackInstruction::Node(node,name_count)=>{ //properties.json to override class or other simple properties let mut properties=PropertiesOverride::default(); @@ -599,19 +602,19 @@ async fn write_files(config:WriteConfig,mut context:DecompiledContext)->AResult< let mut subfolder=folder.clone(); subfolder.push(sanitize(name_override.as_str()).as_ref()); //make folder - std::fs::create_dir(subfolder.clone())?; + tokio::fs::create_dir(subfolder.clone()).await?; //write properties if properties.is_some(){ let mut file=subfolder.clone(); file.push("properties"); assert!(file.set_extension("json"),"could not set extension"); - std::fs::write(file,serde_json::to_string(&properties)?)? + tokio::fs::write(file,serde_json::to_string(&properties)?).await?; } //write item in subfolder - write_item(&context.dom,subfolder,node,name_override.as_str(),config.write_models,config.write_scripts)?; + write_queue.push(write_item(&context.dom,subfolder,node,name_override.clone(),config.write_models,config.write_scripts)); }else{ //write item - write_item(&context.dom,folder.clone(),node,name_override.as_str(),config.write_models,config.write_scripts)?; + write_queue.push(write_item(&context.dom,folder.clone(),node,name_override.clone(),config.write_models,config.write_scripts)); } //queue item to be deleted from dom after child objects are handled (stack is popped from the back) match node.class{ @@ -633,6 +636,16 @@ async fn write_files(config:WriteConfig,mut context:DecompiledContext)->AResult< } } + //run the async + for result in futures::future::join_all(write_queue).await{ + result?; + } + + //run the destroy + for destroy_ref in destroy_queue{ + context.dom.destroy(destroy_ref); + } + //write what remains in template.rbxlx if config.write_template{ let mut file=config.output_folder.clone();