This commit is contained in:
Quaternions 2024-01-11 02:02:49 -08:00
parent 3ed2b71e07
commit 52b43cef2b

View File

@ -1,4 +1,4 @@
use std::io::{Read,Seek}; use std::io::Read;
use clap::{Args,Parser,Subcommand}; use clap::{Args,Parser,Subcommand};
use anyhow::Result as AResult; use anyhow::Result as AResult;
use futures::StreamExt; use futures::StreamExt;
@ -49,6 +49,7 @@ enum Commands{
Upload, Upload,
Compile, Compile,
Decompile, Decompile,
DecompileHistoryIntoGit,
} }
#[derive(Args)] #[derive(Args)]
@ -112,6 +113,14 @@ async fn main()->AResult<()>{
write_models:!cli.no_models.unwrap_or(false), write_models:!cli.no_models.unwrap_or(false),
write_scripts:!cli.no_scripts.unwrap_or(false), write_scripts:!cli.no_scripts.unwrap_or(false),
}), }),
Commands::DecompileHistoryIntoGit=>decompile_history_into_git(DecompileHistoryConfig{
cookie:cookie.unwrap(),
asset_id:cli.asset_id.unwrap(),
output_folder:cli.output.unwrap(),
write_template:!cli.no_template.unwrap_or(false),
write_models:!cli.no_models.unwrap_or(false),
write_scripts:!cli.no_scripts.unwrap_or(false),
}).await,
} }
} }
@ -666,7 +675,7 @@ struct WriteCommitConfig{
write_scripts:bool, write_scripts:bool,
} }
fn write_commit(config:WriteCommitConfig,b:AResult<(AssetVersion,DecompiledContext)>,repo:&git2::Repository,last_commit:Option<&git2::Commit>)->AResult<git2::Oid>{ fn write_commit(config:WriteCommitConfig,b:AResult<(AssetVersion,DecompiledContext)>,repo:&git2::Repository)->AResult<()>{
let (asset_version,context)=b?; let (asset_version,context)=b?;
//clean output dir //clean output dir
@ -676,12 +685,12 @@ fn write_commit(config:WriteCommitConfig,b:AResult<(AssetVersion,DecompiledConte
std::fs::remove_dir_all(src)?; std::fs::remove_dir_all(src)?;
let mut template=config.output_folder.clone(); let mut template=config.output_folder.clone();
template.push("template.rbxlx"); template.push("template.rbxlx");
std::fs::remove_file(template); std::fs::remove_file(template)?;
} }
//write files //write files
write_files(WriteConfig{ write_files(WriteConfig{
output_folder:config.output_folder, output_folder:config.output_folder.clone(),
write_template:config.write_template, write_template:config.write_template,
write_models:config.write_models, write_models:config.write_models,
write_scripts:config.write_scripts, write_scripts:config.write_scripts,
@ -696,14 +705,15 @@ fn write_commit(config:WriteCommitConfig,b:AResult<(AssetVersion,DecompiledConte
tree_index.write_tree()? tree_index.write_tree()?
}; };
let tree=repo.find_tree(tree_id)?; let tree=repo.find_tree(tree_id)?;
let commit_id=match last_commit{
Some(commit)=>repo.commit( match repo.revwalk().unwrap().next(){
Some(Ok(commit_id))=>repo.commit(
Some("HEAD"),//update_ref Some("HEAD"),//update_ref
&sig,//author &sig,//author
&sig,//commiter &sig,//commiter
&format!("v{}", asset_version.assetVersionNumber),//message &format!("v{}", asset_version.assetVersionNumber),//message
&tree,//tree (basically files) &tree,//tree (basically files)
&[commit],//parents &[&repo.find_commit(commit_id)?],//parents
).expect("commit failed"), ).expect("commit failed"),
None=>repo.commit( None=>repo.commit(
Some("HEAD"),//update_ref Some("HEAD"),//update_ref
@ -713,10 +723,11 @@ fn write_commit(config:WriteCommitConfig,b:AResult<(AssetVersion,DecompiledConte
&tree,//tree (basically files) &tree,//tree (basically files)
&[],//parents &[],//parents
).expect("commit failed"), ).expect("commit failed"),
Some(Err(e))=>Err(e)?,
}; };
//commit //commit
Ok(commit_id) Ok(())
} }
struct DecompileHistoryConfig{ struct DecompileHistoryConfig{
@ -735,7 +746,6 @@ async fn decompile_history_into_git(config:DecompileHistoryConfig)->AResult<()>{
let asset_list=get_version_history(&client,&config.cookie.as_str(),config.asset_id).await?; 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 repo=git2::Repository::init(config.output_folder.clone())?;
let mut commits=Vec::new();
//download all versions //download all versions
let asset_id_string=config.asset_id.to_string(); let asset_id_string=config.asset_id.to_string();
@ -761,8 +771,8 @@ async fn decompile_history_into_git(config:DecompileHistoryConfig)->AResult<()>{
write_template:config.write_template, write_template:config.write_template,
write_models:config.write_models, write_models:config.write_models,
write_scripts:config.write_scripts, write_scripts:config.write_scripts,
},b,&repo,commits.last()){ },b,&repo){
Ok(commit_id)=>commits.push(repo.find_commit(commit_id).expect("Failed to find commit")), Ok(())=>(),
Err(e)=>println!("download/unzip/decompile/write error: {}",e), Err(e)=>println!("download/unzip/decompile/write error: {}",e),
} }
}).await; }).await;