Compare commits
3 Commits
feature/ta
...
feature/ta
| Author | SHA1 | Date | |
|---|---|---|---|
| 1434a11f28 | |||
| 1aef1f73d9 | |||
| eebc843cb4 |
103
src/main.rs
103
src/main.rs
@@ -22,6 +22,13 @@ struct Cli{
|
||||
#[arg(long)]
|
||||
cookie_file:Option<std::path::PathBuf>,
|
||||
|
||||
#[arg(long)]
|
||||
no_models:Option<bool>,
|
||||
#[arg(long)]
|
||||
no_scripts:Option<bool>,
|
||||
#[arg(long)]
|
||||
no_template:Option<bool>,
|
||||
|
||||
#[arg(long)]
|
||||
asset_id:Option<AssetID>,
|
||||
|
||||
@@ -90,11 +97,21 @@ async fn main()->AResult<()>{
|
||||
};
|
||||
|
||||
match cli.command{
|
||||
Commands::DownloadHistory=>download_history(cookie.unwrap(),cli.asset_id.unwrap()).await,
|
||||
Commands::DownloadHistory=>download_history(DownloadHistoryConfig{
|
||||
output_folder:cli.output.unwrap(),
|
||||
cookie:cookie.unwrap(),
|
||||
asset_id:cli.asset_id.unwrap(),
|
||||
}).await,
|
||||
Commands::Download=>download_list(cookie.unwrap(),vec![(cli.asset_id.unwrap(),cli.output.unwrap())]).await,
|
||||
Commands::Upload=>upload_list(cookie.unwrap(),cli.group,vec![(cli.asset_id.unwrap(),cli.output.unwrap())]).await,
|
||||
Commands::Compile=>compile(cli.input.unwrap(),cli.output.unwrap()),
|
||||
Commands::Decompile=>decompile(cli.input.unwrap(),cli.output.unwrap()),
|
||||
Commands::Decompile=>decompile(DecompileConfig{
|
||||
input_file:cli.input.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),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,15 +233,21 @@ async fn download_list(cookie:String,asset_id_file_map:AssetIDFileMap)->AResult<
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn download_history(cookie:String,asset_id:AssetID)->AResult<()>{
|
||||
struct DownloadHistoryConfig{
|
||||
output_folder:std::path::PathBuf,
|
||||
cookie:String,
|
||||
asset_id:AssetID,
|
||||
}
|
||||
|
||||
async fn download_history(config:DownloadHistoryConfig)->AResult<()>{
|
||||
let client=reqwest::Client::new();
|
||||
let asset_id_string=asset_id.to_string();
|
||||
let asset_id_string=config.asset_id.to_string();
|
||||
|
||||
//poll paged list of all asset versions
|
||||
let mut cursor:Option<String>=None;
|
||||
let mut asset_list=Vec::new();
|
||||
loop{
|
||||
let mut url=reqwest::Url::parse(format!("https://develop.roblox.com/v1/assets/{}/saved-versions",asset_id).as_str())?;
|
||||
let mut url=reqwest::Url::parse(format!("https://develop.roblox.com/v1/assets/{}/saved-versions",config.asset_id).as_str())?;
|
||||
//url borrow scope
|
||||
{
|
||||
let mut query=url.query_pairs_mut();//borrow here
|
||||
@@ -238,7 +261,7 @@ async fn download_history(cookie:String,asset_id:AssetID)->AResult<()>{
|
||||
}
|
||||
println!("page url={}",url);
|
||||
let resp=client.get(url)
|
||||
.header("Cookie",cookie.clone())
|
||||
.header("Cookie",config.cookie.clone())
|
||||
.send().await?;
|
||||
match resp.json::<VersionPage>().await{
|
||||
Ok(mut page)=>{
|
||||
@@ -252,7 +275,7 @@ async fn download_history(cookie:String,asset_id:AssetID)->AResult<()>{
|
||||
}
|
||||
}
|
||||
asset_list.sort_by(|a,b|a.assetVersionNumber.cmp(&b.assetVersionNumber));
|
||||
let mut path=std::path::PathBuf::new();
|
||||
let mut path=config.output_folder.clone();
|
||||
path.set_file_name("versions.json");
|
||||
tokio::fs::write(path,serde_json::to_string(&asset_list)?).await?;
|
||||
|
||||
@@ -260,8 +283,9 @@ async fn download_history(cookie:String,asset_id:AssetID)->AResult<()>{
|
||||
futures::stream::iter(asset_list)
|
||||
.map(|asset_version|{
|
||||
let client=&client;
|
||||
let cookie=cookie.as_str();
|
||||
let cookie=config.cookie.as_str();
|
||||
let asset_id_str=asset_id_string.as_str();
|
||||
let output_folder=config.output_folder.clone();
|
||||
async move{
|
||||
let mut url=reqwest::Url::parse("https://assetdelivery.roblox.com/v1/asset/")?;
|
||||
//url borrow scope
|
||||
@@ -282,8 +306,8 @@ async fn download_history(cookie:String,asset_id:AssetID)->AResult<()>{
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut path=std::path::PathBuf::new();
|
||||
path.set_file_name(format!("{}_v{}.rbxl",asset_id,asset_version.assetVersionNumber));
|
||||
let mut path=output_folder;
|
||||
path.set_file_name(format!("{}_v{}.rbxl",config.asset_id,asset_version.assetVersionNumber));
|
||||
result=Ok((path,resp.bytes().await?));
|
||||
break;
|
||||
}
|
||||
@@ -366,7 +390,7 @@ enum TrimStackInstruction{
|
||||
}
|
||||
|
||||
enum WriteStackInstruction<'a>{
|
||||
Node(&'a TreeNode),
|
||||
Node(&'a TreeNode,u32),//(Node,NameTally)
|
||||
PushFolder(String),
|
||||
PopFolder,
|
||||
Destroy(Ref),
|
||||
@@ -387,11 +411,14 @@ fn sanitize<'a>(s:&'a str)->std::borrow::Cow<'a,str>{
|
||||
lazy_regex::regex!(r"[^a-zA-Z0-9._-]").replace_all(s,"_")
|
||||
}
|
||||
|
||||
fn write_item(dom:&rbx_dom_weak::WeakDom,mut file:std::path::PathBuf,node:&TreeNode)->AResult<()>{
|
||||
file.push(sanitize(node.name.as_str()).as_ref());
|
||||
fn write_item(dom:&rbx_dom_weak::WeakDom,mut file:std::path::PathBuf,node:&TreeNode,node_name_override:&str,write_models:bool,write_scripts:bool)->AResult<()>{
|
||||
file.push(sanitize(node_name_override).as_ref());
|
||||
match node.class{
|
||||
Class::Folder=>(),
|
||||
Class::ModuleScript|Class::LocalScript|Class::Script=>{
|
||||
if !write_scripts{
|
||||
return Ok(())
|
||||
}
|
||||
assert!(file.set_extension("lua"),"could not set extension");
|
||||
assert!(dom.get_by_ref(node.referent).is_some_and(|item|{
|
||||
//TODO: delete disabled scripts
|
||||
@@ -401,6 +428,9 @@ fn write_item(dom:&rbx_dom_weak::WeakDom,mut file:std::path::PathBuf,node:&TreeN
|
||||
}),"no string property or file failed to write");
|
||||
},
|
||||
Class::Model=>{
|
||||
if !write_models{
|
||||
return Ok(())
|
||||
}
|
||||
assert!(file.set_extension("rbxmx"));
|
||||
let output=std::io::BufWriter::new(std::fs::File::create(file)?);
|
||||
rbx_xml::to_writer_default(output,dom,&[node.referent])?;
|
||||
@@ -409,7 +439,15 @@ fn write_item(dom:&rbx_dom_weak::WeakDom,mut file:std::path::PathBuf,node:&TreeN
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decompile(input_file:std::path::PathBuf,output_folder:std::path::PathBuf)->AResult<()>{
|
||||
struct DecompileConfig{
|
||||
input_file:std::path::PathBuf,
|
||||
output_folder:std::path::PathBuf,
|
||||
write_template:bool,
|
||||
write_models:bool,
|
||||
write_scripts:bool,
|
||||
}
|
||||
|
||||
fn decompile(config:DecompileConfig)->AResult<()>{
|
||||
//rules:
|
||||
//Class Script|LocalScript|ModuleScript->$Name.lua
|
||||
//Class Model->$Name.rbxmx
|
||||
@@ -417,7 +455,7 @@ fn decompile(input_file:std::path::PathBuf,output_folder:std::path::PathBuf)->AR
|
||||
//Everything else goes into template.rbxlx
|
||||
|
||||
//read file
|
||||
let mut input=std::io::BufReader::new(std::fs::File::open(input_file)?);
|
||||
let mut input=std::io::BufReader::new(std::fs::File::open(config.input_file)?);
|
||||
let mut dom=load_dom(&mut input)?;
|
||||
|
||||
let mut tree_refs=std::collections::HashMap::new();
|
||||
@@ -448,6 +486,7 @@ fn decompile(input_file:std::path::PathBuf,output_folder:std::path::PathBuf)->AR
|
||||
parent_node.children.push(referent);
|
||||
tree_refs.insert(referent,node);
|
||||
}
|
||||
//look no further, turn this node and all its children into a model
|
||||
if skip{
|
||||
continue;
|
||||
}
|
||||
@@ -509,15 +548,16 @@ fn decompile(input_file:std::path::PathBuf,output_folder:std::path::PathBuf)->AR
|
||||
|
||||
//generate folders, models, and scripts
|
||||
//delete models and scripts from dom
|
||||
{
|
||||
let mut folder=output_folder.clone();
|
||||
let mut stack=vec![WriteStackInstruction::Node(tree_refs.get(&dom.root_ref()).unwrap())];
|
||||
|
||||
let mut name_tally=std::collections::HashMap::<String,u32>::new();
|
||||
let mut folder=config.output_folder.clone();
|
||||
let mut stack=vec![WriteStackInstruction::Node(tree_refs.get(&dom.root_ref()).unwrap(),0)];
|
||||
while let Some(instruction)=stack.pop(){
|
||||
match instruction{
|
||||
WriteStackInstruction::PushFolder(component)=>folder.push(component),
|
||||
WriteStackInstruction::PopFolder=>assert!(folder.pop(),"weirdness"),
|
||||
WriteStackInstruction::Destroy(referent)=>dom.destroy(referent),
|
||||
WriteStackInstruction::Node(node)=>{
|
||||
WriteStackInstruction::Node(node,name_count)=>{
|
||||
//properties.json to override class or other simple properties
|
||||
let mut properties=PropertiesOverride::default();
|
||||
let has_children=node.children.len()!=0;
|
||||
@@ -533,10 +573,15 @@ fn decompile(input_file:std::path::PathBuf,output_folder:std::path::PathBuf)->AR
|
||||
Class::Script=>properties.ClassName=Some("Script".to_string()),
|
||||
Class::Model=>(),
|
||||
}
|
||||
let name_override=if 0<name_count{
|
||||
format!("{}_{}",node.name,name_count)
|
||||
}else{
|
||||
node.name.clone()
|
||||
};
|
||||
if has_children||properties.is_some(){
|
||||
//push temp subfolder
|
||||
let mut subfolder=folder.clone();
|
||||
subfolder.push(sanitize(node.name.as_str()).as_ref());
|
||||
subfolder.push(sanitize(name_override.as_str()).as_ref());
|
||||
//make folder
|
||||
std::fs::create_dir(subfolder.clone())?;
|
||||
//write properties
|
||||
@@ -547,10 +592,10 @@ fn decompile(input_file:std::path::PathBuf,output_folder:std::path::PathBuf)->AR
|
||||
std::fs::write(file,serde_json::to_string(&properties)?)?
|
||||
}
|
||||
//write item in subfolder
|
||||
write_item(&dom,subfolder,node)?;
|
||||
write_item(&dom,subfolder,node,name_override.as_str(),config.write_models,config.write_scripts)?;
|
||||
}else{
|
||||
//write item
|
||||
write_item(&dom,folder.clone(),node)?;
|
||||
write_item(&dom,folder.clone(),node,name_override.as_str(),config.write_models,config.write_scripts)?;
|
||||
}
|
||||
//queue item to be deleted from dom after child objects are handled (stack is popped from the back)
|
||||
match node.class{
|
||||
@@ -559,20 +604,22 @@ fn decompile(input_file:std::path::PathBuf,output_folder:std::path::PathBuf)->AR
|
||||
}
|
||||
if has_children{
|
||||
stack.push(WriteStackInstruction::PopFolder);
|
||||
name_tally.clear();
|
||||
for referent in &node.children{
|
||||
if let Some(c)=tree_refs.get(referent){
|
||||
stack.push(WriteStackInstruction::Node(c));
|
||||
let v=name_tally.entry(c.name.clone()).and_modify(|v|*v+=1).or_default();
|
||||
stack.push(WriteStackInstruction::Node(c,*v));
|
||||
}
|
||||
}
|
||||
stack.push(WriteStackInstruction::PushFolder(sanitize(node.name.as_str()).to_string()));
|
||||
stack.push(WriteStackInstruction::PushFolder(sanitize(name_override.as_str()).to_string()));
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//write what remains in template.rbxlx
|
||||
{
|
||||
let mut file=output_folder.clone();
|
||||
if config.write_template{
|
||||
let mut file=config.output_folder.clone();
|
||||
file.push("template");
|
||||
assert!(file.set_extension("rbxlx"));
|
||||
let output=std::io::BufWriter::new(std::fs::File::create(file)?);
|
||||
@@ -584,4 +631,4 @@ fn decompile(input_file:std::path::PathBuf,output_folder:std::path::PathBuf)->AR
|
||||
|
||||
fn compile(_folder:std::path::PathBuf,_file:std::path::PathBuf)->AResult<()>{
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user