This commit is contained in:
Quaternions 2024-01-11 01:17:17 -08:00
parent 097c92f602
commit fce4a867f8

View File

@ -121,20 +121,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 +265,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 +284,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 +313,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 +340,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 +444,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(