This commit is contained in:
Quaternions 2024-01-11 02:46:47 -08:00
parent 097c92f602
commit 13ebb96ed6

View File

@ -1,4 +1,4 @@
use std::io::{Read,Seek};
use std::io::Read;
use clap::{Args,Parser,Subcommand};
use anyhow::Result as AResult;
use futures::StreamExt;
@ -121,20 +121,17 @@ enum Cookie{
File(std::path::PathBuf),
}
enum ReaderType<'a,R:Read+Seek>{
GZip(flate2::read::GzDecoder<&'a mut R>),
Raw(&'a mut R),
enum ReaderType<R:Read>{
GZip(flate2::read::GzDecoder<std::io::BufReader<R>>),
Raw(std::io::BufReader<R>),
}
fn maybe_gzip_decode<R:Read+Seek>(input:&mut R)->AResult<ReaderType<R>>{
let mut first_2=[0u8;2];
if let (Ok(()),Ok(()))=(std::io::Read::read_exact(input,&mut first_2),std::io::Seek::rewind(input)){
match &first_2{
b"\x1f\x8b"=>Ok(ReaderType::GZip(flate2::read::GzDecoder::new(input))),
_=>Ok(ReaderType::Raw(input)),
}
}else{
Err(anyhow::Error::msg("failed to peek"))
fn maybe_gzip_decode<R:Read>(input:R)->AResult<ReaderType<R>>{
let mut buf=std::io::BufReader::new(input);
let peek=std::io::BufRead::fill_buf(&mut buf)?;
match &peek[0..2]{
b"\x1f\x8b"=>Ok(ReaderType::GZip(flate2::read::GzDecoder::new(buf))),
_=>Ok(ReaderType::Raw(buf)),
}
}
@ -268,7 +265,7 @@ async fn get_version_history(client:&reqwest::Client,cookie:&str,asset_id:AssetI
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/")?;
//url borrow scope
{
@ -287,7 +284,7 @@ async fn download_asset_version(client:&reqwest::Client,cookie:&str,asset_id_str
continue;
}
return Ok(std::io::Cursor::new(resp.bytes().await?));
return Ok(resp);
}
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 output_folder=config.output_folder.clone();
async move{
let mut readable=download_asset_version(client,cookie,asset_id_str,asset_version.assetVersionNumber.to_string().as_str()).await?;
let contents=match maybe_gzip_decode(&mut readable)?{
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)=>read_readable(readable)?,
ReaderType::Raw(readable)=>read_readable(readable)?,
};
@ -344,21 +340,18 @@ async fn download_history(config:DownloadHistoryConfig)->AResult<()>{
Ok(())
}
fn load_dom<R:Read+Seek>(input:&mut R)->AResult<rbx_dom_weak::WeakDom>{
let mut first_8=[0u8;8];
if let (Ok(()),Ok(()))=(std::io::Read::read_exact(input, &mut first_8),std::io::Seek::rewind(input)){
match &first_8[0..4]{
b"<rob"=>{
match &first_8[4..8]{
b"lox!"=>return rbx_binary::from_reader(input).map_err(anyhow::Error::msg),
b"lox "=>return rbx_xml::from_reader(input,rbx_xml::DecodeOptions::default()).map_err(anyhow::Error::msg),
other=>Err(anyhow::Error::msg(format!("Unknown Roblox file type {:?}",other))),
}
},
_=>Err(anyhow::Error::msg("unsupported file type")),
}
}else{
Err(anyhow::Error::msg("peek failed"))
fn load_dom<R:Read>(input:R)->AResult<rbx_dom_weak::WeakDom>{
let mut buf=std::io::BufReader::new(input);
let peek=std::io::BufRead::fill_buf(&mut buf)?;
match &peek[0..4]{
b"<rob"=>{
match &peek[4..8]{
b"lox!"=>return rbx_binary::from_reader(buf).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))),
}
},
_=>Err(anyhow::Error::msg("unsupported file type")),
}
}
@ -451,8 +444,8 @@ struct DecompiledContext{
tree_refs:std::collections::HashMap<rbx_dom_weak::types::Ref,TreeNode>,
}
fn generate_decompiled_context<R:Read+Seek>(mut input:R)->AResult<DecompiledContext>{
let dom=load_dom(&mut input)?;
fn generate_decompiled_context<R:Read>(input:R)->AResult<DecompiledContext>{
let dom=load_dom(input)?;
let mut tree_refs=std::collections::HashMap::new();
tree_refs.insert(dom.root_ref(),TreeNode::new(