extremely fancy code to transparently handle gzip
This commit is contained in:
parent
e54400a436
commit
5dc69db885
60
src/main.rs
60
src/main.rs
@ -97,7 +97,24 @@ fn get_texture_refs(dom:&rbx_dom_weak::WeakDom) -> Vec<rbx_dom_weak::types::Ref>
|
||||
objects
|
||||
}
|
||||
|
||||
fn get_dom<R:Read+Seek>(input:&mut R)->AResult<rbx_dom_weak::WeakDom>{
|
||||
enum ReaderType<'a, R:Read+Seek>{
|
||||
GZip(flate2::read::GzDecoder<&'a mut R>),
|
||||
Raw(&'a mut R),
|
||||
}
|
||||
|
||||
fn maybe_gzip_decode<R:Read+Seek>(input:&mut R)->AResult<ReaderType<R>>{
|
||||
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<R:Read+Seek>(input:&mut R)->AResult<rbx_dom_weak::WeakDom>{
|
||||
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<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)?,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_id() -> AResult<u32>{
|
||||
match std::fs::read_to_string("id"){
|
||||
Ok(id_file)=>Ok(id_file.parse::<u32>()?),
|
||||
@ -306,24 +336,32 @@ fn download_textures(paths: Vec<std::path::PathBuf>) -> 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<R:Read+Seek+std::io::BufRead>(input:&mut R)->AResult<image::DynamicImage>{
|
||||
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"{
|
||||
let image=match maybe_gzip_decode(&mut input){
|
||||
Ok(ReaderType::GZip(mut readable)) => {
|
||||
//gzip
|
||||
let mut extracted:Vec<u8>=Vec::new();
|
||||
flate2::read::GzDecoder::new(input).read_to_end(&mut extracted)?;
|
||||
//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());
|
||||
image::load(std::io::Cursor::new(extracted),image::ImageFormat::Png)?.to_rgba8()
|
||||
}else{
|
||||
return Err(anyhow::Error::msg("Unknown texture format"));
|
||||
};
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user