forked from StrafesNET/map-tool
rewrite duplicated code as closure
This commit is contained in:
parent
eeac376500
commit
91f452a94b
51
src/main.rs
51
src/main.rs
@ -1106,9 +1106,9 @@ fn extract_textures(paths:Vec<std::path::PathBuf>,vpk_path:std::path::PathBuf)->
|
|||||||
texture_file_name2.set_extension("vmt");
|
texture_file_name2.set_extension("vmt");
|
||||||
println!("texture_name={:?}",texture_file_name);
|
println!("texture_name={:?}",texture_file_name);
|
||||||
let mut found_texture=false;
|
let mut found_texture=false;
|
||||||
//why can't I write a function for this without importing the ZipArchive lib
|
//LMAO imagine having to write type names
|
||||||
{
|
let mut f=|search_file_name,write_file_name|{
|
||||||
if let Some(mut stuff)=match (zippyt.by_name(texture_file_name.to_str().unwrap()),tree.get(texture_file_name.to_str().unwrap())){
|
if let Some(mut stuff)=match (zippyt.by_name(search_file_name),tree.get(search_file_name)){
|
||||||
(Ok(mut zip_file),None)=>{
|
(Ok(mut zip_file),None)=>{
|
||||||
let mut buf=Vec::new();
|
let mut buf=Vec::new();
|
||||||
zip_file.read_to_end(&mut buf)?;
|
zip_file.read_to_end(&mut buf)?;
|
||||||
@ -1118,7 +1118,6 @@ fn extract_textures(paths:Vec<std::path::PathBuf>,vpk_path:std::path::PathBuf)->
|
|||||||
_=>None,
|
_=>None,
|
||||||
}{
|
}{
|
||||||
found_texture=true;
|
found_texture=true;
|
||||||
let texture_name=texture_name.clone();
|
|
||||||
thread_handles.push(s.spawn(move||{
|
thread_handles.push(s.spawn(move||{
|
||||||
let image=vtf::from_bytes(&mut stuff)?.highres_image.decode(0)?.to_rgba8();
|
let image=vtf::from_bytes(&mut stuff)?.highres_image.decode(0)?.to_rgba8();
|
||||||
|
|
||||||
@ -1137,7 +1136,7 @@ fn extract_textures(paths:Vec<std::path::PathBuf>,vpk_path:std::path::PathBuf)->
|
|||||||
|
|
||||||
//write dds
|
//write dds
|
||||||
let mut dest=std::path::PathBuf::from("textures/dds");
|
let mut dest=std::path::PathBuf::from("textures/dds");
|
||||||
dest.push(texture_name);
|
dest.push(write_file_name);
|
||||||
dest.set_extension("dds");
|
dest.set_extension("dds");
|
||||||
std::fs::create_dir_all(dest.parent().unwrap())?;
|
std::fs::create_dir_all(dest.parent().unwrap())?;
|
||||||
let mut writer = std::io::BufWriter::new(std::fs::File::create(dest)?);
|
let mut writer = std::io::BufWriter::new(std::fs::File::create(dest)?);
|
||||||
@ -1145,46 +1144,10 @@ fn extract_textures(paths:Vec<std::path::PathBuf>,vpk_path:std::path::PathBuf)->
|
|||||||
Ok::<(),anyhow::Error>(())
|
Ok::<(),anyhow::Error>(())
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
Ok::<(),anyhow::Error>(())
|
||||||
{
|
|
||||||
if let Some(mut stuff)=match (zippyt.by_name(texture_file_name2.to_str().unwrap()),tree.get(texture_file_name2.to_str().unwrap())){
|
|
||||||
(Ok(mut zip_file),None)=>{
|
|
||||||
let mut buf=Vec::new();
|
|
||||||
zip_file.read_to_end(&mut buf)?;
|
|
||||||
Some(buf)
|
|
||||||
},
|
|
||||||
(_,Some(vpk_entry))=>Some(vpk_entry.get()?.to_vec()),
|
|
||||||
_=>None,
|
|
||||||
}{
|
|
||||||
found_texture=true;
|
|
||||||
let texture_name=texture_name.clone();
|
|
||||||
thread_handles.push(s.spawn(move||{
|
|
||||||
let image=vtf::from_bytes(&mut stuff)?.highres_image.decode(0)?.to_rgba8();
|
|
||||||
|
|
||||||
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
|
f(texture_file_name.as_os_str().to_str().unwrap(),texture_name.clone())?;
|
||||||
let dds = image_dds::dds_from_image(
|
f(texture_file_name2.as_os_str().to_str().unwrap(),texture_name.clone())?;
|
||||||
&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)?;
|
|
||||||
Ok::<(),anyhow::Error>(())
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found_texture{
|
if !found_texture{
|
||||||
println!("no data");
|
println!("no data");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user