Skip to content

Commit

Permalink
fix: empty content
Browse files Browse the repository at this point in the history
  • Loading branch information
Phill030 committed Apr 21, 2024
1 parent 37c44cb commit e4966ff
Showing 1 changed file with 34 additions and 39 deletions.
73 changes: 34 additions & 39 deletions src/library.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use binary_modifier::{BinaryError, BinaryReader, Endian};
use rayon::{iter::IntoParallelIterator, prelude::ParallelIterator};
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use std::{
collections::VecDeque,
fs::{self, create_dir_all},
Expand Down Expand Up @@ -82,49 +82,44 @@ impl<'a> Library<'a> {
}

pub fn open_all_files(&mut self, path: &mut Path) {
let buffer: Vec<u8> = Vec::with_capacity(self.files.iter().max_by_key(|f| f.size as usize).unwrap().size as usize);
let cursor = Cursor::new(&self.buffer);

<VecDeque<LibraryRecord> as Clone>::clone(&self.files)
.into_par_iter()
.for_each(|file_record| {
let mut cursor = cursor.clone();
let mut buffer = buffer.clone();

let data = {
let mut result = vec![
0;
if file_record.zipped {
file_record.zip_size
} else {
file_record.size
} as usize
];

cursor.set_position(u64::from(file_record.offset));
cursor.read_exact(&mut result).unwrap();
result
};

if file_record.zipped {
if Self::is_empty(&data) {
buffer.clear();
self.files.par_iter().for_each(|file_record| {
let mut buffer = Vec::with_capacity(file_record.size as usize);
let mut cursor = Cursor::new(&self.buffer);

let data = {
let mut result = vec![
0;
if file_record.zipped {
file_record.zip_size
} else {
let mut decompressor = flate2::Decompress::new(true);
decompressor
.decompress_vec(&data[..], &mut buffer, flate2::FlushDecompress::Finish)
.unwrap();
}
file_record.size
} as usize
];

cursor.set_position(u64::from(file_record.offset));
cursor.read_exact(&mut result).unwrap();
result
};

if file_record.zipped {
if Self::is_empty(&data) {
buffer.clear();
} else {
buffer = data
let mut decompressor = flate2::Decompress::new(true);
decompressor
.decompress_vec(&data[..], &mut buffer, flate2::FlushDecompress::Finish)
.unwrap();
}
} else {
buffer = data
}

let path = &mut path.join(&file_record.file_name);
create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &buffer).unwrap_or_else(|e| eprintln!("Could not write to file! {e}")); // Write to the file
let path = &mut path.join(&file_record.file_name);
create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &buffer).unwrap_or_else(|e| eprintln!("Could not write to file! {e}")); // Write to the file

buffer.clear();
});
buffer.clear();
});
}

/// Check if a `Vec<u8>` is the magic header `KIWAD`
Expand Down

0 comments on commit e4966ff

Please sign in to comment.