From 3ed2b71e07a62ee0aff0ab9f6d066f1b3998200e Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 11 Jan 2024 01:43:33 -0800 Subject: [PATCH] async battle --- src/main.rs | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/src/main.rs b/src/main.rs index fcbd543..fa68137 100644 --- a/src/main.rs +++ b/src/main.rs @@ -659,6 +659,116 @@ fn decompile(config:DecompileConfig)->AResult<()>{ Ok(()) } +struct WriteCommitConfig{ + output_folder:std::path::PathBuf, + write_template:bool, + write_models:bool, + write_scripts:bool, +} + +fn write_commit(config:WriteCommitConfig,b:AResult<(AssetVersion,DecompiledContext)>,repo:&git2::Repository,last_commit:Option<&git2::Commit>)->AResult{ + let (asset_version,context)=b?; + + //clean output dir + { + let mut src=config.output_folder.clone(); + src.push("src"); + std::fs::remove_dir_all(src)?; + let mut template=config.output_folder.clone(); + template.push("template.rbxlx"); + std::fs::remove_file(template); + } + + //write files + write_files(WriteConfig{ + output_folder:config.output_folder, + write_template:config.write_template, + write_models:config.write_models, + write_scripts:config.write_scripts, + },context)?; + + + let date=asset_version.created; + let sig=git2::Signature::new("roblox-git-history","git_history@strafes.net",&git2::Time::new(date.timestamp(),0)).unwrap(); + let tree_id={ + let mut tree_index = repo.index()?; + tree_index.add_all([config.output_folder].iter(),git2::IndexAddOption::DEFAULT,None)?; + tree_index.write_tree()? + }; + let tree=repo.find_tree(tree_id)?; + let commit_id=match last_commit{ + Some(commit)=>repo.commit( + Some("HEAD"),//update_ref + &sig,//author + &sig,//commiter + &format!("v{}", asset_version.assetVersionNumber),//message + &tree,//tree (basically files) + &[commit],//parents + ).expect("commit failed"), + None=>repo.commit( + Some("HEAD"),//update_ref + &sig,//author + &sig,//commiter + &format!("v{}", asset_version.assetVersionNumber),//message + &tree,//tree (basically files) + &[],//parents + ).expect("commit failed"), + }; + + //commit + Ok(commit_id) +} + +struct DecompileHistoryConfig{ + cookie:String, + asset_id:AssetID, + output_folder:std::path::PathBuf, + write_template:bool, + write_models:bool, + write_scripts:bool, +} + +async fn decompile_history_into_git(config:DecompileHistoryConfig)->AResult<()>{ + let client=reqwest::Client::new(); + + //poll paged list of all asset versions + let asset_list=get_version_history(&client,&config.cookie.as_str(),config.asset_id).await?; + + let repo=git2::Repository::init(config.output_folder.clone())?; + let mut commits=Vec::new(); + + //download all versions + 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 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)) + } + }) + .buffered(CONCURRENT_REQUESTS) + .for_each(|b:AResult<_>|async{ + match write_commit(WriteCommitConfig{ + output_folder:config.output_folder.clone(), + write_template:config.write_template, + write_models:config.write_models, + write_scripts:config.write_scripts, + },b,&repo,commits.last()){ + Ok(commit_id)=>commits.push(repo.find_commit(commit_id).expect("Failed to find commit")), + Err(e)=>println!("download/unzip/decompile/write error: {}",e), + } + }).await; + Ok(()) +} + fn compile(_folder:std::path::PathBuf,_file:std::path::PathBuf)->AResult<()>{ Ok(()) }