Compare commits

...

6 Commits

Author SHA1 Message Date
8569c694f2 ? tech 2024-01-11 02:35:08 -08:00
e722f79928 use fancy chat tech 2024-01-11 02:33:57 -08:00
d06861daa2 gentle clean print instead of error 2024-01-11 02:12:52 -08:00
52b43cef2b winner 2024-01-11 02:06:50 -08:00
3ed2b71e07 async battle 2024-01-11 02:06:50 -08:00
fce4a867f8 refactor 2024-01-11 02:06:50 -08:00

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,
} }
} }
@ -121,20 +130,17 @@ enum Cookie{
File(std::path::PathBuf), File(std::path::PathBuf),
} }
enum ReaderType<'a,R:Read+Seek>{ enum ReaderType<R:Read>{
GZip(flate2::read::GzDecoder<&'a mut R>), GZip(flate2::read::GzDecoder<std::io::BufReader<R>>),
Raw(&'a mut R), Raw(std::io::BufReader<R>),
} }
fn maybe_gzip_decode<R:Read+Seek>(input:&mut R)->AResult<ReaderType<R>>{ fn maybe_gzip_decode<R:Read>(input:R)->AResult<ReaderType<R>>{
let mut first_2=[0u8;2]; let mut buf=std::io::BufReader::new(input);
if let (Ok(()),Ok(()))=(std::io::Read::read_exact(input,&mut first_2),std::io::Seek::rewind(input)){ let peek=std::io::BufRead::fill_buf(&mut buf)?;
match &first_2{ match &peek[0..2]{
b"\x1f\x8b"=>Ok(ReaderType::GZip(flate2::read::GzDecoder::new(input))), b"\x1f\x8b"=>Ok(ReaderType::GZip(flate2::read::GzDecoder::new(buf))),
_=>Ok(ReaderType::Raw(input)), _=>Ok(ReaderType::Raw(buf)),
}
}else{
Err(anyhow::Error::msg("failed to peek"))
} }
} }
@ -268,7 +274,7 @@ async fn get_version_history(client:&reqwest::Client,cookie:&str,asset_id:AssetI
Ok(asset_list) Ok(asset_list)
} }
async fn download_asset_version(client:&reqwest::Client,cookie:&str,asset_id_str:&str,asset_version_str:&str)->AResult<impl Read+Seek>{ async fn download_asset_version(client:&reqwest::Client,cookie:&str,asset_id_str:&str,asset_version_str:&str)->AResult<reqwest::Response>{
let mut url=reqwest::Url::parse("https://assetdelivery.roblox.com/v1/asset/")?; let mut url=reqwest::Url::parse("https://assetdelivery.roblox.com/v1/asset/")?;
//url borrow scope //url borrow scope
{ {
@ -287,7 +293,7 @@ async fn download_asset_version(client:&reqwest::Client,cookie:&str,asset_id_str
continue; continue;
} }
return Ok(std::io::Cursor::new(resp.bytes().await?)); return Ok(resp);
} }
Err(anyhow::Error::msg("all requests failed")) Err(anyhow::Error::msg("all requests failed"))
} }
@ -316,9 +322,8 @@ async fn download_history(config:DownloadHistoryConfig)->AResult<()>{
let asset_id_str=asset_id_string.as_str(); let asset_id_str=asset_id_string.as_str();
let output_folder=config.output_folder.clone(); let output_folder=config.output_folder.clone();
async move{ async move{
let mut readable=download_asset_version(client,cookie,asset_id_str,asset_version.assetVersionNumber.to_string().as_str()).await?; 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?))?{
let contents=match maybe_gzip_decode(&mut readable)?{
ReaderType::GZip(readable)=>read_readable(readable)?, ReaderType::GZip(readable)=>read_readable(readable)?,
ReaderType::Raw(readable)=>read_readable(readable)?, ReaderType::Raw(readable)=>read_readable(readable)?,
}; };
@ -344,21 +349,18 @@ async fn download_history(config:DownloadHistoryConfig)->AResult<()>{
Ok(()) Ok(())
} }
fn load_dom<R:Read+Seek>(input:&mut R)->AResult<rbx_dom_weak::WeakDom>{ fn load_dom<R:Read>(input:R)->AResult<rbx_dom_weak::WeakDom>{
let mut first_8=[0u8;8]; let mut buf=std::io::BufReader::new(input);
if let (Ok(()),Ok(()))=(std::io::Read::read_exact(input, &mut first_8),std::io::Seek::rewind(input)){ let peek=std::io::BufRead::fill_buf(&mut buf)?;
match &first_8[0..4]{ match &peek[0..4]{
b"<rob"=>{ b"<rob"=>{
match &first_8[4..8]{ match &peek[4..8]{
b"lox!"=>return rbx_binary::from_reader(input).map_err(anyhow::Error::msg), b"lox!"=>return rbx_binary::from_reader(buf).map_err(anyhow::Error::msg),
b"lox "=>return rbx_xml::from_reader(input,rbx_xml::DecodeOptions::default()).map_err(anyhow::Error::msg), b"lox "=>return rbx_xml::from_reader_default(buf).map_err(anyhow::Error::msg),
other=>Err(anyhow::Error::msg(format!("Unknown Roblox file type {:?}",other))), other=>Err(anyhow::Error::msg(format!("Unknown Roblox file type {:?}",other))),
} }
}, },
_=>Err(anyhow::Error::msg("unsupported file type")), _=>Err(anyhow::Error::msg("unsupported file type")),
}
}else{
Err(anyhow::Error::msg("peek failed"))
} }
} }
@ -451,8 +453,8 @@ struct DecompiledContext{
tree_refs:std::collections::HashMap<rbx_dom_weak::types::Ref,TreeNode>, tree_refs:std::collections::HashMap<rbx_dom_weak::types::Ref,TreeNode>,
} }
fn generate_decompiled_context<R:Read+Seek>(mut input:R)->AResult<DecompiledContext>{ fn generate_decompiled_context<R:Read>(input:R)->AResult<DecompiledContext>{
let dom=load_dom(&mut input)?; let dom=load_dom(input)?;
let mut tree_refs=std::collections::HashMap::new(); let mut tree_refs=std::collections::HashMap::new();
tree_refs.insert(dom.root_ref(),TreeNode::new( tree_refs.insert(dom.root_ref(),TreeNode::new(
@ -666,6 +668,122 @@ fn decompile(config:DecompileConfig)->AResult<()>{
Ok(()) 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)->AResult<()>{
let (asset_version,context)=b?;
//clean output dir
{
let mut src=config.output_folder.clone();
src.push("src");
match std::fs::remove_dir_all(src){
Ok(())=>(),
Err(e)=>println!("remove_dir_all src failed {}",e),
}
let mut template=config.output_folder.clone();
template.push("template.rbxlx");
match std::fs::remove_file(template){
Ok(())=>(),
Err(e)=>println!("remove_file template.rbxlx failed {}",e),
}
}
//write files
write_files(WriteConfig{
output_folder:config.output_folder.clone(),
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)?;
match repo.head(){
Ok(reference)=>repo.commit(
Some("HEAD"),//update_ref
&sig,//author
&sig,//commiter
&format!("v{}", asset_version.assetVersionNumber),//message
&tree,//tree (basically files)
&[&reference.peel_to_commit()?],//parents
)?,
Err(_)=>repo.commit(
Some("HEAD"),//update_ref
&sig,//author
&sig,//commiter
&format!("v{}", asset_version.assetVersionNumber),//message
&tree,//tree (basically files)
&[],//parents
)?,
};
//commit
Ok(())
}
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())?;
//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){
Ok(())=>(),
Err(e)=>println!("download/unzip/decompile/write/commit error: {}",e),
}
}).await;
Ok(())
}
fn compile(_folder:std::path::PathBuf,_file:std::path::PathBuf)->AResult<()>{ fn compile(_folder:std::path::PathBuf,_file:std::path::PathBuf)->AResult<()>{
Ok(()) Ok(())
} }