From c3ca545119ceff3d1779620efe88e34183e846c3 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 11 Jan 2024 16:06:12 -0800 Subject: [PATCH] write some stupid stuff using unlimited scoped threads --- src/main.rs | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 882b266..bbd776c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -427,7 +427,7 @@ fn sanitize<'a>(s:&'a str)->std::borrow::Cow<'a,str>{ lazy_regex::regex!(r"[^a-zA-Z0-9._-]").replace_all(s,"_") } -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<()>{ +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=>(), @@ -611,10 +611,10 @@ async fn write_files(config:WriteConfig,mut context:DecompiledContext)->AResult< tokio::fs::write(file,serde_json::to_string(&properties)?).await?; } //write item in subfolder - write_queue.push(write_item(&context.dom,subfolder,node,name_override.clone(),config.write_models,config.write_scripts)); + write_queue.push((subfolder,node,name_override.clone())); }else{ //write item - write_queue.push(write_item(&context.dom,folder.clone(),node,name_override.clone(),config.write_models,config.write_scripts)); + write_queue.push((folder.clone(),node,name_override.clone())); } //queue item to be deleted from dom after child objects are handled (stack is popped from the back) match node.class{ @@ -637,9 +637,17 @@ async fn write_files(config:WriteConfig,mut context:DecompiledContext)->AResult< } //run the async - for result in futures::future::join_all(write_queue).await{ - result?; - } + std::thread::scope(|s|{ + let threads:Vec>>=write_queue.into_iter().map(|(write_path,node,node_name_override)| + s.spawn(||write_item(&context.dom,write_path,node,node_name_override,config.write_models,config.write_scripts)) + ).collect(); + for thread in threads{ + match thread.join(){ + Ok(_)=>(), + Err(e)=>println!("thread error: {:?}",e), + } + } + }); //run the destroy for destroy_ref in destroy_queue{ @@ -697,8 +705,8 @@ struct WriteCommitConfig{ write_scripts:bool, } -async fn write_commit(config:WriteCommitConfig,b:AResult<(AssetVersion,DecompiledContext)>,repo:&git2::Repository)->AResult<()>{ - let (asset_version,context)=b?; +async fn write_commit(config:WriteCommitConfig,b:Result,tokio::task::JoinError>,repo:&git2::Repository)->AResult<()>{ + let (asset_version,context)=b??; //clean output dir { @@ -780,21 +788,20 @@ async fn decompile_history_into_git(config:DecompileHistoryConfig)->AResult<()>{ let asset_id_string=config.asset_id.to_string(); futures::stream::iter(asset_list) .map(|asset_version|{ - let client=&client; - let cookie=config.cookie.as_str(); - let asset_id_str=asset_id_string.as_str(); - async move{ - let resp=download_asset_version(client,cookie,asset_id_str,asset_version.assetVersionNumber.to_string().as_str()).await?; + let client=client.clone(); + let cookie=config.cookie.clone(); + let asset_id_str=asset_id_string.clone(); + tokio::task::spawn(async move{ + let resp=download_asset_version(&client,cookie.as_str(),asset_id_str.as_str(),asset_version.assetVersionNumber.to_string().as_str()).await?; let contents=match maybe_gzip_decode(std::io::Cursor::new(resp.bytes().await?))?{ ReaderType::GZip(readable)=>generate_decompiled_context(readable)?, ReaderType::Raw(readable)=>generate_decompiled_context(readable)?, }; - - Ok((asset_version,contents)) - } + Ok::<_,anyhow::Error>((asset_version,contents)) + }) }) .buffered(CONCURRENT_REQUESTS) - .for_each(|b:AResult<_>|async{ + .for_each(|join_handle_result|async{ match write_commit(WriteCommitConfig{ git_committer_name:config.git_committer_name.clone(), git_committer_email:config.git_committer_email.clone(), @@ -802,7 +809,7 @@ async fn decompile_history_into_git(config:DecompileHistoryConfig)->AResult<()>{ write_template:config.write_template, write_models:config.write_models, write_scripts:config.write_scripts, - },b,&repo).await{ + },join_handle_result,&repo).await{ Ok(())=>(), Err(e)=>println!("download/unzip/decompile/write/commit error: {}",e), }