From 1434a11f2863ddd0710a6deabfe76491fa0bada0 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Sat, 6 Jan 2024 15:01:56 -0800 Subject: [PATCH] pass tally into stack instruction to generate name override --- src/main.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 998758a..5c71a24 100644 --- a/src/main.rs +++ b/src/main.rs @@ -390,7 +390,7 @@ enum TrimStackInstruction{ } enum WriteStackInstruction<'a>{ - Node(&'a TreeNode), + Node(&'a TreeNode,u32),//(Node,NameTally) PushFolder(String), PopFolder, Destroy(Ref), @@ -411,8 +411,8 @@ 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,write_models:bool,write_scripts:bool)->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=>{ @@ -549,14 +549,15 @@ fn decompile(config:DecompileConfig)->AResult<()>{ //generate folders, models, and scripts //delete models and scripts from dom + let mut name_tally=std::collections::HashMap::::new(); let mut folder=config.output_folder.clone(); - let mut stack=vec![WriteStackInstruction::Node(tree_refs.get(&dom.root_ref()).unwrap())]; + 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; @@ -572,10 +573,15 @@ fn decompile(config:DecompileConfig)->AResult<()>{ Class::Script=>properties.ClassName=Some("Script".to_string()), Class::Model=>(), } + let name_override=if 0AResult<()>{ std::fs::write(file,serde_json::to_string(&properties)?)? } //write item in subfolder - write_item(&dom,subfolder,node,config.write_models,config.write_scripts)?; + write_item(&dom,subfolder,node,name_override.as_str(),config.write_models,config.write_scripts)?; }else{ //write item - write_item(&dom,folder.clone(),node,config.write_models,config.write_scripts)?; + 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{ @@ -598,12 +604,14 @@ fn decompile(config:DecompileConfig)->AResult<()>{ } 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())); } }, }