diff --git a/src/main.rs b/src/main.rs index e04b706..88116e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,7 +97,24 @@ fn get_texture_refs(dom:&rbx_dom_weak::WeakDom) -> Vec objects } -fn get_dom(input:&mut R)->AResult{ +enum ReaderType<'a, R:Read+Seek>{ + GZip(flate2::read::GzDecoder<&'a mut R>), + Raw(&'a mut R), +} + +fn maybe_gzip_decode(input:&mut R)->AResult>{ + let mut first_2=[0u8;2]; + if let (Ok(()),Ok(()))=(std::io::Read::read_exact(input, &mut first_2),std::io::Seek::rewind(input)){ + match &first_2{ + b"\x1f\x8b"=>Ok(ReaderType::GZip(flate2::read::GzDecoder::new(input))), + _=>Ok(ReaderType::Raw(input)), + } + }else{ + Err(anyhow::Error::msg("failed to peek")) + } +} + +fn load_dom(input:&mut R)->AResult{ let mut first_8=[0u8;8]; if let (Ok(()),Ok(()))=(std::io::Read::read_exact(input, &mut first_8),std::io::Seek::rewind(input)){ match &first_8[0..4]{ @@ -115,6 +132,19 @@ fn get_dom(input:&mut R)->AResult{ } } +fn get_dom(input:&mut R)->AResult{ + match maybe_gzip_decode(input){ + Ok(ReaderType::GZip(mut readable)) => { + //gzip + let mut extracted:Vec=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)?, + } +} + fn get_id() -> AResult{ match std::fs::read_to_string("id"){ Ok(id_file)=>Ok(id_file.parse::()?), @@ -306,24 +336,32 @@ fn download_textures(paths: Vec) -> AResult<()>{ Ok(()) } -fn convert(file_thing:std::fs::DirEntry) -> AResult<()>{ - let mut input = std::io::BufReader::new(std::fs::File::open(file_thing.path())?); +fn load_image(input:&mut R)->AResult{ let mut fourcc=[0u8;4]; input.read_exact(&mut fourcc)?; input.rewind()?; + match &fourcc{ + b"\x89PNG"=>Ok(image::load(input,image::ImageFormat::Png)?), + _=>Err(anyhow::Error::msg("Unknown texture format")), + } +} + +fn convert(file_thing:std::fs::DirEntry) -> AResult<()>{ + let mut input = std::io::BufReader::new(std::fs::File::open(file_thing.path())?); let mut extracted_input=None; - let image=if&fourcc==b"\x89PNG"{ - image::load(input,image::ImageFormat::Png)?.to_rgba8()//this is setting a=0? - }else if &fourcc[0..2]==b"\x1f\x8b"{ - //gzip - let mut extracted:Vec=Vec::new(); - flate2::read::GzDecoder::new(input).read_to_end(&mut extracted)?; - extracted_input=Some(extracted.clone()); - image::load(std::io::Cursor::new(extracted),image::ImageFormat::Png)?.to_rgba8() - }else{ - return Err(anyhow::Error::msg("Unknown texture format")); - }; + let image=match maybe_gzip_decode(&mut input){ + Ok(ReaderType::GZip(mut readable)) => { + //gzip + let mut extracted:Vec=Vec::new(); + //read the entire thing to the end so that I can clone the data and write a png to processed images + readable.read_to_end(&mut extracted)?; + extracted_input=Some(extracted.clone()); + load_image(&mut std::io::Cursor::new(extracted)) + }, + Ok(ReaderType::Raw(readable)) => load_image(readable), + Err(e) => Err(e)?, + }?.to_rgba8();//this is setting a=0? let format=if image.width()%4!=0||image.height()%4!=0{ image_dds::ImageFormat::R8G8B8A8Srgb