pass tally into stack instruction to generate name override

This commit is contained in:
Quaternions 2024-01-06 15:01:56 -08:00
parent 1aef1f73d9
commit 1434a11f28

View File

@ -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::<String,u32>::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 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
@ -586,10 +592,10 @@ fn decompile(config:DecompileConfig)->AResult<()>{
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()));
}
},
}