move writeattributes to mapfixer

This commit is contained in:
Quaternions 2025-01-27 07:04:05 -08:00
parent 41b28fa7d2
commit 1ad9723905

@ -22,7 +22,6 @@ enum Commands {
VPKContents(VPKContentsSubcommand),
BSPContents(BSPContentsSubcommand),
DownloadMeshes(DownloadMeshesSubcommand),
WriteAttributes(WriteAttributesSubcommand),
}
#[derive(Args)]
@ -69,9 +68,6 @@ struct DownloadMeshesSubcommand {
#[arg(long,required=true)]
roblox_files:Vec<PathBuf>
}
#[derive(Args)]
struct WriteAttributesSubcommand {
}
fn main() -> AResult<()> {
let cli = Cli::parse();
@ -84,27 +80,9 @@ fn main() -> AResult<()> {
Commands::BSPContents(subcommand)=>bsp_contents(subcommand.input_file),
Commands::ConvertTextures(_subcommand)=>convert_textures(),
Commands::DownloadMeshes(subcommand)=>download_meshes(subcommand.roblox_files),
Commands::WriteAttributes(_subcommand)=>write_attributes(),
}
}
fn recursive_collect_regex(objects: &mut std::vec::Vec<rbx_dom_weak::types::Ref>,dom: &rbx_dom_weak::WeakDom, instance: &rbx_dom_weak::Instance, regex: &lazy_regex::Lazy<lazy_regex::Regex>){
for &referent in instance.children() {
if let Some(c) = dom.get_by_ref(referent) {
if regex.captures(c.name.as_str()).is_some(){
objects.push(c.referent());//copy ref
}
recursive_collect_regex(objects,dom,c,regex);
}
}
}
fn get_button_refs(dom:&rbx_dom_weak::WeakDom) -> Vec<rbx_dom_weak::types::Ref>{
let mut buttons = std::vec::Vec::new();
recursive_collect_regex(&mut buttons, dom, dom.root(),lazy_regex::regex!(r"Button(\d+)$"));
buttons
}
enum ReaderType<'a, R:Read+Seek>{
GZip(flate2::read::GzDecoder<&'a mut R>),
Raw(&'a mut R),
@ -140,19 +118,6 @@ fn load_dom<R:Read+Seek>(input:&mut R)->AResult<rbx_dom_weak::WeakDom>{
}
}
fn get_dom<R:Read+Seek>(input:&mut R)->AResult<rbx_dom_weak::WeakDom>{
match maybe_gzip_decode(input){
Ok(ReaderType::GZip(mut readable)) => {
//gzip
let mut extracted:Vec<u8>=Vec::new();
readable.read_to_end(&mut extracted)?;
Ok(load_dom(&mut std::io::Cursor::new(extracted))?)
},
Ok(ReaderType::Raw(readable)) => Ok(load_dom(readable)?),
Err(e) => Err(e)?,
}
}
/* The ones I'm interested in:
Beam.Texture
Decal.Texture
@ -202,7 +167,7 @@ fn download_textures(paths:Vec<PathBuf>)->AResult<()>{
}
};
let mut input=std::io::BufReader::new(file);
match get_dom(&mut input){
match load_dom(&mut input){
Ok(dom)=>{
for object in dom.into_raw().1.into_values(){
match object.class.as_str(){
@ -252,7 +217,7 @@ fn download_meshes(paths:Vec<PathBuf>)->AResult<()>{
}
};
let mut input=std::io::BufReader::new(file);
match get_dom(&mut input){
match load_dom(&mut input){
Ok(dom)=>{
for object in dom.into_raw().1.into_values(){
match object.class.as_str(){
@ -370,51 +335,6 @@ fn convert_textures() -> AResult<()>{
Ok(())
}
fn write_attributes() -> AResult<()>{
for entry in std::fs::read_dir("maps/unprocessed")? {
let file_thing=entry?;
println!("processing map={:?}",file_thing.file_name());
let mut input = std::io::BufReader::new(std::fs::File::open(file_thing.path())?);
let mut dom = get_dom(&mut input)?;
let button_refs = get_button_refs(&dom);
for &button_ref in &button_refs {
if let Some(button)=dom.get_by_ref_mut(button_ref){
match button.properties.get_mut("Attributes"){
Some(rbx_dom_weak::types::Variant::Attributes(attributes))=>{
println!("Appending Ref={} to existing attributes for {}",button_ref,button.name);
attributes.insert("Ref".to_string(),rbx_dom_weak::types::Variant::String(button_ref.to_string()));
},
None=>{
println!("Creating new attributes with Ref={} for {}",button_ref,button.name);
let mut attributes=rbx_dom_weak::types::Attributes::new();
attributes.insert("Ref".to_string(),rbx_dom_weak::types::Variant::String(button_ref.to_string()));
button.properties.insert("Attributes".to_string(),rbx_dom_weak::types::Variant::Attributes(attributes));
}
_=>unreachable!("Fetching attributes did not return attributes."),
}
}
}
let mut dest={
let mut dest=PathBuf::from("maps/attributes");
dest.push(file_thing.file_name());
let output = std::io::BufWriter::new(std::fs::File::create(dest)?);
//write workspace:GetChildren()[1]
let workspace_children=dom.root().children();
if workspace_children.len()!=1{
return Err(anyhow::Error::msg("there can only be one model"));
}
rbx_binary::to_writer(output, &dom, &[workspace_children[0]])?;
//move original to processed folder
PathBuf::from("maps/unaltered")
};
dest.push(file_thing.file_name());
std::fs::rename(file_thing.path(), dest)?;
}
Ok(())
}
enum VMTContent{
VMT(String),
VTF(String),