From f63904798020c6d08fff6464d2a0f43ea6b76983 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 8 Jan 2024 23:08:35 -0800 Subject: [PATCH] texture gaming --- src/main.rs | 59 ++++++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/src/main.rs b/src/main.rs index b316846..8dfd44a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1047,50 +1047,39 @@ fn write_attributes() -> AResult<()>{ fn extract_textures(paths:Vec,vpk_path:std::path::PathBuf)->AResult<()>{ let vpk_index=vpk::VPK::read(&vpk_path)?; - let mut vpk_cache=std::collections::HashMap::new(); + let mut deduplicate=std::collections::HashSet::new(); for path in paths{ let bsp=vbsp::Bsp::read(std::fs::read(path)?.as_ref())?; for texture in bsp.textures(){ let texture_name=texture.texture_data().name(); - let texture_file_name=format!("materials/{}.vtf",texture_name); + let texture_file_name=format!("materials/{}.vtf",texture_name.to_lowercase()); + if !deduplicate.insert(texture_file_name.clone()){ + continue; + } println!("texture_name={}",texture_file_name); if let Some(stuff)=vpk_index.tree.get(texture_file_name.as_str()){ - let vpk=if let Some(vpk)=vpk_cache.get(stuff.archive_path.as_str()){ - println!("ayyyy"); - vpk + let image=vtf::from_bytes(&mut stuff.get()?.to_vec())?.highres_image.decode(0)?.to_rgba8(); + + let format=if image.width()%4!=0||image.height()%4!=0{ + image_dds::ImageFormat::R8G8B8A8Srgb }else{ - println!("hiii"); - let vpk=vpk::VPK::read(&std::path::PathBuf::from(&stuff.archive_path))?; - println!("hiiiiii"); - vpk_cache.insert(stuff.archive_path.as_str(),vpk); - vpk_cache.get(stuff.archive_path.as_str()).unwrap() + image_dds::ImageFormat::BC7Srgb }; - if let Some(stuff)=vpk.tree.get(texture_file_name.as_str()){ - let image=vtf::from_bytes(&mut stuff.get()?.to_vec())?.highres_image.decode(0)?.to_rgba8(); + //this fails if the image dimensions are not a multiple of 4 + let dds = image_dds::dds_from_image( + &image, + format, + image_dds::Quality::Slow, + image_dds::Mipmaps::GeneratedAutomatic, + )?; - let format=if image.width()%4!=0||image.height()%4!=0{ - image_dds::ImageFormat::R8G8B8A8Srgb - }else{ - image_dds::ImageFormat::BC7Srgb - }; - //this fails if the image dimensions are not a multiple of 4 - let dds = image_dds::dds_from_image( - &image, - format, - image_dds::Quality::Slow, - image_dds::Mipmaps::GeneratedAutomatic, - )?; - - //write dds - let mut dest=std::path::PathBuf::from("textures/dds"); - dest.push(texture_name); - dest.set_extension("dds"); - std::fs::create_dir_all(dest.parent().unwrap())?; - let mut writer = std::io::BufWriter::new(std::fs::File::create(dest)?); - dds.write(&mut writer)?; - }else{ - println!("zip"); - } + //write dds + let mut dest=std::path::PathBuf::from("textures/dds"); + dest.push(texture_name); + dest.set_extension("dds"); + std::fs::create_dir_all(dest.parent().unwrap())?; + let mut writer = std::io::BufWriter::new(std::fs::File::create(dest)?); + dds.write(&mut writer)?; }else{ println!("nonadat"); }