pass tally into stack instruction to generate name override

This commit is contained in:
Quaternions 2024-01-06 15:01:56 -08:00
parent fdbe3251b3
commit f0de2a41ad

View File

@ -366,7 +366,7 @@ enum TrimStackInstruction{
} }
enum WriteStackInstruction<'a>{ enum WriteStackInstruction<'a>{
Node(&'a TreeNode), Node(&'a TreeNode,u32),//(Node,NameTally)
PushFolder(String), PushFolder(String),
PopFolder, PopFolder,
Destroy(Ref), Destroy(Ref),
@ -387,8 +387,8 @@ fn sanitize<'a>(s:&'a str)->std::borrow::Cow<'a,str>{
lazy_regex::regex!(r"[^a-zA-Z0-9._-]").replace_all(s,"_") 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<()>{ fn write_item(dom:&rbx_dom_weak::WeakDom,mut file:std::path::PathBuf,node:&TreeNode,node_name_override:&str)->AResult<()>{
file.push(sanitize(node.name.as_str()).as_ref()); file.push(sanitize(node_name_override).as_ref());
match node.class{ match node.class{
Class::Folder=>(), Class::Folder=>(),
Class::ModuleScript|Class::LocalScript|Class::Script=>{ Class::ModuleScript|Class::LocalScript|Class::Script=>{
@ -509,15 +509,16 @@ fn decompile(input_file:std::path::PathBuf,output_folder:std::path::PathBuf)->AR
//generate folders, models, and scripts //generate folders, models, and scripts
//delete models and scripts from dom //delete models and scripts from dom
{
let mut name_tally=std::collections::HashMap::<String,u32>::new();
let mut folder=output_folder.clone(); let mut folder=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(){ while let Some(instruction)=stack.pop(){
match instruction{ match instruction{
WriteStackInstruction::PushFolder(component)=>folder.push(component), WriteStackInstruction::PushFolder(component)=>folder.push(component),
WriteStackInstruction::PopFolder=>assert!(folder.pop(),"weirdness"), WriteStackInstruction::PopFolder=>assert!(folder.pop(),"weirdness"),
WriteStackInstruction::Destroy(referent)=>dom.destroy(referent), WriteStackInstruction::Destroy(referent)=>dom.destroy(referent),
WriteStackInstruction::Node(node)=>{ WriteStackInstruction::Node(node,name_count)=>{
//properties.json to override class or other simple properties //properties.json to override class or other simple properties
let mut properties=PropertiesOverride::default(); let mut properties=PropertiesOverride::default();
let has_children=node.children.len()!=0; let has_children=node.children.len()!=0;
@ -533,10 +534,15 @@ fn decompile(input_file:std::path::PathBuf,output_folder:std::path::PathBuf)->AR
Class::Script=>properties.ClassName=Some("Script".to_string()), Class::Script=>properties.ClassName=Some("Script".to_string()),
Class::Model=>(), Class::Model=>(),
} }
let name_override=if 0<name_count{
format!("{}_{}",node.name,name_count)
}else{
node.name.clone()
};
if has_children||properties.is_some(){ if has_children||properties.is_some(){
//push temp subfolder //push temp subfolder
let mut subfolder=folder.clone(); 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 //make folder
std::fs::create_dir(subfolder.clone())?; std::fs::create_dir(subfolder.clone())?;
//write properties //write properties
@ -547,10 +553,10 @@ fn decompile(input_file:std::path::PathBuf,output_folder:std::path::PathBuf)->AR
std::fs::write(file,serde_json::to_string(&properties)?)? std::fs::write(file,serde_json::to_string(&properties)?)?
} }
//write item in subfolder //write item in subfolder
write_item(&dom,subfolder,node)?; write_item(&dom,subfolder,node,name_override.as_str())?;
}else{ }else{
//write item //write item
write_item(&dom,folder.clone(),node)?; write_item(&dom,folder.clone(),node,name_override.as_str())?;
} }
//queue item to be deleted from dom after child objects are handled (stack is popped from the back) //queue item to be deleted from dom after child objects are handled (stack is popped from the back)
match node.class{ match node.class{
@ -559,17 +565,19 @@ fn decompile(input_file:std::path::PathBuf,output_folder:std::path::PathBuf)->AR
} }
if has_children{ if has_children{
stack.push(WriteStackInstruction::PopFolder); stack.push(WriteStackInstruction::PopFolder);
name_tally.clear();
for referent in &node.children{ for referent in &node.children{
if let Some(c)=tree_refs.get(referent){ 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 //write what remains in template.rbxlx
{ {
let mut file=output_folder.clone(); let mut file=output_folder.clone();