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 anyhow::Result as AResult;
use futures::StreamExt;
@ -49,6 +49,7 @@ enum Commands{
Upload,
Compile,
Decompile,
DecompileHistoryIntoGit,
}
#[derive(Args)]
@ -112,6 +113,14 @@ async fn main()->AResult<()>{
write_models:!cli.no_models.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),
}
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 +274,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 +293,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 +322,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 +349,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 +453,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(
@ -666,6 +668,122 @@ 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)->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<()>{
Ok(())
}